001: /*
002: * ============================================================================
003: * GNU Lesser General Public License
004: * ============================================================================
005: *
006: * JasperReports - Free Java report-generating library.
007: * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * 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,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * JasperSoft Corporation
024: * 303 Second Street, Suite 450 North
025: * San Francisco, CA 94107
026: * http://www.jaspersoft.com
027: */
028: package net.sf.jasperreports.engine;
029:
030: import java.util.Map;
031:
032: import net.sf.jasperreports.engine.fill.JRFillField;
033: import net.sf.jasperreports.engine.fill.JRFillGroup;
034: import net.sf.jasperreports.engine.fill.JRFillParameter;
035: import net.sf.jasperreports.engine.fill.JRFillVariable;
036:
037: /**
038: * Defines an abstract representation of a report scriptlet. Scriptlets are useful when a specific behaviour is needed
039: * in certain moments of the report filling process, such as report, column or group initialization. Scriptlets must implement
040: * the abstract methods that define the behaviour at the specified moments.
041: * @author Teodor Danciu (teodord@users.sourceforge.net)
042: * @version $Id: JRAbstractScriptlet.java 1229 2006-04-19 10:27:35Z teodord $
043: */
044: public abstract class JRAbstractScriptlet {
045:
046: /**
047: *
048: */
049: protected Map parametersMap = null;
050: protected Map fieldsMap = null;
051: protected Map variablesMap = null;
052: protected JRFillGroup[] groups = null;
053:
054: /**
055: *
056: */
057: public JRAbstractScriptlet() {
058: }
059:
060: /**
061: *
062: */
063: public void setData(Map parsm, Map fldsm, Map varsm,
064: JRFillGroup[] grps) {
065: parametersMap = parsm;
066: fieldsMap = fldsm;
067: variablesMap = varsm;
068: groups = grps;
069: }
070:
071: /**
072: *
073: */
074: public Object getParameterValue(String parameterName)
075: throws JRScriptletException {
076: JRFillParameter parameter = (JRFillParameter) this .parametersMap
077: .get(parameterName);
078: if (parameter == null) {
079: throw new JRScriptletException("Parameter not found : "
080: + parameterName);
081: }
082: return parameter.getValue();
083: }
084:
085: /**
086: *
087: */
088: public Object getFieldValue(String fieldName)
089: throws JRScriptletException {
090: JRFillField field = (JRFillField) this .fieldsMap.get(fieldName);
091: if (field == null) {
092: throw new JRScriptletException("Field not found : "
093: + fieldName);
094: }
095: return field.getValue();
096: }
097:
098: /**
099: *
100: */
101: public Object getVariableValue(String variableName)
102: throws JRScriptletException {
103: JRFillVariable variable = (JRFillVariable) this .variablesMap
104: .get(variableName);
105: if (variable == null) {
106: throw new JRScriptletException("Variable not found : "
107: + variableName);
108: }
109: return variable.getValue();
110: }
111:
112: /**
113: *
114: */
115: public void setVariableValue(String variableName, Object value)
116: throws JRScriptletException {
117: JRFillVariable variable = (JRFillVariable) this .variablesMap
118: .get(variableName);
119: if (variable == null) {
120: throw new JRScriptletException("Variable not found : "
121: + variableName);
122: }
123:
124: if (value != null
125: && !variable.getValueClass().isInstance(value)) {
126: throw new JRScriptletException(
127: "Incompatible value assigned to variable "
128: + variableName + ". Expected "
129: + variable.getValueClassName() + ".");
130: }
131:
132: variable.setValue(value);
133: }
134:
135: /**
136: *
137: */
138: public void callBeforeReportInit() throws JRScriptletException {
139: this .beforeReportInit();
140: this .beforePageInit();
141: this .beforeColumnInit();
142:
143: if (groups != null && groups.length > 0) {
144: for (int i = 0; i < groups.length; i++) {
145: this .beforeGroupInit(groups[i].getName());
146: }
147: }
148: }
149:
150: /**
151: *
152: */
153: public void callAfterReportInit() throws JRScriptletException {
154: if (groups != null && groups.length > 0) {
155: for (int i = groups.length - 1; i >= 0; i--) {
156: this .afterGroupInit(groups[i].getName());
157: }
158: }
159:
160: this .afterColumnInit();
161: this .afterPageInit();
162: this .afterReportInit();
163: }
164:
165: /**
166: *
167: */
168: public void callBeforePageInit() throws JRScriptletException {
169: this .beforePageInit();
170: this .beforeColumnInit();
171: }
172:
173: /**
174: *
175: */
176: public void callAfterPageInit() throws JRScriptletException {
177: this .afterColumnInit();
178: this .afterPageInit();
179: }
180:
181: /**
182: *
183: */
184: public void callBeforeColumnInit() throws JRScriptletException {
185: this .beforeColumnInit();
186: }
187:
188: /**
189: *
190: */
191: public void callAfterColumnInit() throws JRScriptletException {
192: this .afterColumnInit();
193: }
194:
195: /**
196: *
197: */
198: public void callBeforeGroupInit() throws JRScriptletException {
199: if (groups != null && groups.length > 0) {
200: JRFillGroup group = null;
201: for (int i = 0; i < groups.length; i++) {
202: group = groups[i];
203: if (group.hasChanged()) {
204: this .beforeGroupInit(group.getName());
205: }
206: }
207: }
208: }
209:
210: /**
211: *
212: */
213: public void callAfterGroupInit() throws JRScriptletException {
214: if (groups != null && groups.length > 0) {
215: JRFillGroup group = null;
216: for (int i = groups.length - 1; i >= 0; i--) {
217: group = groups[i];
218: if (group.hasChanged()) {
219: this .afterGroupInit(group.getName());
220: }
221: }
222: }
223: }
224:
225: /**
226: *
227: */
228: public void callBeforeDetailEval() throws JRScriptletException {
229: this .beforeDetailEval();
230: }
231:
232: /**
233: *
234: */
235: public void callAfterDetailEval() throws JRScriptletException {
236: this .afterDetailEval();
237: }
238:
239: /**
240: * Called before the report is initialized.
241: */
242: public abstract void beforeReportInit() throws JRScriptletException;
243:
244: /**
245: * Called after the report is initialized.
246: */
247: public abstract void afterReportInit() throws JRScriptletException;
248:
249: /**
250: * Called before each page is initialized.
251: */
252: public abstract void beforePageInit() throws JRScriptletException;
253:
254: /**
255: * Called after each page is initialized.
256: */
257: public abstract void afterPageInit() throws JRScriptletException;
258:
259: /**
260: * Called before each column is initialized.
261: */
262: public abstract void beforeColumnInit() throws JRScriptletException;
263:
264: /**
265: * Called after each column is initialized.
266: */
267: public abstract void afterColumnInit() throws JRScriptletException;
268:
269: /**
270: * Called before a group is initialized.
271: * @param groupName the group name
272: */
273: public abstract void beforeGroupInit(String groupName)
274: throws JRScriptletException;
275:
276: /**
277: * Called after a group is initialized.
278: * @param groupName the group name
279: */
280: public abstract void afterGroupInit(String groupName)
281: throws JRScriptletException;
282:
283: /**
284: * Called before evaluating each detail.
285: */
286: public abstract void beforeDetailEval() throws JRScriptletException;
287:
288: /**
289: * Called after evaluating each detail.
290: */
291: public abstract void afterDetailEval() throws JRScriptletException;
292:
293: }
|