1 package com.calipso.reportgenerator.common; 2 3 import com.calipso.reportgenerator.reportcalculator.Matrix; 4 import com.calipso.reportgenerator.reportdefinitions.ReportSourceDefinition; 5 import com.calipso.reportgenerator.reportdefinitions.ReportDefinition; 6 import com.calipso.reportgenerator.reportdefinitions.ReportView; 7 8 import java.io.*; 9 import java.util.zip.ZipFile ; 10 import java.util.zip.ZipEntry ; 11 import java.util.zip.ZipOutputStream ; 12 import java.util.*; 13 14 import org.xml.sax.InputSource ; 15 import org.exolab.castor.xml.Unmarshaller; 16 import org.exolab.castor.xml.Marshaller; 17 import org.exolab.castor.xml.ValidationException; 18 import org.exolab.castor.xml.MarshalException; 19 20 23 public class MicroReport implements Serializable { 24 private Matrix matrix; 25 private ReportSourceDefinition reportSourceDefinition; 26 private ReportDefinition reportDefinition; 27 private ReportView reportView; 28 private String name; 29 private Map views; 30 private String userName; 31 private Map definitionsInfo; 32 private Map configuration; 33 private Map params; 34 35 public Map getParams() { 36 return params; 37 } 38 39 public void setParams(Map params) { 40 this.params = params; 41 } 42 43 public MicroReport(Matrix matrix, ReportSourceDefinition reportSourceDefinition, ReportDefinition reportDefinition, ReportView reportView, String name, String userName, Map views, Map params) { 44 this.matrix = matrix; 45 this.reportSourceDefinition = reportSourceDefinition; 46 this.reportDefinition = reportDefinition; 47 this.reportView = reportView; 48 this.name = name; 49 this.userName = userName; 50 this.views = views; 51 this.params = params; 52 } 53 54 55 59 public Map getViews() { 60 if (views == null){ 61 views = new HashMap(); 62 } 63 return views; 64 } 65 66 72 public ZipOutputStream getZip(String outFileName, boolean csvSerialize) throws InfoException { 73 try { 74 ZipOutputStream out = new ZipOutputStream (new FileOutputStream(outFileName)); 75 configuration = null; 76 if(!csvSerialize){ 77 addObjectToZip(1,out, matrix,reportDefinition.getId()+"_Matrix","Matrix"); 78 }else{ 79 addMatrixToZip(out, matrix,reportDefinition.getId()+"_Matrix","Matrix"); 80 } 81 addObjectToZip(2,out, reportSourceDefinition,reportDefinition.getId()+"_ReportSourceDefinition","ReportSourceDefinition"); 82 addObjectToZip(2,out, reportDefinition,reportDefinition.getId()+"_ReportDefinition","ReportDefinition"); 83 if(reportView!=null){ 84 addObjectToZip(2,out, reportView,reportDefinition.getId()+"_ReportView","ReportView"); 85 } 86 for (int i=0;i<getViews().size();i++ ){ 87 addObjectToZip(2,out, getViews().values().toArray()[i],reportDefinition.getId()+"_ReportView"+i,"ReportView"+i); 88 } 89 params = ReportMap.setParametersToSimpleType(params); 90 addObjectToZip(1,out,params,"Params","Params"); 91 addObjectToZip(1,out,getConfiguration(),"description","description"); 92 return out; 93 } 94 catch(Exception e){ 95 throw new InfoException(LanguageTraslator.traslate("266"),e); 96 } 97 } 98 99 private void addMatrixToZip(ZipOutputStream zipOutputStream, Matrix matrix, String name, String typeName) throws IOException, InfoException { 100 ByteArrayOutputStream out = MatrixCsvSerializer.csvSerialize(matrix); 101 ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); 102 int len; 103 byte[] buf = new byte[1024]; 104 105 zipOutputStream.putNextEntry(new ZipEntry (name)); 106 while ((len = in.read(buf)) > 0) { 107 zipOutputStream.write(buf, 0, len); 108 } 109 getConfiguration().put(typeName,name); 110 zipOutputStream.closeEntry(); 111 in.close(); 112 } 113 114 124 protected void addObjectToZip(int serializerType, ZipOutputStream zipOutputStream,Object o,String name, String typeName) throws ValidationException, MarshalException, IOException, InfoException { 125 ByteArrayInputStream in=null; 126 ObjectOutputStream oos; 127 int len; 128 byte[] buf = new byte[1024]; 129 130 if (serializerType == 1){ 131 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 132 oos = new ObjectOutputStream(outputStream); 133 oos.writeObject(o); 134 in = new ByteArrayInputStream(outputStream.toByteArray()); 135 } 136 else if (serializerType== 2){ 137 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 138 Writer writer = null; 139 try{ 140 String writerClassName; 141 String javaVersion = System.getProperty("java.vm.version"); 142 if(javaVersion.startsWith("1.4")){ 143 writerClassName = "org.apache.xalan.serialize.WriterToUTF8"; 144 }else{ 145 writerClassName = "com.sun.org.apache.xml.internal.serializer.WriterToUTF8"; 146 } 147 Class writerClass = Class.forName(writerClassName); 148 writer = (Writer)writerClass.getConstructor(new Class []{OutputStream .class}).newInstance(new Object []{outputStream}); 149 }catch(Exception e){ 150 throw new InfoException(LanguageTraslator.traslate("471"),e); 151 } 152 Marshaller.marshal(o, writer); 153 in = new ByteArrayInputStream(outputStream.toByteArray()); 154 } 155 zipOutputStream.putNextEntry(new ZipEntry (name)); 156 while ((len = in.read(buf)) > 0) { 157 zipOutputStream.write(buf, 0, len); 158 } 159 getConfiguration().put(typeName,name); 160 zipOutputStream.closeEntry(); 161 in.close(); 162 } 163 164 170 public MicroReport(String microReportFileName, ReportGeneratorConfiguration reportGeneratorConfiguration) throws InfoException { 171 try { 172 java.util.zip.ZipFile zipFile = new ZipFile (microReportFileName); InputStream inputStream = zipFile.getInputStream(zipFile.getEntry("description")); 174 ObjectInputStream ois; 175 ois = new ObjectInputStream(inputStream); 176 configuration = (HashMap)ois.readObject(); 177 reportSourceDefinition = (ReportSourceDefinition)loadSerializedXmlObject(zipFile,"ReportSourceDefinition",ReportSourceDefinition.class); 178 try{ 179 String matrixFileName = getConfiguration().get("Matrix").toString(); 180 matrix = (Matrix) MatrixCsvSerializer.deserialize(reportGeneratorConfiguration, zipFile.getInputStream(zipFile.getEntry(matrixFileName)), reportSourceDefinition); 181 }catch(Exception e){ 182 matrix = (Matrix) loadSerializedObject(zipFile,"Matrix"); 183 } 184 reportDefinition = (ReportDefinition)loadSerializedXmlObject(zipFile,"ReportDefinition",ReportDefinition.class); 185 reportView = (ReportView)loadSerializedXmlObject(zipFile,"ReportView",ReportView.class); 186 params = (Map) loadSerializedObject(zipFile,"Params"); 187 loadReportViews(zipFile); 189 }catch(Exception e){ 190 throw new InfoException(LanguageTraslator.traslate("265"),e); 191 } 192 } 193 194 private Object loadSerializedObject(ZipFile zipFile, String name) throws IOException, ClassNotFoundException { 195 InputStream inputStream; 196 if (getConfiguration().containsKey(name)){ 197 ZipEntry zipEntry = zipFile.getEntry(getConfiguration().get(name).toString()); 198 inputStream = zipFile.getInputStream(zipEntry); 199 ObjectInputStream ois = new ObjectInputStream(inputStream); 200 return ois.readObject(); 201 } 202 return null; 203 } 204 205 private Object loadSerializedXmlObject(ZipFile zipFile,String name,Class classLoad) throws IOException, MarshalException, ValidationException { 206 InputStream inputStream; 207 InputSource inputSource; 208 if (getConfiguration().containsKey(name)){ 209 ZipEntry zipEntry = zipFile.getEntry(getConfiguration().get(name).toString()); 210 inputStream = zipFile.getInputStream(zipEntry); 211 inputSource = new InputSource (inputStream); 212 return Unmarshaller.unmarshal(classLoad, inputSource); 213 } 214 return null; 215 } 216 217 private void loadReportViews(ZipFile zipFile) throws IOException, MarshalException, ValidationException { 218 int count=0; 219 boolean eol = false; 220 String viewName; 221 Object addicReportView; 222 223 while (!eol){ 224 viewName = "ReportView"+count; 225 addicReportView = loadSerializedXmlObject(zipFile,viewName,ReportView.class); 226 if (addicReportView !=null){ 227 getViews().put(((ReportView)addicReportView).getId(),addicReportView); 228 } 229 eol = !getConfiguration().containsKey(viewName); 230 count = count + 1; 231 } 232 } 233 234 235 public Matrix getMatrix() { 236 return matrix; 237 } 238 239 public ReportSourceDefinition getReportSourceDefinition() { 240 return reportSourceDefinition; 241 } 242 243 public ReportDefinition getReportDefinition() { 244 return reportDefinition; 245 } 246 247 public ReportView getReportView() { 248 return reportView; 249 } 250 251 public String getName() { 252 return name; 253 } 254 255 public Map getDefinitionsInfo() { 256 if (definitionsInfo == null){ 257 definitionsInfo = new HashMap(); 258 ReportView reportView; 259 DefinitionInfo definitionInfo; 260 for (int i = 0;i<getViews().size();i++){ 261 reportView = (ReportView)getViews().values().toArray()[i]; 262 definitionInfo = new DefinitionInfo(); 263 definitionInfo.setId(reportView.getId()); 264 definitionInfo.setDescription(reportView.getDescription()); 265 definitionsInfo.put(reportView.getId(),definitionInfo); 266 } 267 } 268 return definitionsInfo; 269 } 270 271 public Map getConfiguration() { 272 if (configuration == null){ 273 configuration= new HashMap(); 274 } 275 return configuration; 276 } 277 285 public static boolean sameReport(String fileName, ReportGeneratorConfiguration reportGeneratorConfiguration, String reportDefinitionID, Map params) throws InfoException { 286 ObjectInputStream ois=null; 287 java.util.zip.ZipFile zipFile=null; 288 try { 289 zipFile = new ZipFile (fileName); 290 InputStream inputStream = zipFile.getInputStream(zipFile.getEntry("description")); 291 ois = new ObjectInputStream(inputStream); 292 }catch(Exception e){ 293 throw new InfoException(LanguageTraslator.traslate("460"),e); 294 } 295 Map configuration=null; 296 try { 297 configuration = (HashMap)ois.readObject(); 298 } catch (Exception e){ 299 throw new InfoException(LanguageTraslator.traslate("459"),e); 300 } 301 if (configuration.containsKey("ReportDefinition")){ 302 if ( !(reportDefinitionID+"_ReportDefinition").equalsIgnoreCase(configuration.get("ReportDefinition").toString())){ 303 return false; 304 } 305 }else{ 306 return false; 307 } 308 if (configuration.containsKey("Params")){ 309 try{ 310 ZipEntry zipEntry = zipFile.getEntry(configuration.get("Params").toString()); 311 InputStream inputStream = zipFile.getInputStream(zipEntry); 312 ObjectInputStream ois2 = new ObjectInputStream(inputStream); 313 Map microReportParams = (Map)ois2.readObject(); 314 return equalParams(microReportParams, params); 315 }catch(Exception e){ 316 throw new InfoException(LanguageTraslator.traslate("461"),e); 317 } 318 }else{ 319 return true; 320 } 321 } 322 323 329 private static boolean equalParams(Map microReportParams, Map params) { 330 Iterator microReportValues = microReportParams.entrySet().iterator(); 331 while (microReportValues.hasNext()) { 332 Map.Entry microReportValue = (Map.Entry) microReportValues.next(); 333 if (params.containsKey(microReportValue.getKey())){ 334 if (! params.get(microReportValue.getKey()).equals(microReportValue.getValue())){ 335 return false; 336 } else{ 337 return false; 338 } 339 } 340 } 341 return true; 342 } 343 } 344 | Popular Tags |