001: package com.calipso.reportgenerator.reportmanager;
002:
003: import com.calipso.reportgenerator.common.ReportQuery;
004: import com.calipso.reportgenerator.common.QueryMetric;
005:
006: import java.util.Iterator;
007: import java.util.List;
008: import java.util.HashMap;
009: import java.io.*;
010:
011: /**
012: * Cache de instancias <code>ReportQuery</code> ya ejecutadas
013: */
014:
015: public class ExecutedQueryCollection implements Serializable {
016:
017: private HashMap queries;
018:
019: public ExecutedQueryCollection() {
020: }
021:
022: public void addExecutedQuery(ReportQuery query,
023: ExecutedQuery executedQuery) {
024: try {
025: ByteArrayOutputStream value = new ByteArrayOutputStream();
026: value.write(getBytes(executedQuery));
027: getExecutedQueries().put(query, value);
028: } catch (Exception e) {
029: e.printStackTrace();
030: }
031: }
032:
033: public int getCollectionSize() {
034: return getExecutedQueries().size();
035: }
036:
037: public boolean alreadyExists(ReportQuery query,
038: boolean queryCachedEnabled) {
039: boolean returnVal = false;
040: if (queryCachedEnabled) {
041: try {
042: if (getCollectionSize() == 0) {
043: returnVal = false;
044: } else {
045: Iterator iterator = getExecutedQueries().keySet()
046: .iterator();
047: while (iterator.hasNext()) {
048: ReportQuery currentQuery = (ReportQuery) iterator
049: .next();
050: if (query.getCubeQuery().equivalentQuery(
051: currentQuery.getCubeQuery())) {
052: if (sameMetricsStates(query, currentQuery)
053: && (query.isVisibleTotals() == currentQuery
054: .isVisibleTotals())) {
055: return true;
056: }
057: }
058: }
059: }
060: } catch (Exception e) {
061: e.printStackTrace();
062: }
063: }
064: return returnVal;
065: }
066:
067: private boolean sameMetricsStates(ReportQuery query,
068: ReportQuery currentQuery) {
069: List queryMetrics = query.getMetrics();
070: List currentQueryMetrics = currentQuery.getMetrics();
071: for (int i = 0; i < queryMetrics.size(); i++) {
072: QueryMetric queryMetric = (QueryMetric) queryMetrics.get(i);
073: QueryMetric currentQueryMetric = (QueryMetric) currentQueryMetrics
074: .get(i);
075: if (queryMetric.getVisible() != currentQueryMetric
076: .getVisible()) {
077: return false;
078: }
079: }
080: return true;
081: }
082:
083: /*public ExecutedQuery getExecutedQuery(ReportQuery query) {
084: ExecutedQuery returnExecQuery = null;
085: try {
086: Iterator iterator = getExecutedQueries().keySet().iterator();
087: while(iterator.hasNext()) {
088: ByteArrayOutputStream currentKey = (ByteArrayOutputStream) iterator.next();
089: ReportQuery reportQuery = (ReportQuery) getObject(currentKey);
090: if(query.getCubeQuery().equivalentQuery(reportQuery.getCubeQuery())) {
091: if(sameMetricsStates(query, reportQuery) && (query.isVisibleTotals() == reportQuery.isVisibleTotals())) {
092: ByteArrayOutputStream byteArrayOutputStream = (ByteArrayOutputStream) getExecutedQueries().get(currentKey);
093: returnExecQuery = (ExecutedQuery) getObject(byteArrayOutputStream);
094: break;
095: }
096: }
097: }
098: } catch(Exception e) {
099: e.printStackTrace();
100: }
101: return returnExecQuery;
102: }*/
103:
104: public ExecutedQuery getExecutedQuery(ReportQuery query) {
105: ExecutedQuery returnExecQuery = null;
106: try {
107: Iterator iterator = getExecutedQueries().keySet()
108: .iterator();
109: while (iterator.hasNext()) {
110: ReportQuery reportQuery = (ReportQuery) iterator.next();
111: if (query.getCubeQuery().equivalentQuery(
112: reportQuery.getCubeQuery())) {
113: if (sameMetricsStates(query, reportQuery)
114: && (query.isVisibleTotals() == reportQuery
115: .isVisibleTotals())) {
116: ByteArrayOutputStream byteArrayOutputStream = (ByteArrayOutputStream) getExecutedQueries()
117: .get(reportQuery);
118: returnExecQuery = (ExecutedQuery) getObject(byteArrayOutputStream);
119: break;
120: }
121: }
122: }
123: } catch (Exception e) {
124: e.printStackTrace();
125: }
126: return returnExecQuery;
127: }
128:
129: private byte[] getBytes(Object obj) throws java.io.IOException {
130: ByteArrayOutputStream bos = new ByteArrayOutputStream();
131: ObjectOutputStream oos = new ObjectOutputStream(bos);
132: oos.writeObject(obj);
133: oos.flush();
134: oos.close();
135: bos.close();
136: byte[] data = bos.toByteArray();
137: return data;
138: }
139:
140: private Object getObject(ByteArrayOutputStream stream)
141: throws IOException, ClassNotFoundException {
142: ByteArrayInputStream inputStream = new ByteArrayInputStream(
143: stream.toByteArray());
144: ObjectInputStream objectInputStream = new ObjectInputStream(
145: inputStream);
146: return objectInputStream.readObject();
147: }
148:
149: private HashMap getExecutedQueries() {
150: if (queries == null) {
151: queries = new HashMap();
152: }
153: return queries;
154: }
155: }
|