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 ; 8 import java.io.Serializable ; 9 import java.io.FileReader ; 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 20 public class ReportDefinitionRepository extends Repository implements Serializable { 21 private static CacheRepository cache; 22 23 public static final String NAME_FINALIZATION = "--"; 24 25 26 32 public ReportDefinition loadFromID(String id) throws InfoException { 33 Object object; 34 try { 35 object = super.load(getFileName(id)); 36 } catch (Exception 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 52 private String getFileName(String id) throws InfoException { 53 String 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 e){ 78 } 79 } 80 }catch (Exception e){ 81 throw new InfoException(LanguageTraslator.traslate("213")+":"+getDirectoryName(),e); 82 } 83 if (returnedFileName.equals("")) { 84 return id; }else { 86 return returnedFileName; 87 } 88 } 89 90 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 e) { 99 throw new InfoException(LanguageTraslator.traslate("63"), e); 100 } 101 } 102 103 107 public Map getAllDefinitions() throws InfoException { 108 Hashtable hashtable; 109 hashtable = new Hashtable(); 110 String fileName; 111 String 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 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 e){ 143 throw new InfoException(LanguageTraslator.traslate("213")+":"+getDirectoryName(),e); 144 } 145 return hashtable; 146 } 147 148 153 public Map getAllDfefinitionForEntity(String entityName) throws InfoException { 154 Map map = new Hashtable(); 155 String ee = "EE"; 156 String 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 e){ 176 throw new InfoException(LanguageTraslator.traslate("176")+":"+reportFileName,e); 177 } 178 map.put(reportFileName,definitionInfo); 179 } 180 } 181 }catch (Exception e){ 182 throw new InfoException(LanguageTraslator.traslate("213")+":"+getDirectoryName(),e); 183 } 184 return map; 185 } 186 187 188 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 id) throws InfoException { 205 String fileName = getFileName(id); 206 deleteFile(fileName); 207 getCache().delete(fileName); 208 } 209 210 211 public Class getObjectClass() { 212 return ReportDefinition.class; 213 } 214 215 220 public ReportDefinitionRepository(String directoryName, ReportGeneratorConfiguration reportGeneratorConfiguration) { 221 super(directoryName, reportGeneratorConfiguration); 222 } 223 224 protected Object saveFromSourceFiles(ReportGeneratorConfiguration reportGeneratorConfiguration, String id) throws InfoException { 225 ReportDefinition result = null; 226 try{ 227 FileSystemManager fileSystemManager = FileSystemResolver.getFileSystemManager(getReportGeneratorConfiguration()); 228 FileObject fileObject = fileSystemManager.resolveFile(getReportGeneratorConfiguration().getSourceReportDefinitionsPath()); 229 String fileName = id + ".xml"; 230 result = (ReportDefinition) Unmarshaller.unmarshal(ReportDefinition.class, new FileReader (getReportGeneratorConfiguration().getSourceReportDefinitionsPath() + "/" + fileName)); 231 save(result); 232 }catch (Exception e){ 233 throw new InfoException(LanguageTraslator.traslate("413")); 234 } 235 return result; 236 } 237 238 239 } 240 | Popular Tags |