View Javadoc

1   /*
2    * Copyright 2005-2008 The Kuali Foundation
3    *
4    *
5    * Licensed under the Educational Community License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.opensource.org/licenses/ecl2.php
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.kuali.rice.kew.stats.dao.impl;
18  
19  import java.sql.Connection;
20  import java.sql.PreparedStatement;
21  import java.sql.ResultSet;
22  import java.sql.SQLException;
23  import java.sql.Timestamp;
24  import java.util.ArrayList;
25  import java.util.Calendar;
26  import java.util.Date;
27  import java.util.List;
28  
29  import org.apache.ojb.broker.PersistenceBroker;
30  import org.apache.ojb.broker.accesslayer.LookupException;
31  import org.kuali.rice.kew.stats.Stats;
32  import org.kuali.rice.kew.stats.dao.StatsDAO;
33  import org.kuali.rice.kew.util.KEWConstants;
34  import org.kuali.rice.kew.web.KeyValue;
35  import org.springmodules.orm.ojb.support.PersistenceBrokerDaoSupport;
36  
37  
38  public class StatsDAOOjbImpl extends PersistenceBrokerDaoSupport implements StatsDAO {
39  
40      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(StatsDAOOjbImpl.class);
41  
42      public static final String SQL_NUM_ACTIVE_ITEMS = "select count(*) from krew_actn_itm_t";
43      public static final String SQL_NUM_DOC_TYPES_REPORT = "select count(*) as num from krew_doc_typ_t where cur_ind = 1";
44      public static final String SQL_DOCUMENTS_ROUTED = "select count(*) as count, krew_doc_hdr_t.doc_hdr_stat_cd from krew_doc_hdr_t where krew_doc_hdr_t.crte_dt between ? and ? group by doc_hdr_stat_cd";
45      public static final String SQL_NUM_USERS = "select count(distinct prncpl_id) as prsn_count from KREW_USR_OPTN_T";
46      public static final String SQL_NUM_DOCS_INITIATED = "select count(*), krew_doc_typ_t.doc_typ_nm from krew_doc_hdr_t, krew_doc_typ_t where krew_doc_hdr_t.crte_dt > ? and krew_doc_hdr_t.doc_typ_id = krew_doc_typ_t.doc_typ_id group by krew_doc_typ_t.doc_typ_nm";
47  
48      public void NumActiveItemsReport(Stats stats) throws SQLException, LookupException {
49  
50          LOG.debug("NumActiveItemsReport()");
51          PersistenceBroker broker = this.getPersistenceBroker(false);
52          Connection conn = broker.serviceConnectionManager().getConnection();
53          PreparedStatement ps = conn.prepareStatement(StatsDAOOjbImpl.SQL_NUM_ACTIVE_ITEMS);
54          ResultSet rs = ps.executeQuery();
55  
56          while (rs.next()) {
57              stats.setNumActionItems(new Integer(rs.getInt(1)).toString());
58          }
59  
60          closeDatabaseObjects(rs, ps, conn/*, broker*/);
61      }
62  
63      public void NumberOfDocTypesReport(Stats stats) throws SQLException, LookupException {
64  
65          LOG.debug("NumberOfDocTypesReport()");
66          PersistenceBroker broker = this.getPersistenceBroker(false);
67          Connection conn = broker.serviceConnectionManager().getConnection();
68          PreparedStatement ps = conn.prepareStatement(StatsDAOOjbImpl.SQL_NUM_DOC_TYPES_REPORT);
69          ResultSet rs = ps.executeQuery();
70  
71          while (rs.next()) {
72              stats.setNumDocTypes(new Integer(rs.getInt(1)).toString());
73          }
74  
75          closeDatabaseObjects(rs, ps, conn/*, broker*/);
76      }
77  
78      public void DocumentsRoutedReport(Stats stats, Date begDate, Date endDate) throws SQLException, LookupException {
79  
80          LOG.debug("DocumentsRoutedReport()");
81          PersistenceBroker broker = this.getPersistenceBroker(false);
82          Connection conn = broker.serviceConnectionManager().getConnection();
83          PreparedStatement ps = conn.prepareStatement(StatsDAOOjbImpl.SQL_DOCUMENTS_ROUTED);
84          ps.setTimestamp(1, new Timestamp(begDate.getTime()));
85          ps.setTimestamp(2, new Timestamp(endDate.getTime()));
86          ResultSet rs = ps.executeQuery();
87  
88          while (rs.next()) {
89  
90              String actionType = rs.getString(2);
91              String number = new Integer(rs.getInt(1)).toString();
92              if (actionType.equals(KEWConstants.ROUTE_HEADER_APPROVED_CD)) {
93                  stats.setApprovedNumber(number);
94              } else if (actionType.equals(KEWConstants.ROUTE_HEADER_CANCEL_CD)) {
95                  stats.setCanceledNumber(number);
96              } else if (actionType.equals(KEWConstants.ROUTE_HEADER_DISAPPROVED_CD)) {
97                  stats.setDisapprovedNumber(number);
98              } else if (actionType.equals(KEWConstants.ROUTE_HEADER_ENROUTE_CD)) {
99                  stats.setEnrouteNumber(number);
100             } else if (actionType.equals(KEWConstants.ROUTE_HEADER_EXCEPTION_CD)) {
101                 stats.setExceptionNumber(number);
102             } else if (actionType.equals(KEWConstants.ROUTE_HEADER_FINAL_CD)) {
103                 stats.setFinalNumber(number);
104             } else if (actionType.equals(KEWConstants.ROUTE_HEADER_INITIATED_CD)) {
105                 stats.setInitiatedNumber(number);
106             } else if (actionType.equals(KEWConstants.ROUTE_HEADER_PROCESSED_CD)) {
107                 stats.setProcessedNumber(number);
108             } else if (actionType.equals(KEWConstants.ROUTE_HEADER_SAVED_CD)) {
109                 stats.setSavedNumber(number);
110             }
111         }
112 
113         closeDatabaseObjects(rs, ps, conn/*, broker*/);
114     }
115 
116     public void NumUsersReport(Stats stats) throws SQLException, LookupException {
117 
118         LOG.debug("NumUsersReport()");
119         PersistenceBroker broker = this.getPersistenceBroker(false);
120         Connection conn = broker.serviceConnectionManager().getConnection();
121         PreparedStatement ps = conn.prepareStatement(StatsDAOOjbImpl.SQL_NUM_USERS);
122         ResultSet rs = ps.executeQuery();
123 
124         while (rs.next()) {
125             stats.setNumUsers(new Integer(rs.getInt("prsn_count")).toString());
126         }
127 
128         closeDatabaseObjects(rs, ps, conn/*, broker*/);
129     }
130 
131     public void NumInitiatedDocsByDocTypeReport(Stats stats) throws SQLException, LookupException {
132 
133         LOG.debug("NumInitiatedDocsByDocType()");
134         PersistenceBroker broker = this.getPersistenceBroker(false);
135         Connection conn = broker.serviceConnectionManager().getConnection();
136         PreparedStatement ps = conn.prepareStatement(StatsDAOOjbImpl.SQL_NUM_DOCS_INITIATED);
137         Calendar calendar = Calendar.getInstance();
138         calendar.add(Calendar.DAY_OF_YEAR, -29);
139         calendar.set(Calendar.HOUR_OF_DAY, 0);
140         calendar.set(Calendar.MINUTE, 0);
141         calendar.set(Calendar.SECOND, 0);
142         calendar.set(Calendar.MILLISECOND, 0);
143         ps.setTimestamp(1, new Timestamp(calendar.getTime().getTime()));
144         ResultSet rs = ps.executeQuery();
145 
146         List numDocs = new ArrayList();
147 
148         while (rs.next()) {
149             numDocs.add(new KeyValue(rs.getString(2), new Integer(rs.getInt(1)).toString()));
150         }
151         stats.setNumInitiatedDocsByDocType(numDocs);
152 
153         closeDatabaseObjects(rs, ps, conn/*, broker*/);
154 
155     }
156 
157     private void closeDatabaseObjects(ResultSet rs, PreparedStatement ps, Connection conn/*, PersistenceBroker broker*/) {
158 
159         try {
160             rs.close();
161         } catch (SQLException ex) {
162             LOG.warn("Failed to close ResultSet.", ex);
163         }
164 
165         try {
166             ps.close();
167         } catch (SQLException ex) {
168             LOG.warn("Failed to close PreparedStatement.", ex);
169         }
170 
171         try {
172             conn.close();
173         } catch (SQLException ex) {
174             LOG.warn("Failed to close Connection.", ex);
175         }
176 /*
177         try {
178             broker.close();
179         } catch (Exception ex) {
180             LOG.warn("Failed to close broker.", ex);
181         }
182 */
183     }
184 
185 }