KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > app > statistics > HTMLReport


1 /*
2  * HTMLReport.java
3  *
4  * Version: $Revision: 1.2 $
5  *
6  * Date: $Date: 2005/04/20 14:22:41 $
7  *
8  * Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts
9  * Institute of Technology. All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are
13  * met:
14  *
15  * - Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * - Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution.
21  *
22  * - Neither the name of the Hewlett-Packard Company nor the name of the
23  * Massachusetts Institute of Technology nor the names of their
24  * contributors may be used to endorse or promote products derived from
25  * this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38  * DAMAGE.
39  */

40
41 package org.dspace.app.statistics;
42
43 import org.dspace.app.statistics.Report;
44 import org.dspace.app.statistics.Stat;
45 import org.dspace.app.statistics.Statistics;
46 import org.dspace.app.statistics.ReportTools;
47
48 import java.text.DateFormat JavaDoc;
49
50 import java.util.ArrayList JavaDoc;
51 import java.util.Date JavaDoc;
52 import java.util.Iterator JavaDoc;
53 import java.util.List JavaDoc;
54 import java.util.Map JavaDoc;
55 import java.util.regex.Matcher JavaDoc;
56 import java.util.regex.Pattern JavaDoc;
57
58 /**
59  * This class provides HTML reports for the ReportGenerator class
60  *
61  * @author Richard Jones
62  */

