View Javadoc

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  public class QuickLinksDAOJpaImpl implements QuickLinksDAO {
42      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              final List<Object[]> stats = (List<Object[]>) entityManager.createNamedQuery("ActionItem.QuickLinks.FindActionListStatsByPrincipalId").setParameter("principalId", principalId).getResultList();
51              final List<ActionListStats> docTypes = new ArrayList<ActionListStats>(stats.size());
52              for (Object[] res : stats) {
53                  final String docTypeName = (String) res[0];
54                  final Long count = (Long) res[1];
55  
56                  final List<String> docTypeLabel = (List<String>) entityManager.createNamedQuery("DocumentType.QuickLinks.FindLabelByTypeName").setParameter("docTypeName", docTypeName).getResultList();
57                  if (docTypeLabel.size() > 0) {
58                      docTypes.add(new ActionListStats(docTypeName, docTypeLabel.get(0), count.intValue()));
59                  }
60              }
61              Collections.sort(docTypes);
62              return docTypes;
63          } catch (Exception e) {
64              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          String documentNames = Utilities.getKNSParameterValue(KEWConstants.KEW_NAMESPACE, KNSConstants.DetailTypes.QUICK_LINK_DETAIL_TYPE, KEWConstants.QUICK_LINKS_RESTRICT_DOCUMENT_TYPES);
71          if (documentNames != null) {
72              documentNames = documentNames.trim();
73          }
74          if (documentNames == null || "none".equals(documentNames)) {
75              documentNames = "";
76          }
77  
78          final StringTokenizer st = new StringTokenizer(documentNames, ",");
79          final List<String> docTypesToRestrict = new ArrayList<String>();
80          while (st.hasMoreTokens()) {
81              docTypesToRestrict.add(st.nextToken());
82          }
83  
84          try {
85              final List<Object[]> list = (List<Object[]>) entityManager.createNamedQuery("DocumentType.QuickLinks.FindInitiatedDocumentTypesListByInitiatorWorkflowId").setParameter("initiatorWorkflowId", principalId).getResultList();
86              final List<InitiatedDocumentType> documentTypesByName = new ArrayList<InitiatedDocumentType>(list.size());
87              for (Object[] doc : list) {
88                  final String docTypeName = (String) doc[0];
89                  final String label = (String) doc[1];
90  
91                  final String docTypeTopParent;
92                  final int firstPeriod = docTypeName.indexOf(".");
93                  if (firstPeriod == -1) {
94                      docTypeTopParent = docTypeName.substring(0);
95                  } else {
96                      docTypeTopParent = docTypeName.substring(0, firstPeriod);
97                  }
98                  if (!docTypesToRestrict.contains(docTypeTopParent)) {
99                      // the document types should be cached so this should be pretty quick
100                     final DocumentType docType = KEWServiceLocator.getDocumentTypeService().findByName(docTypeName);
101                     final DocumentTypePolicy quickInitiatePolicy = docType.getSupportsQuickInitiatePolicy();
102                     if (quickInitiatePolicy.getPolicyValue().booleanValue()) {
103                         documentTypesByName.add(new InitiatedDocumentType(docTypeName, label));
104                     }
105                 }
106             }
107             return documentTypesByName;
108         } catch (Exception e) {
109             throw new WorkflowRuntimeException("Error getting initiated document types for user: " + principalId, e);
110         }
111     }
112 
113     public List<KeyValue> getNamedSearches(String principalId) {
114         return getDocumentSearchService().getNamedSearches(principalId);
115     }
116 
117     public List<KeyValue> getRecentSearches(String principalId) {
118         return getDocumentSearchService().getMostRecentSearches(principalId);
119     }
120 
121     @SuppressWarnings("unchecked")
122     public List<WatchedDocument> getWatchedDocuments(final String principalId) {
123         try {
124             return (List<WatchedDocument>) entityManager.createNamedQuery("DocumentRouteHeaderValue.QuickLinks.FindWatchedDocumentsByInitiatorWorkflowId").setParameter("initiatorWorkflowId", principalId).getResultList();
125         } catch (Exception e) {
126             throw new WorkflowRuntimeException("Error getting watched documents for user: " + principalId, e);
127         }
128     }
129 
130     public DocumentTypeService getDocumentTypeService() {
131         return ((DocumentTypeService) KEWServiceLocator.getService(KEWServiceLocator.DOCUMENT_TYPE_SERVICE));
132     }
133 
134     public DocumentSearchService getDocumentSearchService() {
135         return ((DocumentSearchService) KEWServiceLocator.getService(KEWServiceLocator.DOCUMENT_SEARCH_SERVICE));
136     }
137 
138     public EntityManager getEntityManager() {
139         return this.entityManager;
140     }
141 
142     public void setEntityManager(EntityManager entityManager) {
143         this.entityManager = entityManager;
144     }
145 }