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 ; 9 import java.io.Serializable ; 10 import java.io.FileReader ; 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 20 public class ReportViewRepository extends Repository implements Serializable { 21 private static CacheRepository cache; 22 23 public static final String NAME_FINALIZATION = "--"; 24 public static final String REP_FINALIZATION = "__"; 25 26 public Class getObjectClass() { 27 return ReportView.class; 28 } 29 30 35 public ReportViewRepository(String directoryName, ReportGeneratorConfiguration reportGeneratorConfiguration) { 36 super(directoryName, reportGeneratorConfiguration); 37 } 38 39 protected Object saveFromSourceFiles(ReportGeneratorConfiguration reportGeneratorConfiguration, String id) throws InfoException { 40 ReportView result = null; 41 try{ 42 FileSystemManager fileSystemManager = FileSystemResolver.getFileSystemManager(getReportGeneratorConfiguration()); 43 FileObject fileObject = fileSystemManager.resolveFile(getReportGeneratorConfiguration().getSourceReportViewsPath()); 44 String fileName = id + ".xml"; 45 result = (ReportView) Unmarshaller.unmarshal(ReportView.class, new FileReader (getReportGeneratorConfiguration().getSourceReportViewsPath() + "/" + fileName)); 46 save(result); 47 }catch (Exception e){ 48 throw new InfoException(LanguageTraslator.traslate("413")); 49 } 50 return result; 51 52 } 53 54 60 public ReportView loadFromID(String id, String reportDefinitionId, String userId) throws InfoException { 61 Object object; 62 try { 63 object = super.load(getFileName(id, reportDefinitionId,userId)); 64 } catch (Exception 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 80 private String getFileName(String id, String reportDefinitionId, String userId) throws InfoException { 81 String fileName,returnedFileName,sourceName; 82 String reportFileName; 83 String 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 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 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 e) { 133 throw new InfoException(LanguageTraslator.traslate("93"), e); 134 } 135 } 136 137 143 public Map getAllViewForDefinition(String reportDefinitionID, IReportManager reportManager) throws InfoException { 144 Map map= new Hashtable(); 145 String 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 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 e){ 176 throw new InfoException(LanguageTraslator.traslate("213")+":"+getDirectoryName(),e); 177 } 178 return map; 179 } 180 181 182 186 public CacheRepository getCache() { 187 if (cache == null) { 188 cache = new CacheRepository("ReportDefinition"); 189 } 190 return cache; 191 } 192 193 public Map getAllViewForReportUser(String reportDefinitionID, String userID) throws InfoException { 194 Map map = new Hashtable(); 195 String 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 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 e){ 229 throw new InfoException(LanguageTraslator.traslate("213")+":"+getDirectoryName(),e); 230 } 231 return map; 232 } 233 234 public ReportView getDefaultViewForReportUser(String reportDefinitionID, String 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 e){ 247 throw new InfoException(LanguageTraslator.traslate("94"),e); 248 } 249 return returnReportView; 250 251 } 252 253 public void delete(String id, String reportDefinitionId, String userId) throws InfoException { 254 String fileName = getFileName(id,reportDefinitionId,userId); 255 deleteFile(fileName); 256 getCache().delete(fileName); 257 } 258 259 public void assingDefaultView(String id, String reportDefinitionId, String userId) throws InfoException { 260 try { 261 String 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 e){ 268 throw new InfoException(LanguageTraslator.traslate("254"),e); 269 } 270 } 271 272 278 protected void unassignDefault(String reportDefinitionId, String userId) throws InfoException { 279 Map views = getAllViewForReportUser(reportDefinitionId,userId); 280 ReportView reportView=null; 281 String 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 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 |