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