1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.kew.doctype.dao.impl; |
17 | |
|
18 | |
import java.util.ArrayList; |
19 | |
import java.util.Collection; |
20 | |
import java.util.Iterator; |
21 | |
import java.util.List; |
22 | |
|
23 | |
import javax.persistence.EntityManager; |
24 | |
import javax.persistence.PersistenceContext; |
25 | |
import javax.persistence.Query; |
26 | |
|
27 | |
import org.apache.log4j.Logger; |
28 | |
import org.kuali.rice.core.framework.persistence.jpa.OrmUtils; |
29 | |
import org.kuali.rice.core.framework.persistence.jpa.criteria.Criteria; |
30 | |
import org.kuali.rice.core.framework.persistence.jpa.criteria.QueryByCriteria; |
31 | |
import org.kuali.rice.kew.doctype.DocumentTypeAttribute; |
32 | |
import org.kuali.rice.kew.doctype.bo.DocumentType; |
33 | |
import org.kuali.rice.kew.doctype.dao.DocumentTypeDAO; |
34 | |
import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue; |
35 | |
import org.kuali.rice.kew.rule.bo.RuleAttribute; |
36 | |
|
37 | |
|
38 | 0 | public class DocumentTypeDAOJpaImpl implements DocumentTypeDAO { |
39 | |
|
40 | 0 | public static final Logger LOG = Logger.getLogger(DocumentTypeDAOJpaImpl.class); |
41 | |
|
42 | |
@PersistenceContext(unitName="kew-unit") |
43 | |
private EntityManager entityManager; |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
public EntityManager getEntityManager() { |
50 | 0 | return this.entityManager; |
51 | |
} |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
public void setEntityManager(EntityManager entityManager) { |
57 | 0 | this.entityManager = entityManager; |
58 | 0 | } |
59 | |
|
60 | |
public void delete(DocumentType documentType) { |
61 | 0 | DocumentType docType = findById(documentType.getDocumentTypeId()); |
62 | 0 | entityManager.remove(documentType); |
63 | 0 | } |
64 | |
|
65 | |
public DocumentType findById(String docTypeId) { |
66 | 0 | Criteria crit = new Criteria(DocumentType.class.getName()); |
67 | 0 | crit.eq("documentTypeId", docTypeId); |
68 | 0 | return (DocumentType) new QueryByCriteria(entityManager, crit).toQuery().getSingleResult(); |
69 | |
} |
70 | |
|
71 | |
public DocumentType findByName(String name){ |
72 | 0 | return findByName(name, true); |
73 | |
} |
74 | |
|
75 | |
public DocumentType findByName(String name, boolean caseSensitive) { |
76 | 0 | Criteria crit = new Criteria(DocumentType.class.getName()); |
77 | 0 | if(!caseSensitive){ |
78 | 0 | crit.like("UPPER(__JPA_ALIAS[[0]]__.name)", ("%" + name.trim() + "%").toUpperCase()); |
79 | |
|
80 | |
}else{ |
81 | 0 | crit.eq("name", name); |
82 | |
} |
83 | 0 | crit.eq("currentInd", Boolean.TRUE); |
84 | 0 | DocumentType docType = (DocumentType) new QueryByCriteria(entityManager, crit).toQuery().getSingleResult(); |
85 | 0 | return docType; |
86 | |
} |
87 | |
|
88 | |
public Integer getMaxVersionNumber(String docTypeName) { |
89 | 0 | return getMostRecentDocType(docTypeName).getVersion(); |
90 | |
} |
91 | |
|
92 | |
public List<String> getChildDocumentTypeIds(String parentDocumentTypeId) { |
93 | 0 | List<String> childrenIds = new ArrayList<String>(); |
94 | |
try { |
95 | 0 | String sql = "select DOC_TYP_ID from KREW_DOC_TYP_T where CUR_IND = 1 and PARNT_ID = " + parentDocumentTypeId; |
96 | 0 | Query query = entityManager.createNativeQuery(sql); |
97 | 0 | List resultIds = query.getResultList(); |
98 | 0 | for (Object id : resultIds){ |
99 | 0 | childrenIds.add(id.toString()); |
100 | |
} |
101 | |
|
102 | 0 | } catch (Exception e) { |
103 | 0 | LOG.error("Error occured fetching children document type ids for document type " + parentDocumentTypeId, e); |
104 | 0 | throw new RuntimeException(e); |
105 | 0 | } |
106 | |
|
107 | 0 | return childrenIds; |
108 | |
} |
109 | |
|
110 | |
protected DocumentType getMostRecentDocType(String docTypeName) { |
111 | 0 | Criteria crit = new Criteria(DocumentType.class.getName()); |
112 | 0 | crit.eq("name", docTypeName); |
113 | 0 | crit.orderBy("version", false); |
114 | |
|
115 | 0 | Iterator docTypes = new QueryByCriteria(entityManager, crit).toQuery().getResultList().iterator(); |
116 | 0 | while (docTypes.hasNext()) { |
117 | 0 | return (DocumentType) docTypes.next(); |
118 | |
} |
119 | 0 | return null; |
120 | |
} |
121 | |
|
122 | |
public void save(DocumentType documentType) { |
123 | 0 | if (documentType.getDocumentTypeId() == null){ |
124 | 0 | entityManager.persist(documentType); |
125 | |
} else { |
126 | 0 | OrmUtils.merge(entityManager, documentType); |
127 | |
} |
128 | 0 | } |
129 | |
|
130 | |
public List findByDocumentId(String documentId) { |
131 | 0 | Criteria crit = new Criteria(DocumentType.class.getName()); |
132 | 0 | crit.eq("documentId", documentId); |
133 | 0 | return (List) new QueryByCriteria(entityManager, crit).toQuery().getResultList(); |
134 | |
} |
135 | |
|
136 | |
public Collection<DocumentType> find(DocumentType documentType, DocumentType docTypeParent, boolean climbHierarchy) { |
137 | 0 | LOG.debug("documentType: "+ documentType); |
138 | 0 | LOG.debug("docTypeParent: "+ docTypeParent); |
139 | 0 | LOG.debug("climbHierarchy: " + climbHierarchy); |
140 | |
|
141 | 0 | Criteria crit = new Criteria(DocumentType.class.getName()); |
142 | 0 | if (documentType != null && !org.apache.commons.lang.StringUtils.isEmpty(documentType.getLabel())) { |
143 | 0 | crit.like("UPPER(__JPA_ALIAS[[0]]__.label)", documentType.getLabel().trim().toUpperCase()); |
144 | |
} |
145 | 0 | if (documentType != null && !org.apache.commons.lang.StringUtils.isEmpty(documentType.getName())) { |
146 | 0 | String docTypeName = documentType.getName(); |
147 | 0 | crit.like("UPPER(__JPA_ALIAS[[0]]__.name)", ("%" + docTypeName.trim() + "%").toUpperCase()); |
148 | |
} |
149 | 0 | if (documentType != null && documentType.getActive() != null) { |
150 | 0 | crit.eq("active", documentType.getActive()); |
151 | |
} |
152 | 0 | if (documentType != null && documentType.getDocumentTypeId() != null) { |
153 | 0 | crit.eq("documentTypeId", documentType.getDocumentTypeId()); |
154 | |
} |
155 | 0 | if (documentType != null && documentType.getActualApplicationId() != null){ |
156 | 0 | crit.eq("applicationId", documentType.getActualApplicationId()); |
157 | |
} |
158 | 0 | if (docTypeParent != null) { |
159 | 0 | if (!"".equals(docTypeParent.getName()) && docTypeParent.getName() != null) { |
160 | 0 | Criteria parentCrit = new Criteria(DocumentType.class.getName()); |
161 | |
|
162 | 0 | addParentIdOrCriteria(docTypeParent.getDocumentTypeId(), parentCrit); |
163 | 0 | if (climbHierarchy) { |
164 | 0 | assembleChildrenCriteria(docTypeParent.getChildrenDocTypes(), parentCrit); |
165 | |
} |
166 | 0 | parentCrit.eq("currentInd", Boolean.TRUE); |
167 | 0 | crit.and(parentCrit); |
168 | 0 | } |
169 | |
} else { |
170 | 0 | if (documentType != null && !org.apache.commons.lang.StringUtils.isEmpty(documentType.getName())) { |
171 | 0 | DocumentType searchDocumentType = findByName(documentType.getName()); |
172 | 0 | if ((searchDocumentType != null) && climbHierarchy) { |
173 | 0 | LOG.debug("searchDocumentType: "+ searchDocumentType); |
174 | 0 | Criteria criteria = new Criteria(DocumentType.class.getName()); |
175 | |
|
176 | 0 | addParentIdOrCriteria(searchDocumentType.getDocumentTypeId(), criteria); |
177 | 0 | assembleChildrenCriteria(searchDocumentType.getChildrenDocTypes(), criteria); |
178 | 0 | criteria.eq("currentInd", Boolean.TRUE); |
179 | 0 | crit.or(criteria); |
180 | |
} |
181 | |
} |
182 | |
} |
183 | 0 | crit.eq("currentInd", Boolean.TRUE); |
184 | 0 | return new QueryByCriteria(entityManager, crit).toQuery().getResultList(); |
185 | |
} |
186 | |
|
187 | |
private void addParentIdOrCriteria(String parentId, Criteria mainCriteria) { |
188 | 0 | Criteria parentCriteria = new Criteria(DocumentType.class.getName()); |
189 | 0 | parentCriteria.eq("docTypeParentId", parentId); |
190 | 0 | mainCriteria.or(parentCriteria); |
191 | 0 | } |
192 | |
|
193 | |
private void assembleChildrenCriteria(Collection childrenDocTypes, Criteria crit) { |
194 | 0 | if (childrenDocTypes != null) { |
195 | 0 | Iterator childrenDocTypesIter = childrenDocTypes.iterator(); |
196 | 0 | while (childrenDocTypesIter.hasNext()) { |
197 | 0 | DocumentType child = (DocumentType) childrenDocTypesIter.next(); |
198 | 0 | addParentIdOrCriteria(child.getDocumentTypeId(), crit); |
199 | 0 | assembleChildrenCriteria(child.getChildrenDocTypes(), crit); |
200 | 0 | } |
201 | |
} |
202 | 0 | } |
203 | |
|
204 | |
|
205 | |
|
206 | |
public List findAllCurrentRootDocuments() { |
207 | 0 | Criteria crit = new Criteria(DocumentType.class.getName()); |
208 | 0 | crit.isNull("docTypeParentId"); |
209 | 0 | return findAllCurrent(crit); |
210 | |
} |
211 | |
|
212 | |
public List findAllCurrent() { |
213 | 0 | return findAllCurrent(new Criteria(DocumentType.class.getName())); |
214 | |
} |
215 | |
|
216 | |
public List findAllCurrentByName(String name) { |
217 | 0 | Criteria crit = new Criteria(DocumentType.class.getName()); |
218 | 0 | crit.eq("name", name); |
219 | 0 | return findAllCurrent(crit); |
220 | |
} |
221 | |
|
222 | |
public List<DocumentType> findPreviousInstances(String name) { |
223 | 0 | Criteria crit = new Criteria(DocumentType.class.getName()); |
224 | 0 | crit.eq("name", name); |
225 | 0 | crit.eq("currentInd", Boolean.FALSE); |
226 | 0 | return findAll(crit); |
227 | |
} |
228 | |
|
229 | |
private List findAllCurrent(Criteria crit) { |
230 | 0 | crit.eq("currentInd", Boolean.TRUE); |
231 | 0 | return findAll(crit); |
232 | |
} |
233 | |
|
234 | |
private List findAll(Criteria crit) { |
235 | 0 | return (List) new QueryByCriteria(entityManager, crit).toQuery().getResultList(); |
236 | |
} |
237 | |
|
238 | |
public List findDocumentTypeAttributes(RuleAttribute ruleAttribute) { |
239 | 0 | Criteria crit = new Criteria(DocumentTypeAttribute.class.getName()); |
240 | 0 | if (ruleAttribute.getId() != null) { |
241 | 0 | crit.eq("ruleAttributeId", ruleAttribute.getId()); |
242 | |
} |
243 | 0 | return (List) new QueryByCriteria(entityManager, crit).toQuery().getResultList(); |
244 | |
} |
245 | |
|
246 | |
public String findDocumentTypeIdByDocumentId(String documentId) { |
247 | 0 | Criteria crit = new Criteria(DocumentRouteHeaderValue.class.getName()); |
248 | 0 | crit.eq("documentId", documentId); |
249 | |
|
250 | 0 | final DocumentRouteHeaderValue docHeader = (DocumentRouteHeaderValue)new QueryByCriteria(entityManager, crit).toQuery().getSingleResult(); |
251 | 0 | return (docHeader != null) ? docHeader.getDocumentTypeId() : null; |
252 | |
} |
253 | |
|
254 | |
public String findDocumentTypeIdByName(String documentTypeName) { |
255 | 0 | Criteria crit = new Criteria(DocumentType.class.getName()); |
256 | 0 | crit.eq("name", documentTypeName); |
257 | 0 | crit.eq("currentInd", Boolean.TRUE); |
258 | |
|
259 | 0 | final DocumentType documentType = (DocumentType)new QueryByCriteria(entityManager, crit).toQuery().getSingleResult(); |
260 | 0 | return (documentType != null) ? documentType.getDocumentTypeId() : null; |
261 | |
} |
262 | |
|
263 | |
public String findDocumentTypeNameById(String documentTypeId) { |
264 | 0 | Criteria crit = new Criteria(DocumentType.class.getName()); |
265 | 0 | crit.eq("documentTypeId", documentTypeId); |
266 | |
|
267 | 0 | final DocumentType documentType = (DocumentType)new QueryByCriteria(entityManager, crit).toQuery().getSingleResult(); |
268 | 0 | return (documentType != null) ? documentType.getName() : null; |
269 | |
} |
270 | |
|
271 | |
} |