1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.kew.doctype.dao.impl; |
18 | |
|
19 | |
import org.apache.log4j.Logger; |
20 | |
import org.apache.ojb.broker.PersistenceBroker; |
21 | |
import org.apache.ojb.broker.query.Criteria; |
22 | |
import org.apache.ojb.broker.query.QueryByCriteria; |
23 | |
import org.apache.ojb.broker.query.QueryFactory; |
24 | |
import org.apache.ojb.broker.query.ReportQueryByCriteria; |
25 | |
import org.kuali.rice.kew.doctype.DocumentTypeAttribute; |
26 | |
import org.kuali.rice.kew.doctype.bo.DocumentType; |
27 | |
import org.kuali.rice.kew.doctype.dao.DocumentTypeDAO; |
28 | |
import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue; |
29 | |
import org.kuali.rice.kew.rule.bo.RuleAttribute; |
30 | |
import org.kuali.rice.kew.util.Utilities; |
31 | |
import org.springmodules.orm.ojb.OjbFactoryUtils; |
32 | |
import org.springmodules.orm.ojb.support.PersistenceBrokerDaoSupport; |
33 | |
|
34 | |
import java.math.BigDecimal; |
35 | |
import java.sql.Connection; |
36 | |
import java.sql.ResultSet; |
37 | |
import java.sql.Statement; |
38 | |
import java.util.ArrayList; |
39 | |
import java.util.Collection; |
40 | |
import java.util.Iterator; |
41 | |
import java.util.List; |
42 | |
|
43 | |
|
44 | 0 | public class DocumentTypeDAOOjbImpl extends PersistenceBrokerDaoSupport implements DocumentTypeDAO { |
45 | |
|
46 | 0 | public static final Logger LOG = Logger.getLogger(DocumentTypeDAOOjbImpl.class); |
47 | |
|
48 | |
public void delete(DocumentType documentType) { |
49 | 0 | this.getPersistenceBrokerTemplate().delete(documentType); |
50 | 0 | } |
51 | |
|
52 | |
public DocumentType findByDocId(Long docId) { |
53 | 0 | Criteria crit = new Criteria(); |
54 | 0 | crit.addEqualTo("documentTypeId", docId); |
55 | 0 | return (DocumentType) this.getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(DocumentType.class, crit)); |
56 | |
} |
57 | |
|
58 | |
public DocumentType findByName(String name) { |
59 | 0 | return findByName(name, true); |
60 | |
} |
61 | |
|
62 | |
public DocumentType findByName(String name, boolean caseSensitive) { |
63 | 0 | Criteria crit = new Criteria(); |
64 | 0 | if(!caseSensitive){ |
65 | 0 | crit.addEqualTo("UPPER(name)", name.trim().toUpperCase()); |
66 | |
}else{ |
67 | 0 | crit.addEqualTo("name", name); |
68 | |
} |
69 | 0 | crit.addEqualTo("currentInd", Boolean.TRUE); |
70 | 0 | DocumentType docType = (DocumentType) this.getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(DocumentType.class, crit)); |
71 | 0 | return docType; |
72 | |
} |
73 | |
|
74 | |
public Integer getMaxVersionNumber(String docTypeName) { |
75 | 0 | return getMostRecentDocType(docTypeName).getVersion(); |
76 | |
} |
77 | |
|
78 | |
public List getChildDocumentTypeIds(Long parentDocumentTypeId) { |
79 | 0 | List<Long> childrenIds = new ArrayList<Long>(); |
80 | 0 | PersistenceBroker broker = getPersistenceBroker(false); |
81 | 0 | Connection conn = null; |
82 | 0 | Statement st = null; |
83 | 0 | ResultSet rs = null; |
84 | |
try { |
85 | 0 | conn = broker.serviceConnectionManager().getConnection(); |
86 | 0 | st = conn.createStatement(); |
87 | 0 | rs = st.executeQuery("select DOC_TYP_ID from KREW_DOC_TYP_T where CUR_IND = 1 and PARNT_ID = " + parentDocumentTypeId); |
88 | 0 | while (rs.next()) { |
89 | 0 | childrenIds.add(new Long(rs.getLong("DOC_TYP_ID"))); |
90 | |
} |
91 | 0 | } catch (Exception e) { |
92 | 0 | LOG.error("Error occured fetching children document type ids for document type " + parentDocumentTypeId, e); |
93 | 0 | throw new RuntimeException(e); |
94 | |
} finally { |
95 | 0 | try { |
96 | 0 | st.close(); |
97 | 0 | } catch (Exception e) { |
98 | 0 | LOG.warn("Failed to close Statement", e); |
99 | 0 | } |
100 | |
|
101 | |
try { |
102 | 0 | rs.close(); |
103 | 0 | } catch (Exception e) { |
104 | 0 | LOG.warn("Failed to close Resultset", e); |
105 | 0 | } |
106 | |
|
107 | 0 | if (broker != null) { |
108 | |
try { |
109 | 0 | OjbFactoryUtils.releasePersistenceBroker(broker, this.getPersistenceBrokerTemplate().getPbKey()); |
110 | 0 | } catch (Exception e) { |
111 | 0 | LOG.error("Failed closing connection: " + e.getMessage(), e); |
112 | 0 | } |
113 | |
} |
114 | 0 | } |
115 | 0 | return childrenIds; |
116 | |
} |
117 | |
|
118 | |
public DocumentType getMostRecentDocType(String docTypeName) { |
119 | 0 | Criteria crit = new Criteria(); |
120 | 0 | crit.addEqualTo("name", docTypeName); |
121 | 0 | QueryByCriteria query = new QueryByCriteria(DocumentType.class, crit); |
122 | 0 | query.addOrderByDescending("version"); |
123 | |
|
124 | 0 | Iterator docTypes = this.getPersistenceBrokerTemplate().getCollectionByQuery(query).iterator(); |
125 | 0 | while (docTypes.hasNext()) { |
126 | 0 | return (DocumentType) docTypes.next(); |
127 | |
} |
128 | 0 | return null; |
129 | |
} |
130 | |
|
131 | |
public void save(DocumentType documentType) { |
132 | 0 | this.getPersistenceBrokerTemplate().store(documentType); |
133 | 0 | } |
134 | |
|
135 | |
public List findByRouteHeaderId(Long routeHeaderId) { |
136 | 0 | Criteria crit = new Criteria(); |
137 | 0 | crit.addEqualTo("routeHeaderId", routeHeaderId); |
138 | 0 | return (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(DocumentType.class, crit)); |
139 | |
} |
140 | |
|
141 | |
public Collection<DocumentType> find(DocumentType documentType, DocumentType docTypeParent, boolean climbHierarchy) { |
142 | 0 | LOG.debug("documentType: "+ documentType); |
143 | 0 | LOG.debug("docTypeParent: "+ docTypeParent); |
144 | 0 | LOG.debug("climbHierarchy: " + climbHierarchy); |
145 | |
|
146 | 0 | Criteria crit = new Criteria(); |
147 | 0 | if (documentType != null && !Utilities.isEmpty(documentType.getLabel())) { |
148 | 0 | crit.addLike("UPPER(label)", documentType.getLabel().trim().toUpperCase()); |
149 | |
} |
150 | 0 | if (documentType != null && !Utilities.isEmpty(documentType.getName())) { |
151 | 0 | String docTypeName = documentType.getName(); |
152 | 0 | crit.addLike("UPPER(name)", ("%" + docTypeName.trim() + "%").toUpperCase()); |
153 | |
} |
154 | 0 | if (documentType != null && documentType.getActive() != null) { |
155 | 0 | crit.addEqualTo("active", documentType.getActive()); |
156 | |
} |
157 | 0 | if (documentType != null && documentType.getDocumentTypeId() != null) { |
158 | 0 | crit.addEqualTo("documentTypeId", documentType.getDocumentTypeId()); |
159 | |
} |
160 | 0 | if (documentType != null && documentType.getActualServiceNamespace() != null){ |
161 | 0 | crit.addEqualTo("actualServiceNamespace", documentType.getActualServiceNamespace()); |
162 | |
} |
163 | 0 | if (docTypeParent != null) { |
164 | 0 | if (!"".equals(docTypeParent.getName()) && docTypeParent.getName() != null) { |
165 | 0 | Criteria parentCrit = new Criteria(); |
166 | |
|
167 | 0 | addParentIdOrCriteria(docTypeParent.getDocumentTypeId(), parentCrit); |
168 | 0 | if (climbHierarchy) { |
169 | 0 | assembleChildrenCriteria(docTypeParent.getChildrenDocTypes(), parentCrit); |
170 | |
} |
171 | 0 | parentCrit.addEqualTo("currentInd", Boolean.TRUE); |
172 | 0 | crit.addAndCriteria(parentCrit); |
173 | 0 | } |
174 | |
} else { |
175 | 0 | if (documentType != null && !Utilities.isEmpty(documentType.getName())) { |
176 | 0 | DocumentType searchDocumentType = findByName(documentType.getName()); |
177 | 0 | if ((searchDocumentType != null) && climbHierarchy) { |
178 | 0 | LOG.debug("searchDocumentType: "+ searchDocumentType); |
179 | 0 | Criteria criteria = new Criteria(); |
180 | |
|
181 | 0 | addParentIdOrCriteria(searchDocumentType.getDocumentTypeId(), criteria); |
182 | 0 | assembleChildrenCriteria(searchDocumentType.getChildrenDocTypes(), criteria); |
183 | 0 | criteria.addEqualTo("currentInd", Boolean.TRUE); |
184 | 0 | crit.addOrCriteria(criteria); |
185 | |
} |
186 | |
} |
187 | |
} |
188 | 0 | crit.addEqualTo("currentInd", Boolean.TRUE); |
189 | 0 | return (Collection<DocumentType>) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(DocumentType.class, crit)); |
190 | |
} |
191 | |
|
192 | |
private void addParentIdOrCriteria(Long parentId, Criteria mainCriteria) { |
193 | 0 | Criteria parentCriteria = new Criteria(); |
194 | 0 | parentCriteria.addEqualTo("docTypeParentId", parentId); |
195 | 0 | mainCriteria.addOrCriteria(parentCriteria); |
196 | 0 | } |
197 | |
|
198 | |
private void assembleChildrenCriteria(Collection childrenDocTypes, Criteria crit) { |
199 | 0 | if (childrenDocTypes != null) { |
200 | 0 | Iterator childrenDocTypesIter = childrenDocTypes.iterator(); |
201 | 0 | while (childrenDocTypesIter.hasNext()) { |
202 | 0 | DocumentType child = (DocumentType) childrenDocTypesIter.next(); |
203 | 0 | addParentIdOrCriteria(child.getDocumentTypeId(), crit); |
204 | 0 | assembleChildrenCriteria(child.getChildrenDocTypes(), crit); |
205 | 0 | } |
206 | |
} |
207 | 0 | } |
208 | |
|
209 | |
public DocumentType getMostRecentDocType(Long documentTypeId) { |
210 | 0 | Criteria crit = new Criteria(); |
211 | 0 | crit.addEqualTo("documentTypeId", documentTypeId); |
212 | 0 | QueryByCriteria query = new QueryByCriteria(DocumentType.class, crit); |
213 | 0 | query.addOrderByDescending("version"); |
214 | |
|
215 | 0 | Iterator docTypes = this.getPersistenceBrokerTemplate().getCollectionByQuery(query).iterator(); |
216 | 0 | while (docTypes.hasNext()) { |
217 | 0 | return (DocumentType) docTypes.next(); |
218 | |
} |
219 | 0 | return null; |
220 | |
} |
221 | |
|
222 | |
public List findAllCurrentRootDocuments() { |
223 | 0 | Criteria crit = new Criteria(); |
224 | 0 | crit.addIsNull("docTypeParentId"); |
225 | 0 | return findAllCurrent(crit); |
226 | |
} |
227 | |
|
228 | |
public List findAllCurrent() { |
229 | 0 | return findAllCurrent(new Criteria()); |
230 | |
} |
231 | |
|
232 | |
public List findAllCurrentByName(String name) { |
233 | 0 | Criteria crit = new Criteria(); |
234 | 0 | crit.addEqualTo("name", name); |
235 | 0 | return findAllCurrent(crit); |
236 | |
} |
237 | |
|
238 | |
public List<DocumentType> findPreviousInstances(String documentTypeName) { |
239 | 0 | Criteria crit = new Criteria(); |
240 | 0 | crit.addEqualTo("name", documentTypeName); |
241 | 0 | crit.addEqualTo("currentInd", Boolean.FALSE); |
242 | 0 | return findAll(crit); |
243 | |
} |
244 | |
|
245 | |
private List<DocumentType> findAllCurrent(Criteria crit) { |
246 | 0 | crit.addEqualTo("currentInd", Boolean.TRUE); |
247 | 0 | return findAll(crit); |
248 | |
} |
249 | |
|
250 | |
private List<DocumentType> findAll(Criteria crit) { |
251 | 0 | return (List<DocumentType>) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(DocumentType.class, crit)); |
252 | |
} |
253 | |
|
254 | |
public List findDocumentTypeAttributes(RuleAttribute ruleAttribute) { |
255 | 0 | Criteria crit = new Criteria(); |
256 | 0 | crit.addEqualTo("ruleAttributeId", ruleAttribute.getRuleAttributeId()); |
257 | 0 | return (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(DocumentTypeAttribute.class, crit)); |
258 | |
} |
259 | |
|
260 | |
public Long findDocumentTypeIdByDocumentId(Long documentId) { |
261 | 0 | Criteria crit = new Criteria(); |
262 | 0 | crit.addEqualTo("routeHeaderId", documentId); |
263 | 0 | ReportQueryByCriteria query = QueryFactory.newReportQuery(DocumentRouteHeaderValue.class, crit); |
264 | 0 | query.setAttributes(new String[] { "documentTypeId" }); |
265 | |
|
266 | 0 | Iterator iter = getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(query); |
267 | 0 | while (iter.hasNext()) { |
268 | 0 | Object[] row = (Object[]) iter.next(); |
269 | 0 | BigDecimal id = (BigDecimal)row[0]; |
270 | 0 | return new Long(id.longValue()); |
271 | |
} |
272 | 0 | return null; |
273 | |
} |
274 | |
|
275 | |
} |