KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > businesslogic > ireport > data > olap > OLAPQueryExecuter


1 /*
2  * Copyright (C) 2005 - 2006 JasperSoft Corporation. All rights reserved.
3  * http://www.jaspersoft.com.
4  *
5  * Unless you have purchased a commercial license agreement from JasperSoft,
6  * the following license terms apply:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as published by
10  * the Free Software Foundation.
11  *
12  * This program is distributed WITHOUT ANY WARRANTY; and without the
13  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
18  * or write to:
19  *
20  * Free Software Foundation, Inc.,
21  * 59 Temple Place - Suite 330,
22  * Boston, MA USA 02111-1307
23  *
24  *
25  *
26  *
27  * OLAPQueryExecuter.java
28  *
29  * Created on February 22, 2006, 1:27 PM
30  *
31  */

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 JavaDoc;
46 import java.util.Enumeration JavaDoc;
47 import java.util.HashMap JavaDoc;
48 import java.util.Iterator JavaDoc;
49 import java.util.List JavaDoc;
50 import java.util.Map JavaDoc;
51 import java.util.Set JavaDoc;
52 import java.util.Vector JavaDoc;
53
54 import mondrian.olap.Connection;
55 import mondrian.olap.Query;
56 import mondrian.olap.Result;
57
58
59 /**
60  *
61  * @author gtoffoli
62  */

63 public class OLAPQueryExecuter {
64     
65     private Interpreter interpreter = null;
66     private Vector JavaDoc reportParameters = null;
67     private String JavaDoc queryString = "";
68     private HashMap JavaDoc queryParameters = new HashMap JavaDoc();
69         
70     /** Creates a new instance of HQLFieldsReader */
71     public OLAPQueryExecuter(String JavaDoc queryStr, Vector JavaDoc reportParameters) {
72         
73         this.setQueryString(queryStr);
74         this.setReportParameters(reportParameters);
75         
76     }
77     
78     public String JavaDoc prepareQuery() throws Exception JavaDoc
79     {
80        Enumeration JavaDoc enumParams = getReportParameters().elements();
81        
82        while( enumParams.hasMoreElements() ) {
83            
84            JRParameter param = (JRParameter)enumParams.nextElement();
85            String JavaDoc parameterName = param.getName();
86            
87            if (queryString.indexOf("$P!{" + parameterName + "}") > 0)
88            {
89                 Object JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc getReportParameters() {
145         return reportParameters;
146     }
147
148     public void setReportParameters(Vector JavaDoc reportParameters) {
149         this.reportParameters = reportParameters;
150     }
151
152     public String JavaDoc getQueryString() {
153         return queryString;
154     }
155
156     public void setQueryString(String JavaDoc queryString) {
157         this.queryString = queryString;
158     }
159
160     public HashMap JavaDoc getQueryParameters() {
161         return queryParameters;
162     }
163
164     public void setQueryParameters(HashMap JavaDoc queryParameters) {
165         this.queryParameters = queryParameters;
166     }
167     
168
169     public Query createOlapQuery() throws Exception JavaDoc
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 JavaDoc("No OLAP (Mondrian) connection selected.");
178                }
179               
180                Connection mconn = ((MondrianConnection)conn).getMondrianConnection();
181                if (mconn == null)
182                {
183                  throw new Exception JavaDoc("The supplied mondrian.olap.Connection object is null.");
184                }
185                         
186                Query query = mconn.parseQuery( queryString );
187                
188                return query;
189
190            } catch (Exception JavaDoc ex)
191            {
192                ex.printStackTrace();
193                throw ex;
194             } finally {
195                
196               
197            }
198     }
199     
200     public Result executeOlapQuery() throws Exception JavaDoc
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 JavaDoc("No OLAP (Mondrian) connection selected.");
209                }
210               
211                Connection mconn = ((MondrianConnection)conn).getMondrianConnection();
212                if (mconn == null)
213                {
214                  throw new Exception JavaDoc("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 JavaDoc ex)
223            {
224                ex.printStackTrace();
225                throw ex;
226             } finally {
227                
228               
229            }
230     }
231     
232     public Vector JavaDoc getFields(Object JavaDoc obj)
233     {
234         
235         Vector JavaDoc fields = new Vector JavaDoc();
236         java.beans.PropertyDescriptor JavaDoc[] pd = org.apache.commons.beanutils.PropertyUtils.getPropertyDescriptors(obj.getClass());
237         for (int nd =0; nd < pd.length; ++nd)
238         {
239                    String JavaDoc fieldName = pd[nd].getName();
240                    if (pd[nd].getPropertyType() != null && pd[nd].getReadMethod() != null)
241                    {
242                        String JavaDoc returnType = pd[nd].getPropertyType().getName();
243                        it.businesslogic.ireport.JRField field = new it.businesslogic.ireport.JRField(fieldName, Misc.getJRFieldType(returnType));
244                        field.setDescription(""); //Field returned by " +methods[i].getName() + " (real type: "+ returnType +")");
245
fields.addElement(field);
246                    }
247         }
248         
249         return fields;
250     }
251
252
253
254 }
255
Popular Tags