001: package com.calipso.reportgenerator.common;
002:
003: import com.calipso.reportgenerator.reportcalculator.Matrix;
004: import com.calipso.reportgenerator.reportdefinitions.ReportSourceDefinition;
005: import com.calipso.reportgenerator.reportdefinitions.ReportDefinition;
006: import com.calipso.reportgenerator.reportdefinitions.ReportView;
007:
008: import java.io.*;
009: import java.util.zip.ZipFile;
010: import java.util.zip.ZipEntry;
011: import java.util.zip.ZipOutputStream;
012: import java.util.*;
013:
014: import org.xml.sax.InputSource;
015: import org.exolab.castor.xml.Unmarshaller;
016: import org.exolab.castor.xml.Marshaller;
017: import org.exolab.castor.xml.ValidationException;
018: import org.exolab.castor.xml.MarshalException;
019:
020: /**
021: * 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
022: */
023: public class MicroReport implements Serializable {
024: private Matrix matrix;
025: private ReportSourceDefinition reportSourceDefinition;
026: private ReportDefinition reportDefinition;
027: private ReportView reportView;
028: private String name;
029: private Map views;
030: private String userName;
031: private Map definitionsInfo;
032: private Map configuration;
033: private Map params;
034:
035: public Map getParams() {
036: return params;
037: }
038:
039: public void setParams(Map params) {
040: this .params = params;
041: }
042:
043: public MicroReport(Matrix matrix,
044: ReportSourceDefinition reportSourceDefinition,
045: ReportDefinition reportDefinition, ReportView reportView,
046: String name, String userName, Map views, Map params) {
047: this .matrix = matrix;
048: this .reportSourceDefinition = reportSourceDefinition;
049: this .reportDefinition = reportDefinition;
050: this .reportView = reportView;
051: this .name = name;
052: this .userName = userName;
053: this .views = views;
054: this .params = params;
055: }
056:
057: /**
058: * Lista de vistas
059: * @return views
060: */
061: public Map getViews() {
062: if (views == null) {
063: views = new HashMap();
064: }
065: return views;
066: }
067:
068: /**
069: * retorna un Zip con el micro report
070: * @param outFileName
071: * @return
072: * @throws com.calipso.reportgenerator.common.InfoException
073: */
074: public ZipOutputStream getZip(String outFileName,
075: boolean csvSerialize) throws InfoException {
076: try {
077: ZipOutputStream out = new ZipOutputStream(
078: new FileOutputStream(outFileName));
079: configuration = null;
080: if (!csvSerialize) {
081: addObjectToZip(1, out, matrix, reportDefinition.getId()
082: + "_Matrix", "Matrix");
083: } else {
084: addMatrixToZip(out, matrix, reportDefinition.getId()
085: + "_Matrix", "Matrix");
086: }
087: addObjectToZip(2, out, reportSourceDefinition,
088: reportDefinition.getId()
089: + "_ReportSourceDefinition",
090: "ReportSourceDefinition");
091: addObjectToZip(2, out, reportDefinition, reportDefinition
092: .getId()
093: + "_ReportDefinition", "ReportDefinition");
094: if (reportView != null) {
095: addObjectToZip(2, out, reportView, reportDefinition
096: .getId()
097: + "_ReportView", "ReportView");
098: }
099: for (int i = 0; i < getViews().size(); i++) {
100: addObjectToZip(2, out,
101: getViews().values().toArray()[i],
102: reportDefinition.getId() + "_ReportView" + i,
103: "ReportView" + i);
104: }
105: params = ReportMap.setParametersToSimpleType(params);
106: addObjectToZip(1, out, params, "Params", "Params");
107: addObjectToZip(1, out, getConfiguration(), "description",
108: "description");
109: return out;
110: } catch (Exception e) {
111: throw new InfoException(LanguageTraslator.traslate("266"),
112: e);
113: }
114: }
115:
116: private void addMatrixToZip(ZipOutputStream zipOutputStream,
117: Matrix matrix, String name, String typeName)
118: throws IOException, InfoException {
119: ByteArrayOutputStream out = MatrixCsvSerializer
120: .csvSerialize(matrix);
121: ByteArrayInputStream in = new ByteArrayInputStream(out
122: .toByteArray());
123: int len;
124: byte[] buf = new byte[1024];
125:
126: zipOutputStream.putNextEntry(new ZipEntry(name));
127: while ((len = in.read(buf)) > 0) {
128: zipOutputStream.write(buf, 0, len);
129: }
130: getConfiguration().put(typeName, name);
131: zipOutputStream.closeEntry();
132: in.close();
133: }
134:
135: /**
136: * Agrega el objeto al zip
137: * @param serializerType 1- serializado, 2- XML
138: * @param zipOutputStream
139: * @param o Objeto a aagregar
140: * @param name
141: * @throws org.exolab.castor.xml.ValidationException
142: * @throws org.exolab.castor.xml.MarshalException
143: * @throws java.io.IOException
144: */
145: protected void addObjectToZip(int serializerType,
146: ZipOutputStream zipOutputStream, Object o, String name,
147: String typeName) throws ValidationException,
148: MarshalException, IOException, InfoException {
149: ByteArrayInputStream in = null;
150: ObjectOutputStream oos;
151: int len;
152: byte[] buf = new byte[1024];
153:
154: if (serializerType == 1) {
155: ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
156: oos = new ObjectOutputStream(outputStream);
157: oos.writeObject(o);
158: in = new ByteArrayInputStream(outputStream.toByteArray());
159: } else if (serializerType == 2) {
160: ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
161: Writer writer = null;
162: try {
163: String writerClassName;
164: String javaVersion = System
165: .getProperty("java.vm.version");
166: if (javaVersion.startsWith("1.4")) {
167: writerClassName = "org.apache.xalan.serialize.WriterToUTF8";
168: } else {
169: writerClassName = "com.sun.org.apache.xml.internal.serializer.WriterToUTF8";
170: }
171: Class writerClass = Class.forName(writerClassName);
172: writer = (Writer) writerClass.getConstructor(
173: new Class[] { OutputStream.class })
174: .newInstance(new Object[] { outputStream });
175: } catch (Exception e) {
176: throw new InfoException(LanguageTraslator
177: .traslate("471"), e);
178: }
179: Marshaller.marshal(o, writer);
180: in = new ByteArrayInputStream(outputStream.toByteArray());
181: }
182: zipOutputStream.putNextEntry(new ZipEntry(name));
183: while ((len = in.read(buf)) > 0) {
184: zipOutputStream.write(buf, 0, len);
185: }
186: getConfiguration().put(typeName, name);
187: zipOutputStream.closeEntry();
188: in.close();
189: }
190:
191: /**
192: * Inicializa un Micro report desde un zip
193: * @param microReportFileName
194: * @param reportGeneratorConfiguration
195: * @throws com.calipso.reportgenerator.common.InfoException
196: */
197: public MicroReport(String microReportFileName,
198: ReportGeneratorConfiguration reportGeneratorConfiguration)
199: throws InfoException {
200: try {
201: java.util.zip.ZipFile zipFile = new ZipFile(
202: microReportFileName); //todo:revisar pasar file
203: InputStream inputStream = zipFile.getInputStream(zipFile
204: .getEntry("description"));
205: ObjectInputStream ois;
206: ois = new ObjectInputStream(inputStream);
207: configuration = (HashMap) ois.readObject();
208: reportSourceDefinition = (ReportSourceDefinition) loadSerializedXmlObject(
209: zipFile, "ReportSourceDefinition",
210: ReportSourceDefinition.class);
211: try {
212: String matrixFileName = getConfiguration()
213: .get("Matrix").toString();
214: matrix = (Matrix) MatrixCsvSerializer.deserialize(
215: reportGeneratorConfiguration, zipFile
216: .getInputStream(zipFile
217: .getEntry(matrixFileName)),
218: reportSourceDefinition);
219: } catch (Exception e) {
220: matrix = (Matrix) loadSerializedObject(zipFile,
221: "Matrix");
222: }
223: reportDefinition = (ReportDefinition) loadSerializedXmlObject(
224: zipFile, "ReportDefinition", ReportDefinition.class);
225: reportView = (ReportView) loadSerializedXmlObject(zipFile,
226: "ReportView", ReportView.class);
227: params = (Map) loadSerializedObject(zipFile, "Params");
228: //params = ReportMap.setParametersToReportManagerType(params);
229: loadReportViews(zipFile);
230: } catch (Exception e) {
231: throw new InfoException(LanguageTraslator.traslate("265"),
232: e);
233: }
234: }
235:
236: private Object loadSerializedObject(ZipFile zipFile, String name)
237: throws IOException, ClassNotFoundException {
238: InputStream inputStream;
239: if (getConfiguration().containsKey(name)) {
240: ZipEntry zipEntry = zipFile.getEntry(getConfiguration()
241: .get(name).toString());
242: inputStream = zipFile.getInputStream(zipEntry);
243: ObjectInputStream ois = new ObjectInputStream(inputStream);
244: return ois.readObject();
245: }
246: return null;
247: }
248:
249: private Object loadSerializedXmlObject(ZipFile zipFile,
250: String name, Class classLoad) throws IOException,
251: MarshalException, ValidationException {
252: InputStream inputStream;
253: InputSource inputSource;
254: if (getConfiguration().containsKey(name)) {
255: ZipEntry zipEntry = zipFile.getEntry(getConfiguration()
256: .get(name).toString());
257: inputStream = zipFile.getInputStream(zipEntry);
258: inputSource = new InputSource(inputStream);
259: return Unmarshaller.unmarshal(classLoad, inputSource);
260: }
261: return null;
262: }
263:
264: private void loadReportViews(ZipFile zipFile) throws IOException,
265: MarshalException, ValidationException {
266: int count = 0;
267: boolean eol = false;
268: String viewName;
269: Object addicReportView;
270:
271: while (!eol) {
272: viewName = "ReportView" + count;
273: addicReportView = loadSerializedXmlObject(zipFile,
274: viewName, ReportView.class);
275: if (addicReportView != null) {
276: getViews().put(((ReportView) addicReportView).getId(),
277: addicReportView);
278: }
279: eol = !getConfiguration().containsKey(viewName);
280: count = count + 1;
281: }
282: }
283:
284: public Matrix getMatrix() {
285: return matrix;
286: }
287:
288: public ReportSourceDefinition getReportSourceDefinition() {
289: return reportSourceDefinition;
290: }
291:
292: public ReportDefinition getReportDefinition() {
293: return reportDefinition;
294: }
295:
296: public ReportView getReportView() {
297: return reportView;
298: }
299:
300: public String getName() {
301: return name;
302: }
303:
304: public Map getDefinitionsInfo() {
305: if (definitionsInfo == null) {
306: definitionsInfo = new HashMap();
307: ReportView reportView;
308: DefinitionInfo definitionInfo;
309: for (int i = 0; i < getViews().size(); i++) {
310: reportView = (ReportView) getViews().values().toArray()[i];
311: definitionInfo = new DefinitionInfo();
312: definitionInfo.setId(reportView.getId());
313: definitionInfo.setDescription(reportView
314: .getDescription());
315: definitionsInfo.put(reportView.getId(), definitionInfo);
316: }
317: }
318: return definitionsInfo;
319: }
320:
321: public Map getConfiguration() {
322: if (configuration == null) {
323: configuration = new HashMap();
324: }
325: return configuration;
326: }
327:
328: /**
329: * Indica si el reporte es el musmo al buscado
330: *
331: * @param fileName Microreport
332: * @param reportGeneratorConfiguration
333: * @param reportDefinitionID
334: * @param params Parametros de reporte
335: */
336: public static boolean sameReport(String fileName,
337: ReportGeneratorConfiguration reportGeneratorConfiguration,
338: String reportDefinitionID, Map params) throws InfoException {
339: ObjectInputStream ois = null;
340: java.util.zip.ZipFile zipFile = null;
341: try {
342: zipFile = new ZipFile(fileName);
343: InputStream inputStream = zipFile.getInputStream(zipFile
344: .getEntry("description"));
345: ois = new ObjectInputStream(inputStream);
346: } catch (Exception e) {
347: throw new InfoException(LanguageTraslator.traslate("460"),
348: e);
349: }
350: Map configuration = null;
351: try {
352: configuration = (HashMap) ois.readObject();
353: } catch (Exception e) {
354: throw new InfoException(LanguageTraslator.traslate("459"),
355: e);
356: }
357: if (configuration.containsKey("ReportDefinition")) {
358: if (!(reportDefinitionID + "_ReportDefinition")
359: .equalsIgnoreCase(configuration.get(
360: "ReportDefinition").toString())) {
361: return false;
362: }
363: } else {
364: return false;
365: }
366: if (configuration.containsKey("Params")) {
367: try {
368: ZipEntry zipEntry = zipFile.getEntry(configuration.get(
369: "Params").toString());
370: InputStream inputStream = zipFile
371: .getInputStream(zipEntry);
372: ObjectInputStream ois2 = new ObjectInputStream(
373: inputStream);
374: Map microReportParams = (Map) ois2.readObject();
375: return equalParams(microReportParams, params);
376: } catch (Exception e) {
377: throw new InfoException(LanguageTraslator
378: .traslate("461"), e);
379: }
380: } else {
381: return true;
382: }
383: }
384:
385: /**
386: * Compara dos Map y retorna si estan todos las entradas y si todos los valores son iguales
387: * @param microReportParams
388: * @param params
389: * @return
390: */
391: private static boolean equalParams(Map microReportParams, Map params) {
392: Iterator microReportValues = microReportParams.entrySet()
393: .iterator();
394: while (microReportValues.hasNext()) {
395: Map.Entry microReportValue = (Map.Entry) microReportValues
396: .next();
397: if (params.containsKey(microReportValue.getKey())) {
398: if (!params.get(microReportValue.getKey()).equals(
399: microReportValue.getValue())) {
400: return false;
401: } else {
402: return false;
403: }
404: }
405: }
406: return true;
407: }
408: }
|