63 public class HTMLReport extends Report
64 {
65     // FIXME: all of these methods should do some content escaping before
66
// outputting anything
67

68     /** a list of the statistic blocks being managed by this class */
69     private List JavaDoc blocks = new ArrayList JavaDoc();
70     
71     /** the title for the page */
72     private String JavaDoc pageTitle = null;
73     
74     /** the main title for the page */
75     private String JavaDoc mainTitle = null;
76     
77     /** start date for report */
78     private Date JavaDoc start = null;
79     
80     /** end date for report */
81     private Date JavaDoc end = null;
82     
83     /**
84      * constructor for HTML reporting
85      */

86     public void HTMLReport()
87     {
88         // empty constructor
89
}
90     
91     /**
92      * return a string containing the report as generated by this class
93      *
94      * @return the HTML report
95      */

96     public String JavaDoc render()
97     {
98         StringBuffer JavaDoc frag = new StringBuffer JavaDoc();
99         
100         // get the page headings
101
frag.append(header(pageTitle));
102         frag.append(mainTitle());
103         frag.append(dateRange());
104         
105         // output the report blocks
106
// FIXME: perhaps the order of report blocks should be configurable
107
Iterator JavaDoc statSets = blocks.iterator();
108         while (statSets.hasNext())
109         {
110             frag.append(navigation());
111             
112             Statistics stats = (Statistics) statSets.next();
113             frag.append(sectionHeader(stats.getSectionHeader()));
114             frag.append(topLink());
115             frag.append(blockExplanation(stats.getExplanation()));
116             frag.append(floorInfo(stats.getFloor()));
117             frag.append(statBlock(stats));
118         }
119         
120         // output the footer and return
121
frag.append(footer());
122         
123         return frag.toString();
124     }
125     
126     
127     /**
128      * provide a link back to the top of the page
129      *
130      * @return a string containing the link text HTML formatted
131      */

132     public String JavaDoc topLink()
133     {
134         String JavaDoc frag = "<div class=\"reportNavigation\"><a HREF=\"#top\">Top</a></div>";
135         return frag;
136     }
137     
138     
139     /**
140      * build the internal navigation for the report
141      *
142      * @return an HTML string providing internal page navigation
143      */

144     public String JavaDoc navigation()
145     {
146         StringBuffer JavaDoc frag = new StringBuffer JavaDoc();
147         
148         frag.append("<div class=\"reportNavigation\">");
149         frag.append("<a HREF=\"#general_overview\">General Overview</a>");
150         frag.append("&nbsp;|&nbsp;");
151         frag.append("<a HREF=\"#archive_information\">Archive Information</a>");
152         frag.append("&nbsp;|&nbsp;");
153         frag.append("<a HREF=\"#items_viewed\">Items Viewed</a>");
154         frag.append("&nbsp;|&nbsp;");
155         frag.append("<a HREF=\"#all_actions_performed\">All Actions Performed</a>");
156         frag.append("&nbsp;|&nbsp;");
157         frag.append("<a HREF=\"#user_logins\">User Logins</a>");
158         frag.append("&nbsp;|&nbsp;");
159         frag.append("<a HREF=\"#words_searched\">Words Searched</a>");
160         frag.append("&nbsp;|&nbsp;");
161         frag.append("<a HREF=\"#averaging_information\">Averaging Information</a>");
162         frag.append("&nbsp;|&nbsp;");
163         frag.append("<a HREF=\"#log_level_information\">Log Level Information</a>");
164         frag.append("&nbsp;|&nbsp;");
165         frag.append("<a HREF=\"#processing_information\">Processing Information</a>");
166         frag.append("</div>");
167         
168         return frag.toString();
169     }
170     
171     /**
172      * add a statistics block to the report to the class register
173      *
174      * @param stat the statistics object to be added to the report
175      */

176     public void addBlock(Statistics stat)
177     {
178         blocks.add(stat);
179         return;
180     }
181     
182     
183     /**
184      * set the starting date for the report
185      *
186      * @param start the start date for the report
187      */

188     public void setStartDate(Date JavaDoc start)
189     {
190         this.start = start;
191     }
192     
193     
194     /**
195      * set the end date for the report
196      *
197      * @param end the end date for the report
198      */

199     public void setEndDate(Date JavaDoc end)
200     {
201         this.end = end;
202     }
203     
204     
205     /**
206      * output the date range in the relevant format. This requires that the
207      * date ranges have been set using setStartDate() and setEndDate()
208      *
209      * @return a string containing date range information
210      */

211     public String JavaDoc dateRange()
212     {
213         StringBuffer JavaDoc frag = new StringBuffer JavaDoc();
214         DateFormat JavaDoc df = DateFormat.getDateInstance();
215         
216         frag.append("<div class=\"reportDate\">");
217         if (start != null)
218         {
219             frag.append(df.format(start));
220         }
221         else
222         {
223             frag.append("from start of records ");
224         }
225         
226         frag.append(" to ");
227         
228         if (end != null)
229         {
230             frag.append(df.format(end));
231         }
232         else
233         {
234             frag.append(" end of records");
235         }
236         
237         frag.append("</div>\n\n");
238         
239         return frag.toString();
240     }
241     
242     
243     /**
244      * output the title in the relevant format. This requires that the title
245      * has been set with setMainTitle()
246      *
247      * @return a string containing the title of the report
248      */

249     public String JavaDoc mainTitle()
250     {
251         String JavaDoc frag = "<div class=\"reportTitle\"><a name=\"top\">" + mainTitle + "</a></div>\n\n";
252         return frag;
253     }
254     
255     
256     /**
257      * set the main title for the report
258      *
259      * @param name the name of the service
260      * @param serverName the name of the server
261      */

262     public void setMainTitle(String JavaDoc name, String JavaDoc serverName)
263     {
264         mainTitle = "Statistics for " + name + " on " + serverName;
265         if (pageTitle == null)
266         {
267             pageTitle = mainTitle;
268         }
269         return;
270     }
271     
272     
273     /**
274      * output any top headers that this page needs
275      *
276      * @return a string containing the header for the report
277      */

278     public String JavaDoc header()
279     {
280         return header("");
281     }
282     
283     /**
284      * output any top headers that this page needs, and include a title
285      * argument (Title support currently not implemented)
286      *
287      * @param title the title of the item being headered
288      */

289     public String JavaDoc header(String JavaDoc title)
290     {
291         // FIXME: this need to be figured out to integrate nicely into the
292
// whole JSTL thing, but for the moment it's just going to deliver
293
// some styles
294
StringBuffer JavaDoc frag = new StringBuffer JavaDoc();
295         
296         frag.append("<style type=\"text/css\">\n");
297         frag.append("body { font-family: Arial, Helvetica, sans-serif }");
298         frag.append(".reportTitle { width: 100%; clear: both; text-align: center; font-weight: bold; font-size: 200%; margin: 20px; }\n");
299         frag.append(".reportSection { width: 100%; clear: both; font-weight: bold; font-size: 160%; margin: 10px; text-align: center; margin-top: 30px; }\n");
300         frag.append(".reportBlock { border: 1px solid #000000; margin: 10px; }\n");
301         frag.append(".reportOddRow { background: #dddddd; }\n");
302         frag.append(".reportEvenRow { background: #bbbbbb; }\n");
303         frag.append(".reportExplanation { font-style: italic; text-align: center; }\n");
304         frag.append(".reportDate { font-style: italic; text-align: center; font-size: 120% }\n");
305         frag.append(".reportFloor { text-align: center; }\n");
306         frag.append(".rightAlign { text-align: right; }\n");
307         frag.append(".reportNavigation { text-align: center; }\n");
308         frag.append("</style>\n");
309         
310         return frag.toString();
311     }
312    
313    
314     /**
315      * output the section header in HTML format
316      *
317      * @param title the title of the section
318      *
319      * @return a string containing the section title HTML formatted
320      */

321     public String JavaDoc sectionHeader(String JavaDoc title)
322     {
323         // prepare the title to be an <a name="#title"> style link
324
// FIXME: this should be made more generic and used in a number of locations
325
String JavaDoc aName = title.toLowerCase();
326         Pattern JavaDoc space = Pattern.compile(" ");
327         Matcher JavaDoc matchSpace = space.matcher(aName);
328         aName = matchSpace.replaceAll("_");
329
330         String JavaDoc frag = "<div class=\"reportSection\"><a name=\"" + aName + "\">" + title + "</a></div>\n\n";
331         return frag;
332     }
333     
334     
335     /**
336      * output the report block based on the passed mapping, where the mapping
337      * sould be "name of report element" => "value", where both sides of the
338      * mapping should be Strings. This class also assumes that the reference
339      * is a linkable URL to the resource
340      *
341      * @param content the statistic object array to be displayed
342      *
343      * @return a string containing the statistics block HTML formatted
344      */

345     public String JavaDoc statBlock(Statistics content)
346     {
347         StringBuffer JavaDoc frag = new StringBuffer JavaDoc();
348         Stat[] stats = content.getStats();
349         
350         // start the table
351
frag.append("<table align=\"center\" class=\"reportBlock\" cellpadding=\"5\">\n");
352         
353         // prepare the table headers
354
if (content.getStatName() != null || content.getResultName() != null)
355         {
356             frag.append("\t<tr>\n");
357             frag.append("\t\t<th>\n");
358             if (content.getStatName() != null)
359             {
360                 frag.append("\t\t\t" + content.getStatName() + "\n");
361             }
362             else
363             {
364                 frag.append("\t\t\t&nbsp;\n");
365             }
366             frag.append("\t\t</th>\n");
367             frag.append("\t\t<th>\n");
368             if (content.getResultName() != null)
369             {
370                 frag.append("\t\t\t" + content.getResultName() + "\n");
371             }
372             else
373             {
374                 frag.append("\t\t\t&nbsp;\n");
375             }
376             frag.append("\t\t</th>\n");
377             frag.append("\t</tr>\n");
378         }
379         
380         // output the statistics in the table
381
for (int i = 0; i < stats.length; i++)
382         {
383             String JavaDoc style = null;
384  
385             if ((i % 2) == 1)
386             {
387                 style = "reportOddRow";
388             }
389             else
390             {
391                 style = "reportEvenRow";
392             }
393             
394             frag.append("\t<tr class=\"" + style + "\">\n\t\t<td>\n");
395             frag.append("\t\t\t");
396             if (stats[i].getReference() != null)
397             {
398                 frag.append("<a HREF=\"" + stats[i].getReference() + "\" ");
399                 frag.append("target=\"_blank\">");
400             }
401             frag.append(stats[i].getKey());
402             if (stats[i].getReference() != null)
403             {
404                 frag.append("</a>");
405             }
406             frag.append("\n");
407             frag.append("\t\t</td>\n\t\t<td class=\"rightAlign\">\n");
408             frag.append("\t\t\t" + ReportTools.numberFormat(stats[i].getValue()));
409             if (stats[i].getUnits() != null)
410             {
411                 frag.append(" " + stats[i].getUnits());
412             }
413             frag.append("\n");
414             frag.append("\t\t</td>\n\t</tr>\n");
415         }
416         
417         frag.append("</table>\n");
418         
419         return frag.toString();
420     }
421     
422     
423     /**
424      * output the floor information in HTML format
425      *
426      * @param floor the floor number for the section being displayed
427      *
428      * @return a string containing floor information HTML formatted
429      */

430     public String JavaDoc floorInfo(int floor)
431     {
432         if (floor > 0)
433         {
434             StringBuffer JavaDoc frag = new StringBuffer JavaDoc();
435             frag.append("<div class=\"reportFloor\">");
436             frag.append("(more than " + ReportTools.numberFormat(floor) + " times)");
437             frag.append("</div>\n");
438             return frag.toString();
439         }
440         else
441         {
442             return "";
443         }
444     }
445     
446     /**
447      * output the explanation of the report block in HTML format
448      *
449      * @param explanation some text explaining the coming report block
450      *
451      * @return a string containing an explanaton HTML formatted
452      */

453     public String JavaDoc blockExplanation(String JavaDoc explanation)
454     {
455         if (explanation != null)
456         {
457             StringBuffer JavaDoc frag = new StringBuffer JavaDoc();
458             frag.append("<div class=\"reportExplanation\">");
459             frag.append(explanation);
460             frag.append("</div>\n\n");
461             return frag.toString();
462         }
463         else
464         {
465             return "";
466         }
467     }
468     
469     /**
470      * output the final footers for this file
471      *
472      * @return a string containing the report footer
473      */

474     public String JavaDoc footer()
475     {
476         return "";
477     }
478     
479 }
480
Popular Tags