1*1a37d047SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*1a37d047SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*1a37d047SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*1a37d047SAndrew Rist  * distributed with this work for additional information
6*1a37d047SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*1a37d047SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*1a37d047SAndrew Rist  * "License"); you may not use this file except in compliance
9*1a37d047SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*1a37d047SAndrew Rist  *
11*1a37d047SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*1a37d047SAndrew Rist  *
13*1a37d047SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*1a37d047SAndrew Rist  * software distributed under the License is distributed on an
15*1a37d047SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*1a37d047SAndrew Rist  * KIND, either express or implied.  See the License for the
17*1a37d047SAndrew Rist  * specific language governing permissions and limitations
18*1a37d047SAndrew Rist  * under the License.
19*1a37d047SAndrew Rist  *
20*1a37d047SAndrew Rist  *************************************************************/
21*1a37d047SAndrew Rist 
22*1a37d047SAndrew Rist 
23cdf0e10cSrcweir package com.sun.star.report.pentaho.layoutprocessor;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir import com.sun.star.report.pentaho.model.VariablesDeclarationSection;
26cdf0e10cSrcweir 
27cdf0e10cSrcweir import org.jfree.report.DataSourceException;
28cdf0e10cSrcweir import org.jfree.report.ReportData;
29cdf0e10cSrcweir import org.jfree.report.ReportDataFactoryException;
30cdf0e10cSrcweir import org.jfree.report.ReportProcessingException;
31cdf0e10cSrcweir import org.jfree.report.data.GlobalMasterRow;
32cdf0e10cSrcweir import org.jfree.report.data.ReportDataRow;
33cdf0e10cSrcweir import org.jfree.report.flow.FlowController;
34cdf0e10cSrcweir import org.jfree.report.flow.ReportTarget;
35cdf0e10cSrcweir import org.jfree.report.flow.layoutprocessor.ElementLayoutController;
36cdf0e10cSrcweir import org.jfree.report.flow.layoutprocessor.LayoutController;
37cdf0e10cSrcweir import org.jfree.report.flow.layoutprocessor.SectionLayoutController;
38cdf0e10cSrcweir 
39cdf0e10cSrcweir /**
40cdf0e10cSrcweir  * Creation-Date: 11.04.2007, 11:04:02
41cdf0e10cSrcweir  *
42cdf0e10cSrcweir  * @author Thomas Morgner
43cdf0e10cSrcweir  */
44cdf0e10cSrcweir public class OfficeDetailLayoutController extends SectionLayoutController
45cdf0e10cSrcweir {
46cdf0e10cSrcweir 
47cdf0e10cSrcweir     public static final int STATE_PROCESS_VARIABLES = 2;
48cdf0e10cSrcweir     public static final int STATE_PROCESS_NORMAL_FLOW = 3;
49cdf0e10cSrcweir     private boolean waitForJoin;
50cdf0e10cSrcweir     private int state;
51cdf0e10cSrcweir 
OfficeDetailLayoutController()52cdf0e10cSrcweir     public OfficeDetailLayoutController()
53cdf0e10cSrcweir     {
54cdf0e10cSrcweir     }
55cdf0e10cSrcweir 
56cdf0e10cSrcweir     /**
57cdf0e10cSrcweir      * Initializes the layout controller. This method is called exactly once. It
58cdf0e10cSrcweir      * is the creators responsibility to call this method.
59cdf0e10cSrcweir      * <p/>
60cdf0e10cSrcweir      * Calling initialize after the first advance must result in a
61cdf0e10cSrcweir      * IllegalStateException.
62cdf0e10cSrcweir      *
63cdf0e10cSrcweir      * @param node           the currently processed object or layout node.
64cdf0e10cSrcweir      * @param flowController the current flow controller.
65cdf0e10cSrcweir      * @param parent         the parent layout controller that was responsible for
66cdf0e10cSrcweir      *                       instantiating this controller.
67cdf0e10cSrcweir      * @throws org.jfree.report.DataSourceException
68cdf0e10cSrcweir      *          if there was a problem reading data from the datasource.
69cdf0e10cSrcweir      * @throws org.jfree.report.ReportProcessingException
70cdf0e10cSrcweir      *          if there was a general problem during the report processing.
71cdf0e10cSrcweir      * @throws org.jfree.report.ReportDataFactoryException
72cdf0e10cSrcweir      *          if a query failed.
73cdf0e10cSrcweir      */
initialize(final Object node, final FlowController flowController, final LayoutController parent)74cdf0e10cSrcweir     public void initialize(final Object node,
75cdf0e10cSrcweir             final FlowController flowController,
76cdf0e10cSrcweir             final LayoutController parent)
77cdf0e10cSrcweir             throws DataSourceException, ReportDataFactoryException,
78cdf0e10cSrcweir             ReportProcessingException
79cdf0e10cSrcweir     {
80cdf0e10cSrcweir         super.initialize(node, flowController, parent);
81cdf0e10cSrcweir         state = OfficeDetailLayoutController.STATE_PROCESS_VARIABLES;
82cdf0e10cSrcweir     }
83cdf0e10cSrcweir 
84cdf0e10cSrcweir     /**
85cdf0e10cSrcweir      * This method is called for each newly instantiated layout controller. The returned layout controller instance should
86cdf0e10cSrcweir      * have a processing state of either 'OPEN' or 'FINISHING' depending on whether there is any content or any child
87cdf0e10cSrcweir      * nodes to process.
88cdf0e10cSrcweir      *
89cdf0e10cSrcweir      * @param target the report target that receives generated events.
90cdf0e10cSrcweir      * @return the new layout controller instance representing the new state.
91cdf0e10cSrcweir      * @throws org.jfree.report.DataSourceException
92cdf0e10cSrcweir      *          if there was a problem reading data from the datasource.
93cdf0e10cSrcweir      * @throws org.jfree.report.ReportProcessingException
94cdf0e10cSrcweir      *          if there was a general problem during the report processing.
95cdf0e10cSrcweir      * @throws org.jfree.report.ReportDataFactoryException
96cdf0e10cSrcweir      *          if a query failed.
97cdf0e10cSrcweir      */
startElement(final ReportTarget target)98cdf0e10cSrcweir     protected LayoutController startElement(final ReportTarget target)
99cdf0e10cSrcweir             throws DataSourceException, ReportProcessingException, ReportDataFactoryException
100cdf0e10cSrcweir     {
101cdf0e10cSrcweir         final FlowController fc = getFlowController();
102cdf0e10cSrcweir         final GlobalMasterRow masterRow = fc.getMasterRow();
103cdf0e10cSrcweir         final ReportDataRow reportDataRow = masterRow.getReportDataRow();
104cdf0e10cSrcweir         final ReportData reportData = reportDataRow.getReportData();
105cdf0e10cSrcweir         if (!reportData.isReadable())
106cdf0e10cSrcweir         {
107cdf0e10cSrcweir             reportData.isReadable();
108cdf0e10cSrcweir             // If this report has no data, then do not print the detail section. The detail section
109cdf0e10cSrcweir             // is the only section that behaves this way, and for now this is only done in the OO-implementation
110cdf0e10cSrcweir             final SectionLayoutController derived = (SectionLayoutController) clone();
111cdf0e10cSrcweir             derived.setProcessingState(ElementLayoutController.FINISHED);
112cdf0e10cSrcweir             derived.setFlowController(fc);
113cdf0e10cSrcweir             return derived;
114cdf0e10cSrcweir         }
115cdf0e10cSrcweir 
116cdf0e10cSrcweir         if (state == OfficeDetailLayoutController.STATE_PROCESS_VARIABLES)
117cdf0e10cSrcweir         {
118cdf0e10cSrcweir             final VariablesDeclarationSection variables = new VariablesDeclarationSection();
119cdf0e10cSrcweir             final OfficeDetailLayoutController controller = (OfficeDetailLayoutController) clone();
120cdf0e10cSrcweir             controller.state = OfficeDetailLayoutController.STATE_PROCESS_NORMAL_FLOW;
121cdf0e10cSrcweir             controller.waitForJoin = true;
122cdf0e10cSrcweir             return processChild(controller, variables, fc);
123cdf0e10cSrcweir         }
124cdf0e10cSrcweir 
125cdf0e10cSrcweir         return super.startElement(target);
126cdf0e10cSrcweir     }
127cdf0e10cSrcweir 
resetSectionForRepeat()128cdf0e10cSrcweir     protected void resetSectionForRepeat()
129cdf0e10cSrcweir     {
130cdf0e10cSrcweir         super.resetSectionForRepeat();
131cdf0e10cSrcweir         state = STATE_PROCESS_VARIABLES;
132cdf0e10cSrcweir     }
133cdf0e10cSrcweir 
134cdf0e10cSrcweir     /**
135cdf0e10cSrcweir      * Joins with a delegated process flow. This is generally called from a child
136cdf0e10cSrcweir      * flow and should *not* (I mean it!) be called from outside. If you do,
137cdf0e10cSrcweir      * you'll suffer.
138cdf0e10cSrcweir      *
139cdf0e10cSrcweir      * @param flowController the flow controller of the parent.
140cdf0e10cSrcweir      * @return the joined layout controller that incorperates all changes from the
141cdf0e10cSrcweir      *         delegate.
142cdf0e10cSrcweir      */
join(final FlowController flowController)143cdf0e10cSrcweir     public LayoutController join(final FlowController flowController)
144cdf0e10cSrcweir     {
145cdf0e10cSrcweir         if (waitForJoin)
146cdf0e10cSrcweir         {
147cdf0e10cSrcweir             final OfficeDetailLayoutController derived = (OfficeDetailLayoutController) clone();
148cdf0e10cSrcweir             derived.setProcessingState(ElementLayoutController.NOT_STARTED);
149cdf0e10cSrcweir             derived.setFlowController(flowController);
150cdf0e10cSrcweir             derived.waitForJoin = false;
151cdf0e10cSrcweir             return derived;
152cdf0e10cSrcweir         }
153cdf0e10cSrcweir         return super.join(flowController);
154cdf0e10cSrcweir     }
155cdf0e10cSrcweir }
156