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