1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 package com.sun.star.report.pentaho.layoutprocessor;
22 
23 import com.sun.star.report.pentaho.model.OfficeGroupSection;
24 import com.sun.star.report.pentaho.model.VariablesDeclarationSection;
25 
26 import org.jfree.layouting.util.AttributeMap;
27 import org.jfree.report.DataSourceException;
28 import org.jfree.report.JFreeReportInfo;
29 import org.jfree.report.ReportDataFactoryException;
30 import org.jfree.report.ReportProcessingException;
31 import org.jfree.report.expressions.Expression;
32 import org.jfree.report.flow.FlowController;
33 import org.jfree.report.flow.ReportContext;
34 import org.jfree.report.flow.ReportTarget;
35 import org.jfree.report.flow.layoutprocessor.ElementLayoutController;
36 import org.jfree.report.flow.layoutprocessor.LayoutController;
37 import org.jfree.report.flow.layoutprocessor.LayoutControllerFactory;
38 import org.jfree.report.flow.layoutprocessor.LayoutControllerUtil;
39 import org.jfree.report.flow.layoutprocessor.SectionLayoutController;
40 import org.jfree.report.structure.Element;
41 import org.jfree.report.structure.Node;
42 
43 /**
44  * Creation-Date: 25.07.2007, 14:50:45
45  *
46  * @author Thomas Morgner
47  */
48 public class OfficeGroupInstanceSectionLayoutController extends SectionLayoutController
49 {
50 
51     public static final int STATE_PROCESS_VARIABLES = 2;
52     public static final int STATE_PROCESS_NORMAL_FLOW = 3;
53     private int state;
54     private boolean waitForJoin;
55 
OfficeGroupInstanceSectionLayoutController()56     public OfficeGroupInstanceSectionLayoutController()
57     {
58     }
59 
initialize(final Object node, final FlowController flowController, final LayoutController parent)60     public void initialize(final Object node, final FlowController flowController, final LayoutController parent)
61             throws DataSourceException, ReportDataFactoryException, ReportProcessingException
62     {
63         super.initialize(node, flowController, parent);
64         state = STATE_PROCESS_VARIABLES;
65     }
66 
processContent(final ReportTarget target)67     protected LayoutController processContent(final ReportTarget target)
68             throws DataSourceException, ReportProcessingException, ReportDataFactoryException
69     {
70         if (state == OfficeGroupInstanceSectionLayoutController.STATE_PROCESS_VARIABLES)
71         {
72             // todo: Fill the variables section with something sensible ..
73             final VariablesDeclarationSection variables = new VariablesDeclarationSection();
74             final OfficeGroupInstanceSectionLayoutController controller =
75                     (OfficeGroupInstanceSectionLayoutController) clone();
76             controller.state =
77                     OfficeGroupLayoutController.STATE_PROCESS_NORMAL_FLOW;
78             controller.waitForJoin = true;
79             return processChild(controller, variables, getFlowController());
80         }
81         return super.processContent(target);
82     }
83 
84     // isDisplayable is private in version 0.9.1, so until the upgrade we keep this copy of the method
85     // todo: Delete it unce the sun-cvs contains version 0.9.2.
processChild(final SectionLayoutController derived, final Node node, final FlowController flowController)86     protected LayoutController processChild(final SectionLayoutController derived,
87             final Node node,
88             final FlowController flowController)
89             throws DataSourceException, ReportProcessingException,
90             ReportDataFactoryException
91     {
92         final ReportContext reportContext = flowController.getReportContext();
93         final LayoutControllerFactory layoutControllerFactory = reportContext.getLayoutControllerFactory();
94         if (isDisplayable(node))
95         {
96             derived.setProcessingState(ElementLayoutController.WAITING_FOR_JOIN);
97             return layoutControllerFactory.create(flowController, node, derived);
98         }
99         else
100         {
101             derived.setProcessingState(ElementLayoutController.WAITING_FOR_JOIN);
102             final LayoutController childLc = layoutControllerFactory.create(flowController, node, derived);
103             return LayoutControllerUtil.skipInvisibleElement(childLc);
104         }
105     }
106 
isDisplayable(final Node node)107     protected boolean isDisplayable(final Node node) throws DataSourceException
108     {
109         if (!(node instanceof OfficeGroupSection))
110         {
111             return _isDisplayable(node);
112         }
113 
114         final OfficeGroupSection section = (OfficeGroupSection) node;
115         return !section.isRepeatSection() && _isDisplayable(node);
116     }
117 
_isDisplayable(final Node node)118     protected boolean _isDisplayable(final Node node)
119             throws DataSourceException
120     {
121         // temp method until the pending upgrade to 0.9.2. Later we just call super.isDisplayable(..) instead.
122         if (!node.isEnabled())
123         {
124             return false;
125         }
126 
127         final Expression expression = node.getDisplayCondition();
128         if (expression == null)
129         {
130             return true;
131         }
132 
133         final Object result = LayoutControllerUtil.evaluateExpression(getFlowController(), node, expression);
134         return Boolean.TRUE.equals(result);
135     }
136 
resetSectionForRepeat()137     protected void resetSectionForRepeat()
138     {
139         super.resetSectionForRepeat();
140         state = STATE_PROCESS_VARIABLES;
141     }
142 
143     /**
144      * Joins with a delegated process flow. This is generally called from a child
145      * flow and should *not* (I mean it!) be called from outside. If you do,
146      * you'll suffer.
147      *
148      * @param flowController the flow controller of the parent.
149      * @return the joined layout controller that incorperates all changes from the
150      *         delegate.
151      */
join(final FlowController flowController)152     public LayoutController join(final FlowController flowController)
153     {
154         if (waitForJoin)
155         {
156             final OfficeGroupInstanceSectionLayoutController derived = (OfficeGroupInstanceSectionLayoutController) clone();
157             derived.setProcessingState(ElementLayoutController.OPENED);
158             derived.setFlowController(flowController);
159             derived.waitForJoin = false;
160             return derived;
161         }
162         return super.join(flowController);
163     }
164 
computeAttributes(final FlowController fc, final Element element, final ReportTarget target)165     protected AttributeMap computeAttributes(final FlowController fc, final Element element, final ReportTarget target)
166             throws DataSourceException
167     {
168         final AttributeMap map = new AttributeMap(super.computeAttributes(fc, element, target));
169         map.setAttribute(JFreeReportInfo.REPORT_NAMESPACE, "iteration-count", getIterationCount());
170         map.makeReadOnly();
171         return map;
172     }
173 }
174