KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > plugin > jfreereport > components > JFreeReportDataComponent


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12 */

13 package org.pentaho.plugin.jfreereport.components;
14
15 import java.io.InputStream JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.Set JavaDoc;
18
19 import javax.swing.table.TableModel JavaDoc;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.dom4j.Node;
24 import org.jfree.report.DataFactory;
25 import org.jfree.report.JFreeReport;
26 import org.jfree.report.ParameterDataRow;
27 import org.jfree.report.util.ReportProperties;
28 import org.pentaho.core.component.IDataComponent;
29 import org.pentaho.core.connection.IPentahoResultSet;
30 import org.pentaho.core.repository.ISolutionRepository;
31 import org.pentaho.core.runtime.IRuntimeContext;
32 import org.pentaho.core.solution.IActionResource;
33 import org.pentaho.core.system.PentahoSystem;
34 import org.pentaho.messages.Messages;
35 import org.pentaho.plugin.jfreereport.AbstractJFreeReportComponent;
36 import org.pentaho.plugin.jfreereport.helper.PentahoDataFactory;
37 import org.pentaho.plugin.jfreereport.helper.PentahoTableModel;
38 import org.pentaho.plugin.jfreereport.helper.ReportUtils;
39
40 /**
41  * This is step 2 out of 3. This class is a wrapper around an other component,
42  * for instance the SQL- or MDX query component.
43  *
44  * @author Thomas Morgner
45  */

