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