KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > calipso > reportgenerator > reportmanager > ExecutedQueryCollection


1 package com.calipso.reportgenerator.reportmanager;
2
3 import com.calipso.reportgenerator.common.ReportQuery;
4 import com.calipso.reportgenerator.common.QueryMetric;
5
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.HashMap JavaDoc;
9 import java.io.*;
10
11 /**
12  * Cache de instancias <code>ReportQuery</code> ya ejecutadas
13  */

14
15 public class ExecutedQueryCollection implements Serializable{
16
17   private HashMap JavaDoc queries;
18
19   public ExecutedQueryCollection() {
20   }
21
22   public void addExecutedQuery(ReportQuery query, ExecutedQuery executedQuery) {
23     try {
24       ByteArrayOutputStream value = new ByteArrayOutputStream();
25       value.write(getBytes(executedQuery));
26       getExecutedQueries().put(query, value);
27     } catch(Exception JavaDoc e) {
28       e.printStackTrace();
29     }
30   }
31
32   public int getCollectionSize() {
33     return getExecutedQueries().size();
34   }
35
36   public boolean alreadyExists(ReportQuery query, boolean queryCachedEnabled) {
37     boolean returnVal = false;
38     if(queryCachedEnabled) {
39       try {
40         if(getCollectionSize() == 0) {
41           returnVal = false;
42         } else {
43           Iterator JavaDoc iterator = getExecutedQueries().keySet().iterator();
44           while(iterator.hasNext()) {
45             ReportQuery currentQuery = (ReportQuery) iterator.next();
46             if(query.getCubeQuery().equivalentQuery(currentQuery.getCubeQuery())) {
47               if(sameMetricsStates(query, currentQuery) && (query.isVisibleTotals() == currentQuery.isVisibleTotals())) {
48                 return true;
49               }
50             }
51           }
52         }
53       } catch(Exception JavaDoc e) {
54           e.printStackTrace();
55       }
56     }
57     return returnVal;
58   }
59
60   private boolean sameMetricsStates(ReportQuery query, ReportQuery currentQuery) {
61     List JavaDoc queryMetrics = query.getMetrics();
62     List JavaDoc currentQueryMetrics = currentQuery.getMetrics();
63     for(int i = 0 ; i < queryMetrics.size() ; i++) {
64       QueryMetric queryMetric = (QueryMetric) queryMetrics.get(i);
65       QueryMetric currentQueryMetric = (QueryMetric) currentQueryMetrics.get(i);
66       if(queryMetric.getVisible() != currentQueryMetric.getVisible()) {
67         return false;
68       }
69     }
70     return true;
71   }
72
73   /*public ExecutedQuery getExecutedQuery(ReportQuery query) {
74     ExecutedQuery returnExecQuery = null;
75     try {
76       Iterator iterator = getExecutedQueries().keySet().iterator();
77       while(iterator.hasNext()) {
78         ByteArrayOutputStream currentKey = (ByteArrayOutputStream) iterator.next();
79         ReportQuery reportQuery = (ReportQuery) getObject(currentKey);
80         if(query.getCubeQuery().equivalentQuery(reportQuery.getCubeQuery())) {
81           if(sameMetricsStates(query, reportQuery) && (query.isVisibleTotals() == reportQuery.isVisibleTotals())) {
82             ByteArrayOutputStream byteArrayOutputStream = (ByteArrayOutputStream) getExecutedQueries().get(currentKey);
83             returnExecQuery = (ExecutedQuery) getObject(byteArrayOutputStream);
84             break;
85           }
86         }
87       }
88     } catch(Exception e) {
89      e.printStackTrace();
90     }
91     return returnExecQuery;
92   }*/

93
94   public ExecutedQuery getExecutedQuery(ReportQuery query) {
95     ExecutedQuery returnExecQuery = null;
96     try {
97       Iterator JavaDoc iterator = getExecutedQueries().keySet().iterator();
98       while(iterator.hasNext()) {
99         ReportQuery reportQuery = (ReportQuery) iterator.next();
100         if(query.getCubeQuery().equivalentQuery(reportQuery.getCubeQuery())) {
101           if(sameMetricsStates(query, reportQuery) && (query.isVisibleTotals() == reportQuery.isVisibleTotals())) {
102             ByteArrayOutputStream byteArrayOutputStream = (ByteArrayOutputStream) getExecutedQueries().get(reportQuery);
103             returnExecQuery = (ExecutedQuery) getObject(byteArrayOutputStream);
104             break;
105           }
106         }
107       }
108     } catch(Exception JavaDoc e) {
109      e.printStackTrace();
110     }
111     return returnExecQuery;
112   }
113
114   private byte[] getBytes(Object JavaDoc obj) throws java.io.IOException JavaDoc{
115      ByteArrayOutputStream bos = new ByteArrayOutputStream();
116      ObjectOutputStream oos = new ObjectOutputStream(bos);
117      oos.writeObject(obj);
118      oos.flush();
119      oos.close();
120      bos.close();
121      byte [] data = bos.toByteArray();
122      return data;
123   }
124
125   private Object JavaDoc getObject(ByteArrayOutputStream stream) throws IOException, ClassNotFoundException JavaDoc {
126     ByteArrayInputStream inputStream = new ByteArrayInputStream(stream.toByteArray());
127     ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
128     return objectInputStream.readObject();
129   }
130
131
132   private HashMap JavaDoc getExecutedQueries() {
133     if(queries == null) {
134       queries = new HashMap JavaDoc();
135     }
136     return queries;
137   }
138 }
139
Popular Tags