KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > plugin > jfreereport > outputs > AbstractGenerateContentComponent


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  * @created Jul 07, 2006
14  * @author Thomas Morgner
15  */

16 package org.pentaho.plugin.jfreereport.outputs;
17
18 import java.io.File JavaDoc;
19
20 import javax.swing.table.TableModel JavaDoc;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.jfree.io.IOUtils;
25 import org.jfree.report.JFreeReport;
26 import org.jfree.report.ResourceBundleFactory;
27 import org.jfree.report.util.CloseableTableModel;
28 import org.pentaho.messages.Messages;
29 import org.pentaho.plugin.jfreereport.AbstractJFreeReportComponent;
30 import org.pentaho.plugin.jfreereport.helper.PentahoResourceBundleFactory;
31
32 /**
33  * The base class for all content generating components. This class adds the
34  * report-data to the JFreeReport object. If requested, it creates a private
35  * copy of the report before doing any work - cloning is faster than parsing.
36  *
37  * Sub-Actions of this component are usually the last step in the report
38  * processing.
39  *
40  * @author Thomas Morgner
41  */

42 public abstract class AbstractGenerateContentComponent extends
43     AbstractJFreeReportComponent {
44   protected AbstractGenerateContentComponent() {
45   }
46
47   protected boolean validateAction() {
48     if (isDefinedInput(DATACOMPONENT_REPORTTEMP_OBJINPUT) == false) {
49       warn(Messages
50           .getString("AbstractGenerateContentComponent.ERROR_0038_NO_REPORT_OBJECT_INPUT")); //$NON-NLS-1$
51
return false;
52     }
53
54     if (isDefinedInput(DATACOMPONENT_REPORTTEMP_DATAINPUT) == false) {
55       warn(Messages
56           .getString("AbstractGenerateContentComponent.ERROR_0039_NO_REPORT_DATA_PARAMETER")); //$NON-NLS-1$
57
return false;
58     }
59
60     if (isDefinedInput(REPORTGENERATE_YIELDRATE)) {
61       final Object JavaDoc inputValue = getInputValue(REPORTGENERATE_YIELDRATE);
62       if (inputValue instanceof Number JavaDoc) {
63         Number JavaDoc n = (Number JavaDoc) inputValue;
64         if (n.intValue() < 1) {
65           warn(Messages
66               .getString("AbstractGenerateContentComponent.ERROR_0040_YIELD_RATE_POSITIVE")); //$NON-NLS-1$
67
}
68       } else {
69         warn(Messages
70             .getString("AbstractGenerateContentComponent.ERROR_0041_YIELD_RATE_NUMERIC")); //$NON-NLS-1$
71
return false;
72       }
73     }
74     if (isDefinedInput(REPORTGENERATE_PRIORITYINPUT)) {
75       final String JavaDoc inputValue = getInputStringValue(REPORTGENERATE_PRIORITYINPUT);
76       if (REPORTGENERATE_PRIORITYNORMAL.equals(inputValue) == false
77           && REPORTGENERATE_PRIORITYLOWER.equals(inputValue) == false
78           && REPORTGENERATE_PRIORITYLOWEST.equals(inputValue) == false) {
79         warn(Messages
80             .getString("AbstractGenerateContentComponent.ERROR_0042_PRIORITY_MUST_BE")); //$NON-NLS-1$
81
}
82     }
83     return true;
84   }
85
86   protected JFreeReport getReport() {
87     Object JavaDoc maybeJFreeReport = getInputValue(DATACOMPONENT_REPORTTEMP_OBJINPUT);
88     if (maybeJFreeReport instanceof JFreeReport) {
89       return (JFreeReport) maybeJFreeReport;
90     }
91     return null;
92   }
93
94   protected TableModel JavaDoc getData() {
95     if (isDefinedInput(DATACOMPONENT_REPORTTEMP_DATAINPUT)) {
96       Object JavaDoc maybeTableModel = getInputValue(DATACOMPONENT_REPORTTEMP_DATAINPUT);
97       if (maybeTableModel instanceof TableModel JavaDoc) {
98         return (TableModel JavaDoc) maybeTableModel;
99       }
100     }
101     return null;
102   }
103
104   protected boolean validateSystemSettings() {
105     return true;
106   }
107
108   protected boolean executeAction() throws Throwable JavaDoc {
109     JFreeReport report = getReport();
110     if (report == null) {
111       warn(Messages
112           .getString("AbstractGenerateContentComponent.ERROR_0043_NO_REPORT_FOR_ACTION")); //$NON-NLS-1$
113
return false;
114     }
115
116     applyThreadPriority();
117
118     final boolean privateCopy = getInputBooleanValue(
119         REPORTPARAMCOMPONENT_PRIVATEREPORT_OUTPUT, false);
120     if (privateCopy && isDefinedOutput(DATACOMPONENT_REPORTTEMP_OBJINPUT)) {
121       report = (JFreeReport) report.clone();
122     }
123
124     // this might be invalid in case the action is contained in a sub-directory.
125
final String JavaDoc baseName = IOUtils.getInstance().stripFileExtension(
126         getActionName());
127     final String JavaDoc path = getSolutionName() + File.separator + getSolutionPath();
128     final PentahoResourceBundleFactory bundleFactory = new PentahoResourceBundleFactory(
129         path, baseName, getSession());
130     report.setResourceBundleFactory(bundleFactory);
131     // set the default resourcebundle. This allows users to override the
132
// resource-bundle in case they want to keep common strings in a common
133
// collection.
134
report.getReportConfiguration().setConfigProperty(
135         ResourceBundleFactory.DEFAULT_RESOURCE_BUNDLE_CONFIG_KEY, baseName);
136
137     TableModel JavaDoc data = getData();
138     if (data != null) {
139       report.setData(data);
140     }
141
142     return performExport(report);
143   }
144
145   private void applyThreadPriority() {
146     if (isDefinedInput(REPORTGENERATE_PRIORITYINPUT))
147     {
148       try {
149         final String JavaDoc inputValue = getInputStringValue(REPORTGENERATE_PRIORITYINPUT);
150         if (REPORTGENERATE_PRIORITYLOWER.equals(inputValue)) {
151           final int priority = Math.max(
152               Thread.currentThread().getPriority() - 1, 1);
153           Thread.currentThread().setPriority(priority);
154         } else if (REPORTGENERATE_PRIORITYLOWEST.equals(inputValue)) {
155           Thread.currentThread().setPriority(1);
156         }
157       } catch (Exception JavaDoc e) {
158         // Non fatal exception.
159
warn(Messages
160             .getString("AbstractGenerateContentComponent.ERROR_0044_UNABLE_T0_SET_THREAD_PRIORITY")); //$NON-NLS-1$
161
}
162     }
163   }
164
165   protected abstract boolean performExport(final JFreeReport report);
166
167   public void done() {
168     TableModel JavaDoc model = getData();
169     if (model instanceof CloseableTableModel) {
170       CloseableTableModel closeableTableModel = (CloseableTableModel) model;
171       closeableTableModel.close();
172     }
173   }
174
175   public boolean init() {
176     return true;
177   }
178
179   public Log getLogger() {
180     return LogFactory.getLog(getClass());
181   }
182
183   protected int getYieldRate() {
184     if (isDefinedInput(REPORTGENERATE_YIELDRATE)) {
185       final Object JavaDoc inputValue = getInputValue(REPORTGENERATE_YIELDRATE);
186       if (inputValue instanceof Number JavaDoc) {
187         Number JavaDoc n = (Number JavaDoc) inputValue;
188         if (n.intValue() < 1) {
189           return 0;
190         }
191         return n.intValue();
192       }
193     }
194     return 0;
195   }
196
197 }
198
Popular Tags