Debian Codegraph @

source/org/jfree/report/data/GlobalMasterRow.java

raw
1 /**
2 * ========================================
3 * JFreeReport : a free Java report library
4 * ========================================
5 *
6 * Project Info: http://reporting.pentaho.org/
7 *
8 * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors.
9 *
10 * This library is free software; you can redistribute it and/or modify it under the terms
11 * of the GNU Lesser General Public License as published by the Free Software Foundation;
12 * either version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 * See the GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License along with this
19 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307, USA.
21 *
22 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
23 * in the United States and other countries.]
24 *
25 * ------------
26 * $Id: GlobalMasterRow.java 10756 2009-12-02 15:58:24Z tmorgner $
27 * ------------
28 * (C) Copyright 2000-2005, by Object Refinery Limited.
29 * (C) Copyright 2005-2007, by Pentaho Corporation.
30 */
31 package org.jfree.report.data;
32
33 import org.jfree.report.DataRow;
34 import org.jfree.report.DataSourceException;
35 import org.jfree.report.flow.ReportContext;
36
37 /**
38 * This data row holds all statefull information from the datasources of the
39 * report.
40 * <p/>
41 * When doing subreports, a datarow only has access to its own dataset and the
42 * columns from the next direct subreport, which have been marked as exported.
43 *
44 * @author Thomas Morgner
45 */
46 public final class GlobalMasterRow
47 {
48 // private StaticDataRow previousRow;
49 private ReportDataRow reportDataRow;
50 private ParameterDataRow parameterDataRow;
51 private ExpressionDataRow expressionDataRow;
52 private GlobalMasterRow parentDataRow;
53 private FastGlobalView globalView;
54 private ImportedVariablesDataRow importedDataRow;
55
56 private GlobalMasterRow()
57 {
58 }
59
60 public static GlobalMasterRow createReportRow(final ReportContext reportContext)
61 {
62 GlobalMasterRow gmr = new GlobalMasterRow();
63 gmr.globalView = new FastGlobalView();
64 gmr.expressionDataRow = new ExpressionDataRow(gmr, reportContext, 10);
65 return gmr;
66 }
67
68 public static GlobalMasterRow createReportRow(final GlobalMasterRow parentRow,
69 final ReportContext reportContext)
70 {
71 GlobalMasterRow gmr = createReportRow(reportContext);
72 gmr.parentDataRow = parentRow;
73 return gmr;
74 }
75
76 public ExpressionDataRow getExpressionDataRow()
77 {
78 return expressionDataRow;
79 }
80
81 public ReportDataRow getReportDataRow()
82 {
83 return reportDataRow;
84 }
85
86 public void setReportDataRow(final ReportDataRow reportDataRow)
87 throws DataSourceException
88 {
89 if (this.reportDataRow != null)
90 {
91 final int dataColCount = this.reportDataRow.getColumnCount();
92 for (int i = dataColCount - 1; i >= 0; i--)
93 {
94 final String columnName = this.reportDataRow.getColumnName(i);
95 if (columnName != null)
96 {
97 globalView.removeColumn(columnName);
98 }
99 }
100 }
101
102 this.reportDataRow = reportDataRow;
103
104 if (reportDataRow != null)
105 {
106 final boolean readable = reportDataRow.getReportData().isReadable();
107 final int dataColCount = reportDataRow.getColumnCount();
108 for (int i = 0; i < dataColCount; i++)
109 {
110 final String columnName = reportDataRow.getColumnName(i);
111 if (columnName != null)
112 {
113 if (readable)
114 {
115 final Object columnValue = reportDataRow.get(i);
116 globalView.putField(columnName, columnValue, false);
117 }
118 else
119 {
120 globalView.putField(columnName, null, false);
121 }
122 }
123 }
124 }
125 }
126
127 public ParameterDataRow getParameterDataRow()
128 {
129 return parameterDataRow;
130 }
131
132 public void setParameterDataRow(final ParameterDataRow parameterDataRow)
133 throws DataSourceException
134 {
135 if (this.parameterDataRow != null)
136 {
137 final int parameterCount = this.parameterDataRow.getColumnCount();
138 for (int i = parameterCount - 1; i >= 0; i--)
139 {
140 final String columnName = this.parameterDataRow.getColumnName(i);
141 if (columnName != null)
142 {
143 globalView.removeColumn(columnName);
144 }
145 }
146 }
147
148 this.parameterDataRow = parameterDataRow;
149
150 if (parameterDataRow != null)
151 {
152 final int parameterCount = parameterDataRow.getColumnCount();
153 for (int i = 0; i < parameterCount; i++)
154 {
155 final String columnName = parameterDataRow.getColumnName(i);
156 if (columnName != null)
157 {
158 final Object columnValue = parameterDataRow.get(i);
159 globalView.putField(columnName, columnValue, false);
160 }
161 }
162 }
163 }
164
165 public GlobalMasterRow getParentDataRow()
166 {
167 return parentDataRow;
168 }
169
170 public ImportedVariablesDataRow getImportedDataRow()
171 {
172 return importedDataRow;
173 }
174
175 public void setExportedDataRow(final ImportedVariablesDataRow importedDataRow)
176 throws DataSourceException
177 {
178 if (importedDataRow != null)
179 {
180 final int parameterCount = importedDataRow.getColumnCount();
181 for (int i = parameterCount - 1; i >= 0; i--)
182 {
183 final String columnName = importedDataRow.getColumnName(i);
184 if (columnName != null)
185 {
186 globalView.removeColumn(columnName);
187 }
188 }
189 }
190
191 this.importedDataRow = importedDataRow;
192 if (importedDataRow != null)
193 {
194 final int parameterCount = importedDataRow.getColumnCount();
195 for (int i = 0; i < parameterCount; i++)
196 {
197 final String columnName = importedDataRow.getColumnName(i);
198 if (columnName != null)
199 {
200 final Object columnValue = importedDataRow.get(i);
201 globalView.putField(columnName, columnValue, false);
202 }
203 }
204 }
205 }
206
207 /**
208 * Derives an instance of this datarow. That copy is completly disconnected
209 * from the original one and no change made to that copy affects the original
210 * datarow.
211 *
212 * @return the derived datarow.
213 */
214 public GlobalMasterRow derive() throws DataSourceException
215 {
216 return derive(null);
217 }
218
219 private GlobalMasterRow derive(GlobalMasterRow subReportRow)
220 throws DataSourceException
221 {
222 final GlobalMasterRow dataRow = new GlobalMasterRow();
223 dataRow.parameterDataRow = parameterDataRow;
224 dataRow.reportDataRow = reportDataRow;
225 dataRow.expressionDataRow = expressionDataRow.derive(dataRow);
226 dataRow.globalView = globalView.derive();
227 if (parentDataRow != null)
228 {
229 dataRow.parentDataRow = parentDataRow.derive(subReportRow);
230 }
231 dataRow.importedDataRow = importedDataRow;
232 return dataRow;
233 }
234
235 /**
236 * This advances the cursor by one row and updates the flags.
237 *
238 * @return
239 * @throws DataSourceException
240 */
241 public GlobalMasterRow advance() throws DataSourceException
242 {
243 return advance(false, null);
244 }
245
246 private GlobalMasterRow advance(final boolean deepTraversingOnly,
247 final GlobalMasterRow subReportRow)
248 throws DataSourceException
249 {
250 GlobalMasterRow dataRow = new GlobalMasterRow();
251 dataRow.globalView = globalView.advance();
252 dataRow.parameterDataRow = parameterDataRow;
253
254 if (deepTraversingOnly == false && reportDataRow != null)
255 {
256 dataRow.reportDataRow = reportDataRow.advance();
257 }
258 else
259 {
260 dataRow.reportDataRow = reportDataRow;
261 }
262 dataRow.updateGlobalView();
263 if (expressionDataRow != null)
264 {
265 dataRow.expressionDataRow =
266 expressionDataRow.advance(dataRow, deepTraversingOnly);
267 }
268 if (parentDataRow != null)
269 {
270 // the parent row should get a grip on our data as well - just for the
271 // deep traversing fun and so on ..
272 dataRow.parentDataRow = parentDataRow.advance(true, dataRow);
273 }
274 if (importedDataRow != null)
275 {
276 if (subReportRow == null)
277 {
278 throw new NullPointerException();
279 }
280 dataRow.importedDataRow = importedDataRow.advance(subReportRow);
281 dataRow.updateImportedParameterView();
282 }
283 return dataRow;
284 }
285
286 private void updateImportedParameterView() throws DataSourceException
287 {
288 if (importedDataRow == null)
289 {
290 return;
291 }
292
293 final int parameterCount = importedDataRow.getColumnCount();
294 for (int i = 0; i < parameterCount; i++)
295 {
296 final String columnName = importedDataRow.getColumnName(i);
297 if (columnName != null)
298 {
299 final Object columnValue = importedDataRow.get(i);
300 globalView.putField(columnName, columnValue, true);
301 }
302 }
303 }
304
305 /** This updates the global view. */
306 private void updateGlobalView() throws DataSourceException
307 {
308 if (parameterDataRow != null)
309 {
310 final int parameterCount = parameterDataRow.getColumnCount();
311 for (int i = 0; i < parameterCount; i++)
312 {
313 final String columnName = parameterDataRow.getColumnName(i);
314 if (columnName != null)
315 {
316 final Object columnValue = parameterDataRow.get(i);
317 globalView.putField(columnName, columnValue, true);
318 }
319 }
320 }
321 if (reportDataRow != null)
322 {
323 final int dataColCount = reportDataRow.getColumnCount();
324 for (int i = 0; i < dataColCount; i++)
325 {
326 final String columnName = reportDataRow.getColumnName(i);
327 if (columnName != null)
328 {
329 final Object columnValue = reportDataRow.get(i);
330 globalView.putField(columnName, columnValue, true);
331 }
332 }
333 }
334 }
335
336 public boolean isAdvanceable() throws DataSourceException
337 {
338 if (reportDataRow == null)
339 {
340 return false;
341 }
342 return reportDataRow.isAdvanceable();
343 }
344
345 public DataRow getGlobalView()
346 {
347 return globalView;
348 }
349
350 /**
351 * A call back method to communicate structural changes back to the master
352 * rows. (This is only called from the expression row, as all other datarows
353 * are static).
354 *
355 * @param chEvent
356 */
357 public void dataRowChanged(final MasterDataRowChangeEvent chEvent)
358 throws DataSourceException
359 {
360 // rebuild the global view and tracks changes ..
361 final int type = chEvent.getType();
362 if (type == MasterDataRowChangeEvent.COLUMN_ADDED)
363 {
364 globalView.putField(chEvent.getColumnName(), chEvent.getColumnValue(), false);
365 }
366 else if (type == MasterDataRowChangeEvent.COLUMN_UPDATED)
367 {
368 globalView.putField(chEvent.getColumnName(), chEvent.getColumnValue(), true);
369 }
370 else if (type == MasterDataRowChangeEvent.COLUMN_REMOVED)
371 {
372 globalView.removeColumn(chEvent.getColumnName());
373 }
374 }
375 }