View Javadoc

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