1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.kew.routeheader.dao.impl; |
18 | |
|
19 | |
import java.math.BigDecimal; |
20 | |
import java.util.ArrayList; |
21 | |
import java.util.Collection; |
22 | |
import java.util.Iterator; |
23 | |
import java.util.List; |
24 | |
import java.util.Set; |
25 | |
|
26 | |
import javax.persistence.EntityManager; |
27 | |
import javax.persistence.EntityNotFoundException; |
28 | |
import javax.persistence.PersistenceContext; |
29 | |
import javax.persistence.Query; |
30 | |
|
31 | |
import org.apache.commons.lang.StringUtils; |
32 | |
import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader; |
33 | |
import org.kuali.rice.core.framework.persistence.jpa.OrmUtils; |
34 | |
import org.kuali.rice.core.framework.persistence.jpa.criteria.Criteria; |
35 | |
import org.kuali.rice.core.framework.persistence.jpa.criteria.QueryByCriteria; |
36 | |
import org.kuali.rice.core.framework.persistence.platform.DatabasePlatform; |
37 | |
import org.kuali.rice.core.util.RiceConstants; |
38 | |
import org.kuali.rice.kew.actionitem.ActionItem; |
39 | |
import org.kuali.rice.kew.actionlist.service.ActionListService; |
40 | |
import org.kuali.rice.kew.api.WorkflowRuntimeException; |
41 | |
import org.kuali.rice.kew.api.action.ActionRequestStatus; |
42 | |
import org.kuali.rice.kew.docsearch.SearchableAttributeValue; |
43 | |
import org.kuali.rice.kew.exception.LockingException; |
44 | |
import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue; |
45 | |
import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValueContent; |
46 | |
import org.kuali.rice.kew.routeheader.dao.DocumentRouteHeaderDAO; |
47 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
48 | |
|
49 | |
|
50 | 0 | public class DocumentRouteHeaderDAOJpaImpl implements DocumentRouteHeaderDAO { |
51 | |
|
52 | |
@PersistenceContext(unitName="kew-unit") |
53 | |
private EntityManager entityManager; |
54 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DocumentRouteHeaderDAOJpaImpl.class); |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
public EntityManager getEntityManager() { |
61 | 0 | return this.entityManager; |
62 | |
} |
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
public void setEntityManager(EntityManager entityManager) { |
68 | 0 | this.entityManager = entityManager; |
69 | 0 | } |
70 | |
|
71 | |
public void saveRouteHeader(DocumentRouteHeaderValue routeHeader) { |
72 | 0 | DocumentRouteHeaderValueContent documentContent = routeHeader.getDocumentContent(); |
73 | |
|
74 | |
|
75 | 0 | if (routeHeader.getDocumentId() == null){ |
76 | 0 | entityManager.persist(routeHeader); |
77 | |
} else { |
78 | 0 | OrmUtils.merge(entityManager, routeHeader); |
79 | |
} |
80 | |
|
81 | |
|
82 | 0 | documentContent.setDocumentId(routeHeader.getDocumentId()); |
83 | 0 | entityManager.merge(documentContent); |
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | 0 | } |
97 | |
|
98 | |
public DocumentRouteHeaderValueContent getContent(String documentId) { |
99 | 0 | Query query = entityManager.createNamedQuery("DocumentRouteHeaderValueContent.FindByDocumentId"); |
100 | 0 | query.setParameter("documentId", documentId); |
101 | 0 | return (DocumentRouteHeaderValueContent)query.getSingleResult(); |
102 | |
} |
103 | |
|
104 | |
public void clearRouteHeaderSearchValues(String documentId) { |
105 | 0 | List<SearchableAttributeValue> searchableAttributeValues = findSearchableAttributeValues(documentId); |
106 | 0 | for (SearchableAttributeValue searchableAttributeValue:searchableAttributeValues){ |
107 | 0 | entityManager.remove(searchableAttributeValue); |
108 | |
} |
109 | 0 | } |
110 | |
|
111 | |
private List<SearchableAttributeValue> findSearchableAttributeValues(String documentId){ |
112 | 0 | List<SearchableAttributeValue> searchableAttributeValues = new ArrayList<SearchableAttributeValue>(); |
113 | |
|
114 | 0 | for (int i=1;i<=4; i++){ |
115 | 0 | String namedQuery = ""; |
116 | 0 | switch (i) { |
117 | 0 | case 1: namedQuery = "SearchableAttributeFloatValue.FindByDocumentId"; break; |
118 | 0 | case 2: namedQuery = "SearchableAttributeDateTimeValue.FindByDocumentId"; break; |
119 | 0 | case 3: namedQuery = "SearchableAttributeLongValue.FindByDocumentId";break; |
120 | 0 | case 4: namedQuery = "SearchableAttributeStringValue.FindByDocumentId"; break; |
121 | |
} |
122 | 0 | Query query = entityManager.createNamedQuery(namedQuery); |
123 | 0 | query.setParameter("documentId", documentId); |
124 | 0 | searchableAttributeValues.addAll(query.getResultList()); |
125 | |
} |
126 | |
|
127 | 0 | return searchableAttributeValues; |
128 | |
} |
129 | |
|
130 | |
public void lockRouteHeader(final String documentId, final boolean wait) { |
131 | 0 | String sql = getPlatform().getLockRouteHeaderQuerySQL(documentId, wait); |
132 | |
try{ |
133 | 0 | Query query = entityManager.createNativeQuery(sql); |
134 | 0 | query.setParameter(1, documentId); |
135 | 0 | query.getSingleResult(); |
136 | 0 | } catch (Exception e){ |
137 | |
|
138 | 0 | throw new LockingException("Could not aquire lock on document, documentId=" + documentId, e); |
139 | 0 | } |
140 | 0 | } |
141 | |
|
142 | |
public DocumentRouteHeaderValue findRouteHeader(String documentId) { |
143 | 0 | return findRouteHeader(documentId, false); |
144 | |
} |
145 | |
|
146 | |
public DocumentRouteHeaderValue findRouteHeader(String documentId, boolean clearCache) { |
147 | 0 | Query query = entityManager.createNamedQuery("DocumentRouteHeaderValue.FindByDocumentId"); |
148 | 0 | query.setParameter("documentId", documentId); |
149 | |
|
150 | |
|
151 | 0 | if (clearCache) { |
152 | |
|
153 | |
} |
154 | |
|
155 | 0 | DocumentRouteHeaderValue routeHeader = (DocumentRouteHeaderValue) query.getSingleResult(); |
156 | |
|
157 | 0 | return routeHeader; |
158 | |
} |
159 | |
|
160 | |
public Collection<DocumentRouteHeaderValue> findRouteHeaders(Collection<String> documentIds) { |
161 | 0 | return findRouteHeaders(documentIds, false); |
162 | |
} |
163 | |
|
164 | |
public Collection<DocumentRouteHeaderValue> findRouteHeaders(Collection<String> documentIds, boolean clearCache) { |
165 | 0 | if (documentIds == null || documentIds.isEmpty()) { |
166 | 0 | return null; |
167 | |
} |
168 | 0 | Criteria crit = new Criteria(DocumentRouteHeaderValue.class.getName()); |
169 | 0 | crit.in("documentId", documentIds); |
170 | |
|
171 | |
|
172 | 0 | if (clearCache) { |
173 | |
|
174 | |
} |
175 | |
|
176 | 0 | return new QueryByCriteria(entityManager, crit).toQuery().getResultList(); |
177 | |
} |
178 | |
|
179 | |
public void deleteRouteHeader(DocumentRouteHeaderValue routeHeader) { |
180 | |
|
181 | 0 | ActionListService actionListSrv = KEWServiceLocator.getActionListService(); |
182 | 0 | Collection actionItems = actionListSrv.findByDocumentId(routeHeader.getDocumentId()); |
183 | 0 | for (Iterator iter = actionItems.iterator(); iter.hasNext();) { |
184 | 0 | ActionItem actionItem = (ActionItem) iter.next(); |
185 | |
try { |
186 | 0 | KEWServiceLocator.getUserOptionsService().saveRefreshUserOption(actionItem.getPrincipalId()); |
187 | 0 | } catch (Exception e) { |
188 | 0 | LOG.error("error saving refreshUserOption", e); |
189 | 0 | } |
190 | 0 | } |
191 | |
|
192 | 0 | DocumentRouteHeaderValue attachedRouteHeader = findRouteHeader(routeHeader.getDocumentId()); |
193 | 0 | entityManager.remove(attachedRouteHeader); |
194 | 0 | } |
195 | |
|
196 | |
public String getNextDocumentId() { |
197 | 0 | Long nextDocumentId = getPlatform().getNextValSQL("KREW_DOC_HDR_S", entityManager); |
198 | 0 | return nextDocumentId.toString(); |
199 | |
} |
200 | |
|
201 | |
protected DatabasePlatform getPlatform() { |
202 | 0 | return (DatabasePlatform) GlobalResourceLoader.getService(RiceConstants.DB_PLATFORM); |
203 | |
} |
204 | |
|
205 | |
public Collection findPendingByResponsibilityIds(Set responsibilityIds) { |
206 | |
|
207 | 0 | if (responsibilityIds.isEmpty()) { |
208 | 0 | return new ArrayList(); |
209 | |
} |
210 | |
|
211 | 0 | String respIds = "("; |
212 | 0 | int index = 0; |
213 | 0 | for (Iterator iterator = responsibilityIds.iterator(); iterator.hasNext(); index++) { |
214 | 0 | Long responsibilityId = (Long) iterator.next(); |
215 | 0 | respIds += responsibilityId + (index == responsibilityIds.size()-1 ? "" : ","); |
216 | |
} |
217 | 0 | respIds += ")"; |
218 | |
|
219 | 0 | String query = "SELECT DISTINCT(doc_hdr_id) FROM KREW_ACTN_RQST_T "+ |
220 | |
"WHERE (STAT_CD='" + |
221 | |
ActionRequestStatus.INITIALIZED.getCode()+ |
222 | |
"' OR STAT_CD='"+ |
223 | |
ActionRequestStatus.ACTIVATED.getCode()+ |
224 | |
"') AND RSP_ID IN "+respIds; |
225 | |
|
226 | 0 | LOG.debug("Query to find pending documents for requeue: " + query); |
227 | |
|
228 | 0 | List<Long> idList = new ArrayList<Long>(); |
229 | 0 | for (Object tempId : entityManager.createNativeQuery(query).getResultList()) { |
230 | 0 | idList.add(((BigDecimal) tempId).longValueExact()); |
231 | |
} |
232 | |
|
233 | 0 | return idList; |
234 | |
} |
235 | |
|
236 | |
public boolean hasSearchableAttributeValue(String documentId, String searchableAttributeKey, String searchableAttributeValue) { |
237 | 0 | return hasSearchableAttributeValue(documentId, searchableAttributeKey, searchableAttributeValue, "SearchableAttributeDateTimeValue.FindByKey") |
238 | |
|| hasSearchableAttributeValue(documentId, searchableAttributeKey, searchableAttributeValue, "SearchableAttributeStringValue.FindByKey") |
239 | |
|| hasSearchableAttributeValue(documentId, searchableAttributeKey, searchableAttributeValue, "SearchableAttributeLongValue.FindByKey") |
240 | |
|| hasSearchableAttributeValue(documentId, searchableAttributeKey, searchableAttributeValue, "SearchableAttributeFloatValue.FindByKey"); |
241 | |
} |
242 | |
|
243 | |
private boolean hasSearchableAttributeValue(String documentId, String searchableAttributeKey, String searchableAttributeValue, String namedQuery) { |
244 | 0 | Query query = entityManager.createNamedQuery(namedQuery); |
245 | 0 | query.setParameter("documentId", documentId); |
246 | 0 | query.setParameter("searchableAttributeKey", searchableAttributeKey); |
247 | 0 | Collection results = query.getResultList(); |
248 | 0 | if (!results.isEmpty()) { |
249 | 0 | for (Iterator iterator = results.iterator(); iterator.hasNext();) { |
250 | 0 | SearchableAttributeValue attribute = (SearchableAttributeValue) iterator.next(); |
251 | 0 | if (StringUtils.equals(attribute.getSearchableAttributeDisplayValue(), searchableAttributeValue)) { |
252 | 0 | return true; |
253 | |
} |
254 | 0 | } |
255 | |
} |
256 | 0 | return false; |
257 | |
} |
258 | |
|
259 | |
public String getApplicationIdByDocumentId(String documentId) { |
260 | 0 | if (documentId == null) { |
261 | 0 | throw new IllegalArgumentException("Encountered a null document ID."); |
262 | |
} |
263 | |
|
264 | 0 | String applicationId = null; |
265 | |
|
266 | |
try { |
267 | 0 | String sql = "SELECT DT.APPL_ID FROM KREW_DOC_TYP_T DT, KREW_DOC_HDR_T DH "+ |
268 | |
"WHERE DH.DOC_TYP_ID=DT.DOC_TYP_ID AND "+ |
269 | |
"DH.DOC_HDR_ID=?"; |
270 | |
|
271 | 0 | Query query = entityManager.createNativeQuery(sql); |
272 | 0 | query.setParameter(1, documentId); |
273 | |
|
274 | 0 | applicationId = (String)query.getSingleResult(); |
275 | 0 | } catch (EntityNotFoundException enfe) { |
276 | 0 | throw new WorkflowRuntimeException(enfe.getMessage()); |
277 | 0 | } |
278 | |
|
279 | 0 | return applicationId; |
280 | |
} |
281 | |
|
282 | |
public String getDocumentStatus(String documentId) { |
283 | 0 | DocumentRouteHeaderValue document = findRouteHeader(documentId); |
284 | |
|
285 | 0 | return document.getDocRouteStatus(); |
286 | |
} |
287 | |
|
288 | |
public String getAppDocId(String documentId) { |
289 | 0 | Query query = entityManager.createNamedQuery("DocumentRouteHeaderValue.GetAppDocId"); |
290 | 0 | query.setParameter("documentId", documentId); |
291 | 0 | return (String) query.getSingleResult(); |
292 | |
} |
293 | |
|
294 | |
public void save(SearchableAttributeValue searchableAttributeValue) { |
295 | 0 | if (searchableAttributeValue.getSearchableAttributeValueId() == null){ |
296 | 0 | entityManager.persist(searchableAttributeValue); |
297 | |
} else { |
298 | 0 | entityManager.merge(searchableAttributeValue); |
299 | |
} |
300 | 0 | } |
301 | |
|
302 | |
public Collection findByDocTypeAndAppId(String documentTypeName, |
303 | |
String appId) { |
304 | |
try { |
305 | 0 | String sql = |
306 | |
"SELECT DISTINCT " + |
307 | |
" (docHdr.doc_hdr_id) " + |
308 | |
"FROM " + |
309 | |
" KREW_DOC_HDR_T docHdr, " + |
310 | |
" KREW_DOC_TYP_T docTyp " + |
311 | |
"WHERE " + |
312 | |
" docHdr.APP_DOC_ID = ? " + |
313 | |
" AND docHdr.DOC_TYP_ID = docTyp.DOC_TYP_ID " + |
314 | |
" AND docTyp.DOC_TYP_NM = ?"; |
315 | |
|
316 | 0 | Query query = entityManager.createNativeQuery(sql); |
317 | 0 | query.setParameter(1, appId); |
318 | 0 | query.setParameter(2, documentTypeName); |
319 | 0 | Collection<Long> idCollection = new ArrayList<Long>(); |
320 | 0 | for (Object tempId : query.getResultList()) { |
321 | 0 | idCollection.add(((BigDecimal)tempId).longValueExact()); |
322 | |
} |
323 | 0 | return idCollection; |
324 | 0 | } catch (EntityNotFoundException enfe) { |
325 | 0 | throw new WorkflowRuntimeException(enfe.getMessage()); |
326 | |
} |
327 | |
} |
328 | |
|
329 | |
|
330 | |
} |