KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > plugin > jfreereport > JFreeReportComponent


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;
14
15 import org.apache.commons.logging.Log;
16 import org.apache.commons.logging.LogFactory;
17 import org.pentaho.core.component.IComponent;
18 import org.pentaho.core.runtime.IRuntimeContext;
19 import org.pentaho.messages.Messages;
20 import org.pentaho.plugin.jfreereport.components.JFreeReportDataComponent;
21 import org.pentaho.plugin.jfreereport.components.JFreeReportLoadComponent;
22 import org.pentaho.plugin.jfreereport.components.JFreeReportParametersComponent;
23 import org.pentaho.plugin.jfreereport.components.JFreeReportValidateParametersComponent;
24 import org.pentaho.plugin.jfreereport.outputs.JFreeReportAllContentComponent;
25
26 /**
27  * The JFreeReportComponent provides a simple-to-use frontend for the reporting
28  * process. <p/> This component will execute JFreeReport reports in one of two
29  * different modes: <p/>
30  * <h3>sqlMode = true</h3>
31  * <p/> This means that the JFreeReport component is expected to execute an SQL query,
32  * wrap the resulting SQL Resultset in a TableModel, and use that to execute a report
33  * whose definition is in the file system. In this mode, the action-sequence
34  * definition must contain the following elements: In the resource-definition section,
35  * there must be a resource called "report-definition" which defines the location of
36  * the jfreereport xml document. <p/> In the component-definition sction, there must
37  * be the following entries:
38  * <ul>
39  * <li>A "query" parameter which contains an SQL query.</li>
40  * <li>Either for connecting to the SQL datasource:
41  * <ul>
42  * <li>A "jndi" parameter (with the jndi name of the datasource)</li>
43  * <li>or The database parameters "driver", "user-id", "password" and "connection" so
44  * that a database connection can be established for running the afore-mentioned
45  * "query".</li>
46  * </ul>
47  * </li>
48  * </ul>
49  * <h3>sqlMode = false</h3>
50  * <p/> This means that the JFreeReport component is expected to execute a report that
51  * exists in a .jar file (like the reporting demo reports) along with the TableModel
52  * class that provides the data for the report. In this mode, the action-sequence
53  * definition must contain the following elements: In the resource-definition section,
54  * there must be a resource called "report-jar" that points to the .jar file that
55  * contains the report .xml file, and the TableModel implementation. <p/> In the
56  * component-definition section, there must be two entries:
57  * <ul>
58  * <li> "report-location" - This is the location of the report .xml document (e.g.
59  * org/jfree/report/demo/report1.xml)</li>
60  * <li> "class-location" - This is the package-qualified class that implements
61  * TableModel (e.g. org.jfree.report.demo.SampleData1).</li>
62  * </ul>
63  *
64  * @author mbatchel
65  * @created Sep 8, 2005
66  */

