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 java.sql.Connection; |
20 | |
import java.sql.PreparedStatement; |
21 | |
import java.sql.ResultSet; |
22 | |
import java.util.ArrayList; |
23 | |
import java.util.Collection; |
24 | |
import java.util.Iterator; |
25 | |
import java.util.List; |
26 | |
|
27 | |
import org.apache.log4j.Logger; |
28 | |
import org.apache.ojb.broker.PersistenceBroker; |
29 | |
import org.apache.ojb.broker.accesslayer.ReportQueryRsIterator; |
30 | |
import org.apache.ojb.broker.query.Criteria; |
31 | |
import org.apache.ojb.broker.query.QueryByCriteria; |
32 | |
import org.apache.ojb.broker.query.QueryFactory; |
33 | |
import org.apache.ojb.broker.query.ReportQueryByCriteria; |
34 | |
import org.kuali.rice.kew.doctype.DocumentTypeAttribute; |
35 | |
import org.kuali.rice.kew.doctype.bo.DocumentType; |
36 | |
import org.kuali.rice.kew.doctype.dao.DocumentTypeDAO; |
37 | |
import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue; |
38 | |
import org.kuali.rice.kew.rule.bo.RuleAttribute; |
39 | |
import org.springmodules.orm.ojb.OjbFactoryUtils; |
40 | |
import org.springmodules.orm.ojb.support.PersistenceBrokerDaoSupport; |
41 | |
|
42 | |
|
43 | 0 | public class DocumentTypeDAOOjbImpl extends PersistenceBrokerDaoSupport implements DocumentTypeDAO { |
44 | |
|
45 | 0 | public static final Logger LOG = Logger.getLogger(DocumentTypeDAOOjbImpl.class); |
46 | |
|
47 | |
public void delete(DocumentType documentType) { |
48 | 0 | this.getPersistenceBrokerTemplate().delete(documentType); |
49 | 0 | } |
50 | |
|
51 | |
public DocumentType findById(String docTypeId) { |
52 | 0 | Criteria crit = new Criteria(); |
53 | 0 | crit.addEqualTo("documentTypeId", docTypeId); |
54 | 0 | return (DocumentType) this.getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(DocumentType.class, crit)); |
55 | |
} |
56 | |
|
57 | |
public DocumentType findByName(String name) { |
58 | 0 | return findByName(name, true); |
59 | |
} |
60 | |
|
61 | |
public DocumentType findByName(String name, boolean caseSensitive) { |
62 | 0 | Criteria crit = new Criteria(); |
63 | 0 | if(!caseSensitive){ |
64 | 0 | crit.addEqualTo("UPPER(name)", name.trim().toUpperCase()); |
65 | |
}else{ |
66 | 0 | crit.addEqualTo("name", name); |
67 | |
} |
68 | 0 | crit.addEqualTo("currentInd", Boolean.TRUE); |
69 | 0 | DocumentType docType = (DocumentType) this.getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(DocumentType.class, crit)); |
70 | 0 | return docType; |
71 | |
} |
72 | |
|
73 | |
public Integer getMaxVersionNumber(String docTypeName) { |
74 | 0 | return getMostRecentDocType(docTypeName).getVersion(); |
75 | |
} |
76 | |
|
77 | |
public List<String> getChildDocumentTypeIds(String parentDocumentTypeId) { |
78 | 0 | List<String> childrenIds = new ArrayList<String>(); |
79 | 0 | PersistenceBroker broker = getPersistenceBroker(false); |
80 | 0 | Connection conn = null; |
81 | 0 | PreparedStatement st = null; |
82 | 0 | ResultSet rs = null; |
83 | |
try { |
84 | 0 | conn = broker.serviceConnectionManager().getConnection(); |
85 | 0 | st = conn.prepareStatement("select DOC_TYP_ID from KREW_DOC_TYP_T where CUR_IND = 1 and PARNT_ID = ?"); |
86 | 0 | st.setString(1, parentDocumentTypeId); |
87 | 0 | rs = st.executeQuery(); |
88 | 0 | while (rs.next()) { |
89 | 0 | childrenIds.add(rs.getString("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 | |
} |
115 | 0 | return childrenIds; |
116 | |
} |
117 | |
|
118 | |
protected 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 findByDocumentId(String documentId) { |
136 | 0 | Criteria crit = new Criteria(); |
137 | 0 | crit.addEqualTo("documentId", documentId); |
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 && !org.apache.commons.lang.StringUtils.isEmpty(documentType.getLabel())) { |
148 | 0 | crit.addLike("UPPER(label)", documentType.getLabel().trim().toUpperCase()); |
149 | |
} |
150 | 0 | if (documentType != null && !org.apache.commons.lang.StringUtils.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.getActualApplicationId() != null){ |
161 | 0 | crit.addEqualTo("actualApplicationIde", documentType.getActualApplicationId()); |
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 && !org.apache.commons.lang.StringUtils.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(String 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 List findAllCurrentRootDocuments() { |
210 | 0 | Criteria crit = new Criteria(); |
211 | 0 | crit.addIsNull("docTypeParentId"); |
212 | 0 | return findAllCurrent(crit); |
213 | |
} |
214 | |
|
215 | |
public List findAllCurrent() { |
216 | 0 | return findAllCurrent(new Criteria()); |
217 | |
} |
218 | |
|
219 | |
public List findAllCurrentByName(String name) { |
220 | 0 | Criteria crit = new Criteria(); |
221 | 0 | crit.addEqualTo("name", name); |
222 | 0 | return findAllCurrent(crit); |
223 | |
} |
224 | |
|
225 | |
public List<DocumentType> findPreviousInstances(String documentTypeName) { |
226 | 0 | Criteria crit = new Criteria(); |
227 | 0 | crit.addEqualTo("name", documentTypeName); |
228 | 0 | crit.addEqualTo("currentInd", Boolean.FALSE); |
229 | 0 | return findAll(crit); |
230 | |
} |
231 | |
|
232 | |
private List<DocumentType> findAllCurrent(Criteria crit) { |
233 | 0 | crit.addEqualTo("currentInd", Boolean.TRUE); |
234 | 0 | return findAll(crit); |
235 | |
} |
236 | |
|
237 | |
private List<DocumentType> findAll(Criteria crit) { |
238 | 0 | return (List<DocumentType>) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(DocumentType.class, crit)); |
239 | |
} |
240 | |
|
241 | |
public List findDocumentTypeAttributes(RuleAttribute ruleAttribute) { |
242 | 0 | Criteria crit = new Criteria(); |
243 | 0 | crit.addEqualTo("ruleAttributeId", ruleAttribute.getRuleAttributeId()); |
244 | 0 | return (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(DocumentTypeAttribute.class, crit)); |
245 | |
} |
246 | |
|
247 | |
public String findDocumentTypeIdByDocumentId(String documentId) { |
248 | 0 | Criteria crit = new Criteria(); |
249 | 0 | crit.addEqualTo("documentId", documentId); |
250 | 0 | ReportQueryByCriteria query = QueryFactory.newReportQuery(DocumentRouteHeaderValue.class, crit); |
251 | 0 | query.setAttributes(new String[] { "documentTypeId" }); |
252 | |
|
253 | 0 | Iterator iter = getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(query); |
254 | 0 | while (iter.hasNext()) { |
255 | 0 | Object[] row = (Object[]) iter.next(); |
256 | 0 | return (String)row[0]; |
257 | |
} |
258 | 0 | return null; |
259 | |
} |
260 | |
|
261 | |
public String findDocumentTypeIdByName(String documentTypeName) { |
262 | 0 | Criteria crit = new Criteria(); |
263 | 0 | crit.addEqualTo("name", documentTypeName); |
264 | 0 | ReportQueryByCriteria query = QueryFactory.newReportQuery(DocumentType.class, crit); |
265 | 0 | query.setAttributes(new String[] { "documentTypeId" }); |
266 | |
|
267 | 0 | ReportQueryRsIterator iter = (ReportQueryRsIterator)getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(query); |
268 | |
try { |
269 | 0 | while (iter.hasNext()) { |
270 | 0 | Object[] row = (Object[]) iter.next(); |
271 | 0 | return (String)row[0]; |
272 | |
} |
273 | |
} finally { |
274 | 0 | iter.releaseDbResources(); |
275 | 0 | } |
276 | 0 | return null; |
277 | |
} |
278 | |
|
279 | |
public String findDocumentTypeNameById(String documentTypeId) { |
280 | 0 | Criteria crit = new Criteria(); |
281 | 0 | crit.addEqualTo("documentTypeId", documentTypeId); |
282 | 0 | ReportQueryByCriteria query = QueryFactory.newReportQuery(DocumentType.class, crit); |
283 | 0 | query.setAttributes(new String[] { "name" }); |
284 | |
|
285 | 0 | ReportQueryRsIterator iter = (ReportQueryRsIterator)getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(query); |
286 | |
try { |
287 | 0 | while (iter.hasNext()) { |
288 | 0 | Object[] row = (Object[]) iter.next(); |
289 | 0 | return (String)row[0]; |
290 | |
} |
291 | |
} finally { |
292 | 0 | iter.releaseDbResources(); |
293 | 0 | } |
294 | 0 | return null; |
295 | |
} |
296 | |
|
297 | |
} |