KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Iterator JavaDoc;
16 import java.util.Set JavaDoc;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20 import org.jfree.report.JFreeReport;
21 import org.pentaho.messages.Messages;
22 import org.pentaho.plugin.jfreereport.AbstractJFreeReportComponent;
23
24 /**
25  * This sets the report parameters. This is another data preparation step and
26  * should be executed before the query is fired. The data-factory may depend on
27  * these parameters.
28  *
29  * @author Thomas Morgner
30  */

31 public class JFreeReportParametersComponent extends
32     AbstractJFreeReportComponent {
33   private static final long serialVersionUID = -3760254131944082953L;
34
35   public JFreeReportParametersComponent() {
36   }
37
38   protected boolean validateAction() {
39     return isDefinedInput(DATACOMPONENT_REPORTTEMP_OBJINPUT);
40   }
41
42   protected boolean validateSystemSettings() {
43     return true;
44   }
45
46   public void done() {
47
48   }
49
50   protected JFreeReport getReport() {
51     Object JavaDoc maybeReport = getInputValue(DATACOMPONENT_REPORTTEMP_OBJINPUT);
52     if (maybeReport instanceof JFreeReport) {
53       return (JFreeReport) maybeReport;
54     }
55     error(Messages
56         .getString("JFreeReportParametersComponent.ERROR_0033_NO_REPORT_BOUND") + maybeReport); //$NON-NLS-1$
57
return null;
58   }
59
60   protected boolean executeAction() throws Throwable JavaDoc {
61     JFreeReport report = getReport();
62     if (report == null) {
63       error(Messages
64           .getString("JFreeReportParametersComponent.ERROR_0034_NO_REPORT_DEFINITION")); //$NON-NLS-1$
65
return false;
66     }
67
68     final boolean privateCopy = getInputBooleanValue(
69         REPORTPARAMCOMPONENT_PRIVATEREPORT_OUTPUT, false);
70     if (privateCopy && isDefinedOutput(DATACOMPONENT_REPORTTEMP_OBJINPUT)) {
71       report = (JFreeReport) report.clone();
72     }
73
74     // Get input parameters, and set them as properties in the report
75
// object.
76
final Set JavaDoc paramNames = getInputNames();
77     final Iterator JavaDoc it = paramNames.iterator();
78     while (it.hasNext()) {
79       String JavaDoc paramName = (String JavaDoc) it.next();
80       Object JavaDoc paramValue = getInputValue(paramName);
81       if (paramValue == null || "".equals(paramValue)) { //$NON-NLS-1$
82
continue;
83       }
84
85       // we filter some well-known bad-guys. It is dangerous to have the
86
// report-object (the parsed JFreeReport object), the "report-data"
87
// (the tablemodel) or the "data" reference copied to the report.
88

89       if (DATACOMPONENT_REPORTTEMP_OBJINPUT.equals(paramName)) {
90         continue;
91       }
92       if (DATACOMPONENT_REPORTTEMP_DATAINPUT.equals(paramName)) {
93         continue;
94       }
95       if (DATACOMPONENT_DATAINPUT.equals(paramName)) {
96         continue;
97       }
98
99       if (paramValue instanceof Object JavaDoc[]) {
100         Object JavaDoc values[] = (Object JavaDoc[]) paramValue;
101         StringBuffer JavaDoc valuesBuffer = new StringBuffer JavaDoc();
102         // TODO support non-string items
103
for (int i = 0; i < values.length; i++) {
104           if (i == 0) {
105             valuesBuffer.append(values[i].toString());
106           } else {
107             valuesBuffer.append(',').append(values[i].toString());
108           }
109         }
110         report.setProperty(paramName, valuesBuffer.toString());
111       } else {
112         report.setProperty(paramName, paramValue);
113       }
114     }
115
116     if (privateCopy && isDefinedInput(DATACOMPONENT_REPORTTEMP_OBJINPUT)) {
117       addTempParameterObject(DATACOMPONENT_REPORTTEMP_OBJINPUT, report);
118     }
119     return true;
120   }
121
122   public boolean init() {
123     return true;
124   }
125
126   public Log getLogger() {
127     return LogFactory.getLog(JFreeReportParametersComponent.class);
128   }
129 }
130
Popular Tags