67 public class JFreeReportComponent extends AbstractJFreeReportComponent {
68     private static final long serialVersionUID = -4185151399689983507L;
69
70     private JFreeReportValidateParametersComponent validateParametersComponent;
71
72     public JFreeReportComponent() {
73     }
74
75     public boolean init() {
76         return true;
77     }
78
79     public void done() {
80     }
81
82     public boolean validateSystemSettings() {
83         return true;
84     }
85
86     public Log getLogger() {
87         return LogFactory.getLog(JFreeReportComponent.class);
88     }
89
90     protected boolean initAndValidate(IComponent component) {
91         component.setInstanceId(getInstanceId());
92         component.setActionName(getActionName());
93         component.setProcessId(getProcessId());
94         component.setComponentDefinition(getComponentDefinition());
95         component.setRuntimeContext(getRuntimeContext());
96         component.setSession(getSession());
97         component.setLoggingLevel(getLoggingLevel());
98         component.setMessages(getMessages());
99         return (component.validate() == IRuntimeContext.RUNTIME_CONTEXT_VALIDATE_OK);
100     }
101
102     /**
103      * We cannot validate the parameters of all components, as the required parameters
104      * might not have been created.
105      *
106      * @return
107      */

108     public boolean validateAction() {
109
110         validateParametersComponent = new JFreeReportValidateParametersComponent();
111         if (initAndValidate(validateParametersComponent) == false) {
112             error(Messages.getString("JFreeReportComponent.ERROR_0025_COULD_NOT_VALIDATE")); //$NON-NLS-1$
113
return false;
114         }
115         return true;
116     }
117
118     /**
119      * This method gets called from the outside. Based upon our mode call the correct
120      * function.
121      */

122     public boolean executeAction() {
123         if (validateParametersComponent.init() == false) {
124             return false;
125         }
126         if (validateParametersComponent.execute() != IRuntimeContext.RUNTIME_STATUS_SUCCESS) {
127             return false;
128         }
129         validateParametersComponent.done();
130         if (validateParametersComponent.isParameterUiNeeded() || isPromptPending() ) {
131             return true;
132         }
133
134         return executeReportAction();
135     }
136
137     protected boolean executeReportAction() {
138         JFreeReportDataComponent dataComponent = new JFreeReportDataComponent();
139         try {
140             if (initAndValidate(dataComponent) == false) {
141                 return false;
142             }
143             if (dataComponent.init() == false) {
144                 return false;
145             }
146             if (dataComponent.execute() != IRuntimeContext.RUNTIME_STATUS_SUCCESS) {
147                 return false;
148             }
149     
150             JFreeReportLoadComponent loadComponent = new JFreeReportLoadComponent();
151             if (initAndValidate(loadComponent) == false) {
152                 return false;
153             }
154             if (loadComponent.init() == false) {
155                 return false;
156             }
157             if (loadComponent.execute() != IRuntimeContext.RUNTIME_STATUS_SUCCESS) {
158                 return false;
159             }
160             loadComponent.done();
161     
162             JFreeReportParametersComponent parametersComponent = new JFreeReportParametersComponent();
163             if (initAndValidate(parametersComponent) == false) {
164                 warn(Messages.getString("JFreeReportComponent.ERROR_0026_FAILED_TO_LOAD_PARAMETER_COMPONENT") + parametersComponent); //$NON-NLS-1$
165
return false;
166             }
167             if (parametersComponent.init() == false) {
168                 warn(Messages.getString("JFreeReportComponent.ERROR_0027_FAILED_TO_INIT_PARAMETERS_COMPONENT") + parametersComponent); //$NON-NLS-1$
169
return false;
170             }
171             if (parametersComponent.execute() != IRuntimeContext.RUNTIME_STATUS_SUCCESS) {
172                 warn(Messages.getString("JFreeReportComponent.ERROR_0028_FAILED_TO_EXECUTE") + parametersComponent); //$NON-NLS-1$
173
return false;
174             }
175             parametersComponent.done();
176     
177             JFreeReportAllContentComponent allContentComponent = new JFreeReportAllContentComponent();
178             if (initAndValidate(allContentComponent) == false) {
179                 warn(Messages.getString("JFreeReportComponent.ERROR_0029_FAILED_TO_CREATE") + allContentComponent); //$NON-NLS-1$
180
return false;
181             }
182             if (allContentComponent.init() == false) {
183                 warn(Messages.getString("JFreeReportComponent.ERROR_0030_FAILED_TO_INIT") + allContentComponent); //$NON-NLS-1$
184
return false;
185             }
186             if (allContentComponent.execute() != IRuntimeContext.RUNTIME_STATUS_SUCCESS) {
187                 warn(Messages.getString("JFreeReportComponent.ERROR_0031_FAILED_TO_EXECUTE") + allContentComponent); //$NON-NLS-1$
188
return false;
189             }
190             allContentComponent.done();
191             return true;
192         } finally {
193             dataComponent.done();
194         }
195     }
196    
197 }
198
Popular Tags