KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.StringReader JavaDoc;
17 import java.net.URL JavaDoc;
18 import java.util.Iterator JavaDoc;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.jfree.report.JFreeReport;
23 import org.jfree.report.modules.parser.base.ReportGenerator;
24 import org.pentaho.core.repository.ISolutionRepository;
25 import org.pentaho.core.runtime.IActionParameter;
26 import org.pentaho.core.solution.IActionResource;
27 import org.pentaho.core.system.PentahoSystem;
28 import org.pentaho.messages.Messages;
29 import org.pentaho.plugin.jfreereport.AbstractJFreeReportComponent;
30 import org.pentaho.plugin.jfreereport.helper.ReportUtils;
31 import org.xml.sax.InputSource JavaDoc;
32
33 /**
34  * A JFreeReport run contains at least three steps. Step 1: Parse the report definition. Step 2: Grab some data. Step 3: Spit out some content. Alternativly,
35  * show the print-preview. <p/> This class loads or parses the report definition.
36  *
37  * @author Thomas Morgner
38  */

39 public class JFreeReportLoadComponent extends AbstractJFreeReportComponent {
40     private static final long serialVersionUID = -2240691437049710246L;
41
42     public JFreeReportLoadComponent() {
43     }
44
45     protected boolean validateAction() {
46         if (isDefinedResource(REPORTGENERATEDEFN_REPORTDEFN)) {
47             return true;
48         }
49
50         if (isDefinedInput(REPORTGENERATEDEFN_REPORTDEFN)) {
51             IActionParameter o = getInputParameter(REPORTGENERATEDEFN_REPORTDEFN);
52             if ((o != null) && (o.getValue() instanceof String JavaDoc)) {
53                 return true;
54             }
55             return false;
56         }
57
58         // Handle late-bind of report resource name
59
if (isDefinedInput(REPORTLOAD_RESOURCENAME)) {
60             if (isDefinedResource(getInputStringValue(REPORTLOAD_RESOURCENAME))) {
61                 return true;
62             } else {
63                 error(Messages.getErrorString("JFreeReport.ERROR_0004_REPORT_DEFINITION_UNREADABLE")); //$NON-NLS-1$
64
return false;
65             }
66         }
67
68         if (isDefinedResource(DATACOMPONENT_JARINPUT)) {
69             if (!isDefinedInput(REPORTLOAD_REPORTLOC)) {
70                 error(Messages.getErrorString("JFreeReport.ERROR_0011_REPORT_LOCATION_MISSING")); //$NON-NLS-1$
71
return false;
72             }
73
74             final IActionResource resource = getResource(DATACOMPONENT_JARINPUT);
75             return validateResource(resource);
76         }
77         return false;
78     }
79
80     private boolean validateResource(IActionResource resource) {
81         final ISolutionRepository solutionRepository = PentahoSystem.getSolutionRepository(getSession());
82         final InputStream JavaDoc in = solutionRepository.getResourceInputStream(resource);
83         if (in == null) {
84             error(Messages.getErrorString("JFreeReport.ERROR_0010_REPORT_JAR_MISSING", resource.getAddress())); //$NON-NLS-1$
85
return false;
86         }
87
88         try {
89             // not being able to read a single char is definitly a big boo ..
90
if (in.read() == -1) {
91                 error(Messages.getErrorString("JFreeReport.ERROR_0009_REPORT_JAR_UNREADABLE")); //$NON-NLS-1$
92
return false;
93             }
94         } catch (Exception JavaDoc e) {
95             error(Messages.getErrorString("JFreeReport.ERROR_0009_REPORT_JAR_UNREADABLE")); //$NON-NLS-1$
96
return false;
97         }
98
99         if (!isDefinedInput(REPORTLOAD_REPORTLOC)) {
100             error(Messages.getErrorString("JFreeReport.ERROR_0012_CLASS_LOCATION_MISSING")); //$NON-NLS-1$
101
return false;
102         }
103         return true;
104     }
105
106     protected boolean validateSystemSettings() {
107         return true;
108     }
109
110     public void done() {
111
112     }
113
114     protected boolean executeAction() throws Throwable JavaDoc {
115         // fire up the parser ...
116
if (isDefinedResource(REPORTGENERATEDEFN_REPORTDEFN)) {
117             final IActionResource resource = getResource(REPORTGENERATEDEFN_REPORTDEFN);
118             JFreeReport report = parseReport(resource);
119             if (report == null) {
120                 return false;
121             }
122             addTempParameterObject(DATACOMPONENT_REPORTTEMP_OBJINPUT, report);
123             return true;
124         }
125
126         // Handle late-bind of report resource name
127
if (isDefinedInput(REPORTLOAD_RESOURCENAME)) {
128             final String JavaDoc resName = getInputStringValue(REPORTLOAD_RESOURCENAME);
129             if (isDefinedResource(resName)) {
130                 final IActionResource resource = getResource(resName);
131                 JFreeReport report = parseReport(resource);
132                 if (report == null) {
133                     return false;
134                 }
135                 addTempParameterObject(DATACOMPONENT_REPORTTEMP_OBJINPUT, report);
136                 return true;
137             } else {
138                 error(Messages.getErrorString("JFreeReport.ERROR_0004_REPORT_DEFINITION_UNREADABLE")); //$NON-NLS-1$
139
return false;
140             }
141         }
142
143         if (isDefinedInput(REPORTGENERATEDEFN_REPORTDEFN)) {
144             IActionParameter o = getInputParameter(REPORTGENERATEDEFN_REPORTDEFN);
145             if (o != null) {
146                 String JavaDoc repDef = o.getStringValue();
147                 ReportGenerator generator = ReportGenerator.getInstance();
148                 JFreeReport report = generator.parseReport(new InputSource JavaDoc(new StringReader JavaDoc(repDef)), getDefinedResourceURL(null));
149                 if (report == null) {
150                     return false;
151                 }
152                 addTempParameterObject(DATACOMPONENT_REPORTTEMP_OBJINPUT, report);
153                 return true;
154             }
155         }
156
157         // fall-back: Load the report definition from a Jar-file.
158
final IActionResource resource = getResource(DATACOMPONENT_JARINPUT);
159         final ClassLoader JavaDoc loader = ReportUtils.createJarLoader(getSession(), resource);
160         if (loader == null) {
161             error(Messages.getString("JFreeReportLoadComponent.ERROR_0035_COULD_NOT_CREATE_CLASSLOADER")); //$NON-NLS-1$
162
return false;
163         }
164
165         String JavaDoc reportLocation = getInputStringValue(REPORTLOAD_REPORTLOC);
166         URL JavaDoc resourceUrl = loader.getResource(reportLocation);
167         if (resourceUrl == null) {
168             error(Messages.getErrorString("JFreeReport.ERROR_0016_REPORT_RESOURCE_INVALID", //$NON-NLS-1$
169
reportLocation, resource.getAddress()));
170             return false;
171         }
172
173         try {
174             ReportGenerator generator = ReportGenerator.getInstance();
175             JFreeReport report = generator.parseReport(resourceUrl, getDefinedResourceURL(resourceUrl));
176             if (report == null) {
177                 return false;
178             }
179             addTempParameterObject(DATACOMPONENT_REPORTTEMP_OBJINPUT, report);
180             return true;
181         } catch (Exception JavaDoc ex) {
182             error(Messages.getErrorString("JFreeReport.ERROR_0007_COULD_NOT_PARSE", reportLocation), ex); //$NON-NLS-1$
183
return false;
184         }
185     }
186
187     private URL JavaDoc getDefinedResourceURL(URL JavaDoc defaultValue) {
188         if (isDefinedInput(REPORTLOAD_RESURL) == false) {
189             return defaultValue;
190         }
191
192         try {
193             final String JavaDoc inputStringValue = getInputStringValue(Messages.getString(REPORTLOAD_RESURL));
194             return new URL JavaDoc(inputStringValue);
195         } catch (Exception JavaDoc e) {
196             return defaultValue;
197         }
198     }
199
200     private String JavaDoc getBaseServerURL(String JavaDoc pentahoBaseURL) {
201         try {
202         URL JavaDoc url = new URL JavaDoc(pentahoBaseURL);
203         return url.getProtocol() + "://" + url.getHost() + ":" + url.getPort();
204         } catch (Exception JavaDoc e) {
205         }
206         return pentahoBaseURL;
207     }
208     
209     /**
210      * Parses the report, using the given ActionResource as initial report definition.
211      *
212      * @param resource
213      * @return
214      */

215     private JFreeReport parseReport(IActionResource resource) {
216         try {
217             URL JavaDoc resourceUrl = ReportUtils.getURL(getSession(), resource);
218
219             final ISolutionRepository solutionRepository = PentahoSystem.getSolutionRepository(getSession());
220             final InputStream JavaDoc in = solutionRepository.getResourceInputStream(resource);
221
222             ReportGenerator generator = ReportGenerator.getInstance();
223
224             String JavaDoc pentahoBaseURL = PentahoSystem.getApplicationContext().getBaseUrl();
225             generator.setObject("pentahoBaseURL", PentahoSystem.getApplicationContext().getBaseUrl());
226
227           // trim out the server and port
228
generator.setObject("serverBaseURL", getBaseServerURL(pentahoBaseURL));
229
230             generator.setObject("solutionRoot", PentahoSystem.getApplicationContext().getSolutionPath(""));
231             
232             Iterator JavaDoc it = getInputNames().iterator();
233             while (it.hasNext()) {
234                 try {
235                     String JavaDoc inputName = (String JavaDoc) it.next();
236                     String JavaDoc inputValue = getInputStringValue(inputName);
237                     generator.setObject(inputName, inputValue);
238                 } catch (Exception JavaDoc e) {
239                 }
240             }
241
242             return generator.parseReport(new InputSource JavaDoc(in), getDefinedResourceURL(resourceUrl));
243         } catch (Exception JavaDoc ex) {
244             error(Messages.getErrorString("JFreeReport.ERROR_0007_COULD_NOT_PARSE", resource.getAddress()), ex); //$NON-NLS-1$
245
return null;
246         }
247     }
248
249     public boolean init() {
250         return true;
251     }
252
253     public Log getLogger() {
254         return LogFactory.getLog(JFreeReportLoadComponent.class);
255     }
256 }
257
Popular Tags