KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > services > html > ReportPage


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. 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
8  * are 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
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.services.html;
66
67 import com.jcorporate.expresso.core.db.DBException;
68 import com.jcorporate.expresso.core.misc.StringUtil;
69 import com.jcorporate.expresso.ext.report.ExpressoReport;
70 import com.jcorporate.expresso.ext.report.ReportException;
71
72 import javax.servlet.http.HttpServletRequest JavaDoc;
73 import javax.servlet.http.HttpServletResponse JavaDoc;
74 import java.io.BufferedOutputStream JavaDoc;
75 import java.io.FileOutputStream JavaDoc;
76 import java.io.IOException JavaDoc;
77 import java.io.OutputStream JavaDoc;
78 import java.io.PrintWriter JavaDoc;
79 import java.util.ArrayList JavaDoc;
80 import java.util.Date JavaDoc;
81 import java.util.HashMap JavaDoc;
82 import java.util.Hashtable JavaDoc;
83 import java.util.Iterator JavaDoc;
84 import java.util.Map JavaDoc;
85 import java.util.StringTokenizer JavaDoc;
86 import java.util.Vector JavaDoc;
87
88
89 /**
90  * A specialized HTML Element that implements a report. Reports have headers,
91  * possibly embedded reports, possibly page breaks, and various other
92  * embedded elements.
93  * <p/>
94  * <p>Reports also differ from other HTML components in that they can be written
95  * directly to files if required.</p>
96  *
97  * @author Michael Nash
98  * @version $Revision: 1.14 $ $Date: 2004/11/17 20:48:18 $
99  */

