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