KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > calipso > reportgenerator > common > MicroReport


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 JavaDoc;
10 import java.util.zip.ZipEntry JavaDoc;
11 import java.util.zip.ZipOutputStream JavaDoc;
12 import java.util.*;
13
14 import org.xml.sax.InputSource JavaDoc;
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 /**
21  * Representa un reporte completo con sus datos incluidos. Utilizado normalmente para enportar un informe y dejarlo conjelado para su posteriri aŽnalisis off line o en otro momento
22  */

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 JavaDoc name;
29   private Map views;
30   private String JavaDoc 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 JavaDoc name, String JavaDoc 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   /**
56    * Lista de vistas
57    * @return views
58    */

59   public Map getViews() {
60     if (views == null){
61       views = new HashMap();
62     }
63     return views;
64   }
65
66   /**
67    * retorna un Zip con el micro report
68    * @param outFileName
69    * @return
70    * @throws com.calipso.reportgenerator.common.InfoException
71    */

72   public ZipOutputStream JavaDoc getZip(String JavaDoc outFileName, boolean csvSerialize) throws InfoException {
73     try {
74       ZipOutputStream JavaDoc out = new ZipOutputStream JavaDoc(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 JavaDoc e){
95       throw new InfoException(LanguageTraslator.traslate("266"),e);
96     }
97   }
98
99   private void addMatrixToZip(ZipOutputStream JavaDoc zipOutputStream, Matrix matrix, String JavaDoc name, String JavaDoc 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 JavaDoc(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   /**
115    * Agrega el objeto al zip
116    * @param serializerType 1- serializado, 2- XML
117    * @param zipOutputStream
118    * @param o Objeto a aagregar
119    * @param name
120    * @throws org.exolab.castor.xml.ValidationException
121    * @throws org.exolab.castor.xml.MarshalException
122    * @throws java.io.IOException
123    */

124   protected void addObjectToZip(int serializerType, ZipOutputStream JavaDoc zipOutputStream,Object JavaDoc o,String JavaDoc name, String JavaDoc 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 JavaDoc writerClassName;
141         String JavaDoc 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 JavaDoc writerClass = Class.forName(writerClassName);
148           writer = (Writer)writerClass.getConstructor(new Class JavaDoc[]{OutputStream JavaDoc.class}).newInstance(new Object JavaDoc[]{outputStream});
149       }catch(Exception JavaDoc 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 JavaDoc(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   /**
165    * Inicializa un Micro report desde un zip
166    * @param microReportFileName
167    * @param reportGeneratorConfiguration
168    * @throws com.calipso.reportgenerator.common.InfoException
169    */

170   public MicroReport(String JavaDoc microReportFileName, ReportGeneratorConfiguration reportGeneratorConfiguration) throws InfoException {
171     try {
172       java.util.zip.ZipFile JavaDoc zipFile = new ZipFile JavaDoc(microReportFileName); //todo:revisar pasar file
173
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 JavaDoc matrixFileName = getConfiguration().get("Matrix").toString();
180         matrix = (Matrix) MatrixCsvSerializer.deserialize(reportGeneratorConfiguration, zipFile.getInputStream(zipFile.getEntry(matrixFileName)), reportSourceDefinition);
181       }catch(Exception JavaDoc 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       //params = ReportMap.setParametersToReportManagerType(params);
188
loadReportViews(zipFile);
189     }catch(Exception JavaDoc e){
190       throw new InfoException(LanguageTraslator.traslate("265"),e);
191     }
192   }
193
194   private Object JavaDoc loadSerializedObject(ZipFile JavaDoc zipFile, String JavaDoc name) throws IOException, ClassNotFoundException JavaDoc {
195     InputStream inputStream;
196     if (getConfiguration().containsKey(name)){
197       ZipEntry JavaDoc 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 JavaDoc loadSerializedXmlObject(ZipFile JavaDoc zipFile,String JavaDoc name,Class JavaDoc classLoad) throws IOException, MarshalException, ValidationException {
206     InputStream inputStream;
207     InputSource JavaDoc inputSource;
208     if (getConfiguration().containsKey(name)){
209       ZipEntry JavaDoc zipEntry = zipFile.getEntry(getConfiguration().get(name).toString());
210       inputStream = zipFile.getInputStream(zipEntry);
211       inputSource = new InputSource JavaDoc(inputStream);
212       return Unmarshaller.unmarshal(classLoad, inputSource);
213     }
214     return null;
215   }
216
217   private void loadReportViews(ZipFile JavaDoc zipFile) throws IOException, MarshalException, ValidationException {
218     int count=0;
219     boolean eol = false;
220     String JavaDoc viewName;
221     Object JavaDoc 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 JavaDoc 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   /**
278    * Indica si el reporte es el musmo al buscado
279    *
280    * @param fileName Microreport
281    * @param reportGeneratorConfiguration
282    * @param reportDefinitionID
283    * @param params Parametros de reporte
284    */

285   public static boolean sameReport(String JavaDoc fileName, ReportGeneratorConfiguration reportGeneratorConfiguration, String JavaDoc reportDefinitionID, Map params) throws InfoException {
286     ObjectInputStream ois=null;
287     java.util.zip.ZipFile JavaDoc zipFile=null;
288     try {
289       zipFile = new ZipFile JavaDoc(fileName);
290       InputStream inputStream = zipFile.getInputStream(zipFile.getEntry("description"));
291       ois = new ObjectInputStream(inputStream);
292     }catch(Exception JavaDoc e){
293       throw new InfoException(LanguageTraslator.traslate("460"),e);
294     }
295     Map configuration=null;
296     try {
297       configuration = (HashMap)ois.readObject();
298     } catch (Exception JavaDoc 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 JavaDoc 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 JavaDoc e){
316         throw new InfoException(LanguageTraslator.traslate("461"),e);
317       }
318     }else{
319       return true;
320     }
321   }
322
323   /**
324    * Compara dos Map y retorna si estan todos las entradas y si todos los valores son iguales
325    * @param microReportParams
326    * @param params
327    * @return
328    */

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