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