001: /*
002: * Copyright (C) 2005 - 2008 JasperSoft Corporation. All rights reserved.
003: * http://www.jaspersoft.com.
004: *
005: * Unless you have purchased a commercial license agreement from JasperSoft,
006: * the following license terms apply:
007: *
008: * This program is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License version 2 as published by
010: * the Free Software Foundation.
011: *
012: * This program is distributed WITHOUT ANY WARRANTY; and without the
013: * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
014: * See the GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
018: * or write to:
019: *
020: * Free Software Foundation, Inc.,
021: * 59 Temple Place - Suite 330,
022: * Boston, MA USA 02111-1307
023: *
024: *
025: *
026: *
027: * OLAPQueryExecuter.java
028: *
029: * Created on February 22, 2006, 1:27 PM
030: *
031: */
032:
033: package it.businesslogic.ireport.data.olap;
034:
035: import bsh.Interpreter;
036: import it.businesslogic.ireport.IReportConnection;
037: import it.businesslogic.ireport.JRField;
038: import it.businesslogic.ireport.JRParameter;
039: import it.businesslogic.ireport.connection.EJBQLConnection;
040: import it.businesslogic.ireport.connection.MondrianConnection;
041: import it.businesslogic.ireport.gui.MainFrame;
042: import it.businesslogic.ireport.gui.ReportQueryDialog;
043: import it.businesslogic.ireport.util.Misc;
044:
045: import java.util.Collection;
046: import java.util.Enumeration;
047: import java.util.HashMap;
048: import java.util.Iterator;
049: import java.util.List;
050: import java.util.Map;
051: import java.util.Set;
052: import java.util.Vector;
053:
054: import mondrian.olap.Connection;
055: import mondrian.olap.Query;
056: import mondrian.olap.Result;
057:
058: /**
059: *
060: * @author gtoffoli
061: */
062: public class OLAPQueryExecuter {
063:
064: private Interpreter interpreter = null;
065: private Vector reportParameters = null;
066: private String queryString = "";
067: private HashMap queryParameters = new HashMap();
068:
069: /** Creates a new instance of HQLFieldsReader */
070: public OLAPQueryExecuter(String queryStr, Vector reportParameters) {
071:
072: this .setQueryString(queryStr);
073: this .setReportParameters(reportParameters);
074:
075: }
076:
077: public String prepareQuery() throws Exception {
078: Enumeration enumParams = getReportParameters().elements();
079:
080: while (enumParams.hasMoreElements()) {
081:
082: JRParameter param = (JRParameter) enumParams.nextElement();
083: String parameterName = param.getName();
084:
085: if (queryString.indexOf("$P!{" + parameterName + "}") > 0) {
086: Object paramVal = ReportQueryDialog
087: .recursiveInterpreter(getInterpreter(), param
088: .getDefaultValueExpression(),
089: getReportParameters());
090:
091: if (paramVal == null) {
092: paramVal = "";
093: }
094:
095: queryString = Misc.string_replace("" + paramVal, "$P!{"
096: + parameterName + "}", queryString);
097: } else if (getQueryString().indexOf(
098: "$P{" + parameterName + "}") > 0) {
099: Object paramVal = ReportQueryDialog
100: .recursiveInterpreter(getInterpreter(), param
101: .getDefaultValueExpression(),
102: getReportParameters());
103:
104: if (paramVal == null) {
105: paramVal = "";
106: }
107:
108: queryString = Misc.string_replace("" + paramVal, "$P!{"
109: + parameterName + "}", queryString);
110: }
111: }
112: return queryString;
113: }
114:
115: private Interpreter prepareExpressionEvaluator()
116: throws bsh.EvalError {
117:
118: Interpreter interpreter = new Interpreter();
119: interpreter.setClassLoader(interpreter.getClass()
120: .getClassLoader());
121: return interpreter;
122: }
123:
124: public Interpreter getInterpreter() {
125:
126: if (interpreter == null) {
127: try {
128: interpreter = prepareExpressionEvaluator();
129: } catch (Exception ex) {
130:
131: }
132: }
133: return interpreter;
134: }
135:
136: public void setInterpreter(Interpreter interpreter) {
137: this .interpreter = interpreter;
138: }
139:
140: public Vector getReportParameters() {
141: return reportParameters;
142: }
143:
144: public void setReportParameters(Vector reportParameters) {
145: this .reportParameters = reportParameters;
146: }
147:
148: public String getQueryString() {
149: return queryString;
150: }
151:
152: public void setQueryString(String queryString) {
153: this .queryString = queryString;
154: }
155:
156: public HashMap getQueryParameters() {
157: return queryParameters;
158: }
159:
160: public void setQueryParameters(HashMap queryParameters) {
161: this .queryParameters = queryParameters;
162: }
163:
164: public Query createOlapQuery() throws Exception {
165: prepareQuery();
166:
167: try {
168: IReportConnection conn = (IReportConnection) MainFrame
169: .getMainInstance().getProperties().get(
170: "DefaultConnection");
171: if (!(conn instanceof MondrianConnection)) {
172: throw new Exception(
173: "No OLAP (Mondrian) connection selected.");
174: }
175:
176: Connection mconn = ((MondrianConnection) conn)
177: .getMondrianConnection();
178: if (mconn == null) {
179: throw new Exception(
180: "The supplied mondrian.olap.Connection object is null.");
181: }
182:
183: Query query = mconn.parseQuery(queryString);
184:
185: return query;
186:
187: } catch (Exception ex) {
188: ex.printStackTrace();
189: throw ex;
190: } finally {
191:
192: }
193: }
194:
195: public Result executeOlapQuery() throws Exception {
196: prepareQuery();
197: try {
198:
199: IReportConnection conn = (IReportConnection) MainFrame
200: .getMainInstance().getProperties().get(
201: "DefaultConnection");
202: if (!(conn instanceof MondrianConnection)) {
203: throw new Exception(
204: "No OLAP (Mondrian) connection selected.");
205: }
206:
207: Connection mconn = ((MondrianConnection) conn)
208: .getMondrianConnection();
209: if (mconn == null) {
210: throw new Exception(
211: "The supplied mondrian.olap.Connection object is null.");
212: }
213:
214: Query query = mconn.parseQuery(queryString);
215: Result result = mconn.execute(query);
216:
217: return result;
218:
219: } catch (Exception ex) {
220: ex.printStackTrace();
221: throw ex;
222: } finally {
223:
224: }
225: }
226:
227: public Vector getFields(Object obj) {
228:
229: Vector fields = new Vector();
230: java.beans.PropertyDescriptor[] pd = org.apache.commons.beanutils.PropertyUtils
231: .getPropertyDescriptors(obj.getClass());
232: for (int nd = 0; nd < pd.length; ++nd) {
233: String fieldName = pd[nd].getName();
234: if (pd[nd].getPropertyType() != null
235: && pd[nd].getReadMethod() != null) {
236: String returnType = pd[nd].getPropertyType().getName();
237: it.businesslogic.ireport.JRField field = new it.businesslogic.ireport.JRField(
238: fieldName, Misc.getJRFieldType(returnType));
239: field.setDescription(""); //Field returned by " +methods[i].getName() + " (real type: "+ returnType +")");
240: fields.addElement(field);
241: }
242: }
243:
244: return fields;
245: }
246:
247: }
|