1 32 33 package it.businesslogic.ireport.data.olap; 34 35 import bsh.Interpreter; 36 import it.businesslogic.ireport.IReportConnection; 37 import it.businesslogic.ireport.JRField; 38 import it.businesslogic.ireport.JRParameter; 39 import it.businesslogic.ireport.connection.EJBQLConnection; 40 import it.businesslogic.ireport.connection.MondrianConnection; 41 import it.businesslogic.ireport.gui.MainFrame; 42 import it.businesslogic.ireport.gui.ReportQueryDialog; 43 import it.businesslogic.ireport.util.Misc; 44 45 import java.util.Collection ; 46 import java.util.Enumeration ; 47 import java.util.HashMap ; 48 import java.util.Iterator ; 49 import java.util.List ; 50 import java.util.Map ; 51 import java.util.Set ; 52 import java.util.Vector ; 53 54 import mondrian.olap.Connection; 55 import mondrian.olap.Query; 56 import mondrian.olap.Result; 57 58 59 63 public class OLAPQueryExecuter { 64 65 private Interpreter interpreter = null; 66 private Vector reportParameters = null; 67 private String queryString = ""; 68 private HashMap queryParameters = new HashMap (); 69 70 71 public OLAPQueryExecuter(String queryStr, Vector reportParameters) { 72 73 this.setQueryString(queryStr); 74 this.setReportParameters(reportParameters); 75 76 } 77 78 public String prepareQuery() throws Exception 79 { 80 Enumeration enumParams = getReportParameters().elements(); 81 82 while( enumParams.hasMoreElements() ) { 83 84 JRParameter param = (JRParameter)enumParams.nextElement(); 85 String parameterName = param.getName(); 86 87 if (queryString.indexOf("$P!{" + parameterName + "}") > 0) 88 { 89 Object paramVal = ReportQueryDialog.recursiveInterpreter( getInterpreter(), param.getDefaultValueExpression(),getReportParameters()); 90 91 if (paramVal == null) 92 { 93 paramVal = ""; 94 } 95 96 queryString = Misc.string_replace(""+paramVal, "$P!{" + parameterName + "}", queryString); 97 } 98 else if (getQueryString().indexOf("$P{" + parameterName + "}") > 0) 99 { 100 Object paramVal = ReportQueryDialog.recursiveInterpreter( getInterpreter(), param.getDefaultValueExpression(),getReportParameters()); 101 102 if (paramVal == null) 103 { 104 paramVal = ""; 105 } 106 107 queryString = Misc.string_replace(""+paramVal, "$P!{" + parameterName + "}", queryString); 108 } 109 } 110 return queryString; 111 } 112 113 private Interpreter prepareExpressionEvaluator() throws bsh.EvalError { 114 115 Interpreter interpreter = new Interpreter(); 116 interpreter.setClassLoader(interpreter.getClass().getClassLoader()); 117 return interpreter; 118 } 119 120 121 122 123 124 125 126 public Interpreter getInterpreter() { 127 128 if (interpreter == null) 129 { 130 try { 131 interpreter = prepareExpressionEvaluator(); 132 } catch (Exception ex) 133 { 134 135 } 136 } 137 return interpreter; 138 } 139 140 public void setInterpreter(Interpreter interpreter) { 141 this.interpreter = interpreter; 142 } 143 144 public Vector getReportParameters() { 145 return reportParameters; 146 } 147 148 public void setReportParameters(Vector reportParameters) { 149 this.reportParameters = reportParameters; 150 } 151 152 public String getQueryString() { 153 return queryString; 154 } 155 156 public void setQueryString(String queryString) { 157 this.queryString = queryString; 158 } 159 160 public HashMap getQueryParameters() { 161 return queryParameters; 162 } 163 164 public void setQueryParameters(HashMap queryParameters) { 165 this.queryParameters = queryParameters; 166 } 167 168 169 public Query createOlapQuery() throws Exception 170 { 171 prepareQuery(); 172 173 try { 174 IReportConnection conn = (IReportConnection)MainFrame.getMainInstance().getProperties().get("DefaultConnection"); 175 if (!(conn instanceof MondrianConnection)) 176 { 177 throw new Exception ("No OLAP (Mondrian) connection selected."); 178 } 179 180 Connection mconn = ((MondrianConnection)conn).getMondrianConnection(); 181 if (mconn == null) 182 { 183 throw new Exception ("The supplied mondrian.olap.Connection object is null."); 184 } 185 186 Query query = mconn.parseQuery( queryString ); 187 188 return query; 189 190 } catch (Exception ex) 191 { 192 ex.printStackTrace(); 193 throw ex; 194 } finally { 195 196 197 } 198 } 199 200 public Result executeOlapQuery() throws Exception 201 { 202 prepareQuery(); 203 try { 204 205 IReportConnection conn = (IReportConnection)MainFrame.getMainInstance().getProperties().get("DefaultConnection"); 206 if (!(conn instanceof MondrianConnection)) 207 { 208 throw new Exception ("No OLAP (Mondrian) connection selected."); 209 } 210 211 Connection mconn = ((MondrianConnection)conn).getMondrianConnection(); 212 if (mconn == null) 213 { 214 throw new Exception ("The supplied mondrian.olap.Connection object is null."); 215 } 216 217 Query query = mconn.parseQuery( queryString ); 218 Result result = mconn.execute(query); 219 220 return result; 221 222 } catch (Exception ex) 223 { 224 ex.printStackTrace(); 225 throw ex; 226 } finally { 227 228 229 } 230 } 231 232 public Vector getFields(Object obj) 233 { 234 235 Vector fields = new Vector (); 236 java.beans.PropertyDescriptor [] pd = org.apache.commons.beanutils.PropertyUtils.getPropertyDescriptors(obj.getClass()); 237 for (int nd =0; nd < pd.length; ++nd) 238 { 239 String fieldName = pd[nd].getName(); 240 if (pd[nd].getPropertyType() != null && pd[nd].getReadMethod() != null) 241 { 242 String returnType = pd[nd].getPropertyType().getName(); 243 it.businesslogic.ireport.JRField field = new it.businesslogic.ireport.JRField(fieldName, Misc.getJRFieldType(returnType)); 244 field.setDescription(""); fields.addElement(field); 246 } 247 } 248 249 return fields; 250 } 251 252 253 254 } 255 | Popular Tags |