KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tigris > scarab > screens > ReportExport


1 package org.tigris.scarab.screens;
2
3 /* ================================================================
4  * Copyright (c) 2003 CollabNet. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * 3. The end-user documentation included with the redistribution, if
18  * any, must include the following acknowlegement: "This product includes
19  * software developed by CollabNet <http://www.collab.net/>."
20  * Alternately, this acknowlegement may appear in the software itself, if
21  * and wherever such third-party acknowlegements normally appear.
22  *
23  * 4. The hosted project names must not be used to endorse or promote
24  * products derived from this software without prior written
25  * permission. For written permission, please contact info@collab.net.
26  *
27  * 5. Products derived from this software may not use the "Tigris" or
28  * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
29  * prior written permission of CollabNet.
30  *
31  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
32  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34  * IN NO EVENT SHALL COLLABNET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
35  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
37  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
39  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
40  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42  *
43  * ====================================================================
44  *
45  * This software consists of voluntary contributions made by many
46  * individuals on behalf of CollabNet.
47  */

48
49 import java.util.Date JavaDoc;
50 import java.util.List JavaDoc;
51 import java.util.Iterator JavaDoc;
52 import java.text.DateFormat JavaDoc;
53 import java.text.SimpleDateFormat JavaDoc;
54
55 import org.apache.commons.lang.StringUtils;
56
57 import org.apache.turbine.RunData;
58 import org.apache.turbine.TemplateContext;
59
60 import org.tigris.scarab.om.ScarabUser;
61 import org.tigris.scarab.reports.ReportBridge;
62 import org.tigris.scarab.reports.ReportDate;
63 import org.tigris.scarab.reports.ReportGroup;
64 import org.tigris.scarab.reports.ReportOptionAttribute;
65 import org.tigris.scarab.reports.ReportHeading;
66 import org.tigris.scarab.reports.ReportTableModel;
67 import org.tigris.scarab.reports.ReportUserAttribute;
68 import org.tigris.scarab.tools.Format;
69 import org.tigris.scarab.tools.ScarabRequestTool;
70 import org.tigris.scarab.tools.ScarabLocalizationTool;
71 import org.tigris.scarab.util.Log;
72
73 /**
74  * Handles export of a report to non-web formats.
75  *
76  * @author <a HREF="mailto:dlr@collab.net">Daniel Rall</a>
77  * @version $Id: ReportExport.java 8704 2003-10-17 10:02:07Z dep4b $
78  * @see org.tigris.scarab.screens.DataExport
79  * @since Scarab 1.0
80  */

