KickJava   Java API By Example, From Geeks To Geeks.

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


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 March 1, 2006
14  * @author mbatchel
15  */

16 package org.pentaho.plugin.jfreereport.reportcharts;
17
18 import org.jfree.report.event.ReportEvent;
19 import org.jfree.report.function.AbstractFunction;
20 import org.jfree.report.function.Expression;
21 import org.jfree.report.function.FunctionUtilities;
22
23 import java.util.ArrayList JavaDoc;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.jfree.data.general.DefaultPieDataset;
27 import org.pentaho.messages.Messages;
28
29 public class PieSetCollectorFunction extends AbstractFunction {
30
31     private static final long serialVersionUID = -5778788510651234706L;
32
33     DefaultPieDataset pieDataset;
34
35     private ArrayList JavaDoc results;
36
37     private String JavaDoc seriesColumn;
38
39     private String JavaDoc valueColumn;
40
41     private String JavaDoc group;
42
43     private String JavaDoc resetGroup;
44
45     private boolean summaryOnly;
46
47     private int currentIndex;
48
49     private static final Log logger = LogFactory.getLog(PieSetCollectorFunction.class);
50
51     public String JavaDoc getSeriesColumn() {
52         return seriesColumn;
53     }
54
55     public String JavaDoc getValueColumn() {
56         return valueColumn;
57     }
58
59     public String JavaDoc getGroup() {
60         return group;
61     }
62
63     public String JavaDoc getResetGroup() {
64         return resetGroup;
65     }
66
67     public void setSeriesColumn(String JavaDoc value) {
68         seriesColumn = value;
69     }
70
71     public void setValueColumn(String JavaDoc value) {
72         valueColumn = value;
73     }
74
75     public void setGroup(String JavaDoc value) {
76         group = value;
77     }
78
79     public void setResetGroup(String JavaDoc value) {
80         resetGroup = value;
81     }
82
83     public boolean isSummaryOnly() {
84         return summaryOnly;
85     }
86
87     public void setSummaryOnly(boolean value) {
88         summaryOnly = value;
89     }
90
91     public PieSetCollectorFunction() {
92         results = new ArrayList JavaDoc();
93     }
94
95     /**
96      * Return the dataset if we have one
97      */

98     public Object JavaDoc getValue() {
99         return pieDataset;
100     }
101
102     public void reportInitialized(ReportEvent event) {
103         currentIndex = -1;
104         logger.debug(Messages.getString("PIESETCOLL.USER_DEBUG_REPORT_INITIALIZED")); //$NON-NLS-1$
105
if (FunctionUtilities.isDefinedPrepareRunLevel(this, event)) {
106             pieDataset = null;
107             results.clear();
108             if (getResetGroup() == null) {
109                 pieDataset = new DefaultPieDataset();
110                 results.add(pieDataset);
111             }
112         } else {
113             // Activate the current group, which was filled in the prepare run.
114
if (getResetGroup() == null) {
115                 pieDataset = (DefaultPieDataset) results.get(0);
116             }
117         }
118     }
119
120     public void groupStarted(ReportEvent event) {
121         logger.debug(Messages.getString("PIESETCOLL.USER_DEBUG_GROUP_STARTED")); //$NON-NLS-1$
122
if (FunctionUtilities.isDefinedGroup(getResetGroup(), event)) {
123             // reset ...
124
if (FunctionUtilities.isDefinedPrepareRunLevel(this, event)) {
125                 pieDataset = new DefaultPieDataset();
126                 results.add(pieDataset);
127             } else {
128                 if (FunctionUtilities.isLayoutLevel(event)) {
129                     // Activate the current group, which was filled in the
130
// prepare run.
131
currentIndex += 1;
132                     pieDataset = (DefaultPieDataset) results.get(currentIndex);
133                 }
134             }
135         }
136     }
137
138     public void itemsAdvanced(ReportEvent reportEvent) {
139         logger.debug(Messages.getString("PIESETCOLL.USER_DEBUG_ITEMS_ADVANCED")); //$NON-NLS-1$
140
if (FunctionUtilities.isDefinedPrepareRunLevel(this, reportEvent) == false) {
141             // we do not modify the created dataset if this is not the function
142
// computation run. (FunctionLevel '0')
143
return;
144         }
145         if (!summaryOnly) {
146             final Object JavaDoc seriesObject = getDataRow().get(getSeriesColumn());
147             final Comparable JavaDoc seriesComparable;
148             if (seriesObject instanceof Comparable JavaDoc) {
149                 seriesComparable = (Comparable JavaDoc) seriesObject;
150             } else {
151                 // ok, we need some better error management here. Its a
152
// prototype :)
153
seriesComparable = Messages.getString("PIESETCOLL.USER_ERROR_CATEGORY_NOT_COMPARABLE"); //$NON-NLS-1$
154
}
155             final Object JavaDoc valueObject = getDataRow().get(getValueColumn());
156             double value;
157             if (valueObject instanceof Number JavaDoc) {
158                 Number JavaDoc n = (Number JavaDoc) valueObject;
159                 value = n.doubleValue();
160             } else {
161                 value = Double.NaN;
162             }
163             Object JavaDoc isThere = null;
164             try {
165                 isThere = pieDataset.getValue(seriesComparable);
166             } catch (Exception JavaDoc ignored) {
167             }
168             if (isThere != null) {
169                 value += ((Number JavaDoc) isThere).doubleValue();
170             }
171             pieDataset.setValue(seriesComparable, value);
172
173         }
174     }
175
176     public void groupFinished(ReportEvent reportEvent) {
177         logger.debug(Messages.getString("PIESETCOLL.USER_DEBUG_GROUPS_FINISHED")); //$NON-NLS-1$
178
if (FunctionUtilities.isDefinedPrepareRunLevel(this, reportEvent) == false) {
179             // we do not modify the created dataset if this is not the function
180
// computation run. (FunctionLevel '0')
181
return;
182         }
183         if (summaryOnly) {
184             if (FunctionUtilities.isDefinedGroup(getGroup(), reportEvent)) {
185                 // we can be sure that everything has been computed here. So
186
// grab the
187
// values and add them to the dataset.
188
final Object JavaDoc seriesObject = getDataRow().get(getSeriesColumn());
189                 final Comparable JavaDoc seriesComparable;
190                 if (seriesObject instanceof Comparable JavaDoc) {
191                     seriesComparable = (Comparable JavaDoc) seriesObject;
192                 } else {
193                     // ok, we need some better error management here. Its a
194
// prototype :)
195
seriesComparable = Messages.getString("PIESETCOLL.USER_ERROR_SERIES_NOT_COMPARABLE"); //$NON-NLS-1$
196
}
197                 final Object JavaDoc valueObject = getDataRow().get(getValueColumn());
198                 double value;
199                 if (valueObject instanceof Number JavaDoc) {
200                     Number JavaDoc n = (Number JavaDoc) valueObject;
201                     value = n.doubleValue();
202                 } else {
203                     value = Double.NaN;
204                 }
205                 pieDataset.setValue(seriesComparable, value);
206             }
207         }
208     }
209
210     /**
211      * Return a completly separated copy of this function. The copy no longer
212      * shares any changeable objects with the original function.
213      *
214      * @return a copy of this function.
215      */

216     public Expression getInstance() {
217         final PieSetCollectorFunction fn = (PieSetCollectorFunction) super.getInstance();
218         fn.pieDataset = null;
219         fn.results = new ArrayList JavaDoc();
220         return fn;
221     }
222
223 }
224
Popular Tags