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.OfficeNamespaces;
26 import com.sun.star.report.pentaho.model.OfficeGroup;
27 import com.sun.star.report.pentaho.model.OfficeReport;
28 
29 import java.util.ArrayList;
30 import java.util.List;
31 
32 import org.jfree.report.DataSourceException;
33 import org.jfree.report.JFreeReportInfo;
34 import org.jfree.report.ReportDataFactoryException;
35 import org.jfree.report.ReportProcessingException;
36 import org.jfree.report.flow.FlowController;
37 import org.jfree.report.flow.layoutprocessor.LayoutController;
38 import org.jfree.report.flow.layoutprocessor.SectionLayoutController;
39 import org.jfree.report.structure.Element;
40 import org.jfree.report.structure.Node;
41 import org.jfree.report.structure.Section;
42 
43 
44 /**
45  * Creation-Date: 24.04.2007, 16:06:52
46  *
47  * @author Thomas Morgner
48  */
49 public class OfficeTableTemplateLayoutController extends SectionLayoutController
50 {
51 
52     private Node[] nodes;
53 
OfficeTableTemplateLayoutController()54     public OfficeTableTemplateLayoutController()
55     {
56     }
57 
58     /**
59      * Initializes the layout controller. This method is called exactly once. It is the creators responsibility to call
60      * this method.
61      * <p/>
62      * Calling initialize after the first advance must result in a IllegalStateException.
63      *
64      * @param node           the currently processed object or layout node.
65      * @param flowController the current flow controller.
66      * @param parent         the parent layout controller that was responsible for 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, final FlowController flowController, final LayoutController parent)
75             throws DataSourceException, ReportDataFactoryException, ReportProcessingException
76     {
77         final Section section = new Section();
78         section.setNamespace(JFreeReportInfo.REPORT_NAMESPACE);
79         section.setType("template");
80         super.initialize(section, flowController, parent);
81 
82         final OfficeReport report = (OfficeReport) node;
83         final ArrayList tables = new ArrayList();
84         if (report.getPageHeader() != null)
85         {
86             addFromSection(tables, (Section) report.getPageHeader());
87         }
88         if (report.getReportHeader() != null)
89         {
90             addFromSection(tables, (Section) report.getReportHeader());
91         }
92         addPBody(tables, (Section) report.getPreBodySection());
93         addFromBody(tables, (Section) report.getBodySection());
94         addPBody(tables, (Section) report.getPostBodySection());
95         if (report.getReportFooter() != null)
96         {
97             addFromSection(tables, (Section) report.getReportFooter());
98         }
99         if (report.getPageFooter() != null)
100         {
101             addFromSection(tables, (Section) report.getPageFooter());
102         }
103 
104         this.nodes = (Node[]) tables.toArray(new Node[tables.size()]);
105     }
106 
addPBody(final List tables, final Section section)107     private void addPBody(final List tables, final Section section)
108     {
109         if (section != null)
110         {
111             // tables.add(section);
112             final Node[] nodeArray = section.getNodeArray();
113             for (int i = 0; i < nodeArray.length; i++)
114             {
115                 final Node node = nodeArray[i];
116                 tables.add(node);
117             }
118 
119         }
120     }
121 
addFromBody(final List tables, final Section section)122     private void addFromBody(final List tables, final Section section)
123     {
124         final Node[] nodeArray = section.getNodeArray();
125         for (int i = 0; i < nodeArray.length; i++)
126         {
127             final Node node = nodeArray[i];
128             if (node instanceof Section)
129             {
130                 final Section child = (Section) node;
131                 if (node instanceof OfficeGroup)
132                 {
133                     addFromGroup(tables, child);
134                 }
135                 else
136                 {
137                     addFromSection(tables, child);
138                 }
139             }
140         }
141     }
142 
addFromGroup(final List tables, final Section section)143     private void addFromGroup(final List tables, final Section section)
144     {
145         final Node[] nodeArray = section.getNodeArray();
146         for (int i = 0; i < nodeArray.length; i++)
147         {
148             final Node node = nodeArray[i];
149             if (node instanceof Section)
150             {
151                 final Section element = (Section) node;
152                 if (JFreeReportInfo.REPORT_NAMESPACE.equals(element.getNamespace()) && "group-body".equals(element.getType()))
153                 {
154                     addFromBody(tables, element);
155                 }
156                 else
157                 {
158                     addFromSection(tables, element);
159                 }
160             }
161         }
162     }
163 
addFromSection(final List tables, final Section section)164     private void addFromSection(final List tables, final Section section)
165     {
166         final Node[] nodeArray = section.getNodeArray();
167         for (int i = 0; i < nodeArray.length; i++)
168         {
169             final Node node = nodeArray[i];
170             if (node instanceof Element)
171             {
172                 final Element element = (Element) node;
173                 if (OfficeNamespaces.TABLE_NS.equals(element.getNamespace()) && "table".equals(element.getType()))
174                 {
175                     tables.add(element);
176                 }
177             }
178         }
179     }
180 
getNodes()181     public Node[] getNodes()
182     {
183         return nodes;
184     }
185 }
186