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: * GlobalMasterRow.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.states.datarow;
030:
031: import org.jfree.report.DataRow;
032: import org.jfree.report.ParameterDataRow;
033: import org.jfree.report.event.ReportEvent;
034: import org.jfree.report.function.ProcessingContext;
035: import org.jfree.report.states.ReportState;
036:
037: /**
038: * Creation-Date: Dec 13, 2006, 2:52:23 PM
039: *
040: * @author Thomas Morgner
041: */
042: public final class GlobalMasterRow {
043: private ReportDataRow reportDataRow;
044: private ExpressionDataRow expressionDataRow;
045: private ParameterDataRow parameterDataRow;
046: private GlobalMasterRow parentRow;
047: private FastGlobalView globalView;
048: private ImportedVariablesDataRow importedDataRow;
049:
050: private GlobalMasterRow() {
051: }
052:
053: public static GlobalMasterRow createReportRow(
054: final ProcessingContext reportContext) {
055: final GlobalMasterRow gmr = new GlobalMasterRow();
056: gmr.globalView = new FastGlobalView();
057: gmr.expressionDataRow = new ExpressionDataRow(gmr,
058: reportContext);
059: return gmr;
060: }
061:
062: public static GlobalMasterRow createReportRow(
063: final GlobalMasterRow parentRow,
064: final ProcessingContext reportContext) {
065: final GlobalMasterRow gmr = new GlobalMasterRow();
066: gmr.globalView = new FastGlobalView();
067: gmr.expressionDataRow = new ExpressionDataRow(gmr,
068: reportContext);
069: gmr.parentRow = parentRow;
070: return gmr;
071: }
072:
073: public ReportDataRow getReportDataRow() {
074: return reportDataRow;
075: }
076:
077: public void setReportDataRow(final ReportDataRow reportDataRow) {
078: if (this .reportDataRow != null) {
079: final int dataColCount = this .reportDataRow
080: .getColumnCount();
081: for (int i = dataColCount - 1; i >= 0; i--) {
082: final String columnName = this .reportDataRow
083: .getColumnName(i);
084: if (columnName != null) {
085: globalView.removeColumn(columnName);
086: }
087: }
088: }
089:
090: this .reportDataRow = reportDataRow;
091:
092: // todo if the fucking thing is empty, dont try to query it.
093: if (reportDataRow != null) {
094: final boolean readable = reportDataRow.isReadable();
095: final int dataColCount = reportDataRow.getColumnCount();
096: for (int i = 0; i < dataColCount; i++) {
097: final String columnName = reportDataRow
098: .getColumnName(i);
099: if (columnName != null) {
100: if (readable) {
101: final Object columnValue = reportDataRow.get(i);
102: globalView.putField(columnName, columnValue,
103: false);
104: } else {
105: globalView.putField(columnName, null, false);
106: }
107: }
108: }
109: }
110:
111: }
112:
113: public ExpressionDataRow getExpressionDataRow() {
114: return expressionDataRow;
115: }
116:
117: public void setExpressionDataRow(
118: final ExpressionDataRow expressionDataRow) {
119: this .expressionDataRow = expressionDataRow;
120: }
121:
122: public ParameterDataRow getParameterDataRow() {
123: return parameterDataRow;
124: }
125:
126: public void setParameterDataRow(
127: final ParameterDataRow parameterDataRow) {
128: if (this .parameterDataRow != null) {
129: final int parameterCount = this .parameterDataRow
130: .getColumnCount();
131: for (int i = parameterCount - 1; i >= 0; i--) {
132: final String columnName = this .parameterDataRow
133: .getColumnName(i);
134: if (columnName != null) {
135: globalView.removeColumn(columnName);
136: }
137: }
138: }
139:
140: this .parameterDataRow = parameterDataRow;
141:
142: if (parameterDataRow != null) {
143: final int parameterCount = parameterDataRow
144: .getColumnCount();
145: for (int i = 0; i < parameterCount; i++) {
146: final String columnName = parameterDataRow
147: .getColumnName(i);
148: if (columnName != null) {
149: final Object columnValue = parameterDataRow.get(i);
150: globalView.putField(columnName, columnValue, false);
151: }
152: }
153: }
154: }
155:
156: public DataRow getGlobalView() {
157: return globalView;
158: }
159:
160: public void dataRowChanged(final MasterDataRowChangeEvent chEvent) {
161: // rebuild the global view and tracks changes ..
162: final int type = chEvent.getType();
163: if (type == MasterDataRowChangeEvent.COLUMN_ADDED) {
164: globalView.putField(chEvent.getColumnName(), chEvent
165: .getColumnValue(), false);
166: } else if (type == MasterDataRowChangeEvent.COLUMN_UPDATED) {
167: globalView.putField(chEvent.getColumnName(), chEvent
168: .getColumnValue(), true);
169: } else if (type == MasterDataRowChangeEvent.COLUMN_REMOVED) {
170: globalView.removeColumn(chEvent.getColumnName());
171: }
172: }
173:
174: /**
175: * This updates the global view.
176: */
177: private void updateGlobalView() {
178: if (parameterDataRow != null) {
179: final int parameterCount = parameterDataRow
180: .getColumnCount();
181: for (int i = 0; i < parameterCount; i++) {
182: final String columnName = parameterDataRow
183: .getColumnName(i);
184: if (columnName != null) {
185: final Object columnValue = parameterDataRow.get(i);
186: globalView.putField(columnName, columnValue, true);
187: }
188: }
189: }
190:
191: if (reportDataRow != null) {
192: final int dataColCount = reportDataRow.getColumnCount();
193: final boolean readable = reportDataRow.isReadable();
194: for (int i = 0; i < dataColCount; i++) {
195: final String columnName = reportDataRow
196: .getColumnName(i);
197: if (columnName != null) {
198: if (readable) {
199: final Object columnValue = reportDataRow.get(i);
200: globalView.putField(columnName, columnValue,
201: true);
202: } else {
203: globalView.putField(columnName, null, true);
204: }
205: }
206: }
207: }
208: }
209:
210: public boolean isAdvanceable() {
211: if (reportDataRow != null) {
212: return reportDataRow.isAdvanceable();
213: }
214: return false;
215: }
216:
217: public GlobalMasterRow derive() {
218: final GlobalMasterRow o = new GlobalMasterRow();
219: o.globalView = globalView.derive();
220: o.reportDataRow = reportDataRow;
221: o.parameterDataRow = parameterDataRow;
222: o.expressionDataRow = expressionDataRow.derive(o, false);
223: if (parentRow != null) {
224: o.parentRow = parentRow.derive();
225: }
226: o.importedDataRow = importedDataRow;
227: return o;
228: }
229:
230: public void setExportedDataRow(
231: final ImportedVariablesDataRow dataRow) {
232: if (importedDataRow != null) {
233: final int parameterCount = importedDataRow.getColumnCount();
234: for (int i = parameterCount - 1; i >= 0; i--) {
235: final String columnName = importedDataRow
236: .getColumnName(i);
237: if (columnName != null) {
238: globalView.removeColumn(columnName);
239: }
240: }
241: }
242:
243: this .importedDataRow = dataRow;
244: if (importedDataRow != null) {
245: final int parameterCount = importedDataRow.getColumnCount();
246: for (int i = 0; i < parameterCount; i++) {
247: final String columnName = importedDataRow
248: .getColumnName(i);
249: if (columnName != null) {
250: final Object columnValue = importedDataRow.get(i);
251: globalView.putField(columnName, columnValue, false);
252: }
253: }
254: }
255: }
256:
257: public ImportedVariablesDataRow getExportedDataRow() {
258: return importedDataRow;
259: }
260:
261: public GlobalMasterRow getParentDataRow() {
262: return parentRow;
263: }
264:
265: /**
266: * This advances the cursor by one row and updates the flags.
267: *
268: * @return
269: */
270: public GlobalMasterRow advance() {
271: return advance(false, null);
272: }
273:
274: private GlobalMasterRow advance(final boolean deepTraversingOnly,
275: final GlobalMasterRow subReportRow) {
276: final GlobalMasterRow dataRow = new GlobalMasterRow();
277: dataRow.globalView = globalView.advance();
278: dataRow.parameterDataRow = parameterDataRow;
279:
280: if (deepTraversingOnly == false && reportDataRow != null) {
281: dataRow.reportDataRow = reportDataRow.advance();
282: } else {
283: dataRow.reportDataRow = reportDataRow;
284: }
285: dataRow.updateGlobalView();
286: if (expressionDataRow != null) {
287: dataRow.expressionDataRow = expressionDataRow.derive(
288: dataRow, true);
289: }
290: if (parentRow != null) {
291: // the parent row should get a grip on our data as well - just for the
292: // deep traversing fun and so on ..
293: dataRow.parentRow = parentRow.advance(true, dataRow);
294: }
295:
296: if (importedDataRow != null) {
297: if (subReportRow != null) {
298: dataRow.importedDataRow = importedDataRow
299: .refresh(subReportRow);
300: final int parameterCount = dataRow.importedDataRow
301: .getColumnCount();
302: for (int i = 0; i < parameterCount; i++) {
303: final String columnName = dataRow.importedDataRow
304: .getColumnName(i);
305: if (columnName != null) {
306: final Object columnValue = dataRow.importedDataRow
307: .get(i);
308: dataRow.globalView.putField(columnName,
309: columnValue, true);
310: }
311: }
312: }
313: }
314: return dataRow;
315: }
316:
317: public void fireReportEvent(final ReportEvent event) {
318: if (expressionDataRow != null) {
319: expressionDataRow.fireReportEvent(event);
320: }
321: if (parentRow != null) {
322: final ReportState parentState = event.getState()
323: .getParentSubReportState();
324: final ReportEvent deepEvent;
325: if (parentState == null) {
326: deepEvent = event;
327: } else {
328: deepEvent = new ReportEvent(parentState, event
329: .getState(), event.getType()
330: | ReportEvent.DEEP_TRAVERSING_EVENT);
331: }
332: parentRow.fireReportEvent(deepEvent);
333:
334: if (parentRow.importedDataRow != null) {
335: // This advance is just an refresh ...
336: parentRow.importedDataRow = parentRow.importedDataRow
337: .refresh(this );
338: final int parameterCount = parentRow.importedDataRow
339: .getColumnCount();
340: for (int i = 0; i < parameterCount; i++) {
341: final String columnName = parentRow.importedDataRow
342: .getColumnName(i);
343: if (columnName != null) {
344: final Object columnValue = parentRow.importedDataRow
345: .get(i);
346: parentRow.globalView.putField(columnName,
347: columnValue, true);
348: }
349: }
350: }
351: }
352: }
353:
354: public boolean isPrepareEventListener() {
355: if (parentRow != null) {
356: if (parentRow.isPrepareEventListener()) {
357: return true;
358: }
359: }
360:
361: if (expressionDataRow == null) {
362: return false;
363: }
364: return expressionDataRow.isPrepareEventListener();
365: }
366: }
|