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 ; 11 import java.util.Hashtable ; 12 import java.io.Serializable ; 13 import java.io.FileReader ; 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 23 public class ReportSourceDefinitionRepository extends Repository implements Serializable { 24 private static CacheRepository cache; 25 26 32 public ReportSourceDefinition loadFromID(String id) throws InfoException { 33 Object 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 e) { 43 throw new InfoException(LanguageTraslator.traslate("59"), e); 44 } 45 } 46 47 52 public void save(ReportSourceDefinition reportSourceDefinition) throws InfoException { 53 try { 54 super.save(reportSourceDefinition, reportSourceDefinition.getId()); 55 } catch (Exception e) { 56 throw new InfoException(LanguageTraslator.traslate("60"), e); 57 } 58 } 59 60 64 public Map getAllDfefinitions() throws InfoException { 65 Map map= new Hashtable (); 66 String 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 e){ 83 throw new InfoException(LanguageTraslator.traslate("177")+":"+fileName,e); 84 } 85 map.put(fileName,definitionInfo); 86 } 87 }catch (Exception e){ 88 throw new InfoException(LanguageTraslator.traslate("213")+":"+getDirectoryName(),e); 89 } 90 return map; 91 } 92 93 97 public CacheRepository getCache() { 98 if (cache == null) { 99 cache = new CacheRepository("ReportSourceDefinition"); 100 } 101 return cache; 102 } 103 104 public void delete(String id) throws InfoException { 105 String fileName = id; 106 deleteFile(fileName); 107 getCache().delete(fileName); 108 } 109 110 public Class getObjectClass() { 111 return ReportSourceDefinition.class; 112 } 113 114 119 public ReportSourceDefinitionRepository(String directoryName, ReportGeneratorConfiguration reportGeneratorConfiguration) { 120 super(directoryName, reportGeneratorConfiguration); 121 } 122 123 protected Object saveFromSourceFiles(ReportGeneratorConfiguration reportGeneratorConfiguration, String id) throws InfoException { 124 ReportSourceDefinition result = null; 125 try{ 126 FileSystemManager fileSystemManager = FileSystemResolver.getFileSystemManager(getReportGeneratorConfiguration()); 127 FileObject fileObject = fileSystemManager.resolveFile(getReportGeneratorConfiguration().getSourceReportSourceDefinitionsPath()); 128 String fileName = id + ".xml"; 129 result = (ReportSourceDefinition) Unmarshaller.unmarshal(ReportSourceDefinition.class, new FileReader (getReportGeneratorConfiguration().getSourceReportSourceDefinitionsPath() + "/" + fileName)); 130 save(result); 131 }catch (Exception e){ 132 throw new InfoException(LanguageTraslator.traslate("413")); 133 } 134 return result; 135 } 136 137 141 public void deleteAll() throws InfoException { 142 getCache().deleteAll(); 143 deleteAllFiles(); 144 } 145 146 } 147 | Popular Tags |