81 public class ReportExport extends DataExport
82 {
83     /**
84      * What to show if a header cell is empty.
85      */

86     private static final String JavaDoc NO_HEADER = "=======";
87
88     /**
89      * Writes the response.
90      *
91      * Modelled after the <code>#reportTable()</code> Velocimacro.
92      */

93     public void doBuildTemplate(RunData data, TemplateContext context)
94         throws Exception JavaDoc
95     {
96         super.doBuildTemplate(data, context);
97
98         ScarabLocalizationTool l10n = getLocalizationTool(context);
99         ScarabUser user = (ScarabUser)data.getUser();
100         TSVPrinter printer = new TSVPrinter(getWriter(data));
101
102         ScarabRequestTool scarabR = getScarabRequestTool(context);
103         ReportBridge report = scarabR.getReport();
104
105         // --------------------------------------------------------
106
// This code is not generally excercised as the action will
107
// not redirect to this screen if the following conditions are
108
// present. But is here to protect against url hacking.
109
try
110         {
111             String JavaDoc message = (String JavaDoc)scarabR.getInfoMessage();
112             if (message != null && message.length() > 0)
113             {
114                 printer.print(message);
115             }
116         }
117         catch (Exception JavaDoc e)
118         {
119             printer.print(l10n.get("ErrorOccurredCheckingMessage"));
120         }
121         
122         if (!report.isReadyForCalculation())
123         {
124             printer.print(l10n.get("IncompleteReportDefinition"));
125             return;
126         }
127
128         if (report.getReportDefinition().reportQueryIsExpensive())
129         {
130             printer.print(l10n.format("ReportIsTooExpensive", String.valueOf(
131                 report.getReportDefinition().maximumHeadings())));
132             return;
133         }
134         // --------------------------------------------------------
135

136         ReportTableModel model = report.getModel(user);
137
138         List JavaDoc rowHeadings = model.getRowHeadings();
139         List JavaDoc columnHeadings = model.getColumnHeadings();
140         int multiplier = 1;
141         for (int level = 0; level < columnHeadings.size(); level++)
142         {
143             ReportHeading columnHeadingLevel =
144                 (ReportHeading) columnHeadings.get(level);
145             int maxBlank = rowHeadings.size() - 1;
146             if (maxBlank > 0)
147             {
148                 for (int i = 1; i <= maxBlank; i++)
149                 {
150                     // Empty heading cell.
151
printer.print(NO_HEADER);
152                 }
153             }
154
155             boolean singleAttribute = columnHeadingLevel.singleAttribute();
156             // It would be preferable to concatenate an arrow
157
// pointing to the right to the return value of
158
// displayAttribute(), but I wasn't able to come up with a
159
// good "down arrow" for the X-axis headings.
160
printer.print(singleAttribute
161                           ? model.displayAttribute(columnHeadingLevel.get(0))
162                           : NO_HEADER);
163             int colspan = model.getColspan(level);
164             for (int i = 1; i <= multiplier; i++)
165             {
166                 for (int nextIndex = 1; nextIndex <= columnHeadingLevel.size();
167                      nextIndex++)
168                 {
169                     Object JavaDoc heading = columnHeadingLevel.get(nextIndex - 1);
170                     printer.print(reportLabel(model, heading, singleAttribute,
171                                               l10n));
172                 }
173                 if (colspan > 1)
174                 {
175                     for (int j = 2; j <= colspan; j++)
176                     {
177                         printer.print(NO_HEADER);
178                     }
179                 }
180             }
181
182             // End of first heading row.
183
printer.println();
184             multiplier *= columnHeadingLevel.size();
185         }
186
187         for (Iterator JavaDoc i = rowHeadings.iterator(); i.hasNext(); )
188         {
189             i.next();
190             printer.print(NO_HEADER);
191         }
192         int nbrColumns = model.getColumnCount();
193         for (int i = 0; i < nbrColumns; i++)
194         {
195             printer.print(NO_HEADER);
196         }
197         printer.println();
198
199         int nbrRows = model.getRowCount();
200
201         if (rowHeadings.size() > 1
202             || ((ReportHeading) rowHeadings.get(0)).singleAttribute())
203         {
204             for (int level = 0; level < rowHeadings.size(); level++)
205             {
206                 ReportHeading curHeading =
207                     (ReportHeading) rowHeadings.get(level);
208                 boolean singleAttribute = curHeading.singleAttribute();
209                 // "Down arrow" is prepend to displayAttribute() in
210
// web view.
211
printer.print(singleAttribute
212                               ? model.displayAttribute(curHeading.get(0))
213                               : NO_HEADER);
214                     
215             }
216             for (int i = 0; i < nbrRows; i++)
217             {
218                 printer.print(NO_HEADER);
219             }
220             printer.println();
221         }
222
223         DateFormat JavaDoc dateFormat = new SimpleDateFormat JavaDoc(Format.DATE_TIME_FMT);
224         for (int rowIndex = 0; rowIndex < nbrRows; rowIndex++)
225         {
226             for (int level = 0; level < rowHeadings.size(); level++)
227             {
228                 ReportHeading curHeading =
229                     (ReportHeading) rowHeadings.get(level);
230                 boolean singleAttribute = curHeading.singleAttribute();
231                 int rowspan = model.getRowspan(level);
232                 if ((rowIndex % rowspan) == 0)
233                 {
234                     int index = (rowIndex / rowspan) % curHeading.size();
235                     printer.print(reportLabel(model, curHeading.get(index),
236                                               singleAttribute, l10n));
237                 }
238                 else
239                 {
240                     printer.print(NO_HEADER);
241                 }
242             }
243
244             for (int columnIndex = 0; columnIndex < nbrColumns; columnIndex++)
245             {
246                 Object JavaDoc cell = model.getValueAt(rowIndex, columnIndex);
247                 String JavaDoc cellData = (ReportTableModel.isDate(cell)
248                                    ? dateFormat.format((Date JavaDoc) cell)
249                                    : cell.toString());
250                 if (StringUtils.isEmpty(cellData))
251                 {
252                     cellData = NO_CONTENT;
253                 }
254                 printer.print(cellData);
255             }
256
257             printer.println();
258         }
259     }
260
261     /**
262      * Makes a report label. Modelled after the
263      * <code>#reportLabel()</code> Velocimacro.
264      */

265     private String JavaDoc reportLabel(ReportTableModel model, Object JavaDoc cellLabel,
266                                boolean singleAttribute,
267                                ScarabLocalizationTool l10n)
268     {
269         if (model.isReportDate(cellLabel))
270         {
271             return Format.getDate(Format.DATE_TIME_FMT,
272                                   ((ReportDate) cellLabel).dateValue());
273         }
274         else if (model.isOption(cellLabel))
275         {
276             ReportOptionAttribute roa = (ReportOptionAttribute) cellLabel;
277             return (singleAttribute
278                     ? model.displayOption(roa)
279                     : model.displayAttribute(roa) + ": "
280                       + model.displayOption(roa));
281         }
282         else if (model.isOptionGroup(cellLabel))
283         {
284             return ((ReportGroup) cellLabel).getName();
285         }
286         else if (model.isAttributeAndUser(cellLabel))
287         {
288             ReportUserAttribute rua = (ReportUserAttribute) cellLabel;
289             return (singleAttribute
290                     ? model.displayUser(rua)
291                     : model.displayAttribute(rua) + ": "
292                       + model.displayUser(rua));
293         }
294         else if (model.isUser(cellLabel))
295         {
296             return l10n.get("Author") + ": "
297                 + ((ScarabUser) cellLabel).getUserName();
298         }
299         else
300         {
301             Log.get().debug("Unhandled cell label type: "
302                             + cellLabel.getClass().getName());
303             return cellLabel.toString();
304         }
305     }
306 }
307
Popular Tags