View Javadoc
1   /**
2    * Copyright 2005-2016 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 java.sql.SQLException;
19  import java.sql.Timestamp;
20  import java.util.ArrayList;
21  import java.util.Calendar;
22  import java.util.Date;
23  import java.util.List;
24  
25  import javax.persistence.EntityManager;
26  import javax.persistence.Query;
27  
28  import org.kuali.rice.core.api.util.ConcreteKeyValue;
29  import org.kuali.rice.core.api.util.KeyValue;
30  import org.kuali.rice.kew.api.KewApiConstants;
31  import org.kuali.rice.kew.stats.Stats;
32  import org.kuali.rice.kew.stats.dao.StatsDAO;
33  
34  /**
35   * This is a description of what this class does - ddean don't forget to fill this in.
36   *
37   * @author Kuali Rice Team (rice.collab@kuali.org)
38   *
39   */
40  public class StatsDaoJpa implements StatsDAO {
41  
42      public static final String STATS_DOCUMENTS_ROUTED_REPORT = "select count(drhv) as cnt, drhv.docRouteStatus "
43                              + "from DocumentRouteHeaderValue drhv "
44                              + "where drhv.createDate between :beginDate and :endDate group by drhv.docRouteStatus";
45      public static final String STATS_NUM_ACTIVE_ITEMS_REPORT = "select count(ai) from ActionItem ai";
46      public static final String STATS_NUM_INITIATED_DOCS_BY_DOC_TYPE_REPORT = "select count(drhv), dt.name from "
47              + "DocumentRouteHeaderValue drhv, DocumentType dt where drhv.createDate > :createDate and "
48              + "drhv.documentTypeId = dt.documentTypeId group by dt.name";
49      public static final String STATS_NUM_USERS_REPORT = "select count(distinct(uo.workflowId)) from UserOptions uo";
50      public static final String STATS_NUM_DOC_TYPES_REPORT =
51                      "select count(dt) from DocumentType dt where dt.currentInd = true";
52  
53      private EntityManager entityManager;
54  
55      @Override
56  	public void DocumentsRoutedReport(Stats stats, Date begDate, Date endDate) throws SQLException {
57          Query query = getEntityManager().createQuery(STATS_DOCUMENTS_ROUTED_REPORT);
58          query.setParameter("beginDate", new Timestamp(begDate.getTime()));
59          query.setParameter("endDate", new Timestamp(endDate.getTime()));
60  
61          @SuppressWarnings("unchecked")
62          List<Object[]> resultList = query.getResultList();
63  
64          for (Object[] result : resultList) {
65              String actionType = result[1].toString();
66              String number = result[0].toString();
67              if (actionType.equals(KewApiConstants.ROUTE_HEADER_CANCEL_CD)) {
68                  stats.setCanceledNumber(number);
69              } else if (actionType.equals(KewApiConstants.ROUTE_HEADER_DISAPPROVED_CD)) {
70                  stats.setDisapprovedNumber(number);
71              } else if (actionType.equals(KewApiConstants.ROUTE_HEADER_ENROUTE_CD)) {
72                  stats.setEnrouteNumber(number);
73              } else if (actionType.equals(KewApiConstants.ROUTE_HEADER_EXCEPTION_CD)) {
74                  stats.setExceptionNumber(number);
75              } else if (actionType.equals(KewApiConstants.ROUTE_HEADER_FINAL_CD)) {
76                  stats.setFinalNumber(number);
77              } else if (actionType.equals(KewApiConstants.ROUTE_HEADER_INITIATED_CD)) {
78                  stats.setInitiatedNumber(number);
79              } else if (actionType.equals(KewApiConstants.ROUTE_HEADER_PROCESSED_CD)) {
80                  stats.setProcessedNumber(number);
81              } else if (actionType.equals(KewApiConstants.ROUTE_HEADER_SAVED_CD)) {
82                  stats.setSavedNumber(number);
83              }
84          }
85      }
86  
87      @Override
88  	public void NumActiveItemsReport(Stats stats) throws SQLException {
89          stats.setNumActionItems(getEntityManager().createQuery(STATS_NUM_ACTIVE_ITEMS_REPORT)
90                  .getSingleResult().toString());
91      }
92  
93      @Override
94  	public void NumInitiatedDocsByDocTypeReport(Stats stats) throws SQLException {
95          Query query = getEntityManager().createQuery(STATS_NUM_INITIATED_DOCS_BY_DOC_TYPE_REPORT);
96          Calendar calendar = Calendar.getInstance();
97          calendar.add(Calendar.DAY_OF_YEAR, -29);
98          calendar.set(Calendar.HOUR_OF_DAY, 0);
99          calendar.set(Calendar.MINUTE, 0);
100         calendar.set(Calendar.SECOND, 0);
101         calendar.set(Calendar.MILLISECOND, 0);
102         query.setParameter("createDate", new Timestamp(calendar.getTime().getTime()));
103 
104         @SuppressWarnings("unchecked")
105         List<Object[]> resultList = query.getResultList();
106 
107         List<KeyValue> numDocs = new ArrayList<KeyValue>(resultList.size());
108         for (Object[] result : resultList) {
109             numDocs.add(new ConcreteKeyValue(result[1].toString(),result[0].toString()));
110         }
111 
112         stats.setNumInitiatedDocsByDocType(numDocs);
113     }
114 
115     @Override
116 	public void NumUsersReport(Stats stats) throws SQLException {
117         stats.setNumUsers(getEntityManager().createQuery(STATS_NUM_USERS_REPORT).getSingleResult().toString());
118     }
119 
120     @Override
121 	public void NumberOfDocTypesReport(Stats stats) throws SQLException {
122         stats.setNumDocTypes(getEntityManager().createQuery(
123                 STATS_NUM_DOC_TYPES_REPORT).getSingleResult().toString());
124     }
125 
126     public EntityManager getEntityManager() {
127         return this.entityManager;
128     }
129 
130     public void setEntityManager(EntityManager entityManager) {
131         this.entityManager = entityManager;
132     }
133 
134 }