46 public class JFreeReportDataComponent extends AbstractJFreeReportComponent {
47
48   private static final long serialVersionUID = -1708477862117476001L;
49
50   private IDataComponent dataComponent;
51
52   public JFreeReportDataComponent() {
53   }
54
55   /**
56    * Creates the data component. A wrapped data component is only created, if
57    * there is a source node.
58    *
59    * @return
60    */

61   private IDataComponent createDataComponent() throws Exception JavaDoc {
62     Node sourceNode = getComponentDefinition().selectSingleNode(
63         DATACOMPONENT_SOURCE);
64     if (sourceNode != null) {
65       String JavaDoc dataComponentClass = sourceNode.getText();
66       if (DATACOMPONENT_SQLSOURCE.equalsIgnoreCase(dataComponentClass)) {
67         dataComponentClass = DATACOMPONENT_SQLCLASS;
68       } else if (DATACOMPONENT_MDXSOURCE.equalsIgnoreCase(dataComponentClass)) {
69         dataComponentClass = DATACOMPONENT_MDXCLASS;
70       }
71       if (dataComponentClass != null) {
72         Class JavaDoc componentClass = Class.forName(dataComponentClass);
73         IDataComponent dComponent = (IDataComponent) componentClass
74             .newInstance();
75         dComponent.setInstanceId(getInstanceId());
76         dComponent.setActionName(getActionName());
77         dComponent.setProcessId(getProcessId());
78         dComponent.setComponentDefinition(getComponentDefinition());
79         dComponent.setRuntimeContext(getRuntimeContext());
80         dComponent.setSession(getSession());
81         dComponent.setLoggingLevel(getLoggingLevel());
82         dComponent.setMessages(getMessages());
83         return dComponent;
84       }
85     }
86     return null;
87   }
88
89   /**
90    * Validates the parameters of this action.
91    *
92    * @return
93    */

94   protected boolean validateAction() {
95     try {
96       dataComponent = createDataComponent();
97       if (dataComponent != null) {
98         // if that fails, then we know we messed up again.
99
// Abort, we cant continue anyway.
100
if (dataComponent.validate() == IRuntimeContext.RUNTIME_CONTEXT_VALIDATE_OK) {
101           return true;
102         } else {
103           return false;
104         }
105       }
106     } catch (Throwable JavaDoc t) {
107       // if there is an error, we can safely abort.
108
error(Messages
109           .getErrorString("JFreeReport.ERROR_0021_DATA_COMPONENT_FAILED"), t); //$NON-NLS-1$
110
return false;
111     }
112
113     // Second path to get the data: Use a user supplied result-set.
114
if (isDefinedInput(DATACOMPONENT_DATAINPUT)) {
115       return true;
116     }
117
118     // Third path: The Jar-mode. (I hate that one. It is illogical and
119
// with the introduction of datafactories, JFreeReport has much better
120
// means (since JFreeReport 0.8.7-5)
121

122     if (isDefinedResource(DATACOMPONENT_JARINPUT)) {
123       if (!isDefinedInput(DATACOMPONENT_CLASSLOCINPUT)) {
124         error(Messages
125             .getErrorString("JFreeReport.ERROR_0012_CLASS_LOCATION_MISSING")); //$NON-NLS-1$
126
return false;
127       }
128
129       final IActionResource resource = getResource(DATACOMPONENT_JARINPUT);
130       return validateResource(resource);
131     }
132
133     // hey, maybe we dont have to do anything ...
134
if (isDefinedInput(DATACOMPONENT_REPORTTEMP_OBJINPUT)) {
135       Object JavaDoc maybeReport = getInputValue(DATACOMPONENT_REPORTTEMP_OBJINPUT);
136       if (maybeReport instanceof JFreeReport) {
137         JFreeReport report = (JFreeReport) maybeReport;
138         if (report.getDataFactory() != null) {
139           // ok, thats fine. The data factory will take care of getting
140
// the data
141
return true;
142         }
143       }
144     }
145
146     return false;
147   }
148
149   private boolean validateResource(IActionResource resource) {
150     final ISolutionRepository solutionRepository = PentahoSystem
151         .getSolutionRepository(getSession());
152     final InputStream JavaDoc in = solutionRepository.getResourceInputStream(resource);
153     if (in == null) {
154       error(Messages.getErrorString(
155           "JFreeReport.ERROR_0010_REPORT_JAR_MISSING", resource.getAddress())); //$NON-NLS-1$
156
return false;
157     }
158
159     try {
160       // not being able to read a single char is definitly a big boo ..
161
if (in.read() == -1) {
162         error(Messages
163             .getErrorString("JFreeReport.ERROR_0009_REPORT_JAR_UNREADABLE")); //$NON-NLS-1$
164
return false;
165       }
166     } catch (Exception JavaDoc e) {
167       error(Messages
168           .getErrorString("JFreeReport.ERROR_0009_REPORT_JAR_UNREADABLE")); //$NON-NLS-1$
169
return false;
170     }
171
172     if (!isDefinedInput(DATACOMPONENT_CLASSLOCINPUT)) {
173       error(Messages
174           .getErrorString("JFreeReport.ERROR_0012_CLASS_LOCATION_MISSING")); //$NON-NLS-1$
175
return false;
176     }
177     return true;
178   }
179
180   public IDataComponent getDataComponent() {
181     return dataComponent;
182   }
183
184   protected boolean validateSystemSettings() {
185     return true;
186   }
187
188   public void done() {
189     // help the garbage collector ...
190
if (dataComponent != null) {
191       dataComponent.dispose();
192       dataComponent.done();
193     }
194     dataComponent = null;
195   }
196
197   protected boolean executeAction() throws Throwable JavaDoc {
198     if (dataComponent != null) {
199       if (dataComponent.execute() != IRuntimeContext.RUNTIME_STATUS_SUCCESS) {
200         return false;
201       }
202       IPentahoResultSet resultset = dataComponent.getResultSet();
203       addTempParameterObject(DATACOMPONENT_REPORTTEMP_DATAINPUT,
204           new PentahoTableModel(resultset));
205       return true;
206     }
207
208     if (isDefinedInput(DATACOMPONENT_DATAINPUT)) {
209       Object JavaDoc dataObject = getInputValue(DATACOMPONENT_DATAINPUT);
210       if (dataObject instanceof IPentahoResultSet) {
211         IPentahoResultSet resultset = (IPentahoResultSet) dataObject;
212         resultset.beforeFirst();
213         addTempParameterObject(DATACOMPONENT_REPORTTEMP_DATAINPUT,
214             new PentahoTableModel(resultset));
215         return true;
216       }
217
218       if (dataObject instanceof TableModel JavaDoc) {
219         TableModel JavaDoc model = (TableModel JavaDoc) dataObject;
220         addTempParameterObject(DATACOMPONENT_REPORTTEMP_DATAINPUT, model);
221         return true;
222       }
223     }
224
225     if (isDefinedResource(DATACOMPONENT_JARINPUT)) {
226       final IActionResource resource = getResource(DATACOMPONENT_JARINPUT);
227       final ClassLoader JavaDoc loader = ReportUtils.createJarLoader(getSession(),
228           resource);
229       if (loader == null) {
230         error(Messages
231             .getString("JFreeReportDataComponent.ERROR_0035_COULD_NOT_CREATE_CLASSLOADER")); //$NON-NLS-1$
232
return false;
233       }
234
235       String JavaDoc classLocation = getInputStringValue(DATACOMPONENT_CLASSLOCINPUT);
236       final ReportProperties reportProperties = createParameters();
237
238       DataFactory dataFactory = new PentahoDataFactory(loader);
239       TableModel JavaDoc model = dataFactory.queryData(classLocation,
240           new ParameterDataRow(reportProperties));
241       addTempParameterObject(DATACOMPONENT_REPORTTEMP_DATAINPUT, model);
242       return true;
243     }
244
245     // hey, maybe we dont have to do anything ...
246
if (isDefinedInput(DATACOMPONENT_REPORTTEMP_OBJINPUT)) {
247       Object JavaDoc maybeReport = getInputValue(DATACOMPONENT_REPORTTEMP_OBJINPUT);
248       if (maybeReport instanceof JFreeReport) {
249         JFreeReport report = (JFreeReport) maybeReport;
250         if (report.getDataFactory() != null) {
251           // ok, thats fine. The data factory will take care of getting
252
// the data
253
return true;
254         }
255       }
256     }
257
258     error(Messages
259         .getString("JFreeReport.ERROR_0022_DATA_INPUT_INVALID_OBJECT")); //$NON-NLS-1$
260
return false;
261   }
262
263   protected ReportProperties createParameters() {
264     ReportProperties props = new ReportProperties();
265     // Get input parameters, and set them as properties in the report
266
// object.
267
final Set JavaDoc paramNames = getInputNames();
268     final Iterator JavaDoc it = paramNames.iterator();
269     while (it.hasNext()) {
270       String JavaDoc paramName = (String JavaDoc) it.next();
271       Object JavaDoc paramValue = getInputValue(paramName);
272       if (paramValue instanceof Object JavaDoc[]) {
273         Object JavaDoc values[] = (Object JavaDoc[]) paramValue;
274         StringBuffer JavaDoc valuesBuffer = new StringBuffer JavaDoc();
275         // TODO support non-string items
276
for (int i = 0; i < values.length; i++) {
277           if (i == 0) {
278             valuesBuffer.append(values[i].toString());
279           } else {
280             valuesBuffer.append(',').append(values[i].toString());
281           }
282         }
283         props.put(paramName, valuesBuffer.toString());
284         props.setMarked(paramName, true);
285       } else {
286         props.put(paramName, paramValue);
287         props.setMarked(paramName, true);
288       }
289     }
290     return props;
291   }
292
293   protected ClassLoader JavaDoc createJarClassLoader() {
294     final IActionResource resource = getResource(DATACOMPONENT_JARINPUT);
295     return ReportUtils.createJarLoader(getSession(), resource);
296   }
297
298   public boolean init() {
299     // forward that thing.
300
if (dataComponent != null) {
301       return dataComponent.init();
302     }
303     return true;
304   }
305
306   public Log getLogger() {
307     return LogFactory.getLog(JFreeReportDataComponent.class);
308   }
309 }
310
Popular Tags