1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
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.core.framework.services.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 | 0 | 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 | 0 | final List<Object[]> stats = entityManager.createNamedQuery("ActionItem.QuickLinks.FindActionListStatsByPrincipalId").setParameter("principalId", principalId).setParameter("delegationType", DelegationType |
51 | |
.SECONDARY.getCode()).getResultList(); |
52 | 0 | final List<ActionListStats> docTypes = new ArrayList<ActionListStats>(stats.size()); |
53 | 0 | for (Object[] res : stats) { |
54 | 0 | final String docTypeName = (String) res[0]; |
55 | 0 | final Long count = (Long) res[1]; |
56 | |
|
57 | 0 | final List<String> docTypeLabel = entityManager.createNamedQuery("DocumentType.QuickLinks.FindLabelByTypeName").setParameter("docTypeName", docTypeName).getResultList(); |
58 | 0 | if (docTypeLabel.size() > 0) { |
59 | 0 | docTypes.add(new ActionListStats(docTypeName, docTypeLabel.get(0), count.intValue())); |
60 | |
} |
61 | 0 | } |
62 | 0 | Collections.sort(docTypes); |
63 | 0 | return docTypes; |
64 | 0 | } catch (Exception e) { |
65 | 0 | 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 | 0 | String documentNames = CoreFrameworkServiceLocator.getParameterService().getParameterValueAsString(KewApiConstants.KEW_NAMESPACE, KRADConstants.DetailTypes.QUICK_LINK_DETAIL_TYPE, KewApiConstants.QUICK_LINKS_RESTRICT_DOCUMENT_TYPES); |
73 | 0 | if (documentNames != null) { |
74 | 0 | documentNames = documentNames.trim(); |
75 | |
} |
76 | 0 | if (documentNames == null || "none".equals(documentNames)) { |
77 | 0 | documentNames = ""; |
78 | |
} |
79 | |
|
80 | 0 | final StringTokenizer st = new StringTokenizer(documentNames, ","); |
81 | 0 | final List<String> docTypesToRestrict = new ArrayList<String>(); |
82 | 0 | while (st.hasMoreTokens()) { |
83 | 0 | docTypesToRestrict.add(st.nextToken()); |
84 | |
} |
85 | |
|
86 | |
try { |
87 | 0 | final List<Object[]> list = entityManager.createNamedQuery("DocumentType.QuickLinks.FindInitiatedDocumentTypesListByInitiatorWorkflowId").setParameter("initiatorWorkflowId", principalId).getResultList(); |
88 | 0 | final List<InitiatedDocumentType> documentTypesByName = new ArrayList<InitiatedDocumentType>(list.size()); |
89 | 0 | for (Object[] doc : list) { |
90 | 0 | final String docTypeName = (String) doc[0]; |
91 | 0 | final String label = (String) doc[1]; |
92 | |
|
93 | |
final String docTypeTopParent; |
94 | 0 | final int firstPeriod = docTypeName.indexOf("."); |
95 | 0 | if (firstPeriod == -1) { |
96 | 0 | docTypeTopParent = docTypeName.substring(0); |
97 | |
} else { |
98 | 0 | docTypeTopParent = docTypeName.substring(0, firstPeriod); |
99 | |
} |
100 | 0 | if (!docTypesToRestrict.contains(docTypeTopParent)) { |
101 | |
|
102 | 0 | final DocumentType docType = KEWServiceLocator.getDocumentTypeService().findByName(docTypeName); |
103 | 0 | final DocumentTypePolicy quickInitiatePolicy = docType.getSupportsQuickInitiatePolicy(); |
104 | 0 | if (quickInitiatePolicy.getPolicyValue().booleanValue()) { |
105 | 0 | documentTypesByName.add(new InitiatedDocumentType(docTypeName, label)); |
106 | |
} |
107 | |
} |
108 | 0 | } |
109 | 0 | return documentTypesByName; |
110 | 0 | } catch (Exception e) { |
111 | 0 | 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 | 0 | return getDocumentSearchService().getNamedSearches(principalId); |
118 | |
} |
119 | |
|
120 | |
@Override |
121 | |
public List<KeyValue> getRecentSearches(String principalId) { |
122 | 0 | return getDocumentSearchService().getMostRecentSearches(principalId); |
123 | |
} |
124 | |
|
125 | |
@Override |
126 | |
@SuppressWarnings("unchecked") |
127 | |
public List<WatchedDocument> getWatchedDocuments(final String principalId) { |
128 | |
try { |
129 | 0 | return entityManager.createNamedQuery("DocumentRouteHeaderValue.QuickLinks.FindWatchedDocumentsByInitiatorWorkflowId").setParameter("initiatorWorkflowId", principalId).getResultList(); |
130 | 0 | } catch (Exception e) { |
131 | 0 | throw new WorkflowRuntimeException("Error getting watched documents for user: " + principalId, e); |
132 | |
} |
133 | |
} |
134 | |
|
135 | |
public DocumentTypeService getDocumentTypeService() { |
136 | 0 | return ((DocumentTypeService) KEWServiceLocator.getService(KEWServiceLocator.DOCUMENT_TYPE_SERVICE)); |
137 | |
} |
138 | |
|
139 | |
public DocumentSearchService getDocumentSearchService() { |
140 | 0 | return ((DocumentSearchService) KEWServiceLocator.getService(KEWServiceLocator.DOCUMENT_SEARCH_SERVICE)); |
141 | |
} |
142 | |
|
143 | |
public EntityManager getEntityManager() { |
144 | 0 | return this.entityManager; |
145 | |
} |
146 | |
|
147 | |
public void setEntityManager(EntityManager entityManager) { |
148 | 0 | this.entityManager = entityManager; |
149 | 0 | } |
150 | |
} |