View Javadoc

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