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.dao;
22  
23  import java.text.Format;
24  import java.text.MessageFormat;
25  import java.util.List;
26  import java.util.Map;
27  
28  import org.apache.log4j.Logger;
29  
30  import com.ebay.carad.os.vitalsigns.DataPoint;
31  import com.ebay.carad.os.vitalsigns.IDashboardReport;
32  import com.ebay.carad.os.vitalsigns.IReportingListenerContainer;
33  import com.ebay.carad.os.vitalsigns.ReportingException;
34  
35  /***
36   * <p>An IDataDAO implementation that stores its data in a SQL DB.  Default
37   * queries are provided in the form of a <tt>Format</tt>, but can be substituted
38   * for a particular SQL-syntax for other types of databases.</p>
39   * 
40   * <p>Uses a <tt>ISqlDao</tt> to perform its' queries.</p>
41   * 
42   * <p>TODO : add various SQL syntaxes to SqlDataDAO that allow a string like "MySQL.dialect" or "MSSQL.dialect" to determine what default queries to use.</p>
43   * 
44   * @see #setInsertQuery(String)
45   * @see #setSelectQuery(String)
46   * 
47   * @author Jeremy Thomerson
48   * @version $Id$
49   */
50  public class SqlDataDAO implements IDataDAO {
51  
52      private static final Logger LOGGER = Logger.getLogger(IDataDAO.class);
53      private ISqlDAO mSqlDAO;
54      private Format mInsertQuery = new MessageFormat("INSERT INTO ReportData (logtime, reportid, data) VALUES ({0}, {1}, {2})");
55      private Format mSelectQuery = new MessageFormat("SELECT data, logtime FROM ReportData WHERE reportid = {0} ORDER BY logtime DESC");
56  
57      public SqlDataDAO() {
58          super();
59      }
60  
61      public void storeReportData(float data, IReportingListenerContainer agent, IDashboardReport report) throws ReportingException {
62      	LOGGER.info("storing data [" + data + "] for report: " + report.getID() + " [" + report.getTitle() + "]");
63          Object[] params = new Object[] { 
64                  Long.toString(System.currentTimeMillis()), 
65                  Integer.toString(report.getID()), 
66                  report.getThisRunData() == null ? null : Float.toString(report.getThisRunData().floatValue()) 
67                  };
68          String qry = mInsertQuery.format(params);
69          LOGGER.debug("query: " + qry);
70          mSqlDAO.updateWithSQL(qry);
71      }
72  
73      public DataPoint[] getData(IDashboardReport report) {
74      	LOGGER.info("retrieving data for report: " + report.getID() + " [" + report.getTitle() + "]");
75          Object[] params = new Object[] { Integer.toString(report.getID()) };
76          String qry = mSelectQuery.format(params);
77          LOGGER.debug("query: " + qry);
78          List list = mSqlDAO.findBySqlQuery(qry);
79          DataPoint[] points = new DataPoint[list.size()];
80          for (int i = 0; i < points.length; i++) {
81              Map cols = (Map) list.get(i);
82              points[i] = new DataPoint();
83              points[i].setData(cols.get("data") == null ? 0 : Float.parseFloat(cols.get("data").toString()));
84              points[i].setLogTime(Long.parseLong(cols.get("logtime").toString()));
85          }
86          return points;
87      }
88  
89      public void setSqlDAO(ISqlDAO sqlDAO) {
90          mSqlDAO = sqlDAO;
91      }
92  
93      public ISqlDAO getSqlDAO() {
94          return mSqlDAO;
95      }
96      
97      /***
98       * <p>The query to insert a new DataPoint record.  String is formatted with <tt>MessageFormat</tt>, so
99       * the rules that apply to <tt>MessageFormat</tt> apply to this String.  The parameters given to
100      * the format are: 
101      * <tt>new Object[] { new Long(System.currentTimeMillis()), new Integer(report.getID()), new Float(report.getThisRunData()) }</tt>.
102      * </p>
103      * 
104      * @param query the query to insert a new DataPoint record
105      * @see MessageFormat
106      */
107     public void setInsertQuery(String query) {
108         mInsertQuery = new MessageFormat(query);
109     }
110     
111     /***
112      * <p>The query to select all DataPoint records for a given report.  
113      * String is formatted with <tt>MessageFormat</tt>, so the rules that apply to 
114      * <tt>MessageFormat</tt> apply to this String.  The parameters given to
115      * the format are: 
116      * <tt>new Object[] { new Integer(report.getID()) }</tt>.</p>
117      * 
118      * <p>
119      * Note that the following column names must be returned by this query:
120      * <ul>
121      *  <li>logtime - the moment in time that this DataPoint refers to</li>
122      *  <li>data - the float data that was stored for the moment in time for this report</li>
123      * </p>
124      * 
125      * @param query the query to select a set of DataPoint records based on report ID 
126      * @see MessageFormat
127      */
128     public void setSelectQuery(String query) {
129         mSelectQuery = new MessageFormat(query);
130     }
131     
132 }
133