001 /**
002 * Copyright 2005-2012 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 org.kuali.rice.core.api.delegation.DelegationType;
019 import org.kuali.rice.core.api.util.KeyValue;
020 import org.kuali.rice.coreservice.framework.CoreFrameworkServiceLocator;
021 import org.kuali.rice.kew.api.WorkflowRuntimeException;
022 import org.kuali.rice.kew.docsearch.service.DocumentSearchService;
023 import org.kuali.rice.kew.doctype.DocumentTypePolicy;
024 import org.kuali.rice.kew.doctype.bo.DocumentType;
025 import org.kuali.rice.kew.doctype.service.DocumentTypeService;
026 import org.kuali.rice.kew.quicklinks.ActionListStats;
027 import org.kuali.rice.kew.quicklinks.InitiatedDocumentType;
028 import org.kuali.rice.kew.quicklinks.WatchedDocument;
029 import org.kuali.rice.kew.quicklinks.dao.QuickLinksDAO;
030 import org.kuali.rice.kew.service.KEWServiceLocator;
031 import org.kuali.rice.kew.api.KewApiConstants;
032 import org.kuali.rice.krad.util.KRADConstants;
033
034 import javax.persistence.EntityManager;
035 import javax.persistence.PersistenceContext;
036 import java.util.ArrayList;
037 import java.util.Collections;
038 import java.util.List;
039 import java.util.StringTokenizer;
040
041 public class QuickLinksDAOJpaImpl implements QuickLinksDAO {
042
043 @PersistenceContext(unitName = "kew-unit")
044 private EntityManager entityManager;
045
046 @Override
047 @SuppressWarnings("unchecked")
048 public List<ActionListStats> getActionListStats(final String principalId) {
049 try {
050 final List<Object[]> stats = entityManager.createNamedQuery("ActionItem.QuickLinks.FindActionListStatsByPrincipalId").setParameter("principalId", principalId).setParameter("delegationType", DelegationType
051 .SECONDARY.getCode()).getResultList();
052 final List<ActionListStats> docTypes = new ArrayList<ActionListStats>(stats.size());
053 for (Object[] res : stats) {
054 final String docTypeName = (String) res[0];
055 final Long count = (Long) res[1];
056
057 final List<String> docTypeLabel = entityManager.createNamedQuery("DocumentType.QuickLinks.FindLabelByTypeName").setParameter("docTypeName", docTypeName).getResultList();
058 if (docTypeLabel.size() > 0) {
059 docTypes.add(new ActionListStats(docTypeName, docTypeLabel.get(0), count.intValue()));
060 }
061 }
062 Collections.sort(docTypes);
063 return docTypes;
064 } catch (Exception e) {
065 throw new WorkflowRuntimeException("Error getting action list stats for user: " + principalId, e);
066 }
067 }
068
069 @Override
070 @SuppressWarnings("unchecked")
071 public List<InitiatedDocumentType> getInitiatedDocumentTypesList(final String principalId) {
072 String documentNames = CoreFrameworkServiceLocator.getParameterService().getParameterValueAsString(KewApiConstants.KEW_NAMESPACE, KRADConstants.DetailTypes.QUICK_LINK_DETAIL_TYPE, KewApiConstants.QUICK_LINKS_RESTRICT_DOCUMENT_TYPES);
073 if (documentNames != null) {
074 documentNames = documentNames.trim();
075 }
076 if (documentNames == null || "none".equals(documentNames)) {
077 documentNames = "";
078 }
079
080 final StringTokenizer st = new StringTokenizer(documentNames, ",");
081 final List<String> docTypesToRestrict = new ArrayList<String>();
082 while (st.hasMoreTokens()) {
083 docTypesToRestrict.add(st.nextToken());
084 }
085
086 try {
087 final List<Object[]> list = entityManager.createNamedQuery("DocumentType.QuickLinks.FindInitiatedDocumentTypesListByInitiatorWorkflowId").setParameter("initiatorWorkflowId", principalId).getResultList();
088 final List<InitiatedDocumentType> documentTypesByName = new ArrayList<InitiatedDocumentType>(list.size());
089 for (Object[] doc : list) {
090 final String docTypeName = (String) doc[0];
091 final String label = (String) doc[1];
092
093 final String docTypeTopParent;
094 final int firstPeriod = docTypeName.indexOf(".");
095 if (firstPeriod == -1) {
096 docTypeTopParent = docTypeName.substring(0);
097 } else {
098 docTypeTopParent = docTypeName.substring(0, firstPeriod);
099 }
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 }