001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.kew.stats.dao.impl;
017
018import org.apache.ojb.broker.accesslayer.LookupException;
019import org.kuali.rice.core.api.util.ConcreteKeyValue;
020import org.kuali.rice.core.api.util.KeyValue;
021import org.kuali.rice.kew.stats.Stats;
022import org.kuali.rice.kew.stats.dao.StatsDAO;
023import org.kuali.rice.kew.api.KewApiConstants;
024
025import javax.persistence.EntityManager;
026import javax.persistence.PersistenceContext;
027import javax.persistence.Query;
028import java.sql.SQLException;
029import java.sql.Timestamp;
030import java.util.ArrayList;
031import java.util.Calendar;
032import java.util.Date;
033import java.util.List;
034
035/**
036 * This is a description of what this class does - ddean don't forget to fill this in. 
037 * 
038 * @author Kuali Rice Team (rice.collab@kuali.org)
039 *
040 */
041// There isn't an obvious place to put these @NamedQueries since they are just doing select count(*) from various tables.
042// Thus I'm using the query literals in this class, move these NamedQuerys to wherever they need to go.
043// @NamedQueries({
044//    @NamedQuery(name="Stats.DocumentsRoutedReport",  query="select count(*) as count, drhv.docRouteStatus from DocumentRouteHeaderValue drhv where drhv.createDate between :beginDate and :endDate group by docRouteStatus"),
045//    @NamedQuery(name="Stats.NumActiveItemsReport",  query="select count(*) from ActionItem ai"),
046//    @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"),
047//    @NamedQuery(name="Stats.NumUsersReport",  query="select count(distinct workflowId) from UserOptions uo"),
048//    @NamedQuery(name="Stats.NumberOfDocTypesReport",  query="select count(*) from DocumentType dt where dt.currentInd = true")
049//  })
050public class StatsDaoJpaImpl implements StatsDAO {
051
052    @PersistenceContext
053    private EntityManager entityManager;
054    
055    @Override
056        public void DocumentsRoutedReport(Stats stats, Date begDate, Date endDate) throws SQLException, LookupException {
057        Query query = entityManager.createQuery("select count(*) as count, drhv.docRouteStatus from DocumentRouteHeaderValue drhv where drhv.createDate between :beginDate and :endDate group by docRouteStatus");
058//        Query query = entityManager.createNamedQuery("Stats.DocumentsRoutedReport");
059        query.setParameter("beginDate", new Timestamp(begDate.getTime()));
060        query.setParameter("endDate", new Timestamp(endDate.getTime()));
061        
062        @SuppressWarnings("unchecked")
063        List<Object[]> resultList = query.getResultList();
064        
065        for (Object[] result : resultList) {
066            String actionType = result[1].toString();
067            String number = result[0].toString();
068            if (actionType.equals(KewApiConstants.ROUTE_HEADER_CANCEL_CD)) {
069                stats.setCanceledNumber(number);
070            } else if (actionType.equals(KewApiConstants.ROUTE_HEADER_DISAPPROVED_CD)) {
071                stats.setDisapprovedNumber(number);
072            } else if (actionType.equals(KewApiConstants.ROUTE_HEADER_ENROUTE_CD)) {
073                stats.setEnrouteNumber(number);
074            } else if (actionType.equals(KewApiConstants.ROUTE_HEADER_EXCEPTION_CD)) {
075                stats.setExceptionNumber(number);
076            } else if (actionType.equals(KewApiConstants.ROUTE_HEADER_FINAL_CD)) {
077                stats.setFinalNumber(number);
078            } else if (actionType.equals(KewApiConstants.ROUTE_HEADER_INITIATED_CD)) {
079                stats.setInitiatedNumber(number);
080            } else if (actionType.equals(KewApiConstants.ROUTE_HEADER_PROCESSED_CD)) {
081                stats.setProcessedNumber(number);
082            } else if (actionType.equals(KewApiConstants.ROUTE_HEADER_SAVED_CD)) {
083                stats.setSavedNumber(number);
084            }
085        }
086    }
087
088    @Override
089        public void NumActiveItemsReport(Stats stats) throws SQLException, LookupException {
090        stats.setNumActionItems(entityManager.createQuery("select count(*) from ActionItem ai").getSingleResult().toString());
091//        stats.setNumActionItems(entityManager.createNamedQuery("Stats.NumActiveItemsReport").getSingleResult().toString());
092    }
093
094    @Override
095        public void NumInitiatedDocsByDocTypeReport(Stats stats) throws SQLException, LookupException {
096        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");
097//        Query query = entityManager.createNamedQuery("Stats.NumInitiatedDocsByDocTypeReport");
098        Calendar calendar = Calendar.getInstance();
099        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}