001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: */
013: package org.pentaho.plugin.jfreereport.components;
014:
015: import java.io.FileNotFoundException;
016: import java.io.InputStream;
017: import java.util.Iterator;
018: import java.util.Set;
019:
020: import javax.swing.table.TableModel;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024: import org.dom4j.Node;
025: import org.jfree.report.DataFactory;
026: import org.jfree.report.JFreeReport;
027: import org.jfree.report.ParameterDataRow;
028: import org.jfree.report.util.ReportProperties;
029: import org.pentaho.core.component.IDataComponent;
030: import org.pentaho.core.component.IPreparedComponent;
031: import org.pentaho.commons.connection.IPentahoResultSet;
032: import org.pentaho.core.repository.ISolutionRepository;
033: import org.pentaho.core.runtime.IRuntimeContext;
034: import org.pentaho.core.solution.IActionResource;
035: import org.pentaho.core.system.PentahoSystem;
036: import org.pentaho.messages.Messages;
037: import org.pentaho.plugin.jfreereport.AbstractJFreeReportComponent;
038: import org.pentaho.plugin.jfreereport.helper.PentahoDataFactory;
039: import org.pentaho.plugin.jfreereport.helper.PentahoTableDataFactory;
040: import org.pentaho.plugin.jfreereport.helper.PentahoTableModel;
041: import org.pentaho.plugin.jfreereport.helper.ReportUtils;
042:
043: /**
044: * This is step 2 out of 3. This class is a wrapper around an other component,
045: * for instance the SQL- or MDX query component.
046: *
047: * @author Thomas Morgner
048: */
049: public class JFreeReportDataComponent extends
050: AbstractJFreeReportComponent {
051:
052: private static final long serialVersionUID = -1708477862117476001L;
053:
054: private IDataComponent dataComponent;
055:
056: public JFreeReportDataComponent() {
057: }
058:
059: /**
060: * Validates the parameters of this action.
061: *
062: * @return
063: */
064: protected boolean validateAction() {
065: return true;
066: }
067:
068: public IDataComponent getDataComponent() {
069: return dataComponent;
070: }
071:
072: protected boolean validateSystemSettings() {
073: return true;
074: }
075:
076: public void done() {
077: // help the garbage collector ...
078: if (dataComponent != null) {
079: dataComponent.dispose();
080: dataComponent.done();
081: }
082: dataComponent = null;
083: }
084:
085: private PentahoTableDataFactory getQueryComponentDataFactory()
086: throws ClassNotFoundException, InstantiationException,
087: IllegalAccessException, Exception {
088: PentahoTableDataFactory factory = null;
089: dataComponent = null;
090: final Node sourceNode = getComponentDefinition()
091: .selectSingleNode(DATACOMPONENT_SOURCE);
092: if (sourceNode != null) {
093: String dataComponentClass = sourceNode.getText();
094: if (DATACOMPONENT_SQLSOURCE
095: .equalsIgnoreCase(dataComponentClass)) {
096: dataComponentClass = DATACOMPONENT_SQLCLASS;
097: } else if (DATACOMPONENT_MDXSOURCE
098: .equalsIgnoreCase(dataComponentClass)) {
099: dataComponentClass = DATACOMPONENT_MDXCLASS;
100: }
101: if (dataComponentClass != null) {
102: try {
103: final Class componentClass = Class
104: .forName(dataComponentClass);
105: dataComponent = (IDataComponent) componentClass
106: .newInstance();
107: dataComponent.setInstanceId(getInstanceId());
108: dataComponent.setActionName(getActionName());
109: dataComponent.setProcessId(getProcessId());
110: dataComponent
111: .setComponentDefinition(getComponentDefinition());
112: dataComponent
113: .setRuntimeContext(getRuntimeContext());
114: dataComponent.setSession(getSession());
115: dataComponent.setLoggingLevel(getLoggingLevel());
116: dataComponent.setMessages(getMessages());
117: // if that fails, then we know we messed up again.
118: // Abort, we cant continue anyway.
119: if ((dataComponent.validate() == IRuntimeContext.RUNTIME_CONTEXT_VALIDATE_OK)
120: && dataComponent.init()
121: && (dataComponent.execute() == IRuntimeContext.RUNTIME_STATUS_SUCCESS)) {
122: final IPentahoResultSet resultset = dataComponent
123: .getResultSet();
124: factory = new PentahoTableDataFactory(
125: DATACOMPONENT_DEFAULTINPUT,
126: new PentahoTableModel(resultset));
127: } else {
128: throw new IllegalArgumentException(
129: Messages
130: .getErrorString("JFreeReport.ERROR_0021_DATA_COMPONENT_FAILED"));
131: }
132: } catch (ClassNotFoundException e) {
133: } catch (InstantiationException e) {
134: // TODO Auto-generated catch block
135: e.printStackTrace();
136: } catch (IllegalAccessException e) {
137: // TODO Auto-generated catch block
138: e.printStackTrace();
139: }
140: }
141: }
142: return factory;
143: }
144:
145: private PentahoTableDataFactory getJarDataFactory()
146: throws Exception {
147: PentahoTableDataFactory factory = null;
148: if (isDefinedResource(DATACOMPONENT_JARINPUT)) {
149: final IActionResource resource = getResource(DATACOMPONENT_JARINPUT);
150: final ISolutionRepository solutionRepository = PentahoSystem
151: .getSolutionRepository(getSession());
152: final InputStream in;
153: try {
154: in = solutionRepository.getResourceInputStream(
155: resource, true);
156: try {
157: // not being able to read a single char is definitly a big boo ..
158: if (in.read() == -1) {
159: throw new Exception(
160: Messages
161: .getErrorString("JFreeReport.ERROR_0009_REPORT_JAR_UNREADABLE")); //$NON-NLS-1$
162: } else {
163: final ClassLoader loader = ReportUtils
164: .createJarLoader(getSession(), resource);
165: if (loader == null) {
166: throw new Exception(
167: Messages
168: .getString("JFreeReportDataComponent.ERROR_0035_COULD_NOT_CREATE_CLASSLOADER")); //$NON-NLS-1$
169: } else if (!isDefinedInput(DATACOMPONENT_CLASSLOCINPUT)) {
170: throw new Exception(
171: Messages
172: .getErrorString("JFreeReport.ERROR_0012_CLASS_LOCATION_MISSING")); //$NON-NLS-1$
173: } else {
174: final String classLocation = getInputStringValue(DATACOMPONENT_CLASSLOCINPUT);
175: // Get input parameters, and set them as properties in the report
176: // object.
177: final ReportProperties reportProperties = new ReportProperties();
178: final Set paramNames = getInputNames();
179: final Iterator it = paramNames.iterator();
180: while (it.hasNext()) {
181: final String paramName = (String) it
182: .next();
183: final Object paramValue = getInputValue(paramName);
184: if (paramValue instanceof Object[]) {
185: final Object[] values = (Object[]) paramValue;
186: final StringBuffer valuesBuffer = new StringBuffer();
187: // TODO support non-string items
188: for (int i = 0; i < values.length; i++) {
189: if (i == 0) {
190: valuesBuffer
191: .append(values[i]
192: .toString());
193: } else {
194: valuesBuffer
195: .append(',')
196: .append(
197: values[i]
198: .toString());
199: }
200: }
201: reportProperties.put(paramName,
202: valuesBuffer.toString());
203: } else {
204: reportProperties.put(paramName,
205: paramValue);
206: }
207: }
208:
209: final DataFactory dataFactory = new PentahoDataFactory(
210: loader);
211: final TableModel model = dataFactory
212: .queryData(classLocation,
213: new ParameterDataRow(
214: reportProperties));
215:
216: factory = new PentahoTableDataFactory(
217: DATACOMPONENT_DEFAULTINPUT, model);
218: }
219: }
220: } catch (Exception e) {
221: throw new Exception(
222: Messages
223: .getErrorString("JFreeReport.ERROR_0009_REPORT_JAR_UNREADABLE"));
224: }
225: } catch (FileNotFoundException e1) {
226: throw new Exception(Messages.getErrorString(
227: "JFreeReport.ERROR_0010_REPORT_JAR_MISSING",
228: resource.getAddress()));
229: }
230: }
231: return factory;
232: }
233:
234: private PentahoTableDataFactory getInputParamDataFactory() {
235:
236: PentahoTableDataFactory factory = null;
237: if (isDefinedInput(DATACOMPONENT_DATAINPUT)
238: || isDefinedInput(DATACOMPONENT_DEFAULTINPUT)) {
239:
240: factory = new PentahoTableDataFactory();
241: final Iterator iter = getInputNames().iterator();
242: while (iter.hasNext()) {
243: String name = (String) iter.next();
244: final Object dataObject = getInputValue(name);
245: // if input name is "data", rename to "default" which is the name that jfreereport is expecting.
246: if (name.equals(DATACOMPONENT_DATAINPUT)) {
247: name = DATACOMPONENT_DEFAULTINPUT;
248: }
249: if (dataObject instanceof IPreparedComponent) {
250: final IPreparedComponent comp = (IPreparedComponent) dataObject;
251: factory.addPreparedComponent(name, comp);
252: } else if (dataObject instanceof IPentahoResultSet) {
253: final IPentahoResultSet resultset = (IPentahoResultSet) dataObject;
254: resultset.beforeFirst();
255: factory.addTable(name, new PentahoTableModel(
256: resultset));
257: } else if (dataObject instanceof TableModel) {
258: final TableModel model = (TableModel) dataObject;
259: factory.addTable(name, model);
260: }
261: }
262: }
263: return factory;
264: }
265:
266: private PentahoTableDataFactory getDataFactory()
267: throws ClassNotFoundException, InstantiationException,
268: IllegalAccessException, Exception {
269: PentahoTableDataFactory factory = null;
270: factory = getQueryComponentDataFactory();
271: if (factory == null) {
272: factory = getInputParamDataFactory();
273: }
274: if (factory == null) {
275: factory = getJarDataFactory();
276: }
277: if (factory == null) {
278: boolean result = false;
279: // hey, maybe we dont have to do anything ...
280: if (isDefinedInput(DATACOMPONENT_REPORTTEMP_OBJINPUT)) {
281: final Object maybeReport = getInputValue(DATACOMPONENT_REPORTTEMP_OBJINPUT);
282: if (maybeReport instanceof JFreeReport) {
283: final JFreeReport report = (JFreeReport) maybeReport;
284: if (report.getDataFactory() != null) {
285: // ok, thats fine. The data factory will take care of getting
286: // the data
287: result = true;
288: }
289: }
290: }
291: if (!result) {
292: throw new Exception(
293: Messages
294: .getString("JFreeReport.ERROR_0022_DATA_INPUT_INVALID_OBJECT"));
295: }
296: }
297: return factory;
298: }
299:
300: protected boolean executeAction() throws Throwable {
301: boolean result = false;
302: try {
303: PentahoTableDataFactory factory = getDataFactory();
304: if (factory != null) {
305: addTempParameterObject(
306: DATACOMPONENT_REPORTTEMP_DATAFACTORY, factory);
307: }
308: result = true;
309: } catch (ClassNotFoundException ex) {
310: error(
311: Messages
312: .getErrorString("JFreeReport.ERROR_0021_DATA_COMPONENT_FAILED"), ex); //$NON-NLS-1$
313: } catch (InstantiationException ex) {
314: error(
315: Messages
316: .getErrorString("JFreeReport.ERROR_0021_DATA_COMPONENT_FAILED"), ex); //$NON-NLS-1$
317: } catch (IllegalAccessException ex) {
318: error(
319: Messages
320: .getErrorString("JFreeReport.ERROR_0021_DATA_COMPONENT_FAILED"), ex); //$NON-NLS-1$
321: } catch (Exception ex) {
322: error(ex.getMessage()); //$NON-NLS-1$
323: }
324: return result;
325: }
326:
327: public boolean init() {
328: return true;
329: }
330:
331: public Log getLogger() {
332: return LogFactory.getLog(JFreeReportDataComponent.class);
333: }
334: }
|