001 /*
002 * Copyright 2009 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.rice.kew.quicklinks.dao.impl;
017
018 import java.util.ArrayList;
019 import java.util.Collections;
020 import java.util.List;
021 import java.util.StringTokenizer;
022
023 import javax.persistence.EntityManager;
024 import javax.persistence.PersistenceContext;
025
026 import org.kuali.rice.kew.docsearch.service.DocumentSearchService;
027 import org.kuali.rice.kew.doctype.DocumentTypePolicy;
028 import org.kuali.rice.kew.doctype.bo.DocumentType;
029 import org.kuali.rice.kew.doctype.service.DocumentTypeService;
030 import org.kuali.rice.kew.exception.WorkflowRuntimeException;
031 import org.kuali.rice.kew.quicklinks.ActionListStats;
032 import org.kuali.rice.kew.quicklinks.InitiatedDocumentType;
033 import org.kuali.rice.kew.quicklinks.WatchedDocument;
034 import org.kuali.rice.kew.quicklinks.dao.QuickLinksDAO;
035 import org.kuali.rice.kew.service.KEWServiceLocator;
036 import org.kuali.rice.kew.util.KEWConstants;
037 import org.kuali.rice.kew.util.Utilities;
038 import org.kuali.rice.kew.web.KeyValue;
039 import org.kuali.rice.kns.util.KNSConstants;
040
041 public class QuickLinksDAOJpaImpl implements QuickLinksDAO {
042 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(QuickLinksDAOJpaImpl.class);
043
044 @PersistenceContext(unitName = "kew-unit")
045 private EntityManager entityManager;
046
047 @SuppressWarnings("unchecked")
048 public List<ActionListStats> getActionListStats(final String principalId) {
049 try {
050 final List<Object[]> stats = (List<Object[]>) entityManager.createNamedQuery("ActionItem.QuickLinks.FindActionListStatsByPrincipalId").setParameter("principalId", principalId).getResultList();
051 final List<ActionListStats> docTypes = new ArrayList<ActionListStats>(stats.size());
052 for (Object[] res : stats) {
053 final String docTypeName = (String) res[0];
054 final Long count = (Long) res[1];
055
056 final List<String> docTypeLabel = (List<String>) entityManager.createNamedQuery("DocumentType.QuickLinks.FindLabelByTypeName").setParameter("docTypeName", docTypeName).getResultList();
057 if (docTypeLabel.size() > 0) {
058 docTypes.add(new ActionListStats(docTypeName, docTypeLabel.get(0), count.intValue()));
059 }
060 }
061 Collections.sort(docTypes);
062 return docTypes;
063 } catch (Exception e) {
064 throw new WorkflowRuntimeException("Error getting action list stats for user: " + principalId, e);
065 }
066 }
067
068 @SuppressWarnings("unchecked")
069 public List<InitiatedDocumentType> getInitiatedDocumentTypesList(final String principalId) {
070 String documentNames = Utilities.getKNSParameterValue(KEWConstants.KEW_NAMESPACE, KNSConstants.DetailTypes.QUICK_LINK_DETAIL_TYPE, KEWConstants.QUICK_LINKS_RESTRICT_DOCUMENT_TYPES);
071 if (documentNames != null) {
072 documentNames = documentNames.trim();
073 }
074 if (documentNames == null || "none".equals(documentNames)) {
075 documentNames = "";
076 }
077
078 final StringTokenizer st = new StringTokenizer(documentNames, ",");
079 final List<String> docTypesToRestrict = new ArrayList<String>();
080 while (st.hasMoreTokens()) {
081 docTypesToRestrict.add(st.nextToken());
082 }
083
084 try {
085 final List<Object[]> list = (List<Object[]>) entityManager.createNamedQuery("DocumentType.QuickLinks.FindInitiatedDocumentTypesListByInitiatorWorkflowId").setParameter("initiatorWorkflowId", principalId).getResultList();
086 final List<InitiatedDocumentType> documentTypesByName = new ArrayList<InitiatedDocumentType>(list.size());
087 for (Object[] doc : list) {
088 final String docTypeName = (String) doc[0];
089 final String label = (String) doc[1];
090
091 final String docTypeTopParent;
092 final int firstPeriod = docTypeName.indexOf(".");
093 if (firstPeriod == -1) {
094 docTypeTopParent = docTypeName.substring(0);
095 } else {
096 docTypeTopParent = docTypeName.substring(0, firstPeriod);
097 }
098 if (!docTypesToRestrict.contains(docTypeTopParent)) {
099 // 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 }