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 
22 
23 package com.sun.star.report.pentaho.layoutprocessor;
24 
25 import com.sun.star.report.pentaho.model.VariablesDeclarationSection;
26 
27 import org.jfree.report.DataSourceException;
28 import org.jfree.report.ReportData;
29 import org.jfree.report.ReportDataFactoryException;
30 import org.jfree.report.ReportProcessingException;
31 import org.jfree.report.data.GlobalMasterRow;
32 import org.jfree.report.data.ReportDataRow;
33 import org.jfree.report.flow.FlowController;
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.SectionLayoutController;
38 
39 /**
40  * Creation-Date: 11.04.2007, 11:04:02
41  *
42  * @author Thomas Morgner
43  */
44 public class OfficeDetailLayoutController extends SectionLayoutController
45 {
46 
47     public static final int STATE_PROCESS_VARIABLES = 2;
48     public static final int STATE_PROCESS_NORMAL_FLOW = 3;
49     private boolean waitForJoin;
50     private int state;
51 
OfficeDetailLayoutController()52     public OfficeDetailLayoutController()
53     {
54     }
55 
56     /**
57      * Initializes the layout controller. This method is called exactly once. It
58      * is the creators responsibility to call this method.
59      * <p/>
60      * Calling initialize after the first advance must result in a
61      * IllegalStateException.
62      *
63      * @param node           the currently processed object or layout node.
64      * @param flowController the current flow controller.
65      * @param parent         the parent layout controller that was responsible for
66      *                       instantiating this controller.
67      * @throws org.jfree.report.DataSourceException
68      *          if there was a problem reading data from the datasource.
69      * @throws org.jfree.report.ReportProcessingException
70      *          if there was a general problem during the report processing.
71      * @throws org.jfree.report.ReportDataFactoryException
72      *          if a query failed.
73      */
initialize(final Object node, final FlowController flowController, final LayoutController parent)74     public void initialize(final Object node,
75             final FlowController flowController,
76             final LayoutController parent)
77             throws DataSourceException, ReportDataFactoryException,
78             ReportProcessingException
79     {
80         super.initialize(node, flowController, parent);
81         state = OfficeDetailLayoutController.STATE_PROCESS_VARIABLES;
82     }
83 
84     /**
85      * This method is called for each newly instantiated layout controller. The returned layout controller instance should
86      * have a processing state of either 'OPEN' or 'FINISHING' depending on whether there is any content or any child
87      * nodes to process.
88      *
89      * @param target the report target that receives generated events.
90      * @return the new layout controller instance representing the new state.
91      * @throws org.jfree.report.DataSourceException
92      *          if there was a problem reading data from the datasource.
93      * @throws org.jfree.report.ReportProcessingException
94      *          if there was a general problem during the report processing.
95      * @throws org.jfree.report.ReportDataFactoryException
96      *          if a query failed.
97      */
startElement(final ReportTarget target)98     protected LayoutController startElement(final ReportTarget target)
99             throws DataSourceException, ReportProcessingException, ReportDataFactoryException
100     {
101         final FlowController fc = getFlowController();
102         final GlobalMasterRow masterRow = fc.getMasterRow();
103         final ReportDataRow reportDataRow = masterRow.getReportDataRow();
104         final ReportData reportData = reportDataRow.getReportData();
105         if (!reportData.isReadable())
106         {
107             reportData.isReadable();
108             // If this report has no data, then do not print the detail section. The detail section
109             // is the only section that behaves this way, and for now this is only done in the OO-implementation
110             final SectionLayoutController derived = (SectionLayoutController) clone();
111             derived.setProcessingState(ElementLayoutController.FINISHED);
112             derived.setFlowController(fc);
113             return derived;
114         }
115 
116         if (state == OfficeDetailLayoutController.STATE_PROCESS_VARIABLES)
117         {
118             final VariablesDeclarationSection variables = new VariablesDeclarationSection();
119             final OfficeDetailLayoutController controller = (OfficeDetailLayoutController) clone();
120             controller.state = OfficeDetailLayoutController.STATE_PROCESS_NORMAL_FLOW;
121             controller.waitForJoin = true;
122             return processChild(controller, variables, fc);
123         }
124 
125         return super.startElement(target);
126     }
127 
resetSectionForRepeat()128     protected void resetSectionForRepeat()
129     {
130         super.resetSectionForRepeat();
131         state = STATE_PROCESS_VARIABLES;
132     }
133 
134     /**
135      * Joins with a delegated process flow. This is generally called from a child
136      * flow and should *not* (I mean it!) be called from outside. If you do,
137      * you'll suffer.
138      *
139      * @param flowController the flow controller of the parent.
140      * @return the joined layout controller that incorperates all changes from the
141      *         delegate.
142      */
join(final FlowController flowController)143     public LayoutController join(final FlowController flowController)
144     {
145         if (waitForJoin)
146         {
147             final OfficeDetailLayoutController derived = (OfficeDetailLayoutController) clone();
148             derived.setProcessingState(ElementLayoutController.NOT_STARTED);
149             derived.setFlowController(flowController);
150             derived.waitForJoin = false;
151             return derived;
152         }
153         return super.join(flowController);
154     }
155 }
156