View Javadoc

1   /*
2    * The contents of this file are subject to the terms 
3    * of the Common Development and Distribution License 
4    * (the "License").  You may not use this file except 
5    * in compliance with the License.
6    * 
7    * You can obtain a copy of the license at 
8    * http://www.sun.com/cddl/cddl.html. 
9    * See the License for the specific language governing 
10   * permissions and limitations under the License.
11   * 
12   * When distributing Covered Code, include this CDDL 
13   * HEADER in each file and include the License file at 
14   * license.txt.  If applicable, add the following below 
15   * this CDDL HEADER, with the fields enclosed by brackets 
16   * "[]" replaced with your own identifying information: 
17   * Portions Copyright [yyyy] [name of copyright owner]
18   * 
19   * Portions Copyright 2004 eBay, Inc.
20   */
21  package com.ebay.carad.os.vitalsigns.listeners;
22  
23  import java.text.Format;
24  import java.text.MessageFormat;
25  import java.util.ArrayList;
26  import java.util.HashMap;
27  import java.util.Iterator;
28  import java.util.List;
29  import java.util.Map;
30  
31  import com.ebay.carad.os.vitalsigns.IDashboardAgent;
32  import com.ebay.carad.os.vitalsigns.IDashboardReport;
33  import com.ebay.carad.os.vitalsigns.listeners.outputtemplates.ITemplateProcessor;
34  import com.ebay.carad.os.vitalsigns.util.ReportUtil;
35  
36  /***
37   * <p>Writes HTML reports that can include the graphs created by
38   * <tt>LineGraphImageCreationListener</tt>.  The reports can be customized
39   * by supplying your own Freemarker template, and supplying the relative
40   * path to it on your classpath.</p>
41   * 
42   * @author Jeremy Kraybill
43   * @author Jeremy Thomerson
44   * @version $Id$
45   */
46  public class HtmlGraphListener extends LineGraphImageCreationListener {
47  
48  //    private static final Logger LOGGER = Logger.getLogger(HtmlGraphListener.class);
49  
50      private ITemplateProcessor mTemplateProcessor = ITemplateProcessor.DEFAULT_IMPLEMENTATION;
51      private String mTemplatePath = "templates/standard/HtmlGraphListener.fml";
52      private Format mRelativePath = new MessageFormat("report{0,number,#}.html");
53  
54      protected void doAfterImagesCreated(float[] delta, IDashboardReport report, IDashboardAgent agent) {
55          Map context = new HashMap();
56          context.put("delta", delta);
57          context.put("deltasAsHtml", ReportUtil.getDeltasAsHtml(delta, report.getMoreIsBetter()));
58          context.put("exportAsCsv", Boolean.valueOf(isExportedAsCSV(agent, report)));
59          
60          List charts = new ArrayList();
61          if (includeHourChart(report)) {
62              charts.add("hour");
63          }
64          if (includeDayChart(report)) {
65              charts.add("day");
66          }
67          if (includeWeekChart(report)) {
68              charts.add("week");
69          }
70          if (includeMonthChart(report)) {
71              charts.add("month");
72          }
73          if (includeYearChart(report)) {
74              charts.add("year");
75          }
76          context.put("charts", charts);
77          mTemplateProcessor.processContext(context, agent, report, mTemplatePath, mRelativePath.format(new Object[] { new Integer(report.getID()) }));
78      }
79  
80      private boolean isExportedAsCSV(IDashboardAgent agent, IDashboardReport config) {
81          return checkForCsvListener(agent.getReportingListeners()) || checkForCsvListener(config.getReportingListeners());
82      }
83  
84      private boolean checkForCsvListener(List reportingListeners) {
85          for (Iterator it = reportingListeners.iterator(); it.hasNext(); ) {
86              if (it.next() instanceof CsvReportListener) {
87                  return true;
88              }
89          }
90          return false;
91      }
92  
93      public String getTemplatePath() {
94          return mTemplatePath;
95      }
96  
97      /***
98       * Default value is: HtmlGraphListener.fml
99       * @param templatePath path that template resides within classpath
100      */
101     public void setTemplatePath(String templatePath) {
102         mTemplatePath = templatePath;
103     }
104     /***
105      * Relative output path for the generated HTML, passed through a MessageFormat
106      * with a single parameter - the report ID that is being generated.
107      * 
108      * @param path A String that can be used in MessageFormat
109      */
110     public void setRelativeOutputPath(String path) {
111     	mRelativePath = new MessageFormat(path);
112     }
113     
114     /***
115      * @param templateProcessor The templateProcessor to set.
116      */
117     public void setTemplateProcessor(ITemplateProcessor templateProcessor) {
118         mTemplateProcessor = templateProcessor;
119     }
120 
121 }