001: /**
002: * ===========================================
003: * JFreeReport : a free Java reporting library
004: * ===========================================
005: *
006: * Project Info: http://reporting.pentaho.org/
007: *
008: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
009: *
010: * This library is free software; you can redistribute it and/or modify it under the terms
011: * of the GNU Lesser General Public License as published by the Free Software Foundation;
012: * either version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: * See the GNU Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public License along with this
019: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: *
022: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023: * in the United States and other countries.]
024: *
025: * ------------
026: * TotalGroupCountFunction.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.function;
030:
031: import java.io.IOException;
032: import java.io.ObjectInputStream;
033: import java.util.HashMap;
034:
035: import org.jfree.report.Group;
036: import org.jfree.report.event.ReportEvent;
037: import org.jfree.report.states.ReportStateKey;
038: import org.jfree.report.util.IntegerCache;
039:
040: /**
041: * A report function that counts the total of groups in a report. If a null-groupname is given, all groups are counted.
042: * <p/>
043: * A group can be defined using the property "group". If the group property is not set, all group starts get counted.
044: *
045: * @author Thomas Morgner
046: */
047: public class TotalGroupCountFunction extends GroupCountFunction {
048: /**
049: * A map of results, keyed by the process-key.
050: */
051: private transient HashMap results;
052: /**
053: * The currently computed result.
054: */
055: private transient Integer result;
056: /**
057: * The global state key is used to store the result for the whole report.
058: */
059: private transient ReportStateKey globalStateKey;
060: /**
061: * The current group key is used to store the result for the current group.
062: */
063: private transient ReportStateKey groupStateKey;
064:
065: /**
066: * Default constructor.
067: */
068: public TotalGroupCountFunction() {
069: results = new HashMap();
070: }
071:
072: /**
073: * Receives notification that the report has started.
074: *
075: * @param event the event.
076: */
077: public void reportInitialized(final ReportEvent event) {
078: super .reportInitialized(event);
079: globalStateKey = event.getState().getProcessKey();
080: if (FunctionUtilities.isDefinedPrepareRunLevel(this , event)) {
081: results.clear();
082: result = IntegerCache.getInteger(getCount());
083: results.put(globalStateKey, result);
084: } else {
085: result = (Integer) results.get(globalStateKey);
086: }
087: }
088:
089: /**
090: * Receives notification that a group has started.
091: *
092: * @param event the event.
093: */
094: public void groupStarted(final ReportEvent event) {
095: super .groupStarted(event);
096:
097: final Group group = FunctionUtilities.getCurrentGroup(event);
098:
099: if (group.getName().equals(getParentGroup())) {
100: groupStateKey = event.getState().getProcessKey();
101: if (FunctionUtilities.isDefinedPrepareRunLevel(this , event)) {
102: result = IntegerCache.getInteger(getCount());
103: results.put(globalStateKey, result);
104: results.put(groupStateKey, result);
105: return;
106: } else {
107: // Activate the current group, which was filled in the prepare run.
108: result = (Integer) results.get(groupStateKey);
109: }
110: }
111:
112: final String definedGroupName = getGroup();
113: if (definedGroupName == null
114: || group.getName().equals(definedGroupName)) {
115: // count all groups...
116: if (FunctionUtilities.isDefinedPrepareRunLevel(this , event)) {
117: result = IntegerCache.getInteger(getCount());
118: results.put(globalStateKey, result);
119: results.put(groupStateKey, result);
120: }
121: }
122: }
123:
124: /**
125: * Returns the computed value.
126: *
127: * @return the computed value.
128: */
129: public Object getValue() {
130: return result;
131: }
132:
133: /**
134: * Return a completly separated copy of this function. The copy does no longer share any changeable objects with the
135: * original function.
136: *
137: * @return a copy of this function.
138: */
139: public Expression getInstance() {
140: final TotalGroupCountFunction fn = (TotalGroupCountFunction) super
141: .getInstance();
142: fn.results = new HashMap();
143: return fn;
144: }
145:
146: /**
147: * Helper function for the serialization.
148: *
149: * @param in the input stream.
150: * @throws IOException if an IO error occured.
151: * @throws ClassNotFoundException if a required class could not be found.
152: */
153: private void readObject(final ObjectInputStream in)
154: throws IOException, ClassNotFoundException {
155: in.defaultReadObject();
156: this .results = new HashMap();
157: }
158:
159: }
|