001: package com.calipso.reportgenerator.reportmanager;
002:
003: import com.calipso.reportgenerator.reportdefinitions.ReportView;
004: import com.calipso.reportgenerator.common.*;
005: import com.calipso.reportgenerator.services.FileSystemResolver;
006: import com.calipso.reportgenerator.common.InfoException;
007: import java.io.IOException;
008: import java.io.Serializable;
009: import java.io.FileReader;
010: import java.util.*;
011: import org.apache.commons.vfs.FileObject;
012: import org.apache.commons.vfs.FileSystemException;
013: import org.apache.commons.vfs.FileSystemManager;
014: import org.exolab.castor.xml.Unmarshaller;
015:
016: /**
017: * Repositorio de vistas de reportes
018: */
019: public class ReportViewRepository extends Repository implements
020: Serializable {
021: private static CacheRepository cache;
022:
023: public static final String NAME_FINALIZATION = "--";
024: public static final String REP_FINALIZATION = "__";
025:
026: public Class getObjectClass() {
027: return ReportView.class;
028: }
029:
030: /**
031: * Inicializa el repositorio
032: * @param directoryName
033: * @param reportGeneratorConfiguration
034: */
035: public ReportViewRepository(String directoryName,
036: ReportGeneratorConfiguration reportGeneratorConfiguration) {
037: super (directoryName, reportGeneratorConfiguration);
038: }
039:
040: protected Object saveFromSourceFiles(
041: ReportGeneratorConfiguration reportGeneratorConfiguration,
042: String id) throws InfoException {
043: ReportView result = null;
044: try {
045: FileSystemManager fileSystemManager = FileSystemResolver
046: .getFileSystemManager(getReportGeneratorConfiguration());
047: FileObject fileObject = fileSystemManager
048: .resolveFile(getReportGeneratorConfiguration()
049: .getSourceReportViewsPath());
050: String fileName = id + ".xml";
051: result = (ReportView) Unmarshaller.unmarshal(
052: ReportView.class, new FileReader(
053: getReportGeneratorConfiguration()
054: .getSourceReportViewsPath()
055: + "/" + fileName));
056: save(result);
057: } catch (Exception e) {
058: throw new InfoException(LanguageTraslator.traslate("413"));
059: }
060: return result;
061:
062: }
063:
064: /**
065: * Carga una definición a partir de un identificador del report view
066: * @param id Identificador
067: * @return Objeto instanciado
068: * @throws com.calipso.reportgenerator.common.InfoException Si no pudo obtener el report view
069: */
070: public ReportView loadFromID(String id, String reportDefinitionId,
071: String userId) throws InfoException {
072: Object object;
073: try {
074: object = super .load(getFileName(id, reportDefinitionId,
075: userId));
076: } catch (Exception e) {
077: throw new InfoException(LanguageTraslator.traslate("91"), e);
078: }
079: if (object != null) {
080: return (ReportView) object;
081: } else {
082: return null;
083: }
084: }
085:
086: /**
087: * Devuelve el nombre real del archivo correspondiente al objeto
088: * @param id Identificador del report view
089: * @return
090: */
091: private String getFileName(String id, String reportDefinitionId,
092: String userId) throws InfoException {
093: String fileName, returnedFileName, sourceName;
094: String reportFileName;
095: String userName;
096:
097: returnedFileName = "";
098: FileObject fileObject;
099: try {
100: fileObject = getFileSystemManager().resolveFile(
101: getDirectoryName());
102: } catch (FileSystemException e) {
103: throw new InfoException(LanguageTraslator.traslate("212")
104: + ":" + getDirectoryName(), e);
105: }
106: try {
107: for (int i = 0; i < fileObject.getChildren().length; i++) {
108: fileName = fileObject.getChildren()[i].getName()
109: .getBaseName();
110: if (fileName.indexOf(REP_FINALIZATION) < 0) {
111: continue;
112: }
113: sourceName = fileName.substring(0,
114: fileName.indexOf(NAME_FINALIZATION))
115: .toUpperCase();
116: reportFileName = fileName.substring(fileName
117: .indexOf(NAME_FINALIZATION) + 2, fileName
118: .indexOf(REP_FINALIZATION));
119: userName = fileName.substring(fileName
120: .indexOf(REP_FINALIZATION) + 2, fileName
121: .length());
122: if (sourceName.equalsIgnoreCase(id.toUpperCase())
123: && reportFileName
124: .equalsIgnoreCase(reportDefinitionId)) {
125: if (!userName.equalsIgnoreCase("")) {
126: if (userName.equalsIgnoreCase(userName)) {
127: returnedFileName = fileName;
128: break;
129: }
130: } else {
131: returnedFileName = fileName;
132: }
133: }
134: }
135: } catch (Exception e) {
136: throw new InfoException(LanguageTraslator.traslate("213")
137: + ":" + getDirectoryName(), e);
138: }
139: if (returnedFileName.equals("")) {
140: throw new InfoException(LanguageTraslator.traslate("92"));
141: } else {
142: return returnedFileName;
143: }
144: }
145:
146: /**
147: * Graba una definición
148: * @param reportView
149: */
150:
151: public void save(ReportView reportView) throws InfoException {
152: try {
153: super .save(reportView, reportView.getId()
154: + NAME_FINALIZATION
155: + reportView.getReportDefinitionId()
156: + REP_FINALIZATION + reportView.getUserID());
157: } catch (IOException e) {
158: throw new InfoException(LanguageTraslator.traslate("93"), e);
159: }
160: }
161:
162: /**
163: * Retorna una lista de todas las vistas disponibles para la definicion
164: * @param reportDefinitionID Nombre del report definition para el cual se definió la vista
165: * @param reportManager El report manager para obener datos de las otras definiciones
166: * @return
167: */
168: public Map getAllViewForDefinition(String reportDefinitionID,
169: IReportManager reportManager) throws InfoException {
170: Map map = new Hashtable();
171: String fileName, reportFileName, id;
172: FileObject fileObject;
173:
174: try {
175: fileObject = getFileSystemManager().resolveFile(
176: getDirectoryName());
177: } catch (FileSystemException e) {
178: throw new InfoException(LanguageTraslator.traslate("212")
179: + ":" + getDirectoryName(), e);
180: }
181: try {
182: for (int i = 0; i < fileObject.getChildren().length; i++) {
183: fileName = fileObject.getChildren()[i].getName()
184: .getBaseName();
185: System.out.println("ReportView:" + fileName);
186: reportFileName = fileName.substring(fileName
187: .indexOf(NAME_FINALIZATION) + 2, fileName
188: .indexOf(REP_FINALIZATION));
189: if (reportDefinitionID.equalsIgnoreCase(reportFileName)) {
190: id = fileName.substring(0, fileName
191: .indexOf(NAME_FINALIZATION));
192: DefinitionInfo definitionInfo = new DefinitionInfo();
193: definitionInfo.setId(id);
194: try {
195: definitionInfo
196: .setDescription(new DefinitionTraslator(
197: (ReportView) load(fileName),
198: ((ReportManager) reportManager)
199: .getReportDefinitionFromID(reportFileName),
200: reportGeneratorConfiguration
201: .getLocaleLanguage(),
202: reportGeneratorConfiguration
203: .getCountry())
204: .getDescription());
205: } catch (Exception e) {
206: if (!fileName.substring(fileName.length() - 4,
207: fileName.length() - 3)
208: .equalsIgnoreCase(".")) {
209: throw new InfoException(LanguageTraslator
210: .traslate("178")
211: + ":" + reportFileName, e);
212: } else {
213: break;
214: }
215: }
216: map.put(id, definitionInfo);
217: }
218: }
219: } catch (Exception e) {
220: throw new InfoException(LanguageTraslator.traslate("213")
221: + ":" + getDirectoryName(), e);
222: }
223: return map;
224: }
225:
226: /**
227: * Devuelve el objeto cache del repositorio
228: * @return
229: */
230: public CacheRepository getCache() {
231: if (cache == null) {
232: cache = new CacheRepository("ReportDefinition");
233: }
234: return cache;
235: }
236:
237: public Map getAllViewForReportUser(String reportDefinitionID,
238: String userID) throws InfoException {
239: Map map = new Hashtable();
240: String fileName, userName, reportFileName, id;
241: FileObject fileObject;
242:
243: try {
244: fileObject = getFileSystemManager().resolveFile(
245: getDirectoryName());
246: } catch (FileSystemException e) {
247: throw new InfoException(LanguageTraslator.traslate("212")
248: + ":" + getDirectoryName(), e);
249: }
250: try {
251: for (int i = 0; i < fileObject.getChildren().length; i++) {
252: fileName = fileObject.getChildren()[i].getName()
253: .getBaseName();
254: userName = fileName.substring(fileName
255: .indexOf(REP_FINALIZATION) + 2, fileName
256: .length());
257: if (fileName.indexOf(REP_FINALIZATION) < 0) {
258: continue;
259: }
260: reportFileName = fileName.substring(fileName
261: .indexOf(NAME_FINALIZATION) + 2, fileName
262: .indexOf(REP_FINALIZATION));
263: if (reportDefinitionID.equalsIgnoreCase(reportFileName)
264: && userName.equalsIgnoreCase(userID)) {
265: id = fileName.substring(0, fileName
266: .indexOf(NAME_FINALIZATION));
267: DefinitionInfo definitionInfo = new DefinitionInfo();
268: definitionInfo.setId(id);
269: try {
270: definitionInfo
271: .setDescription(((ReportView) load(fileName))
272: .getDescription());
273: } catch (Exception e) {
274: if (!fileName.substring(fileName.length() - 4,
275: fileName.length() - 3)
276: .equalsIgnoreCase(".")) {
277: throw new InfoException(LanguageTraslator
278: .traslate("178")
279: + ":" + id, e);
280: } else {
281: break;
282: }
283: }
284: map.put(id, definitionInfo);
285: }
286: }
287: } catch (Exception e) {
288: throw new InfoException(LanguageTraslator.traslate("213")
289: + ":" + getDirectoryName(), e);
290: }
291: return map;
292: }
293:
294: public ReportView getDefaultViewForReportUser(
295: String reportDefinitionID, String userID)
296: throws InfoException {
297: Map map = getAllViewForReportUser(reportDefinitionID, userID);
298: ReportView reportView = null;
299: ReportView returnReportView = null;
300: try {
301: for (int i = 0; i < map.size(); i++) {
302: reportView = (ReportView) load(getFileName(
303: ((DefinitionInfo) map.values().toArray()[i])
304: .getId(), reportDefinitionID, userID));
305: if (reportView.getDefault()) {
306: returnReportView = reportView;
307: break;
308: }
309: }
310: } catch (Exception e) {
311: throw new InfoException(LanguageTraslator.traslate("94"), e);
312: }
313: return returnReportView;
314:
315: }
316:
317: public void delete(String id, String reportDefinitionId,
318: String userId) throws InfoException {
319: String fileName = getFileName(id, reportDefinitionId, userId);
320: deleteFile(fileName);
321: getCache().delete(fileName);
322: }
323:
324: public void assingDefaultView(String id, String reportDefinitionId,
325: String userId) throws InfoException {
326: try {
327: String fileName = getFileName(id, reportDefinitionId,
328: userId);
329: unassignDefault(reportDefinitionId, userId);
330: ReportView reportView = (ReportView) load(fileName);
331: reportView.setDefault(true);
332: save(reportView);
333: } catch (Exception e) {
334: throw new InfoException(LanguageTraslator.traslate("254"),
335: e);
336: }
337: }
338:
339: /**
340: * Quita el default anterior
341: * @param reportDefinitionId
342: * @param userId
343: * @throws InfoException
344: */
345: protected void unassignDefault(String reportDefinitionId,
346: String userId) throws InfoException {
347: Map views = getAllViewForReportUser(reportDefinitionId, userId);
348: ReportView reportView = null;
349: String reportViewName = "";
350: try {
351: for (int i = 0; i < views.size(); i++) {
352: reportView = (ReportView) load(getFileName(
353: ((DefinitionInfo) views.values().toArray()[i])
354: .getId(), reportDefinitionId, userId));
355: reportViewName = reportView.getDescription();
356: if (reportView.getDefault()) {
357: reportView.setDefault(false);
358: save(reportView);
359: }
360: }
361: } catch (Exception e) {
362: throw new InfoException(LanguageTraslator.traslate("255")
363: + ":" + reportViewName, e);
364: }
365: }
366:
367: public void deleteAll() throws InfoException {
368: getCache().deleteAll();
369: deleteAllFiles();
370: }
371:
372: }
|