View Javadoc
1   /**
2    * Copyright 2005-2014 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 org.kuali.rice.core.api.delegation.DelegationType;
19  import org.kuali.rice.core.api.util.KeyValue;
20  import org.kuali.rice.coreservice.framework.CoreFrameworkServiceLocator;
21  import org.kuali.rice.kew.api.KewApiConstants;
22  import org.kuali.rice.kew.api.WorkflowRuntimeException;
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.routeheader.DocumentRouteHeaderValue;
32  import org.kuali.rice.kew.service.KEWServiceLocator;
33  import org.kuali.rice.krad.util.KRADConstants;
34  
35  import javax.persistence.EntityManager;
36  import java.util.ArrayList;
37  import java.util.Collections;
38  import java.util.List;
39  import java.util.StringTokenizer;
40  
41  public class QuickLinksDAOJpa implements QuickLinksDAO {
42  
43      private EntityManager entityManager;
44  
45      public static final String FIND_WATCHED_DOCUMENTS_BY_INITIATOR_WORKFLOW_ID_NAME =
46              "DocumentRouteHeaderValue.QuickLinks.FindWatchedDocumentsByInitiatorWorkflowId";
47      public static final String FIND_WATCHED_DOCUMENTS_BY_INITIATOR_WORKFLOW_ID_QUERY = "SELECT d FROM "
48              + "DocumentRouteHeaderValue d WHERE d.initiatorWorkflowId = :initiatorWorkflowId AND "
49              + "d.docRouteStatus IN ('"+ KewApiConstants.ROUTE_HEADER_ENROUTE_CD +"','"
50              + KewApiConstants.ROUTE_HEADER_EXCEPTION_CD +"') ORDER BY d.createDate DESC";
51  
52      @Override
53  	@SuppressWarnings("unchecked")
54      public List<ActionListStats> getActionListStats(final String principalId) {
55          try {
56              final List<Object[]> stats = getEntityManager().createNamedQuery("ActionItem.GetQuickLinksDocumentTypeNameAndCount").
57                      setParameter("principalId", principalId).setParameter("delegationType", DelegationType
58                      .SECONDARY.getCode()).getResultList();
59              final List<ActionListStats> docTypes = new ArrayList<ActionListStats>(stats.size());
60              for (Object[] res : stats) {
61                  final String docTypeName = (String) res[0];
62                  final Long count = (Long) res[1];
63  
64                  final DocumentType docType = getDocumentTypeService().findByName(docTypeName);
65                  if(docType != null){
66                      docTypes.add(new ActionListStats(docTypeName, docType.getLabel(), count.intValue()));
67                  }
68              }
69              Collections.sort(docTypes);
70              return docTypes;
71          } catch (Exception e) {
72              throw new WorkflowRuntimeException("Error getting action list stats for user: " + principalId, e);
73          }
74      }
75  
76      @Override
77  	@SuppressWarnings("unchecked")
78      public List<InitiatedDocumentType> getInitiatedDocumentTypesList(final String principalId) {
79          String documentNames = CoreFrameworkServiceLocator.getParameterService().getParameterValueAsString(KewApiConstants.KEW_NAMESPACE, KRADConstants.DetailTypes.QUICK_LINK_DETAIL_TYPE, KewApiConstants.QUICK_LINKS_RESTRICT_DOCUMENT_TYPES);
80          if (documentNames != null) {
81              documentNames = documentNames.trim();
82          }
83          if (documentNames == null || "none".equals(documentNames)) {
84              documentNames = "";
85          }
86  
87          final StringTokenizer st = new StringTokenizer(documentNames, ",");
88          final List<String> docTypesToRestrict = new ArrayList<String>();
89          while (st.hasMoreTokens()) {
90              docTypesToRestrict.add(st.nextToken());
91          }
92  
93          try {
94              final List<Object[]> list = getEntityManager().createNamedQuery(
95                      "DocumentType.QuickLinks.FindInitiatedDocumentTypesListByInitiatorWorkflowId").
96                      setParameter("initiatorWorkflowId", principalId).getResultList();
97              final List<InitiatedDocumentType> documentTypesByName = new ArrayList<InitiatedDocumentType>(list.size());
98              for (Object[] doc : list) {
99                  final String docTypeName = (String) doc[0];
100                 final String label = (String) doc[1];
101 
102                 final String docTypeTopParent;
103                 final int firstPeriod = docTypeName.indexOf(".");
104                 if (firstPeriod == -1) {
105                     docTypeTopParent = docTypeName.substring(0);
106                 } else {
107                     docTypeTopParent = docTypeName.substring(0, firstPeriod);
108                 }
109                 if (!docTypesToRestrict.contains(docTypeTopParent)) {
110                     // the document types should be cached so this should be pretty quick
111                     final DocumentType docType = KEWServiceLocator.getDocumentTypeService().findByName(docTypeName);
112                     final DocumentTypePolicy quickInitiatePolicy = docType.getSupportsQuickInitiatePolicy();
113                     if (quickInitiatePolicy.getPolicyValue().booleanValue()) {
114                         documentTypesByName.add(new InitiatedDocumentType(docTypeName, label));
115                     }
116                 }
117             }
118             return documentTypesByName;
119         } catch (Exception e) {
120             throw new WorkflowRuntimeException("Error getting initiated document types for user: " + principalId, e);
121         }
122     }
123 
124     @Override
125 	public List<KeyValue> getNamedSearches(String principalId) {
126         return getDocumentSearchService().getNamedSearches(principalId);
127     }
128 
129     @Override
130 	public List<KeyValue> getRecentSearches(String principalId) {
131         return getDocumentSearchService().getMostRecentSearches(principalId);
132     }
133 
134     @Override
135 	@SuppressWarnings("unchecked")
136     public List<WatchedDocument> getWatchedDocuments(final String principalId) {
137         try {
138             List<DocumentRouteHeaderValue> documentRouteHeaderValues =  getEntityManager().createNamedQuery(
139                     QuickLinksDAOJpa.FIND_WATCHED_DOCUMENTS_BY_INITIATOR_WORKFLOW_ID_NAME).
140                     setParameter("initiatorWorkflowId", principalId).getResultList();
141             List<WatchedDocument> watchedDocuments = new ArrayList<WatchedDocument>();
142             for(DocumentRouteHeaderValue documentRouteHeader : documentRouteHeaderValues){
143                 WatchedDocument watchedDocument = new WatchedDocument(documentRouteHeader.getDocumentId(),
144                         documentRouteHeader.getDocRouteStatusLabel(),documentRouteHeader.getDocTitle());
145                 watchedDocuments.add(watchedDocument);
146             }
147             return watchedDocuments;
148         } catch (Exception e) {
149             throw new WorkflowRuntimeException("Error getting watched documents for user: " + principalId, e);
150         }
151     }
152 
153     public DocumentTypeService getDocumentTypeService() {
154         return ((DocumentTypeService) KEWServiceLocator.getService(KEWServiceLocator.DOCUMENT_TYPE_SERVICE));
155     }
156 
157     public DocumentSearchService getDocumentSearchService() {
158         return ((DocumentSearchService) KEWServiceLocator.getService(KEWServiceLocator.DOCUMENT_SEARCH_SERVICE));
159     }
160 
161     public EntityManager getEntityManager() {
162         return this.entityManager;
163     }
164 
165     public void setEntityManager(EntityManager entityManager) {
166         this.entityManager = entityManager;
167     }
168 }