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