001: package com.calipso.reportgenerator.reportmanager;
002:
003: import com.calipso.reportgenerator.reportdefinitions.ReportDefinition;
004: import com.calipso.reportgenerator.common.*;
005: import com.calipso.reportgenerator.services.FileSystemResolver;
006:
007: import java.io.IOException;
008: import java.io.Serializable;
009: import java.io.FileReader;
010: import java.util.*;
011: import com.calipso.reportgenerator.common.InfoException;
012: import org.apache.commons.vfs.FileObject;
013: import org.apache.commons.vfs.FileSystemException;
014: import org.apache.commons.vfs.FileSystemManager;
015: import org.exolab.castor.xml.Unmarshaller;
016:
017: /**
018: * Repositorio de definiciones de reportes
019: */
020: public class ReportDefinitionRepository extends Repository implements
021: Serializable {
022: private static CacheRepository cache;
023:
024: public static final String NAME_FINALIZATION = "--";
025:
026: /**
027: * Carga una definición a partir de un identificador
028: * @param id Identificador
029: * @return Objeto instanciado
030: * @throws InfoException Si no pudo obtener la definición
031: */
032: public ReportDefinition loadFromID(String id) throws InfoException {
033: Object object;
034: try {
035: object = super .load(getFileName(id));
036: } catch (Exception e) {
037: throw new InfoException(LanguageTraslator.traslate("61"), e);
038: }
039: if (object != null) {
040: return (ReportDefinition) object;
041: } else {
042: return null;
043: }
044: }
045:
046: /**
047: * Devuelve el nombre real del archivo correspondiente al objeto
048: * @param id Identificador del report definition
049: * @return
050: */
051: private String getFileName(String id) throws InfoException {
052: String fileName, returnedFileName, sourceName;
053: returnedFileName = "";
054: FileObject fileObject;
055: int index;
056:
057: try {
058: fileObject = getFileSystemManager().resolveFile(
059: getDirectoryName());
060: } catch (FileSystemException e) {
061: throw new InfoException(LanguageTraslator.traslate("212")
062: + ":" + getDirectoryName(), e);
063: }
064: try {
065: for (int i = 0; i < fileObject.getChildren().length; i++) {
066: try {
067: fileName = fileObject.getChildren()[i].getName()
068: .getBaseName();
069: index = id.length() + NAME_FINALIZATION.length();
070: if ((id.length() + NAME_FINALIZATION.length()) > fileName
071: .length()) {
072: index = fileName.length();
073: }
074: ;
075: sourceName = fileName.substring(0, index)
076: .toUpperCase();
077: if (sourceName
078: .equalsIgnoreCase((id.toUpperCase() + NAME_FINALIZATION)
079: .substring(0, index))) {
080: returnedFileName = fileName;
081: }
082: } catch (Exception e) {
083: }
084: }
085: } catch (Exception e) {
086: throw new InfoException(LanguageTraslator.traslate("213")
087: + ":" + getDirectoryName(), e);
088: }
089: if (returnedFileName.equals("")) {
090: return id;//throw new InfoException(LanguageTraslator.traslate("62"));
091: } else {
092: return returnedFileName;
093: }
094: }
095:
096: /**
097: * Graba una definición
098: * @param reportDefinition
099: */
100:
101: public void save(ReportDefinition reportDefinition)
102: throws InfoException {
103: try {
104: super .save(reportDefinition, reportDefinition.getId()
105: + NAME_FINALIZATION + "EE"
106: + reportDefinition.getEntity());
107: } catch (IOException e) {
108: throw new InfoException(LanguageTraslator.traslate("63"), e);
109: }
110: }
111:
112: /**
113: * Retorna una lista de todas las definiciones de reportes registradas
114: * @return
115: */
116: public Map getAllDefinitions() throws InfoException {
117: Hashtable hashtable;
118: hashtable = new Hashtable();
119: String fileName;
120: String id;
121: FileObject fileObject;
122: int index;
123:
124: try {
125: fileObject = getFileSystemManager().resolveFile(
126: getDirectoryName());
127: } catch (FileSystemException e) {
128: throw new InfoException(LanguageTraslator.traslate("212")
129: + ":" + getDirectoryName(), e);
130: }
131: try {
132: for (int i = 0; i < fileObject.getChildren().length; i++) {
133: fileName = fileObject.getChildren()[i].getName()
134: .getBaseName();
135: index = fileName.indexOf(NAME_FINALIZATION);
136: if (index < 0) {
137: index = fileName.length();
138: }
139: id = (fileName.substring(0, index));
140: DefinitionInfo definitionInfo = new DefinitionInfo();
141: definitionInfo.setId(id);
142: try {
143: definitionInfo
144: .setDescription(new DefinitionTraslator(
145: (ReportDefinition) load(fileName),
146: reportGeneratorConfiguration
147: .getLocaleLanguage(),
148: reportGeneratorConfiguration
149: .getCountry())
150: .getDescription());
151: } catch (Exception e) {
152: if (!fileName.substring(fileName.length() - 4,
153: fileName.length() - 3)
154: .equalsIgnoreCase(".")) {
155: throw new InfoException(LanguageTraslator
156: .traslate("176")
157: + ":" + id, e);
158: } else {
159: break;
160: }
161: }
162: hashtable.put(id, definitionInfo);
163: }
164: } catch (Exception e) {
165: throw new InfoException(LanguageTraslator.traslate("213")
166: + ":" + getDirectoryName(), e);
167: }
168: return hashtable;
169: }
170:
171: /**
172: * Retorna una lista de todas las definiciones de reportes registradas para una entidad
173: * @param entityName Nombre de la entidad
174: * @return
175: */
176: public Map getAllDfefinitionForEntity(String entityName)
177: throws InfoException {
178: Map map = new Hashtable();
179: String ee = "EE";
180: String fileName, entityFileName, reportFileName;
181: FileObject fileObject;
182:
183: try {
184: fileObject = getFileSystemManager().resolveFile(
185: getDirectoryName());
186: } catch (FileSystemException e) {
187: throw new InfoException(LanguageTraslator.traslate("212")
188: + ":" + getDirectoryName(), e);
189: }
190: try {
191: for (int i = 0; i < fileObject.getChildren().length; i++) {
192: fileName = fileObject.getChildren()[i].getName()
193: .getBaseName();
194: entityFileName = fileName.substring(fileName
195: .indexOf(ee) + 2, fileName.length());
196: reportFileName = fileName.substring(0, fileName
197: .indexOf(NAME_FINALIZATION));
198: if (entityName.equalsIgnoreCase(entityFileName)) {
199: DefinitionInfo definitionInfo = new DefinitionInfo();
200: definitionInfo.setId(reportFileName);
201: try {
202: definitionInfo
203: .setDescription(new DefinitionTraslator(
204: (ReportDefinition) load(fileName),
205: reportGeneratorConfiguration
206: .getLocaleLanguage(),
207: reportGeneratorConfiguration
208: .getCountry())
209: .getDescription());
210: } catch (Exception e) {
211: throw new InfoException(LanguageTraslator
212: .traslate("176")
213: + ":" + reportFileName, e);
214: }
215: map.put(reportFileName, definitionInfo);
216: }
217: }
218: } catch (Exception e) {
219: throw new InfoException(LanguageTraslator.traslate("213")
220: + ":" + getDirectoryName(), e);
221: }
222: return map;
223: }
224:
225: /**
226: * Devuelve el objeto cache del repositorio
227: * @return
228: */
229: public CacheRepository getCache() {
230: if (cache == null) {
231: cache = new CacheRepository("ReportDefinition");
232: }
233: return cache;
234: }
235:
236: public void deleteAll() throws InfoException {
237: getCache().deleteAll();
238: deleteAllFiles();
239: }
240:
241: public void delete(String id) throws InfoException {
242: String fileName = getFileName(id);
243: deleteFile(fileName);
244: getCache().delete(fileName);
245: }
246:
247: public Class getObjectClass() {
248: return ReportDefinition.class;
249: }
250:
251: /**
252: * Inicializa el repositorio
253: * @param directoryName
254: * @param reportGeneratorConfiguration
255: */
256: public ReportDefinitionRepository(String directoryName,
257: ReportGeneratorConfiguration reportGeneratorConfiguration) {
258: super (directoryName, reportGeneratorConfiguration);
259: }
260:
261: protected Object saveFromSourceFiles(
262: ReportGeneratorConfiguration reportGeneratorConfiguration,
263: String id) throws InfoException {
264: ReportDefinition result = null;
265: try {
266: FileSystemManager fileSystemManager = FileSystemResolver
267: .getFileSystemManager(getReportGeneratorConfiguration());
268: FileObject fileObject = fileSystemManager
269: .resolveFile(getReportGeneratorConfiguration()
270: .getSourceReportDefinitionsPath());
271: String fileName = id + ".xml";
272: result = (ReportDefinition) Unmarshaller.unmarshal(
273: ReportDefinition.class, new FileReader(
274: getReportGeneratorConfiguration()
275: .getSourceReportDefinitionsPath()
276: + "/" + fileName));
277: save(result);
278: } catch (Exception e) {
279: throw new InfoException(LanguageTraslator.traslate("413"));
280: }
281: return result;
282: }
283:
284: }
|