KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > plugin > jfreereport > reportcharts > CategorySetCollectorFunction


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 Oct 31, 2005
14  * @author mbatchel
15  */

16 package org.pentaho.plugin.jfreereport.reportcharts;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Arrays JavaDoc;
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.jfree.data.category.DefaultCategoryDataset;
23 import org.jfree.report.DataRow;
24 import org.jfree.report.event.ReportEvent;
25 import org.jfree.report.function.AbstractFunction;
26 import org.jfree.report.function.Expression;
27 import org.jfree.report.function.FunctionUtilities;
28 import org.pentaho.messages.Messages;
29
30 /**
31  * Creation-Date: 22.09.2005, 18:30:22
32  *
33  * @author Thomas Morgner
34  */

35 public class CategorySetCollectorFunction extends AbstractFunction {
36     private static final long serialVersionUID = -8138304452870844825L;
37
38     private ArrayList JavaDoc seriesNames;
39
40     private String JavaDoc categoryColumn;
41
42     private ArrayList JavaDoc valueColumns;
43
44     private ArrayList JavaDoc ignoreColumns;
45
46     private DefaultCategoryDataset categoryDataset;
47
48     private String JavaDoc group;
49
50     private String JavaDoc resetGroup;
51
52     private ArrayList JavaDoc results;
53
54     private int currentIndex;
55
56     private boolean summaryOnly = true;
57
58     private boolean generatedReport = false;
59
60     private int categoryStartColumn;
61
62     private boolean seriesColumn = false;
63
64     private static final Log logger = LogFactory.getLog(CategorySetCollectorFunction.class);
65
66     public CategorySetCollectorFunction() {
67         this.results = new ArrayList JavaDoc();
68         this.seriesNames = new ArrayList JavaDoc();
69         this.valueColumns = new ArrayList JavaDoc();
70         this.ignoreColumns = new ArrayList JavaDoc();
71     }
72
73     public void setCategoryStartColumn(int value) {
74         categoryStartColumn = value;
75     }
76
77     public int getCategoryStartColumn() {
78         return categoryStartColumn;
79     }
80
81     public void setGeneratedReport(boolean value) {
82         generatedReport = value;
83     }
84
85     public boolean isGeneratedReport() {
86         return generatedReport;
87     }
88
89     public void setIgnoreColumn(final int index, final String JavaDoc field) {
90         if (ignoreColumns.size() == index) {
91             ignoreColumns.add(field);
92         } else {
93             ignoreColumns.set(index, field);
94         }
95     }
96
97     public String JavaDoc getIgnoreColumn(final int index) {
98         return (String JavaDoc) ignoreColumns.get(index);
99     }
100
101     public int getIgnoreColumnCount() {
102         return ignoreColumns.size();
103     }
104
105     public String JavaDoc[] getIgnoreColumn() {
106         return (String JavaDoc[]) ignoreColumns.toArray(new String JavaDoc[ignoreColumns.size()]);
107     }
108
109     public void setIgnoreColumn(final String JavaDoc[] fields) {
110         this.ignoreColumns.clear();
111         this.ignoreColumns.addAll(Arrays.asList(fields));
112     }
113
114     /*
115      * ---------------------------------------------------------------- Standard
116      * accessors - we use indexed properties for the series config.
117      */

118
119     public void setSeriesName(final int index, final String JavaDoc field) {
120         if (seriesNames.size() == index) {
121             seriesNames.add(field);
122         } else {
123             seriesNames.set(index, field);
124         }
125     }
126
127     public String JavaDoc getSeriesName(final int index) {
128         return (String JavaDoc) seriesNames.get(index);
129     }
130
131     public int getSeriesNameCount() {
132         return seriesNames.size();
133     }
134
135     public String JavaDoc[] getSeriesName() {
136         return (String JavaDoc[]) seriesNames.toArray(new String JavaDoc[seriesNames.size()]);
137     }
138
139     public void setSeriesName(final String JavaDoc[] fields) {
140         this.seriesNames.clear();
141         this.seriesNames.addAll(Arrays.asList(fields));
142     }
143
144     public void setValueColumn(final int index, final String JavaDoc field) {
145         if (valueColumns.size() == index) {
146             valueColumns.add(field);
147         } else {
148             valueColumns.set(index, field);
149         }
150     }
151
152     public boolean isSummaryOnly() {
153         return summaryOnly;
154     }
155
156     public void setSummaryOnly(boolean value) {
157         summaryOnly = value;
158     }
159
160     public boolean isSeriesColumn() {
161         return seriesColumn;
162     }
163
164     public void setSeriesColumn(boolean value) {
165         seriesColumn = value;
166     }
167
168     public String JavaDoc getValueColumn(final int index) {
169         return (String JavaDoc) valueColumns.get(index);
170     }
171
172     public int getValueColumnCount() {
173         return valueColumns.size();
174     }
175
176     public String JavaDoc[] getValueColumn() {
177         return (String JavaDoc[]) valueColumns.toArray(new String JavaDoc[valueColumns.size()]);
178     }
179
180     public void setValueColumn(final String JavaDoc[] fields) {
181         this.valueColumns.clear();
182         this.valueColumns.addAll(Arrays.asList(fields));
183     }
184
185     public String JavaDoc getCategoryColumn() {
186         return categoryColumn;
187     }
188
189     public void setCategoryColumn(final String JavaDoc categoryColumn) {
190         this.categoryColumn = categoryColumn;
191     }
192
193     public String JavaDoc getGroup() {
194         return group;
195     }
196
197     public void setGroup(final String JavaDoc group) {
198         this.group = group;
199     }
200
201     public String JavaDoc getResetGroup() {
202         return resetGroup;
203     }
204
205     public void setResetGroup(final String JavaDoc resetGroup) {
206         this.resetGroup = resetGroup;
207     }
208
209     /*
210      * ---------------------------------------------------------------- Now the
211      * function implementation ...
212      */

213
214     /**
215      * That one is easy: Either we have a valid data set, or we have none ...
216      *
217      * @return the dataset
218      */

219     public Object JavaDoc getValue() {
220         return categoryDataset;
221     }
222
223     public void reportInitialized(ReportEvent event) {
224         currentIndex = -1;
225         logger.debug(Messages.getString("CATEGORYSETCOLL.USER_DEBUG_REPORT_INITIALIZED")); //$NON-NLS-1$
226
if (FunctionUtilities.isDefinedPrepareRunLevel(this, event)) {
227             categoryDataset = null;
228             results.clear();
229             if (getResetGroup() == null) {
230                 categoryDataset = new DefaultCategoryDataset();
231                 results.add(categoryDataset);
232             }
233         } else {
234             // Activate the current group, which was filled in the prepare run.
235
if (getResetGroup() == null) {
236                 categoryDataset = (DefaultCategoryDataset) results.get(0);
237             }
238         }
239     }
240
241     public void groupStarted(ReportEvent event) {
242         logger.debug(Messages.getString("CATEGORYSETCOLL.USER_DEBUG_GROUP_STARTED")); //$NON-NLS-1$
243
if (FunctionUtilities.isDefinedGroup(getResetGroup(), event)) {
244             // reset ...
245
if (FunctionUtilities.isDefinedPrepareRunLevel(this, event)) {
246                 categoryDataset = new DefaultCategoryDataset();
247                 results.add(categoryDataset);
248             } else {
249                 if (FunctionUtilities.isLayoutLevel(event)) {
250                     // Activate the current group, which was filled in the
251
// prepare run.
252
currentIndex += 1;
253                     categoryDataset = (DefaultCategoryDataset) results.get(currentIndex);
254                 }
255             }
256         }
257     }
258
259     public void itemsAdvanced(ReportEvent reportEvent) {
260         logger.debug(Messages.getString("CATEGORYSETCOLL.USER_DEBUG_ITEMS_ADVANCED")); //$NON-NLS-1$
261
if (FunctionUtilities.isDefinedPrepareRunLevel(this, reportEvent) == false) {
262             // we do not modify the created dataset if this is not the function
263
// computation run. (FunctionLevel '0')
264
return;
265         }
266         if (!summaryOnly) {
267             final Object JavaDoc categoryObject = getDataRow().get(getCategoryColumn());
268             final Comparable JavaDoc categoryComparable;
269             if (categoryObject instanceof Comparable JavaDoc) {
270                 categoryComparable = (Comparable JavaDoc) categoryObject;
271             } else {
272                 // ok, we need some better error management here. Its a
273
// prototype :)
274
categoryComparable = Messages.getString("CATEGORYSETCOLL.USER_ERROR_CATEGORY_NOT_COMPARABLE"); //$NON-NLS-1$
275
}
276             // I love to be paranoid!
277
final int maxIndex = Math.min(this.seriesNames.size(), this.valueColumns.size());
278             for (int i = 0; i < maxIndex; i++) {
279                 String JavaDoc seriesName = (String JavaDoc) seriesNames.get(i);
280                 final String JavaDoc column = (String JavaDoc) valueColumns.get(i);
281                 final Object JavaDoc valueObject = getDataRow().get(column);
282                 if (isSeriesColumn()) {
283                     Object JavaDoc tmp = getDataRow().get(seriesName);
284                     if (tmp != null) {
285                         seriesName = tmp.toString();
286                     }
287                 }
288                 double value;
289                 if (valueObject instanceof Number JavaDoc) {
290                     Number JavaDoc n = (Number JavaDoc) valueObject;
291                     value = n.doubleValue();
292                 } else {
293                     value = Double.NaN;
294                 }
295                 Object JavaDoc isThere = null;
296                 try {
297                     isThere = categoryDataset.getValue(seriesName, categoryComparable);
298                 } catch (Exception JavaDoc ignored) {
299                 }
300                 if (isThere != null) {
301                     value += ((Number JavaDoc) isThere).doubleValue();
302                     categoryDataset.setValue(value, seriesName, categoryComparable);
303                 } else {
304                     categoryDataset.addValue(value, seriesName, categoryComparable);
305                 }
306             }
307         }
308     }
309
310     public void groupFinished(ReportEvent reportEvent) {
311         logger.debug(Messages.getString("CATEGORYSETCOLL.USER_DEBUG_GROUPS_FINISHED")); //$NON-NLS-1$
312
if (FunctionUtilities.isDefinedPrepareRunLevel(this, reportEvent) == false) {
313             // we do not modify the created dataset if this is not the function
314
// computation run. (FunctionLevel '0')
315
return;
316         }
317         if (summaryOnly) {
318             if (FunctionUtilities.isDefinedGroup(getGroup(), reportEvent)) {
319                 // we can be sure that everything has been computed here. So
320
// grab the
321
// values and add them to the dataset.
322
if (!generatedReport) {
323                     final Object JavaDoc categoryObject = getDataRow().get(getCategoryColumn());
324                     final Comparable JavaDoc categoryComparable;
325                     if (categoryObject instanceof Comparable JavaDoc) {
326                         categoryComparable = (Comparable JavaDoc) categoryObject;
327                     } else {
328                         // ok, we need some better error management here. Its a
329
// prototype :)
330
categoryComparable = Messages.getString("CATEGORYSETCOLL.USER_ERROR_CATEGORY_NOT_COMPARABLE"); //$NON-NLS-1$
331
}
332
333                     // I love to be paranoid!
334
final int maxIndex = Math.min(this.seriesNames.size(), this.valueColumns.size());
335                     for (int i = 0; i < maxIndex; i++) {
336                         String JavaDoc seriesName = (String JavaDoc) seriesNames.get(i);
337                         final String JavaDoc column = (String JavaDoc) valueColumns.get(i);
338                         final Object JavaDoc valueObject = getDataRow().get(column);
339                         if (isSeriesColumn()) {
340                             Object JavaDoc tmp = getDataRow().get(seriesName);
341                             if (tmp != null) {
342                                 seriesName = tmp.toString();
343                             }
344                         }
345                         final double value;
346                         if (valueObject instanceof Number JavaDoc) {
347                             Number JavaDoc n = (Number JavaDoc) valueObject;
348                             value = n.doubleValue();
349                         } else {
350                             value = Double.NaN;
351                         }
352                         categoryDataset.addValue(value, seriesName, categoryComparable);
353                     }
354                 } else {
355                     DataRow theRow = getDataRow();
356                     int colCount = theRow.getColumnCount();
357                     for (int i = categoryStartColumn; i < colCount; i++) {
358                         String JavaDoc seriesName = theRow.getColumnName(i);
359                         if (seriesName.startsWith("Summary_")) { //$NON-NLS-1$
360
if (ignoreColumns.indexOf(seriesName) >= 0) {
361                                 continue;
362                             }
363                             seriesName = seriesName.substring(8, seriesName.length() - 10);
364                             Object JavaDoc valueObject = theRow.get(i);
365                             double value;
366                             if (valueObject instanceof Number JavaDoc) {
367                                 Number JavaDoc n = (Number JavaDoc) valueObject;
368                                 value = n.doubleValue();
369                             } else {
370                                 value = Double.NaN;
371                             }
372                             categoryDataset.addValue(value, seriesName, seriesName);
373                         }
374                     }
375                 }
376             }
377         }
378     }
379
380     /**
381      * Return a completly separated copy of this function. The copy no longer
382      * shares any changeable objects with the original function.
383      *
384      * @return a copy of this function.
385      */

386     public Expression getInstance() {
387         final CategorySetCollectorFunction fn = (CategorySetCollectorFunction) super.getInstance();
388         fn.categoryDataset = null;
389         fn.results = new ArrayList JavaDoc();
390         return fn;
391     }
392
393 }
394
Popular Tags