View Javadoc

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