Debian Codegraph @

source/org/jfree/report/flow/layoutprocessor/ElementLayoutController.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: ElementLayoutController.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
32 package org.jfree.report.flow.layoutprocessor;
33
34 import org.jfree.report.DataSourceException;
35 import org.jfree.report.ReportDataFactoryException;
36 import org.jfree.report.ReportProcessingException;
37 import org.jfree.report.data.ExpressionSlot;
38 import org.jfree.report.data.PrecomputeNodeKey;
39 import org.jfree.report.data.PrecomputedExpressionSlot;
40 import org.jfree.report.data.PrecomputedValueRegistry;
41 import org.jfree.report.data.RunningExpressionSlot;
42 import org.jfree.report.data.StaticExpressionRuntimeData;
43 import org.jfree.report.expressions.Expression;
44 import org.jfree.report.flow.FlowControlOperation;
45 import org.jfree.report.flow.FlowController;
46 import org.jfree.report.flow.ReportTarget;
47 import org.jfree.report.flow.LayoutExpressionRuntime;
48 import org.jfree.report.structure.Element;
49 import org.jfree.layouting.util.AttributeMap;
50
51 /**
52 * Creation-Date: 24.11.2006, 13:56:30
53 *
54 * @author Thomas Morgner
55 */
56 public abstract class ElementLayoutController
57 implements LayoutController
58 {
59 protected static class ElementPrecomputeKey implements PrecomputeNodeKey
60 {
61 private String name;
62 private String id;
63 private String namespace;
64 private String tagName;
65
66 public ElementPrecomputeKey(final Element element)
67 {
68 this.name = element.getName();
69 this.tagName = element.getType();
70 this.namespace = element.getNamespace();
71 this.id = element.getId();
72 }
73
74 public boolean equals(final Object obj)
75 {
76 if (this == obj)
77 {
78 return true;
79 }
80 if (obj == null || getClass() != obj.getClass())
81 {
82 return false;
83 }
84
85 final ElementPrecomputeKey that = (ElementPrecomputeKey) obj;
86
87 if (id != null ? !id.equals(that.id) : that.id != null)
88 {
89 return false;
90 }
91 if (name != null ? !name.equals(that.name) : that.name != null)
92 {
93 return false;
94 }
95 if (namespace != null ? !namespace.equals(
96 that.namespace) : that.namespace != null)
97 {
98 return false;
99 }
100 if (tagName != null ? !tagName.equals(
101 that.tagName) : that.tagName != null)
102 {
103 return false;
104 }
105
106 return true;
107 }
108
109 public int hashCode()
110 {
111 int result = (name != null ? name.hashCode() : 0);
112 result = 29 * result + (id != null ? id.hashCode() : 0);
113 result = 29 * result + (namespace != null ? namespace.hashCode() : 0);
114 result = 29 * result + (tagName != null ? tagName.hashCode() : 0);
115 return result;
116 }
117
118 public boolean equals(final PrecomputeNodeKey otherKey)
119 {
120 return false;
121 }
122 }
123
124 public static final int NOT_STARTED = 0;
125 public static final int OPENED = 1;
126 public static final int WAITING_FOR_JOIN = 2;
127 public static final int FINISHING = 3;
128 //public static final int JOINING = 4;
129 public static final int FINISHED = 4;
130
131 private int processingState;
132 private FlowController flowController;
133 private Element element;
134 private LayoutController parent;
135 private boolean precomputing;
136 private AttributeMap attributeMap;
137 private int expressionsCount;
138 private int iterationCount;
139
140 protected ElementLayoutController()
141 {
142 this.processingState = ElementLayoutController.NOT_STARTED;
143 }
144
145
146 public String toString()
147 {
148 return "ElementLayoutController{" +
149 "processingState=" + processingState +
150 ", element=" + element +
151 ", precomputing=" + precomputing +
152 ", expressionsCount=" + expressionsCount +
153 ", iterationCount=" + iterationCount +
154 '}';
155 }
156
157 /**
158 * Retrieves the parent of this layout controller. This allows childs to query
159 * their context.
160 *
161 * @return the layout controller's parent to <code>null</code> if there is no
162 * parent.
163 */
164 public LayoutController getParent()
165 {
166 return parent;
167 }
168
169
170 /**
171 * Initializes the layout controller. This method is called exactly once. It
172 * is the creators responsibility to call this method.
173 * <p/>
174 * Calling initialize after the first advance must result in a
175 * IllegalStateException.
176 *
177 * @param node the currently processed object or layout node.
178 * @param flowController the current flow controller.
179 * @param parent the parent layout controller that was responsible for
180 * instantiating this controller.
181 * @throws DataSourceException if there was a problem reading data from
182 * the datasource.
183 * @throws ReportProcessingException if there was a general problem during
184 * the report processing.
185 * @throws ReportDataFactoryException if a query failed.
186 */
187 public void initialize(final Object node,
188 final FlowController flowController,
189 final LayoutController parent)
190 throws DataSourceException, ReportDataFactoryException,
191 ReportProcessingException
192 {
193
194 if (processingState != ElementLayoutController.NOT_STARTED)
195 {
196 throw new IllegalStateException();
197 }
198
199 this.element = (Element) node;
200 this.flowController = flowController;
201 this.parent = parent;
202 this.iterationCount = -1;
203 }
204
205 /**
206 * Advances the layout controller to the next state. This method delegates the
207 * call to one of the following methods: <ul> <li>{@link
208 * #startElement(org.jfree.report.flow.ReportTarget)} <li>{@link
209 * #processContent(org.jfree.report.flow.ReportTarget)} <li>{@link
210 * #finishElement(org.jfree.report.flow.ReportTarget)} </ul>
211 *
212 * @param target the report target that receives generated events.
213 * @return the new layout controller instance representing the new state.
214 *
215 * @throws DataSourceException if there was a problem reading data from
216 * the datasource.
217 * @throws ReportProcessingException if there was a general problem during
218 * the report processing.
219 * @throws ReportDataFactoryException if a query failed.
220 */
221 public final LayoutController advance(final ReportTarget target)
222 throws DataSourceException, ReportProcessingException,
223 ReportDataFactoryException
224 {
225 final int processingState = getProcessingState();
226 switch (processingState)
227 {
228 case ElementLayoutController.NOT_STARTED:
229 return startElement(target);
230 case ElementLayoutController.OPENED:
231 return processContent(target);
232 case ElementLayoutController.FINISHING:
233 return finishElement(target);
234 // case ElementLayoutController.JOINING:
235 // return joinWithParent();
236 default:
237 throw new IllegalStateException();
238 }
239 }
240
241 /**
242 * This method is called for each newly instantiated layout controller. The
243 * returned layout controller instance should have a processing state of
244 * either 'OPEN' or 'FINISHING' depending on whether there is any content or
245 * any child nodes to process.
246 *
247 * @param target the report target that receives generated events.
248 * @return the new layout controller instance representing the new state.
249 *
250 * @throws DataSourceException if there was a problem reading data from
251 * the datasource.
252 * @throws ReportProcessingException if there was a general problem during
253 * the report processing.
254 * @throws ReportDataFactoryException if a query failed.
255 */
256 protected LayoutController startElement(final ReportTarget target)
257 throws DataSourceException, ReportProcessingException,
258 ReportDataFactoryException
259 {
260 final Element s = getElement();
261
262 FlowController fc = getFlowController();
263 // Step 3: Add the expressions. Any expressions defined for the subreport
264 // will work on the queried dataset.
265 fc = startData(target, fc);
266
267 final Expression[] expressions = s.getExpressions();
268 fc = performElementPrecomputation(expressions, fc);
269
270 if (s.isVirtual() == false)
271 {
272 attributeMap = computeAttributes(fc, s, target);
273 target.startElement(attributeMap);
274 }
275
276 final ElementLayoutController derived = (ElementLayoutController) clone();
277 derived.setProcessingState(ElementLayoutController.OPENED);
278 derived.setFlowController(fc);
279 derived.expressionsCount = expressions.length;
280 derived.attributeMap = attributeMap;
281 derived.iterationCount += 1;
282 return derived;
283 }
284
285 public AttributeMap getAttributeMap()
286 {
287 return attributeMap;
288 }
289
290 public int getExpressionsCount()
291 {
292 return expressionsCount;
293 }
294
295 public int getIterationCount()
296 {
297 return iterationCount;
298 }
299
300
301 protected FlowController startData(final ReportTarget target,
302 final FlowController fc)
303 throws DataSourceException, ReportProcessingException,
304 ReportDataFactoryException
305 {
306 return fc;
307 }
308
309 protected AttributeMap computeAttributes(final FlowController fc,
310 final Element element,
311 final ReportTarget target)
312 throws DataSourceException
313 {
314 final LayoutExpressionRuntime ler =
315 LayoutControllerUtil.getExpressionRuntime(fc, element);
316 return LayoutControllerUtil.processAttributes(element, target, ler);
317 }
318
319
320 /**
321 * Processes any content in this element. This method is called when the
322 * processing state is 'OPENED'. The returned layout controller will retain
323 * the 'OPENED' state as long as there is more content available. Once all
324 * content has been processed, the returned layout controller should carry a
325 * 'FINISHED' state.
326 *
327 * @param target the report target that receives generated events.
328 * @return the new layout controller instance representing the new state.
329 *
330 * @throws DataSourceException if there was a problem reading data from
331 * the datasource.
332 * @throws ReportProcessingException if there was a general problem during
333 * the report processing.
334 * @throws ReportDataFactoryException if a query failed.
335 */
336 protected abstract LayoutController processContent(final ReportTarget target)
337 throws DataSourceException, ReportProcessingException,
338 ReportDataFactoryException;
339
340 /**
341 * Finishes the processing of this element. This method is called when the
342 * processing state is 'FINISHING'. The element should be closed now and all
343 * privatly owned resources should be freed. If the element has a parent, it
344 * would be time to join up with the parent now, else the processing state
345 * should be set to 'FINISHED'.
346 *
347 * @param target the report target that receives generated events.
348 * @return the new layout controller instance representing the new state.
349 *
350 * @throws DataSourceException if there was a problem reading data from
351 * the datasource.
352 * @throws ReportProcessingException if there was a general problem during the
353 * report processing.
354 * @throws ReportDataFactoryException if there was an error trying query data.
355 */
356 protected LayoutController finishElement(final ReportTarget target)
357 throws ReportProcessingException, DataSourceException,
358 ReportDataFactoryException
359 {
360 final FlowController fc = handleDefaultEndElement(target);
361 final ElementLayoutController derived = (ElementLayoutController) clone();
362 derived.setProcessingState(ElementLayoutController.FINISHED);
363 derived.setFlowController(fc);
364 return derived;
365 }
366
367 protected FlowController handleDefaultEndElement (final ReportTarget target)
368 throws ReportProcessingException, DataSourceException,
369 ReportDataFactoryException
370 {
371 final Element e = getElement();
372 // Step 1: call End Element
373 if (e.isVirtual() == false)
374 {
375 target.endElement(getAttributeMap());
376 }
377
378 FlowController fc = getFlowController();
379 final PrecomputedValueRegistry pcvr =
380 fc.getPrecomputedValueRegistry();
381 // Step 2: Remove the expressions of this element
382 final int expressionsCount = getExpressionsCount();
383 if (expressionsCount != 0)
384 {
385 final ExpressionSlot[] activeExpressions = fc.getActiveExpressions();
386 for (int i = activeExpressions.length - expressionsCount; i < activeExpressions.length; i++)
387 {
388 final ExpressionSlot slot = activeExpressions[i];
389 pcvr.addFunction(slot.getName(), slot.getValue());
390 }
391 fc = fc.deactivateExpressions();
392 }
393
394 if (isPrecomputing() == false)
395 {
396 pcvr.finishElement(new ElementPrecomputeKey(e));
397 }
398
399 return fc;
400 }
401 //
402 // /**
403 // * Joins the layout controller with the parent. This simply calls
404 // * {@link #join(org.jfree.report.flow.FlowController)} on the parent. A join
405 // * operation is necessary to propagate changes in the flow-controller to the
406 // * parent for further processing.
407 // *
408 // * @return the joined parent.
409 // * @throws IllegalStateException if this layout controller has no parent.
410 // */
411 // protected LayoutController joinWithParent()
412 // throws ReportProcessingException, ReportDataFactoryException,
413 // DataSourceException
414 // {
415 // final LayoutController parent = getParent();
416 // if (parent == null)
417 // {
418 // // skip to the next step ..
419 // throw new IllegalStateException("There is no parent to join with. " +
420 // "This should not happen in a sane environment!");
421 // }
422 //
423 // return parent.join(getFlowController());
424 // }
425
426 public boolean isAdvanceable()
427 {
428 return processingState != ElementLayoutController.FINISHED;
429 }
430
431 public Element getElement()
432 {
433 return element;
434 }
435
436 public FlowController getFlowController()
437 {
438 return flowController;
439 }
440
441 public int getProcessingState()
442 {
443 return processingState;
444 }
445
446 public void setProcessingState(final int processingState)
447 {
448 this.processingState = processingState;
449 }
450
451 public void setFlowController(final FlowController flowController)
452 {
453 this.flowController = flowController;
454 }
455
456 public void setParent(final LayoutController parent)
457 {
458 this.parent = parent;
459 }
460
461 public Object clone()
462 {
463 try
464 {
465 return super.clone();
466 }
467 catch (CloneNotSupportedException e)
468 {
469 throw new IllegalStateException("Clone must be supported.");
470 }
471 }
472
473 public boolean isPrecomputing()
474 {
475 return precomputing;
476 }
477
478 protected FlowController performElementPrecomputation(
479 final Expression[] expressions,
480 FlowController fc)
481 throws ReportProcessingException, ReportDataFactoryException,
482 DataSourceException
483 {
484 final Element element = getElement();
485 final PrecomputedValueRegistry pcvr = fc.getPrecomputedValueRegistry();
486 if (isPrecomputing() == false)
487 {
488 pcvr.startElement(new ElementPrecomputeKey(element));
489 }
490
491 if (expressions.length > 0)
492 {
493 final ExpressionSlot[] slots = new ExpressionSlot[expressions.length];
494 final StaticExpressionRuntimeData runtimeData =
495 LayoutControllerUtil.getStaticExpressionRuntime(fc, element);
496
497 for (int i = 0; i < expressions.length; i++)
498 {
499 final Expression expression = expressions[i];
500 if (isPrecomputing() == false && expression.isPrecompute())
501 {
502 // ok, we have to precompute the expression's value. For that
503 // we fork a new layout process, compute the value and then come
504 // back with the result.
505 final Object value = LayoutControllerUtil.performPrecompute
506 (i, new ElementPrecomputeKey(element),
507 this, getFlowController());
508 slots[i] = new PrecomputedExpressionSlot(expression.getName(), value,
509 expression.isPreserve());
510 }
511 else
512 {
513 // thats a bit easier; we dont have to do anything special ..
514 slots[i] = new RunningExpressionSlot(expression, runtimeData,
515 pcvr.currentNode());
516 }
517 }
518
519 fc = fc.activateExpressions(slots);
520 }
521 return fc;
522 }
523
524
525 protected FlowController tryRepeatingCommit(final FlowController fc)
526 throws DataSourceException
527 {
528 if (isPrecomputing() == false)
529 {
530 // ok, the user wanted us to repeat. So we repeat if the group in which
531 // we are in, is not closed (and at least one advance has been fired
532 // since the last repeat request [to prevent infinite loops]) ...
533 final boolean advanceRequested = fc.isAdvanceRequested();
534 final boolean advanceable = fc.getMasterRow().isAdvanceable();
535 if (advanceable && advanceRequested)
536 {
537 // we check against the commited target; But we will not use the
538 // commited target if the group is no longer active...
539 final FlowController cfc =
540 fc.performOperation(FlowControlOperation.COMMIT);
541 final boolean groupFinished =
542 LayoutControllerUtil.isGroupFinished(cfc, getElement());
543 if (groupFinished == false)
544 {
545 return cfc;
546 }
547 }
548 }
549 return null;
550 }
551
552
553 /**
554 * Derives a copy of this controller that is suitable to perform a
555 * precomputation.
556 *
557 * @param fc
558 * @return
559 */
560 public LayoutController createPrecomputeInstance(final FlowController fc)
561 {
562 final ElementLayoutController lc = (ElementLayoutController) clone();
563 lc.setFlowController(fc);
564 lc.setParent(null);
565 lc.precomputing = true;
566 return lc;
567 }
568
569
570 public Object getNode()
571 {
572 return getElement();
573 }
574 }