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.io.FileWriter;
24  import java.io.IOException;
25  import java.text.DateFormat;
26  import java.text.SimpleDateFormat;
27  import java.util.Date;
28  import java.util.Locale;
29  
30  import com.ebay.carad.os.vitalsigns.DataPoint;
31  import com.ebay.carad.os.vitalsigns.IDashboardAgent;
32  import com.ebay.carad.os.vitalsigns.IDashboardReport;
33  import com.ebay.carad.os.vitalsigns.ReportingException;
34  
35  /***
36   * <p>Reports the data from a given report as a CSV file.</p>
37   * 
38   * <p>WARNING: not thread safe! Use of a DateFormat makes this not thread safe.  In current 
39   * implementation, if it is only used in one DashboardAgent, this should be fine.</p>
40   * 
41   * @author Jeremy Thomerson
42   * @author Jeremy Kraybill
43   * @version $Id$
44   */
45  public class CsvReportListener extends AbstractReportingListener {
46  
47      //private static final Logger LOGGER = Logger.getLogger(CsvReportListener.class);
48  
49      // not static to avoid having to synchronize around the instance for separate instances to use this.
50      private final DateFormat mDateFormatter = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss.SSS", Locale.getDefault()); 
51  
52      public void reportRan(IDashboardAgent agent, IDashboardReport report) {
53          String chartBase = agent.getDestinationPath() + "report" + report.getID();
54          FileWriter fw = null;
55          try {
56              fw = new FileWriter(chartBase + ".csv");
57              fw.write(report.getTitle() + "\n" + report.getSubTitle() + "\n\n");
58              DataPoint[] data = agent.getDefaultDataDAO().getData(report);
59              for (int i = 0; i < data.length; i++) {
60                  DataPoint row = data[i];
61                  fw.write(mDateFormatter.format(new Date(row.getLogTime())) + "," + row.getData() + "\n");
62              }
63          } catch(IOException ioe) {
64              throw new ReportingException("Exception while writing report: " + ioe.getMessage(), ioe);
65          } finally {
66              try {
67                  if (fw != null) {
68                      fw.close();
69                  }
70              } catch (IOException ioe) {
71                  throw new ReportingException("Exception while closing stream: " + ioe.getMessage(), ioe);
72              }
73          }
74      }
75  
76  }
77