KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.calipso.reportgenerator.reportmanager;
2
3
4 import com.calipso.reportgenerator.reportdefinitions.ReportView;
5 import com.calipso.reportgenerator.common.*;
6 import com.calipso.reportgenerator.services.FileSystemResolver;
7 import com.calipso.reportgenerator.common.InfoException;
8 import java.io.IOException JavaDoc;
9 import java.io.Serializable JavaDoc;
10 import java.io.FileReader JavaDoc;
11 import java.util.*;
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 vistas de reportes
19  */

20 public class ReportViewRepository extends Repository implements Serializable JavaDoc{
21   private static CacheRepository cache;
22
23   public static final String JavaDoc NAME_FINALIZATION = "--";
24   public static final String JavaDoc REP_FINALIZATION = "__";
25
26   public Class JavaDoc getObjectClass() {
27     return ReportView.class;
28   }
29
30   /**
31  * Inicializa el repositorio
32  * @param directoryName
33  * @param reportGeneratorConfiguration
34  */

35   public ReportViewRepository(String JavaDoc directoryName, ReportGeneratorConfiguration reportGeneratorConfiguration) {
36     super(directoryName, reportGeneratorConfiguration);
37   }
38
39   protected Object JavaDoc saveFromSourceFiles(ReportGeneratorConfiguration reportGeneratorConfiguration, String JavaDoc id) throws InfoException {
40     ReportView result = null;
41     try{
42       FileSystemManager fileSystemManager = FileSystemResolver.getFileSystemManager(getReportGeneratorConfiguration());
43       FileObject fileObject = fileSystemManager.resolveFile(getReportGeneratorConfiguration().getSourceReportViewsPath());
44       String JavaDoc fileName = id + ".xml";
45       result = (ReportView) Unmarshaller.unmarshal(ReportView.class, new FileReader JavaDoc(getReportGeneratorConfiguration().getSourceReportViewsPath() + "/" + fileName));
46       save(result);
47     }catch (Exception JavaDoc e){
48       throw new InfoException(LanguageTraslator.traslate("413"));
49     }
50     return result;
51
52   }
53
54   /**
55    * Carga una definición a partir de un identificador del report view
56    * @param id Identificador
57    * @return Objeto instanciado
58    * @throws com.calipso.reportgenerator.common.InfoException Si no pudo obtener el report view
59    */

60   public ReportView loadFromID(String JavaDoc id, String JavaDoc reportDefinitionId, String JavaDoc userId) throws InfoException {
61     Object JavaDoc object;
62     try {
63       object = super.load(getFileName(id, reportDefinitionId,userId));
64     } catch (Exception JavaDoc e) {
65       throw new InfoException(LanguageTraslator.traslate("91"), e);
66     }
67     if (object != null) {
68       return (ReportView) object;
69     }
70     else {
71       return null;
72     }
73   }
74
75   /**
76    * Devuelve el nombre real del archivo correspondiente al objeto
77    * @param id Identificador del report view
78    * @return
79    */

80   private String JavaDoc getFileName(String JavaDoc id, String JavaDoc reportDefinitionId, String JavaDoc userId) throws InfoException {
81     String JavaDoc fileName,returnedFileName,sourceName;
82     String JavaDoc reportFileName;
83     String JavaDoc userName;
84
85     returnedFileName = "";
86     FileObject fileObject;
87     try {
88       fileObject = getFileSystemManager().resolveFile(getDirectoryName());
89     }
90     catch (FileSystemException e) {
91       throw new InfoException(LanguageTraslator.traslate("212")+":"+getDirectoryName(),e);
92     }
93     try {
94       for (int i = 0; i < fileObject.getChildren().length; i++) {
95         fileName = fileObject.getChildren()[i].getName().getBaseName();
96          if (fileName.indexOf(REP_FINALIZATION)<0){
97           continue;
98         }
99         sourceName = fileName.substring(0, fileName.indexOf(NAME_FINALIZATION)).toUpperCase();
100         reportFileName = fileName.substring(fileName.indexOf(NAME_FINALIZATION)+2, fileName.indexOf(REP_FINALIZATION));
101         userName = fileName.substring(fileName.indexOf(REP_FINALIZATION)+2 , fileName.length());
102         if (sourceName.equalsIgnoreCase(id.toUpperCase()) && reportFileName.equalsIgnoreCase(reportDefinitionId)) {
103           if (!userName.equalsIgnoreCase("")){
104             if (userName.equalsIgnoreCase(userName)){
105               returnedFileName = fileName;
106               break;
107             }
108           } else {
109             returnedFileName = fileName;
110           }
111         }
112       }
113     }catch (Exception JavaDoc e){
114        throw new InfoException(LanguageTraslator.traslate("213")+":"+getDirectoryName(),e);
115     }
116     if (returnedFileName.equals("")) {
117       throw new InfoException(LanguageTraslator.traslate("92"));
118     }
119     else {
120       return returnedFileName;
121     }
122   }
123
124   /**
125    * Graba una definición
126    * @param reportView
127    */

128
129   public void save(ReportView reportView) throws InfoException {
130     try {
131       super.save(reportView, reportView.getId()+ NAME_FINALIZATION + reportView.getReportDefinitionId()+ REP_FINALIZATION +reportView.getUserID());
132     } catch (IOException JavaDoc e) {
133       throw new InfoException(LanguageTraslator.traslate("93"), e);
134     }
135   }
136
137   /**
138    * Retorna una lista de todas las vistas disponibles para la definicion
139    * @param reportDefinitionID Nombre del report definition para el cual se definió la vista
140    * @param reportManager El report manager para obener datos de las otras definiciones
141    * @return
142    */

143   public Map getAllViewForDefinition(String JavaDoc reportDefinitionID, IReportManager reportManager) throws InfoException {
144     Map map= new Hashtable();
145     String JavaDoc fileName, reportFileName, id;
146     FileObject fileObject;
147
148     try {
149       fileObject = getFileSystemManager().resolveFile(getDirectoryName());
150     }
151     catch (FileSystemException e) {
152       throw new InfoException(LanguageTraslator.traslate("212")+":"+getDirectoryName(),e);
153     }
154     try {
155       for (int i = 0; i < fileObject.getChildren().length; i++) {
156         fileName = fileObject.getChildren()[i].getName().getBaseName();
157         System.out.println("ReportView:"+fileName);
158         reportFileName = fileName.substring(fileName.indexOf(NAME_FINALIZATION)+2, fileName.indexOf(REP_FINALIZATION));
159         if (reportDefinitionID.equalsIgnoreCase(reportFileName)) {
160           id = fileName.substring(0,fileName.indexOf(NAME_FINALIZATION));
161           DefinitionInfo definitionInfo = new DefinitionInfo();
162           definitionInfo.setId(id);
163           try {
164             definitionInfo.setDescription(new DefinitionTraslator((ReportView)load(fileName),((ReportManager)reportManager).getReportDefinitionFromID(reportFileName),reportGeneratorConfiguration.getLocaleLanguage(),reportGeneratorConfiguration.getCountry()).getDescription());
165           }catch(Exception JavaDoc e){
166             if (!fileName.substring(fileName.length()-4,fileName.length()-3).equalsIgnoreCase(".")){
167               throw new InfoException(LanguageTraslator.traslate("178")+":"+reportFileName,e);
168             }else{
169               break;
170             }
171           }
172           map.put(id,definitionInfo);
173         }
174       }
175     }catch (Exception JavaDoc e){
176       throw new InfoException(LanguageTraslator.traslate("213")+":"+getDirectoryName(),e);
177     }
178     return map;
179   }
180
181
182   /**
183    * Devuelve el objeto cache del repositorio
184    * @return
185    */

186   public CacheRepository getCache() {
187     if (cache == null) {
188       cache = new CacheRepository("ReportDefinition");
189     }
190     return cache;
191   }
192
193   public Map getAllViewForReportUser(String JavaDoc reportDefinitionID, String JavaDoc userID) throws InfoException {
194     Map map = new Hashtable();
195     String JavaDoc fileName, userName, reportFileName, id;
196     FileObject fileObject;
197
198     try {
199       fileObject = getFileSystemManager().resolveFile(getDirectoryName());
200     }
201     catch (FileSystemException e) {
202       throw new InfoException(LanguageTraslator.traslate("212")+":"+getDirectoryName(),e);
203     }
204     try {
205       for (int i = 0; i < fileObject.getChildren().length; i++) {
206         fileName = fileObject.getChildren()[i].getName().getBaseName();
207         userName = fileName.substring(fileName.indexOf(REP_FINALIZATION)+2 , fileName.length());
208         if (fileName.indexOf(REP_FINALIZATION)<0){
209           continue;
210         }
211         reportFileName = fileName.substring(fileName.indexOf(NAME_FINALIZATION)+2, fileName.indexOf(REP_FINALIZATION));
212         if (reportDefinitionID.equalsIgnoreCase(reportFileName)&& userName.equalsIgnoreCase(userID)) {
213           id = fileName.substring(0,fileName.indexOf(NAME_FINALIZATION));
214           DefinitionInfo definitionInfo = new DefinitionInfo();
215           definitionInfo.setId(id);
216           try {
217             definitionInfo.setDescription(((ReportView)load(fileName)).getDescription());
218           }catch(Exception JavaDoc e){
219             if (!fileName.substring(fileName.length()-4,fileName.length()-3).equalsIgnoreCase(".")){
220               throw new InfoException(LanguageTraslator.traslate("178")+":"+id,e);
221             }else{
222               break;
223             }
224           }
225           map.put(id,definitionInfo);
226         }
227       }
228     }catch (Exception JavaDoc e){
229       throw new InfoException(LanguageTraslator.traslate("213")+":"+getDirectoryName(),e);
230     }
231     return map;
232   }
233
234   public ReportView getDefaultViewForReportUser(String JavaDoc reportDefinitionID, String JavaDoc userID) throws InfoException{
235     Map map = getAllViewForReportUser(reportDefinitionID,userID);
236     ReportView reportView=null;
237     ReportView returnReportView=null;
238     try {
239       for (int i= 0;i<map.size();i++){
240         reportView = (ReportView) load(getFileName(((DefinitionInfo)map.values().toArray()[i]).getId(),reportDefinitionID,userID));
241         if (reportView.getDefault()) {
242           returnReportView = reportView;
243           break;
244         }
245       }
246     }catch(Exception JavaDoc e){
247       throw new InfoException(LanguageTraslator.traslate("94"),e);
248     }
249     return returnReportView;
250
251   }
252
253   public void delete(String JavaDoc id, String JavaDoc reportDefinitionId, String JavaDoc userId) throws InfoException {
254     String JavaDoc fileName = getFileName(id,reportDefinitionId,userId);
255     deleteFile(fileName);
256     getCache().delete(fileName);
257   }
258
259   public void assingDefaultView(String JavaDoc id, String JavaDoc reportDefinitionId, String JavaDoc userId) throws InfoException {
260     try {
261       String JavaDoc fileName = getFileName(id,reportDefinitionId,userId);
262       unassignDefault(reportDefinitionId,userId);
263       ReportView reportView = (ReportView)load(fileName);
264       reportView.setDefault(true);
265       save(reportView);
266     }
267     catch(Exception JavaDoc e){
268       throw new InfoException(LanguageTraslator.traslate("254"),e);
269     }
270   }
271
272   /**
273    * Quita el default anterior
274    * @param reportDefinitionId
275    * @param userId
276    * @throws InfoException
277    */

278   protected void unassignDefault(String JavaDoc reportDefinitionId, String JavaDoc userId) throws InfoException {
279     Map views = getAllViewForReportUser(reportDefinitionId,userId);
280     ReportView reportView=null;
281     String JavaDoc reportViewName="";
282     try {
283       for (int i= 0;i<views.size();i++){
284         reportView = (ReportView) load(getFileName(((DefinitionInfo)views.values().toArray()[i]).getId(),reportDefinitionId,userId));
285         reportViewName = reportView.getDescription();
286         if (reportView.getDefault()){
287           reportView.setDefault(false);
288           save(reportView);
289         }
290       }
291     }catch(Exception JavaDoc e){
292       throw new InfoException(LanguageTraslator.traslate("255")+":"+reportViewName,e);
293     }
294   }
295
296   public void deleteAll() throws InfoException {
297     getCache().deleteAll();
298     deleteAllFiles();
299   }
300
301 }
302
Popular Tags