Coverage Report - org.kuali.rice.kew.quicklinks.dao.impl.QuickLinksDAOJpaImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
QuickLinksDAOJpaImpl
0%
0/52
0%
0/20
3.111
 
 1  
 /*
 2  
  * Copyright 2009 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.quicklinks.dao.impl;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.Collections;
 20  
 import java.util.List;
 21  
 import java.util.StringTokenizer;
 22  
 
 23  
 import javax.persistence.EntityManager;
 24  
 import javax.persistence.PersistenceContext;
 25  
 
 26  
 import org.kuali.rice.kew.docsearch.service.DocumentSearchService;
 27  
 import org.kuali.rice.kew.doctype.DocumentTypePolicy;
 28  
 import org.kuali.rice.kew.doctype.bo.DocumentType;
 29  
 import org.kuali.rice.kew.doctype.service.DocumentTypeService;
 30  
 import org.kuali.rice.kew.exception.WorkflowRuntimeException;
 31  
 import org.kuali.rice.kew.quicklinks.ActionListStats;
 32  
 import org.kuali.rice.kew.quicklinks.InitiatedDocumentType;
 33  
 import org.kuali.rice.kew.quicklinks.WatchedDocument;
 34  
 import org.kuali.rice.kew.quicklinks.dao.QuickLinksDAO;
 35  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 36  
 import org.kuali.rice.kew.util.KEWConstants;
 37  
 import org.kuali.rice.kew.util.Utilities;
 38  
 import org.kuali.rice.kew.web.KeyValue;
 39  
 import org.kuali.rice.kns.util.KNSConstants;
 40  
 
 41  0
 public class QuickLinksDAOJpaImpl implements QuickLinksDAO {
 42  0
     private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(QuickLinksDAOJpaImpl.class);
 43  
 
 44  
     @PersistenceContext(unitName = "kew-unit")
 45  
     private EntityManager entityManager;
 46  
 
 47  
     @SuppressWarnings("unchecked")
 48  
     public List<ActionListStats> getActionListStats(final String principalId) {
 49  
         try {
 50  0
             final List<Object[]> stats = (List<Object[]>) entityManager.createNamedQuery("ActionItem.QuickLinks.FindActionListStatsByPrincipalId").setParameter("principalId", principalId).getResultList();
 51  0
             final List<ActionListStats> docTypes = new ArrayList<ActionListStats>(stats.size());
 52  0
             for (Object[] res : stats) {
 53  0
                 final String docTypeName = (String) res[0];
 54  0
                 final Long count = (Long) res[1];
 55  
 
 56  0
                 final List<String> docTypeLabel = (List<String>) entityManager.createNamedQuery("DocumentType.QuickLinks.FindLabelByTypeName").setParameter("docTypeName", docTypeName).getResultList();
 57  0
                 if (docTypeLabel.size() > 0) {
 58  0
                     docTypes.add(new ActionListStats(docTypeName, docTypeLabel.get(0), count.intValue()));
 59  
                 }
 60  0
             }
 61  0
             Collections.sort(docTypes);
 62  0
             return docTypes;
 63  0
         } catch (Exception e) {
 64  0
             throw new WorkflowRuntimeException("Error getting action list stats for user: " + principalId, e);
 65  
         }
 66  
     }
 67  
 
 68  
     @SuppressWarnings("unchecked")
 69  
     public List<InitiatedDocumentType> getInitiatedDocumentTypesList(final String principalId) {
 70  0
         String documentNames = Utilities.getKNSParameterValue(KEWConstants.KEW_NAMESPACE, KNSConstants.DetailTypes.QUICK_LINK_DETAIL_TYPE, KEWConstants.QUICK_LINKS_RESTRICT_DOCUMENT_TYPES);
 71  0
         if (documentNames != null) {
 72  0
             documentNames = documentNames.trim();
 73  
         }
 74  0
         if (documentNames == null || "none".equals(documentNames)) {
 75  0
             documentNames = "";
 76  
         }
 77  
 
 78  0
         final StringTokenizer st = new StringTokenizer(documentNames, ",");
 79  0
         final List<String> docTypesToRestrict = new ArrayList<String>();
 80  0
         while (st.hasMoreTokens()) {
 81  0
             docTypesToRestrict.add(st.nextToken());
 82  
         }
 83  
 
 84  
         try {
 85  0
             final List<Object[]> list = (List<Object[]>) entityManager.createNamedQuery("DocumentType.QuickLinks.FindInitiatedDocumentTypesListByInitiatorWorkflowId").setParameter("initiatorWorkflowId", principalId).getResultList();
 86  0
             final List<InitiatedDocumentType> documentTypesByName = new ArrayList<InitiatedDocumentType>(list.size());
 87  0
             for (Object[] doc : list) {
 88  0
                 final String docTypeName = (String) doc[0];
 89  0
                 final String label = (String) doc[1];
 90  
 
 91  
                 final String docTypeTopParent;
 92  0
                 final int firstPeriod = docTypeName.indexOf(".");
 93  0
                 if (firstPeriod == -1) {
 94  0
                     docTypeTopParent = docTypeName.substring(0);
 95  
                 } else {
 96  0
                     docTypeTopParent = docTypeName.substring(0, firstPeriod);
 97  
                 }
 98  0
                 if (!docTypesToRestrict.contains(docTypeTopParent)) {
 99  
                     // the document types should be cached so this should be pretty quick
 100  0
                     final DocumentType docType = KEWServiceLocator.getDocumentTypeService().findByName(docTypeName);
 101  0
                     final DocumentTypePolicy quickInitiatePolicy = docType.getSupportsQuickInitiatePolicy();
 102  0
                     if (quickInitiatePolicy.getPolicyValue().booleanValue()) {
 103  0
                         documentTypesByName.add(new InitiatedDocumentType(docTypeName, label));
 104  
                     }
 105  
                 }
 106  0
             }
 107  0
             return documentTypesByName;
 108  0
         } catch (Exception e) {
 109  0
             throw new WorkflowRuntimeException("Error getting initiated document types for user: " + principalId, e);
 110  
         }
 111  
     }
 112  
 
 113  
     public List<KeyValue> getNamedSearches(String principalId) {
 114  0
         return getDocumentSearchService().getNamedSearches(principalId);
 115  
     }
 116  
 
 117  
     public List<KeyValue> getRecentSearches(String principalId) {
 118  0
         return getDocumentSearchService().getMostRecentSearches(principalId);
 119  
     }
 120  
 
 121  
     @SuppressWarnings("unchecked")
 122  
     public List<WatchedDocument> getWatchedDocuments(final String principalId) {
 123  
         try {
 124  0
             return (List<WatchedDocument>) entityManager.createNamedQuery("DocumentRouteHeaderValue.QuickLinks.FindWatchedDocumentsByInitiatorWorkflowId").setParameter("initiatorWorkflowId", principalId).getResultList();
 125  0
         } catch (Exception e) {
 126  0
             throw new WorkflowRuntimeException("Error getting watched documents for user: " + principalId, e);
 127  
         }
 128  
     }
 129  
 
 130  
     public DocumentTypeService getDocumentTypeService() {
 131  0
         return ((DocumentTypeService) KEWServiceLocator.getService(KEWServiceLocator.DOCUMENT_TYPE_SERVICE));
 132  
     }
 133  
 
 134  
     public DocumentSearchService getDocumentSearchService() {
 135  0
         return ((DocumentSearchService) KEWServiceLocator.getService(KEWServiceLocator.DOCUMENT_SEARCH_SERVICE));
 136  
     }
 137  
 
 138  
     public EntityManager getEntityManager() {
 139  0
         return this.entityManager;
 140  
     }
 141  
 
 142  
     public void setEntityManager(EntityManager entityManager) {
 143  0
         this.entityManager = entityManager;
 144  0
     }
 145  
 }