100 public class ReportPage
101         extends HtmlElement implements ExpressoReport {
102
103     private String JavaDoc thisClass = this.getClass().getName() + ".";
104     private String JavaDoc reportTitle = null;
105     private Page myPage = null;
106     private Table currentTable = null;
107     private String JavaDoc fileName = null;
108     private HashMap JavaDoc myParams = null;
109     private Vector JavaDoc defaultParams = new Vector JavaDoc(1);
110     private String JavaDoc dbName = "";
111     private String JavaDoc reportCode = "";
112
113     /**
114      * Constructor
115      *
116      * @throws HtmlException If superclass constructor fails
117      */

118     public ReportPage()
119             throws HtmlException {
120         myPage = new Page("Report Page");
121     } /* ReportPage() */
122
123
124     /**
125      * The report code
126      *
127      * @param newCode java.lang.String
128      */

129     public void setReportCode(String JavaDoc newCode) {
130         reportCode = newCode;
131     }
132
133     /**
134      * Dummy implementation. Override if your report has default values
135      * to implement.
136      *
137      * @throws DBException upon error
138      */

139     public void populateDefaultValues() throws DBException {
140     }
141
142     /**
143      * Retrieve the report code
144      *
145      * @return java.lang.String
146      */

147     public String JavaDoc getReportCode() {
148         return reportCode;
149     }
150
151     /**
152      * Constructor
153      * Create a report page for output to a file
154      *
155      * @param newFileName Filename for output
156      * @param newTitle Title of the report
157      * @throws HtmlException If the report page cannot be created
158      */

159     public ReportPage(String JavaDoc newFileName, String JavaDoc newTitle)
160             throws HtmlException {
161         this();
162         setFileName(newFileName);
163         setTitle(newTitle);
164     } /* ReportPage(String, String) */
165
166     /**
167      * Add a new line to the report
168      *
169      * @param newLine Line to add to the report output
170      * @throws HtmlException If the line cannot be added
171      */

172     public synchronized void addLine(String JavaDoc newLine)
173             throws HtmlException {
174         sendLine(newLine);
175     } /* addLine(String) */
176
177
178     /**
179      * @param newCode the new parameter code
180      * @param newDescrip the new description
181      * @param newType the java type
182      * @param newDefaultValue the default value
183      * @throws HtmlException upon error
184      */

185     protected void addParam(String JavaDoc newCode, String JavaDoc newDescrip, String JavaDoc newType,
186                             String JavaDoc newDefaultValue)
187             throws HtmlException {
188         ReportPageParam newParam = new ReportPageParam(newCode, newDescrip,
189                 newType,
190                 newDefaultValue);
191         defaultParams.addElement(newParam);
192     } /* addParam(String, String, STring, String) */
193
194
195     /**
196      * Add a new row to the report's current table
197      *
198      * @param rowString Pipe-seperated values, one for each column for the new row
199      * in the table
200      * @throws HtmlException A new row could not be added
201      */

202     public synchronized void addRow(String JavaDoc rowString)
203             throws HtmlException {
204         Row myRow = new Row();
205         currentTable.add(myRow);
206
207         StringTokenizer JavaDoc stk = new StringTokenizer JavaDoc(rowString, "|");
208
209         while (stk.hasMoreTokens()) {
210             myRow.add(new Cell(new Text(stk.nextToken())));
211         }
212     } /* addRow(String) */
213
214
215     /**
216      * Add a date/time stamp to the report output
217      *
218      * @throws HtmlException If the date/time stampt cannot be added
219      */

220     public synchronized void addTimeStamp()
221             throws HtmlException {
222         Date JavaDoc Now = new Date JavaDoc(System.currentTimeMillis());
223         myPage.add(new Paragraph(new Text(Now.toString())));
224     } /* addTimeStamp() */
225
226
227     /**
228      * Close the report, writing the final HTML.
229      * Also closes the output if we're in cgi mode
230      *
231      * @throws HtmlException If a problem occurrs saving the report
232      */

233     public synchronized void close()
234             throws HtmlException {
235         save();
236     } /* close() */
237
238
239     public synchronized void display(HttpServletRequest JavaDoc req,
240                                      HttpServletResponse JavaDoc res, String JavaDoc charset)
241             throws HtmlException {
242         if (StringUtil.notNull(charset).equals("")) {
243             charset = "ISO-8859-1";
244         }
245
246         res.setContentType("text/html; charset=" + charset);
247
248         try {
249             PrintWriter JavaDoc out = res.getWriter();
250             display(out);
251         } catch (IOException JavaDoc ie) {
252             throw new HtmlException(ie);
253         }
254     } /* display(HttpServletRequest, HttpServletResponse, String) */
255
256
257     /**
258      * Display to the screen (browser) in the normal way
259      *
260      * @param out PrintWriter to send to client
261      * @throws HtmlException If the report could not be written
262      */

263     public void display(PrintWriter JavaDoc out)
264             throws HtmlException {
265         myPage.display(out);
266     } /* display(PrintWriter) */
267
268
269     /**
270      * depth is ignored
271      *
272      * @param depth the number of tabs to indent
273      * @param out the output stream
274      * @throws HtmlException upon error
275      */

276     protected void display(PrintWriter JavaDoc out, int depth)
277             throws HtmlException {
278         display(out);
279     }
280
281     /**
282      * End the current table in the output stream of this report
283      *
284      * @throws HtmlException If the table could not be ended correctly
285      */

286     public synchronized void endTable()
287             throws HtmlException {
288         myPage.add(currentTable);
289         currentTable = null;
290     } /* endTable() */
291
292
293     /**
294      * Get the current table being used by the report
295      *
296      * @return Table HTML Table object being currently used
297      * @throws HtmlException If the current table cannot be accessed
298      */

299     public Table getCurrentTable()
300             throws HtmlException {
301         return currentTable;
302     } /* getCurrentTable() */
303
304
305     /**
306      * Retrieve the db name
307      *
308      * @return java.lang.String
309      */

310     public String JavaDoc getDBName() {
311         return dbName;
312     } /* getDBName() */
313
314
315     /**
316      * Return a Vector of default parameter objects
317      *
318      * @return Vector of default parameters
319      */

320     public Vector JavaDoc getDefaultParams() {
321         return (Vector JavaDoc) defaultParams.clone();
322     } /* getDefaultParams() */
323
324
325     /**
326      * Retrieve the default parameter value for the given parameter name
327      *
328      * @param parameterName the name of the parameter. Must be listed
329      * in the list of parameter names
330      * @return java.lang.String. The default parameter value. May be null.
331      * @throws IllegalArgumentException if the parameter name does not exist
332      * in the report's parameter list.
333      */

334     public synchronized String JavaDoc getDefaultValue(String JavaDoc parameterName) {
335         StringUtil.assertNotBlank(parameterName,
336                 "getDefaultValue(String) parameter: parameterName cannot be blank");
337         for (Iterator JavaDoc i = defaultParams.iterator(); i.hasNext();) {
338             ReportPageParam oneParam = (ReportPageParam) i.next();
339             if (parameterName.equals(oneParam.getCode())) {
340                 return oneParam.getDefaultValue();
341             }
342         }
343
344         throw new IllegalArgumentException JavaDoc(parameterName + " is not a parameter in the report");
345     }
346
347
348     /**
349      * Return a reference to the current page
350      *
351      * @return the page
352      */

353     protected Page getPage() {
354         return myPage;
355     } /* getPage() */
356
357     /**
358      * Return the value of the named parameter
359      *
360      * @param paramCode of the parameter
361      * @return Value of the given parameter
362      * @throws HtmlException if there is no such parameter
363      */

364     public String JavaDoc getParam(String JavaDoc paramCode)
365             throws HtmlException {
366         String JavaDoc myName = (thisClass + "getParam(String)");
367
368         if (myParams == null) {
369             throw new HtmlException(myName + ":No parameters defined for " +
370                     " this report. Can't get parameter '" +
371                     paramCode + "'");
372         }
373
374         String JavaDoc retVal = (String JavaDoc) myParams.get(paramCode);
375
376         if (retVal == null) {
377             throw new HtmlException(myName + ":No such parameter as '" +
378                     paramCode + "'");
379         }
380
381         return retVal;
382     } /* getParam(String) */
383
384
385     /**
386      * Return the parameters for this report
387      *
388      * @return Hashtable of Parameters of this report
389      * @throws HtmlException If an error occurs retrieving the parameters
390      */

391     public synchronized Hashtable JavaDoc getParams()
392             throws HtmlException {
393         if (myParams == null) {
394             return new Hashtable JavaDoc(1);
395         } else {
396             return new Hashtable JavaDoc(myParams);
397         }
398     } /* getParams() */
399
400
401     /**
402      * Return the title of this report
403      *
404      * @return Report title of this report
405      */

406     public String JavaDoc getTitle() {
407         return reportTitle;
408     } /* getTitle() */
409
410     /**
411      * Save output to a file
412      *
413      * @throws HtmlException If the output could not be written
414      */

415     public void save()
416             throws HtmlException {
417         String JavaDoc myName = (thisClass + "save()");
418
419         try {
420             FileOutputStream JavaDoc fout = new FileOutputStream JavaDoc(fileName);
421             BufferedOutputStream JavaDoc bout = new BufferedOutputStream JavaDoc(fout);
422             PrintWriter JavaDoc out = new PrintWriter JavaDoc(bout);
423             myPage.display(out);
424         } catch (IOException JavaDoc ie) {
425             throw new HtmlException(myName + ":I/O Error writing " + fileName +
426                     ":" + ie.getMessage());
427         }
428     } /* save() */
429
430
431     /**
432      * Send the given line to either the server (if we are running in CORBA-based mode)
433      * or straight to output (cgi mode)
434      *
435      * @param theLine The line to output
436      * @throws HtmlException If the line could not be output
437      */

438     private void sendLine(String JavaDoc theLine)
439             throws HtmlException {
440         myPage.add(new Text(theLine));
441     } /* sendLine(String) */
442
443
444     /**
445      * @param newDBName new data context
446      */

447     public synchronized void setDBName(String JavaDoc newDBName) {
448         dbName = StringUtil.notNull(newDBName);
449     } /* setDBName(String) */
450
451     /**
452      * Set the file name for output
453      *
454      * @param newFileName New output file name
455      * @throws HtmlException If the parameter is invalid
456      */

457     public synchronized void setFileName(String JavaDoc newFileName)
458             throws HtmlException {
459         fileName = newFileName;
460     } /* setFileName(String) */
461
462
463     /**
464      * Set the filename and the title of this report
465      *
466      * @param newFileName Name of the output file to write
467      * @param newTitle Title of the report
468      * @throws HtmlException If the parameters are not valid
469      */

470     public synchronized void setFileName(String JavaDoc newFileName, String JavaDoc newTitle)
471             throws HtmlException {
472         setTitle(newTitle);
473         setFileName(newFileName);
474     } /* setFileName(String, String) */
475
476
477     /**
478      * Set the parameters for this report to the given hashtable
479      *
480      * @param newParams New parameters hashtable
481      * @throws HtmlException If these parameters cannot be set
482      */

483     public synchronized void setParams(Hashtable JavaDoc newParams)
484             throws HtmlException {
485         if (newParams != null) {
486             myParams = new HashMap JavaDoc(newParams);
487         }
488     } /* setParams(Hashtable) */
489
490
491     /**
492      * Set the title of this report
493      *
494      * @param newTitle New title of report
495      */

496     public synchronized void setTitle(String JavaDoc newTitle) {
497         reportTitle = newTitle;
498     } /* setTitle(String) */
499
500     /**
501      * Begin a new table in the report output stream
502      *
503      * @param caption Caption for the table
504      * @param colHeaders A pipe-seperated list of column headers
505      * @throws HtmlException A new table could not be started
506      */

507     public synchronized void startTable(String JavaDoc caption, String JavaDoc colHeaders)
508             throws HtmlException {
509         currentTable = new Table();
510         currentTable.setCaption(caption);
511         currentTable.setBorder(1);
512         currentTable.addHeading(colHeaders);
513     } /* startTable(String, String) */
514
515
516     /**
517      * Print the Report Page. Override this in your own derived class to provide
518      * custom printing. Otherwise, this just calls the display() function to
519      * maintain backwards compatibility
520      *
521      * @param os The OutputStream to send to.
522      * @throws ReportException upon error.
523      * @throws IOException upon IO error
524      */

525     public void printReport(OutputStream JavaDoc os) throws ReportException, java.io.IOException JavaDoc {
526         try {
527             java.io.PrintWriter JavaDoc printWriter = new java.io.PrintWriter JavaDoc(os);
528             this.display(printWriter);
529             printWriter.flush();
530         } catch (HtmlException ex) {
531             throw new ReportException("Error producing report", ex);
532         }
533     }
534
535     /**
536      * Sets the report parameters
537      *
538      * @param parameters Map of parameters
539      */

540     public void setReportParameters(Map JavaDoc parameters) {
541         if (myParams == null) {
542             myParams = new HashMap JavaDoc(parameters.size());
543         }
544         //Put in default parameters, and set in the new ones.
545
if (defaultParams != null) {
546             for (Iterator JavaDoc i = this.defaultParams.iterator(); i.hasNext();) {
547                 ReportPageParam param = (ReportPageParam) i.next();
548                 myParams.put(param.getCode(), param.getDefaultValue());
549             }
550         }
551
552
553         for (Iterator JavaDoc i = parameters.keySet().iterator(); i.hasNext();) {
554             String JavaDoc key = (String JavaDoc) i.next();
555             String JavaDoc value = (String JavaDoc) parameters.get(key);
556             if (value != null && value.length() > 0) {
557                 myParams.put(key, value);
558             }
559         }
560     }
561
562     /**
563      * Protected &quot;getter&quot; function so derived classes can get the
564      * report parameters
565      *
566      * @return java.util.Map
567      */

568     protected Map JavaDoc getReportParameters() {
569         return myParams;
570     }
571
572     /**
573      * Sets the data context for this report.
574      *
575      * @param newDataContext the new data context
576      */

577     public void setDataContext(String JavaDoc newDataContext) {
578         dbName = newDataContext;
579     }
580
581     /**
582      * Retrieve the data context for this report.
583      *
584      * @return java.lang.String
585      */

586     protected String JavaDoc getDataContext() {
587         return dbName;
588     }
589
590     /**
591      * Retrieve the report mime type. In this case, HTML text.
592      *
593      * @return java.lang.String: &quot;text/html&quot;
594      */

595     public String JavaDoc getReportMimeType() {
596         return "text/html";
597     }
598
599     /**
600      * Retrieve the recommended file extension for the report. Return the
601      * value without the '.'. So an example file extension for an Excel
602      * report would be &quot;xls&quot;, and XML formatted report would be
603      * &quot;xml&quot; [although if you're using special XML languages, of course
604      * you have the freedom to specify the extension]
605      *
606      * @return java.lang.String in the format specified above.
607      */

608     public String JavaDoc getReportFileExtension() {
609         return "html";
610     }
611
612     /**
613      * Retrieve a list of parameters that this report supports. This function
614      * may be blank, in which case, there still could be parameters for the report
615      * but the automatic ui functions such as <code>ReportServer</code> won't
616      * present them as options.
617      *
618      * @return java.util.List of Strings. or null if there are no parameters
619      */

620     public java.util.List JavaDoc getParameterNames() {
621         if (defaultParams != null && defaultParams.size() > 0) {
622             ArrayList JavaDoc al = new ArrayList JavaDoc(defaultParams.size());
623             for (Iterator JavaDoc i = defaultParams.iterator(); i.hasNext();) {
624                 ReportPageParam oneparams = (ReportPageParam) i.next();
625                 al.add(oneparams.getCode());
626             }
627             return al;
628         } else {
629             return null;
630         }
631     }
632
633 }
634
635 /* ReportPage */
Popular Tags