KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.calipso.reportgenerator.reportmanager;
2
3 import com.calipso.reportgenerator.reportdefinitions.ReportSourceDefinition;
4 import com.calipso.reportgenerator.common.ReportGeneratorConfiguration;
5 import com.calipso.reportgenerator.common.LanguageTraslator;
6 import com.calipso.reportgenerator.common.DefinitionInfo;
7 import com.calipso.reportgenerator.common.InfoException;
8 import com.calipso.reportgenerator.services.FileSystemResolver;
9
10 import java.util.Map JavaDoc;
11 import java.util.Hashtable JavaDoc;
12 import java.io.Serializable JavaDoc;
13 import java.io.FileReader JavaDoc;
14
15 import org.apache.commons.vfs.FileObject;
16 import org.apache.commons.vfs.FileSystemException;
17 import org.apache.commons.vfs.FileSystemManager;
18 import org.exolab.castor.xml.Unmarshaller;
19
20 /**
21  * Repositorio de definiciones de orígenes de datos(DataSource)
22  */

23 public class ReportSourceDefinitionRepository extends Repository implements Serializable JavaDoc{
24   private static CacheRepository cache;
25
26   /**
27    * Recupera una definición desde un identificador
28    * @param id Identificador de la definición
29    * @return
30    * @throws InfoException
31    */

32   public ReportSourceDefinition loadFromID(String JavaDoc id) throws InfoException {
33     Object JavaDoc object;
34     try {
35       object = super.load(id);
36       if (object != null) {
37         return (ReportSourceDefinition) object;
38       }
39       else {
40         return null;
41       }
42     } catch (Exception JavaDoc e) {
43       throw new InfoException(LanguageTraslator.traslate("59"), e);
44     }
45   }
46
47   /**
48    * Graba un report source definition
49    * @param reportSourceDefinition
50    * @throws InfoException Si no se pudo grabar la definición
51    */

52   public void save(ReportSourceDefinition reportSourceDefinition) throws InfoException {
53     try {
54       super.save(reportSourceDefinition, reportSourceDefinition.getId());
55     } catch (Exception JavaDoc e) {
56       throw new InfoException(LanguageTraslator.traslate("60"), e);
57     }
58   }
59
60   /**
61    * Retorna una lista de todas las definiciones disponibles
62    * @return
63    */

64   public Map JavaDoc getAllDfefinitions() throws InfoException {
65     Map JavaDoc map= new Hashtable JavaDoc();
66     String JavaDoc fileName;
67     FileObject fileObject;
68
69     try {
70       fileObject = getFileSystemManager().resolveFile(getDirectoryName());
71     }
72     catch (FileSystemException e) {
73       throw new InfoException(LanguageTraslator.traslate("212")+":"+getDirectoryName(),e);
74     }
75     try {
76       for (int i = 0; i < fileObject.getChildren().length; i++) {
77         fileName = fileObject.getChildren()[i].getName().getBaseName();
78         DefinitionInfo definitionInfo = new DefinitionInfo();
79         definitionInfo.setId(fileName);
80         try {
81           definitionInfo.setDescription(new DefinitionTraslator((ReportSourceDefinition)load(fileName),reportGeneratorConfiguration.getLocaleLanguage(),reportGeneratorConfiguration.getCountry()).getDescription());
82         }catch(Exception JavaDoc e){
83           throw new InfoException(LanguageTraslator.traslate("177")+":"+fileName,e);
84         }
85         map.put(fileName,definitionInfo);
86       }
87     }catch (Exception JavaDoc e){
88       throw new InfoException(LanguageTraslator.traslate("213")+":"+getDirectoryName(),e);
89     }
90     return map;
91   }
92
93   /**
94    * Devuelve el objeto cache del repositorio
95    * @return
96    */

97   public CacheRepository getCache() {
98     if (cache == null) {
99       cache = new CacheRepository("ReportSourceDefinition");
100     }
101     return cache;
102   }
103
104   public void delete(String JavaDoc id) throws InfoException {
105     String JavaDoc fileName = id;
106     deleteFile(fileName);
107     getCache().delete(fileName);
108   }
109
110   public Class JavaDoc getObjectClass() {
111     return ReportSourceDefinition.class;
112   }
113
114   /**
115    * Inicializa el repositorio
116    * @param directoryName
117    * @param reportGeneratorConfiguration
118    */

119   public ReportSourceDefinitionRepository(String JavaDoc directoryName, ReportGeneratorConfiguration reportGeneratorConfiguration) {
120     super(directoryName, reportGeneratorConfiguration);
121   }
122
123   protected Object JavaDoc saveFromSourceFiles(ReportGeneratorConfiguration reportGeneratorConfiguration, String JavaDoc id) throws InfoException {
124     ReportSourceDefinition result = null;
125     try{
126       FileSystemManager fileSystemManager = FileSystemResolver.getFileSystemManager(getReportGeneratorConfiguration());
127       FileObject fileObject = fileSystemManager.resolveFile(getReportGeneratorConfiguration().getSourceReportSourceDefinitionsPath());
128       String JavaDoc fileName = id + ".xml";
129       result = (ReportSourceDefinition) Unmarshaller.unmarshal(ReportSourceDefinition.class, new FileReader JavaDoc(getReportGeneratorConfiguration().getSourceReportSourceDefinitionsPath() + "/" + fileName));
130       save(result);
131     }catch (Exception JavaDoc e){
132       throw new InfoException(LanguageTraslator.traslate("413"));
133     }
134     return result;
135   }
136
137   /**
138    * Borra el repositorio
139    * @throws InfoException
140    */

141   public void deleteAll() throws InfoException {
142     getCache().deleteAll();
143     deleteAllFiles();
144   }
145
146 }
147
Popular Tags