KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > calipso > reportgenerator > reportmanager > ReportDefinitionRepository


1 package com.calipso.reportgenerator.reportmanager;
2
3 import com.calipso.reportgenerator.reportdefinitions.ReportDefinition;
4 import com.calipso.reportgenerator.common.*;
5 import com.calipso.reportgenerator.services.FileSystemResolver;
6
7 import java.io.IOException JavaDoc;
8 import java.io.Serializable JavaDoc;
9 import java.io.FileReader JavaDoc;
10 import java.util.*;
11 import com.calipso.reportgenerator.common.InfoException;
12 import org.apache.commons.vfs.FileObject;
13 import org.apache.commons.vfs.FileSystemException;
14 import org.apache.commons.vfs.FileSystemManager;
15 import org.exolab.castor.xml.Unmarshaller;
16
17 /**
18  * Repositorio de definiciones de reportes
19  */

20 public class ReportDefinitionRepository extends Repository implements Serializable JavaDoc{
21   private static CacheRepository cache;
22
23   public static final String JavaDoc NAME_FINALIZATION = "--";
24
25
26   /**
27    * Carga una definición a partir de un identificador
28    * @param id Identificador
29    * @return Objeto instanciado
30    * @throws InfoException Si no pudo obtener la definición
31    */

32   public ReportDefinition loadFromID(String JavaDoc id) throws InfoException {
33     Object JavaDoc object;
34     try {
35       object = super.load(getFileName(id));
36     } catch (Exception JavaDoc e) {
37       throw new InfoException(LanguageTraslator.traslate("61"), e);
38     }
39     if (object != null) {
40       return (ReportDefinition) object;
41     }
42     else {
43       return null;
44     }
45   }
46
47   /**
48    * Devuelve el nombre real del archivo correspondiente al objeto
49    * @param id Identificador del report definition
50    * @return
51    */

52   private String JavaDoc getFileName(String JavaDoc id) throws InfoException {
53     String JavaDoc fileName,returnedFileName,sourceName;
54     returnedFileName = "";
55     FileObject fileObject;
56     int index;
57
58     try {
59       fileObject = getFileSystemManager().resolveFile(getDirectoryName());
60     }
61     catch (FileSystemException e) {
62       throw new InfoException(LanguageTraslator.traslate("212")+":"+getDirectoryName(),e);
63     }
64     try {
65       for (int i = 0; i < fileObject.getChildren().length; i++) {
66         try {
67           fileName = fileObject.getChildren()[i].getName().getBaseName();
68           index = id.length() + NAME_FINALIZATION.length();
69           if ((id.length() + NAME_FINALIZATION.length())>fileName.length()){
70             index = fileName.length();
71           };
72           sourceName = fileName.substring(0,index).toUpperCase();
73           if (sourceName.equalsIgnoreCase((id.toUpperCase() + NAME_FINALIZATION).substring(0,index))) {
74             returnedFileName = fileName;
75           }
76         }
77         catch(Exception JavaDoc e){
78         }
79       }
80     }catch (Exception JavaDoc e){
81       throw new InfoException(LanguageTraslator.traslate("213")+":"+getDirectoryName(),e);
82     }
83     if (returnedFileName.equals("")) {
84       return id;//throw new InfoException(LanguageTraslator.traslate("62"));
85
}else {
86       return returnedFileName;
87     }
88   }
89
90   /**
91    * Graba una definición
92    * @param reportDefinition
93    */

94
95   public void save(ReportDefinition reportDefinition) throws InfoException {
96     try {
97       super.save(reportDefinition, reportDefinition.getId() + NAME_FINALIZATION + "EE" + reportDefinition.getEntity());
98     } catch (IOException JavaDoc e) {
99       throw new InfoException(LanguageTraslator.traslate("63"), e);
100     }
101   }
102
103   /**
104    * Retorna una lista de todas las definiciones de reportes registradas
105    * @return
106    */

107   public Map getAllDefinitions() throws InfoException {
108     Hashtable hashtable;
109     hashtable = new Hashtable();
110     String JavaDoc fileName;
111     String JavaDoc id;
112     FileObject fileObject;
113     int index;
114
115     try {
116       fileObject = getFileSystemManager().resolveFile(getDirectoryName());
117     }
118     catch (FileSystemException e) {
119       throw new InfoException(LanguageTraslator.traslate("212")+":"+getDirectoryName(),e);
120     }
121     try {
122       for (int i = 0; i < fileObject.getChildren().length; i++) {
123         fileName = fileObject.getChildren()[i].getName().getBaseName();
124         index = fileName.indexOf(NAME_FINALIZATION);
125         if (index< 0){
126           index =fileName.length();
127         }
128         id = (fileName.substring(0,index));
129         DefinitionInfo definitionInfo = new DefinitionInfo();
130         definitionInfo.setId(id);
131         try {
132           definitionInfo.setDescription(new DefinitionTraslator((ReportDefinition)load(fileName),reportGeneratorConfiguration.getLocaleLanguage(),reportGeneratorConfiguration.getCountry()).getDescription());
133         }catch(Exception JavaDoc e){
134           if (!fileName.substring(fileName.length()-4,fileName.length()-3).equalsIgnoreCase(".")){
135             throw new InfoException(LanguageTraslator.traslate("176")+":"+id,e);
136           }else{
137             break;
138           }
139         }
140         hashtable.put(id,definitionInfo);
141       }
142     }catch (Exception JavaDoc e){
143       throw new InfoException(LanguageTraslator.traslate("213")+":"+getDirectoryName(),e);
144     }
145     return hashtable;
146   }
147
148   /**
149    * Retorna una lista de todas las definiciones de reportes registradas para una entidad
150    * @param entityName Nombre de la entidad
151    * @return
152    */

153   public Map getAllDfefinitionForEntity(String JavaDoc entityName) throws InfoException {
154     Map map = new Hashtable();
155     String JavaDoc ee = "EE";
156     String JavaDoc fileName, entityFileName, reportFileName;
157     FileObject fileObject;
158
159     try {
160       fileObject = getFileSystemManager().resolveFile(getDirectoryName());
161     }
162     catch (FileSystemException e) {
163       throw new InfoException(LanguageTraslator.traslate("212")+":"+getDirectoryName(),e);
164     }
165     try {
166       for (int i = 0; i < fileObject.getChildren().length; i++) {
167         fileName = fileObject.getChildren()[i].getName().getBaseName();
168         entityFileName = fileName.substring(fileName.indexOf(ee) + 2, fileName.length());
169         reportFileName = fileName.substring(0, fileName.indexOf(NAME_FINALIZATION));
170         if (entityName.equalsIgnoreCase(entityFileName)) {
171           DefinitionInfo definitionInfo = new DefinitionInfo();
172           definitionInfo.setId(reportFileName);
173           try {
174             definitionInfo.setDescription(new DefinitionTraslator((ReportDefinition)load(fileName),reportGeneratorConfiguration.getLocaleLanguage(),reportGeneratorConfiguration.getCountry()).getDescription());
175           }catch(Exception JavaDoc e){
176             throw new InfoException(LanguageTraslator.traslate("176")+":"+reportFileName,e);
177           }
178           map.put(reportFileName,definitionInfo);
179         }
180       }
181     }catch (Exception JavaDoc e){
182       throw new InfoException(LanguageTraslator.traslate("213")+":"+getDirectoryName(),e);
183     }
184     return map;
185   }
186
187
188   /**
189    * Devuelve el objeto cache del repositorio
190    * @return
191    */

192   public CacheRepository getCache() {
193     if (cache == null) {
194       cache = new CacheRepository("ReportDefinition");
195     }
196     return cache;
197   }
198
199   public void deleteAll() throws InfoException {
200     getCache().deleteAll();
201     deleteAllFiles();
202   }
203
204   public void delete(String JavaDoc id) throws InfoException {
205     String JavaDoc fileName = getFileName(id);
206     deleteFile(fileName);
207     getCache().delete(fileName);
208   }
209
210
211   public Class JavaDoc getObjectClass() {
212     return ReportDefinition.class;
213   }
214
215   /**
216    * Inicializa el repositorio
217    * @param directoryName
218    * @param reportGeneratorConfiguration
219    */

220   public ReportDefinitionRepository(String JavaDoc directoryName, ReportGeneratorConfiguration reportGeneratorConfiguration) {
221     super(directoryName, reportGeneratorConfiguration);
222   }
223
224   protected Object JavaDoc saveFromSourceFiles(ReportGeneratorConfiguration reportGeneratorConfiguration, String JavaDoc id) throws InfoException {
225     ReportDefinition result = null;
226     try{
227       FileSystemManager fileSystemManager = FileSystemResolver.getFileSystemManager(getReportGeneratorConfiguration());
228       FileObject fileObject = fileSystemManager.resolveFile(getReportGeneratorConfiguration().getSourceReportDefinitionsPath());
229       String JavaDoc fileName = id + ".xml";
230       result = (ReportDefinition) Unmarshaller.unmarshal(ReportDefinition.class, new FileReader JavaDoc(getReportGeneratorConfiguration().getSourceReportDefinitionsPath() + "/" + fileName));
231       save(result);
232     }catch (Exception JavaDoc e){
233       throw new InfoException(LanguageTraslator.traslate("413"));
234     }
235     return result;
236   }
237
238
239 }
240
Popular Tags