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 ; 7 import java.util.List ; 8 import java.util.HashMap ; 9 import java.io.*; 10 11 14 15 public class ExecutedQueryCollection implements Serializable{ 16 17 private HashMap 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 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 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 e) { 54 e.printStackTrace(); 55 } 56 } 57 return returnVal; 58 } 59 60 private boolean sameMetricsStates(ReportQuery query, ReportQuery currentQuery) { 61 List queryMetrics = query.getMetrics(); 62 List 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 93 94 public ExecutedQuery getExecutedQuery(ReportQuery query) { 95 ExecutedQuery returnExecQuery = null; 96 try { 97 Iterator 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 e) { 109 e.printStackTrace(); 110 } 111 return returnExecQuery; 112 } 113 114 private byte[] getBytes(Object obj) throws java.io.IOException { 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 getObject(ByteArrayOutputStream stream) throws IOException, ClassNotFoundException { 126 ByteArrayInputStream inputStream = new ByteArrayInputStream(stream.toByteArray()); 127 ObjectInputStream objectInputStream = new ObjectInputStream(inputStream); 128 return objectInputStream.readObject(); 129 } 130 131 132 private HashMap getExecutedQueries() { 133 if(queries == null) { 134 queries = new HashMap (); 135 } 136 return queries; 137 } 138 } 139 | Popular Tags |