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: * EJBQLFieldsReader.java
028: *
029: * Created on February 22, 2006, 1:27 PM
030: *
031: */
032:
033: package it.businesslogic.ireport.data.ejbql;
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.gui.MainFrame;
041: import it.businesslogic.ireport.gui.ReportQueryDialog;
042: import it.businesslogic.ireport.util.Misc;
043:
044: import java.util.Collection;
045: import java.util.Enumeration;
046: import java.util.HashMap;
047: import java.util.Iterator;
048: import java.util.List;
049: import java.util.Map;
050: import java.util.Set;
051: import java.util.Vector;
052:
053: import javax.persistence.EntityManager;
054: import javax.persistence.Query;
055:
056: /**
057: *
058: * @author gtoffoli
059: */
060: public class EJBQLFieldsReader {
061:
062: private Interpreter interpreter = null;
063: private Vector reportParameters = null;
064: private String queryString = "";
065: private HashMap queryParameters = new HashMap();
066: private String singleClassName = null;
067:
068: /** Creates a new instance of HQLFieldsReader */
069: public EJBQLFieldsReader(String queryStr, Vector reportParameters) {
070:
071: this .setQueryString(queryStr);
072: this .setReportParameters(reportParameters);
073: }
074:
075: public String prepareQuery() throws Exception {
076: Enumeration enumParams = getReportParameters().elements();
077:
078: while (enumParams.hasMoreElements()) {
079:
080: JRParameter param = (JRParameter) enumParams.nextElement();
081: String parameterName = param.getName();
082:
083: if (queryString.indexOf("$P!{" + parameterName + "}") > 0) {
084: Object paramVal = ReportQueryDialog
085: .recursiveInterpreter(getInterpreter(), param
086: .getDefaultValueExpression(),
087: getReportParameters());
088:
089: if (paramVal == null) {
090: paramVal = "";
091: }
092:
093: queryString = Misc.string_replace("" + paramVal, "$P!{"
094: + parameterName + "}", queryString);
095: } else if (getQueryString().indexOf(
096: "$P{" + parameterName + "}") > 0) {
097: Object paramVal = ReportQueryDialog
098: .recursiveInterpreter(getInterpreter(), param
099: .getDefaultValueExpression(),
100: getReportParameters());
101: String parameterReplacement = "_"
102: + getLiteral(parameterName);
103:
104: queryParameters.put(parameterReplacement, paramVal);
105:
106: }
107: }
108: return queryString;
109: }
110:
111: /**
112: * Binds a paramter value to a query paramter.
113: *
114: * @param parameter the report parameter
115: */
116: protected void setParameter(Query query, String hqlParamName,
117: Object parameterValue) throws Exception {
118: //ClassLoader cl = MainFrame.getMainInstance().getReportClassLoader();
119:
120: if (parameterValue == null) {
121: query
122: .setParameter(getLiteral(hqlParamName),
123: parameterValue);
124: return;
125: }
126:
127: query.setParameter(hqlParamName, parameterValue);
128: }
129:
130: private Interpreter prepareExpressionEvaluator()
131: throws bsh.EvalError {
132:
133: Interpreter interpreter = new Interpreter();
134: interpreter.setClassLoader(interpreter.getClass()
135: .getClassLoader());
136: return interpreter;
137: }
138:
139: /**
140: * Takes a name and returns the same if it is a Java identifier;
141: * else it substitutes the illegal characters so that it can be an identifier
142: *
143: * @param name
144: */
145: public static String getLiteral(String name) {
146: if (isValidLiteral(name)) {
147: return name;
148: }
149:
150: StringBuffer buffer = new StringBuffer(name.length() + 5);
151:
152: char[] literalChars = new char[name.length()];
153: name.getChars(0, literalChars.length, literalChars, 0);
154:
155: for (int i = 0; i < literalChars.length; i++) {
156: if (i == 0
157: && !Character
158: .isJavaIdentifierStart(literalChars[i])) {
159: buffer.append((int) literalChars[i]);
160: } else if (i != 0
161: && !Character.isJavaIdentifierPart(literalChars[i])) {
162: buffer.append((int) literalChars[i]);
163: } else {
164: buffer.append(literalChars[i]);
165: }
166: }
167:
168: return buffer.toString();
169: }
170:
171: /**
172: * Checks if the input is a valid Java literal
173: * @param literal
174: * @author Gaganis Giorgos (gaganis@users.sourceforge.net)
175: */
176: private static boolean isValidLiteral(String literal) {
177: boolean result = true;
178:
179: char[] literalChars = new char[literal.length()];
180: literal.getChars(0, literalChars.length, literalChars, 0);
181:
182: for (int i = 0; i < literalChars.length; i++) {
183: if (i == 0
184: && !Character
185: .isJavaIdentifierStart(literalChars[i])) {
186: result = false;
187: break;
188: }
189:
190: if (i != 0
191: && !Character.isJavaIdentifierPart(literalChars[i])) {
192: result = false;
193: break;
194: }
195: }
196:
197: return result;
198: }
199:
200: public Interpreter getInterpreter() {
201:
202: if (interpreter == null) {
203: try {
204: interpreter = prepareExpressionEvaluator();
205: } catch (Exception ex) {
206:
207: }
208: }
209: return interpreter;
210: }
211:
212: public void setInterpreter(Interpreter interpreter) {
213: this .interpreter = interpreter;
214: }
215:
216: public Vector getReportParameters() {
217: return reportParameters;
218: }
219:
220: public void setReportParameters(Vector reportParameters) {
221: this .reportParameters = reportParameters;
222: }
223:
224: public String getQueryString() {
225: return queryString;
226: }
227:
228: public void setQueryString(String queryString) {
229: this .queryString = queryString;
230: }
231:
232: public HashMap getQueryParameters() {
233: return queryParameters;
234: }
235:
236: public void setQueryParameters(HashMap queryParameters) {
237: this .queryParameters = queryParameters;
238: }
239:
240: public Vector readFields() throws Exception {
241: prepareQuery();
242:
243: Vector fields = new Vector();
244: EntityManager em = null;
245: Query query = null;
246: setSingleClassName(null);
247: try {
248:
249: IReportConnection conn = (IReportConnection) MainFrame
250: .getMainInstance().getProperties().get(
251: "DefaultConnection");
252: if (!(conn instanceof EJBQLConnection)) {
253: throw new Exception("No EJBQL connection selected.");
254: }
255:
256: em = ((EJBQLConnection) conn).getEntityManager();
257:
258: query = em.createQuery(queryString);
259:
260: for (Iterator iter = queryParameters.keySet().iterator(); iter
261: .hasNext();) {
262: String parameterName = (String) iter.next();
263: query.setParameter(parameterName, queryParameters
264: .get(parameterName));
265: }
266:
267: query.setMaxResults(1);
268: List list = query.getResultList();
269:
270: if (list.size() > 0) {
271: Object obj = list.get(0);
272:
273: if (obj != null && obj.getClass().isArray()) {
274: // Fields array...
275: Object[] fiels_obj = (Object[]) obj;
276: for (int i = 0; i < fiels_obj.length; ++i) {
277: fields.add(createField(fiels_obj[i], i));
278: }
279: } else {
280: setSingleClassName(obj.getClass().getName());
281: fields = getFields(obj);
282: }
283: }
284:
285: return fields;
286:
287: } catch (Exception ex) {
288: ex.printStackTrace();
289: throw ex;
290: } finally {
291:
292: }
293: }
294:
295: public Vector getFields(Object obj) {
296:
297: Vector fields = new Vector();
298: java.beans.PropertyDescriptor[] pd = org.apache.commons.beanutils.PropertyUtils
299: .getPropertyDescriptors(obj.getClass());
300: for (int nd = 0; nd < pd.length; ++nd) {
301: String fieldName = pd[nd].getName();
302: if (pd[nd].getPropertyType() != null
303: && pd[nd].getReadMethod() != null) {
304: String returnType = pd[nd].getPropertyType().getName();
305: it.businesslogic.ireport.JRField field = new it.businesslogic.ireport.JRField(
306: fieldName, Misc.getJRFieldType(returnType));
307: field.setDescription(""); //Field returned by " +methods[i].getName() + " (real type: "+ returnType +")");
308: fields.addElement(field);
309: }
310: }
311:
312: return fields;
313:
314: }
315:
316: public JRField createField(Object obj, int pos) {
317: String fieldName = "COLUMN_" + (pos + 1);
318: if (pos < 0) {
319: fieldName = obj.getClass().getName();
320: if (fieldName.indexOf(".") > 0) {
321: fieldName = fieldName
322: .substring(fieldName.indexOf(".") + 1);
323: }
324: if (fieldName.length() == 0)
325: fieldName = "COLUMN_1";
326: }
327: JRField field = new JRField(fieldName, obj.getClass().getName());
328: field.setDescription("");
329:
330: return field;
331: }
332:
333: public String getSingleClassName() {
334: return singleClassName;
335: }
336:
337: public void setSingleClassName(String singleClassName) {
338: this.singleClassName = singleClassName;
339: }
340:
341: }
|