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