1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.kuali.rice.kew.actionrequest.dao.impl;
18
19 import org.apache.commons.lang.StringUtils;
20 import org.kuali.rice.core.util.OrmUtils;
21 import org.kuali.rice.kew.actionrequest.ActionRequestValue;
22 import org.kuali.rice.kew.actionrequest.dao.ActionRequestDAO;
23 import org.kuali.rice.kew.util.KEWConstants;
24 import org.kuali.rice.kim.bo.Group;
25
26 import javax.persistence.EntityManager;
27 import javax.persistence.PersistenceContext;
28 import javax.persistence.Query;
29 import java.io.Serializable;
30 import java.sql.Timestamp;
31 import java.util.Collection;
32 import java.util.Collections;
33 import java.util.List;
34
35
36
37
38
39
40 public class ActionRequestDAOJpaImpl implements ActionRequestDAO {
41
42 @PersistenceContext(name = "kew-unit")
43 private EntityManager entityManager;
44
45
46
47
48 public EntityManager getEntityManager() {
49 return this.entityManager;
50 }
51
52
53
54
55 public void setEntityManager(EntityManager entityManager) {
56 this.entityManager = entityManager;
57 }
58
59 public void delete(Long actionRequestId) {
60 ActionRequestValue actionRequestValue = (ActionRequestValue) entityManager.find(ActionRequestValue.class, actionRequestId);
61 entityManager.remove(actionRequestValue);
62 }
63
64 public void deleteByRouteHeaderId(Long routeHeaderId) {
65
66 Query query = entityManager.createNamedQuery("ActionRequestValue.FindByRouteHeaderId");
67 query.setParameter("routeHeaderId", routeHeaderId);
68 List<ActionRequestValue> actionRequestValues = (List<ActionRequestValue>) query.getSingleResult();
69 for(ActionRequestValue arv : actionRequestValues) {
70 entityManager.remove(arv);
71 }
72 }
73
74 public boolean doesDocumentHaveUserRequest(String principalId, Long documentId) {
75 Query query = entityManager.createNamedQuery("ActionRequestValue.GetUserRequestCount");
76 query.setParameter("principalId", principalId);
77 query.setParameter("routeHeaderId", documentId);
78 query.setParameter("recipientTypeCd", KEWConstants.ACTION_REQUEST_USER_RECIPIENT_CD);
79 query.setParameter("currentIndicator", Boolean.TRUE);
80
81 return ((Long)query.getSingleResult()) > 0;
82 }
83
84 public List<?> findActivatedByGroup(Group group) {
85
86 Query query = entityManager.createNamedQuery("ActionRequestValue.FindActivatedByGroup");
87 query.setParameter("groupId", group.getGroupId());
88 query.setParameter("currentIndicator", Boolean.TRUE);
89 query.setParameter("status", KEWConstants.ACTION_REQUEST_ACTIVATED);
90
91 return query.getResultList();
92 }
93
94 public List findAllByDocId(Long routeHeaderId) {
95 Query query = entityManager.createNamedQuery("ActionRequestValue.FindAllByDocId");
96 query.setParameter("routeHeaderId", routeHeaderId);
97 query.setParameter("currentIndicator", Boolean.TRUE);
98
99 return query.getResultList();
100 }
101
102 public List findAllPendingByDocId(Long routeHeaderId) {
103 Query query = entityManager.createNamedQuery("ActionRequestValue.FindAllPendingByDocId");
104 query.setParameter("routeHeaderId", routeHeaderId);
105 query.setParameter("currentIndicator", Boolean.TRUE);
106
107 return query.getResultList();
108 }
109
110 @SuppressWarnings("unchecked")
111 public List<ActionRequestValue> findAllRootByDocId(Long routeHeaderId) {
112 Query query = entityManager.createNamedQuery("ActionRequestValue.FindAllRootByDocId");
113 query.setParameter("routeHeaderId", routeHeaderId);
114 query.setParameter("currentIndicator", Boolean.TRUE);
115
116 return (List<ActionRequestValue>) query.getResultList();
117 }
118
119 public List findByRouteHeaderIdIgnoreCurrentInd(Long routeHeaderId) {
120 Query query = entityManager.createNamedQuery("ActionRequestValue.FindByRouteHeaderId");
121 query.setParameter("routeHeaderId", routeHeaderId);
122
123 return query.getResultList();
124 }
125
126 @SuppressWarnings("unchecked")
127 public List<ActionRequestValue> findByStatusAndDocId(String statusCd, Long routeHeaderId) {
128 Query query = entityManager.createNamedQuery("ActionRequestValue.FindByStatusAndDocId");
129 query.setParameter("routeHeaderId", routeHeaderId);
130 query.setParameter("status", statusCd);
131 query.setParameter("currentIndicator", Boolean.TRUE);
132
133 return (List<ActionRequestValue>)query.getResultList();
134 }
135
136 public List findPendingByActionRequestedAndDocId(String actionRequestedCd, Long routeHeaderId) {
137 Query query = entityManager.createNamedQuery("ActionRequestValue.FindPendingByActionRequestedAndDocId");
138 query.setParameter("routeHeaderId", routeHeaderId);
139 query.setParameter("currentIndicator", Boolean.TRUE);
140 query.setParameter("actionRequested", actionRequestedCd);
141
142 return query.getResultList();
143 }
144
145 public List findPendingByDocIdAtOrBelowRouteLevel(Long routeHeaderId, Integer routeLevel) {
146 Query query = entityManager.createNamedQuery("ActionRequestValue.FindPendingByDocIdAtOrBelowRouteLevel");
147 query.setParameter("routeHeaderId", routeHeaderId);
148 query.setParameter("currentIndicator", Boolean.TRUE);
149 query.setParameter("routeLevel", routeLevel);
150 query.setParameter("status", KEWConstants.ACTION_REQUEST_DONE_STATE);
151
152 return query.getResultList();
153 }
154
155 public List findPendingByResponsibilityIds(Collection responsibilityIds) {
156 if (responsibilityIds == null || responsibilityIds.size() == 0)
157 return Collections.emptyList();
158
159 Query query = entityManager.createNamedQuery("ActionRequestValue.FindPendingByDocIdAtOrBelowRouteLevel");
160 query.setParameter("responsibilityIds", responsibilityIds);
161
162 return query.getResultList();
163 }
164
165 public List findPendingRootRequestsByDocIdAtOrBelowRouteLevel(Long routeHeaderId, Integer routeLevel) {
166 Query query = entityManager.createNamedQuery("ActionRequestValue.FindPendingRootRequestsByDocIdAtOrBelowRouteLevel");
167 query.setParameter("routeHeaderId", routeHeaderId);
168 query.setParameter("currentIndicator", Boolean.TRUE);
169 query.setParameter("status", KEWConstants.ACTION_REQUEST_DONE_STATE);
170 query.setParameter("routeLevel", routeLevel);
171
172 return query.getResultList();
173 }
174
175 public List findPendingRootRequestsByDocIdAtRouteLevel(Long routeHeaderId, Integer routeLevel) {
176 Query query = entityManager.createNamedQuery("ActionRequestValue.FindPendingRootRequestsByDocIdAtRouteLevel");
177 query.setParameter("routeHeaderId", routeHeaderId);
178 query.setParameter("currentIndicator", Boolean.TRUE);
179 query.setParameter("status", KEWConstants.ACTION_REQUEST_DONE_STATE);
180 query.setParameter("routeLevel", routeLevel);
181
182 return query.getResultList();
183 }
184
185 public List findPendingRootRequestsByDocIdAtRouteNode(Long routeHeaderId, Long nodeInstanceId) {
186 Query query = entityManager.createNamedQuery("ActionRequestValue.FindPendingRootRequestsByDocIdAtRouteNode");
187 query.setParameter("routeHeaderId", routeHeaderId);
188 query.setParameter("currentIndicator", Boolean.TRUE);
189 query.setParameter("routeNodeInstanceId", nodeInstanceId);
190
191 return query.getResultList();
192 }
193
194 public List findPendingRootRequestsByDocumentType(Long documentTypeId) {
195 Query query = entityManager.createNamedQuery("ActionRequestValue.FindPendingRootRequestsByDocumentType");
196 query.setParameter("documentTypeId", documentTypeId);
197 query.setParameter("currentIndicator", Boolean.TRUE);
198
199 return query.getResultList();
200 }
201
202 public List findRootRequestsByDocIdAtRouteNode(Long documentId, Long nodeInstanceId) {
203 Query query = entityManager.createNamedQuery("ActionRequestValue.FindRootRequestsByDocIdAtRouteNode");
204 query.setParameter("routeHeaderId", documentId);
205 query.setParameter("currentIndicator", Boolean.TRUE);
206 query.setParameter("routeNodeInstanceId", nodeInstanceId);
207
208 return query.getResultList();
209 }
210
211 public ActionRequestValue getActionRequestByActionRequestId(Long actionRequestId) {
212 return entityManager.find(ActionRequestValue.class, actionRequestId);
213 }
214
215 @SuppressWarnings("unchecked")
216 public List<String> getRequestGroupIds(Long documentId) {
217 Query query = entityManager.createNamedQuery("ActionRequestValue.GetRequestGroupIds");
218 query.setParameter("routeHeaderId", documentId);
219 query.setParameter("currentIndicator", Boolean.TRUE);
220 query.setParameter("recipientTypeCd", KEWConstants.ACTION_REQUEST_GROUP_RECIPIENT_CD);
221
222 return query.getResultList();
223 }
224
225 public void saveActionRequest(ActionRequestValue actionRequest) {
226 if ( actionRequest.getAnnotation() != null && actionRequest.getAnnotation().length() > 2000 ) {
227 actionRequest.setAnnotation( StringUtils.abbreviate(actionRequest.getAnnotation(), 2000) );
228 }
229 if(actionRequest.getActionRequestId() == null) {
230 loadDefaultValues(actionRequest);
231 entityManager.persist(actionRequest);
232 }else{
233 OrmUtils.merge(entityManager, actionRequest);
234 }
235 }
236 private void loadDefaultValues(ActionRequestValue actionRequest) {
237 checkNull(actionRequest.getActionRequested(), "action requested");
238 checkNull(actionRequest.getResponsibilityId(), "responsibility ID");
239 checkNull(actionRequest.getRouteLevel(), "route level");
240 checkNull(actionRequest.getDocVersion(), "doc version");
241 if (actionRequest.getForceAction() == null) {
242 actionRequest.setForceAction(Boolean.FALSE);
243 }
244 if (actionRequest.getStatus() == null) {
245 actionRequest.setStatus(KEWConstants.ACTION_REQUEST_INITIALIZED);
246 }
247 if (actionRequest.getPriority() == null) {
248 actionRequest.setPriority(KEWConstants.ACTION_REQUEST_DEFAULT_PRIORITY);
249 }
250 if (actionRequest.getCurrentIndicator() == null) {
251 actionRequest.setCurrentIndicator(true);
252 }
253 actionRequest.setCreateDate(new Timestamp(System.currentTimeMillis()));
254 }
255
256 private void checkNull(Serializable value, String valueName) throws RuntimeException {
257 if (value == null) {
258 throw new RuntimeException("Null value for " + valueName);
259 }
260 }
261
262 public List findActivatedByGroup(String groupId) {
263 Query query = entityManager.createNamedQuery("ActionRequestValue.FindByStatusAndGroupId");
264 query.setParameter("status", KEWConstants.ACTION_REQUEST_ACTIVATED);
265 query.setParameter("currentIndicator", Boolean.TRUE);
266 query.setParameter("groupId", groupId);
267
268 return query.getResultList();
269 }
270
271 }