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