001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.doctype.dao;
018:
019: import java.sql.Connection;
020: import java.sql.ResultSet;
021: import java.sql.Statement;
022: import java.util.ArrayList;
023: import java.util.Collection;
024: import java.util.Iterator;
025: import java.util.List;
026:
027: import org.apache.log4j.Logger;
028: import org.apache.ojb.broker.PersistenceBroker;
029: import org.apache.ojb.broker.query.Criteria;
030: import org.apache.ojb.broker.query.QueryByCriteria;
031: import org.apache.ojb.broker.query.QueryFactory;
032: import org.apache.ojb.broker.query.ReportQueryByCriteria;
033: import org.springmodules.orm.ojb.OjbFactoryUtils;
034: import org.springmodules.orm.ojb.PersistenceBrokerTemplate;
035: import org.springmodules.orm.ojb.support.PersistenceBrokerDaoSupport;
036:
037: import edu.iu.uis.eden.doctype.DocumentType;
038: import edu.iu.uis.eden.doctype.DocumentTypeAttribute;
039: import edu.iu.uis.eden.routeheader.DocumentRouteHeaderValue;
040: import edu.iu.uis.eden.routetemplate.RuleAttribute;
041: import edu.iu.uis.eden.util.Utilities;
042:
043: public class DocumentTypeDAOOjbImpl extends PersistenceBrokerDaoSupport
044: implements DocumentTypeDAO {
045:
046: public static final Logger LOG = Logger
047: .getLogger(DocumentTypeDAOOjbImpl.class);
048:
049: public void delete(DocumentType documentType) {
050: this .getPersistenceBrokerTemplate().delete(documentType);
051: }
052:
053: public DocumentType findByDocId(Long docId) {
054: Criteria crit = new Criteria();
055: crit.addEqualTo("documentTypeId", docId);
056: return (DocumentType) this .getPersistenceBrokerTemplate()
057: .getObjectByQuery(
058: new QueryByCriteria(DocumentType.class, crit));
059: }
060:
061: public DocumentType findByName(String name) {
062: Criteria crit = new Criteria();
063: crit.addEqualTo("name", name);
064: crit.addEqualTo("currentInd", new Boolean(true));
065: DocumentType docType = (DocumentType) this
066: .getPersistenceBrokerTemplate().getObjectByQuery(
067: new QueryByCriteria(DocumentType.class, crit));
068: return docType;
069: }
070:
071: public Integer getMaxVersionNumber(String docTypeName) {
072: return getMostRecentDocType(docTypeName).getVersion();
073: }
074:
075: public List getChildDocumentTypeIds(Long parentDocumentTypeId) {
076: List childrenIds = new ArrayList();
077: PersistenceBroker broker = getPersistenceBroker(false);
078: Connection conn = null;
079: Statement st = null;
080: ResultSet rs = null;
081: try {
082: conn = broker.serviceConnectionManager().getConnection();
083: st = conn.createStatement();
084: rs = st
085: .executeQuery("select DOC_TYP_ID from EN_DOC_TYP_T where DOC_TYP_CUR_IND = 1 and DOC_TYP_PARNT_ID = "
086: + parentDocumentTypeId);
087: while (rs.next()) {
088: childrenIds.add(new Long(rs.getLong("DOC_TYP_ID")));
089: }
090: } catch (Exception e) {
091: LOG.error(
092: "Error occured fetching children document type ids for document type "
093: + parentDocumentTypeId, e);
094: throw new RuntimeException(e);
095: } finally {
096: try {
097: st.close();
098: } catch (Exception e) {
099: LOG.warn("Failed to close Statement", e);
100: }
101:
102: try {
103: rs.close();
104: } catch (Exception e) {
105: LOG.warn("Failed to close Resultset", e);
106: }
107:
108: if (broker != null) {
109: try {
110: OjbFactoryUtils.releasePersistenceBroker(broker,
111: this .getPersistenceBrokerTemplate()
112: .getPbKey());
113: } catch (Exception e) {
114: LOG.error("Failed closing connection: "
115: + e.getMessage(), e);
116: }
117: }
118: }
119: return childrenIds;
120: }
121:
122: public DocumentType getMostRecentDocType(String docTypeName) {
123: Criteria crit = new Criteria();
124: crit.addEqualTo("name", docTypeName);
125: QueryByCriteria query = new QueryByCriteria(DocumentType.class,
126: crit);
127: query.addOrderByDescending("version");
128:
129: Iterator docTypes = this .getPersistenceBrokerTemplate()
130: .getCollectionByQuery(query).iterator();
131: while (docTypes.hasNext()) {
132: return (DocumentType) docTypes.next();
133: }
134: return null;
135: }
136:
137: public void save(DocumentType documentType) {
138: this .getPersistenceBrokerTemplate().store(documentType);
139: }
140:
141: public List findByRouteHeaderId(Long routeHeaderId) {
142: Criteria crit = new Criteria();
143: crit.addEqualTo("routeHeaderId", routeHeaderId);
144: return (List) this .getPersistenceBrokerTemplate()
145: .getCollectionByQuery(
146: new QueryByCriteria(DocumentType.class, crit));
147: }
148:
149: public Collection find(DocumentType documentType,
150: DocumentType docTypeParent, boolean climbHierarchy) {
151: LOG.debug("documentType: " + documentType);
152: LOG.debug("docTypeParent: " + docTypeParent);
153: LOG.debug("climbHierarchy: " + climbHierarchy);
154:
155: Criteria crit = new Criteria();
156: if (documentType != null
157: && !Utilities.isEmpty(documentType.getLabel())) {
158: crit.addLike("UPPER(label)", documentType.getLabel().trim()
159: .toUpperCase());
160: }
161: if (documentType != null
162: && !Utilities.isEmpty(documentType.getName())) {
163: String docTypeName = documentType.getName();
164: crit.addLike("UPPER(name)",
165: ("%" + docTypeName.trim() + "%").toUpperCase());
166: }
167: if (documentType != null && documentType.getActiveInd() != null) {
168: crit.addEqualTo("activeInd", documentType.getActiveInd());
169: }
170: if (documentType != null
171: && documentType.getDocumentTypeId() != null) {
172: crit.addEqualTo("documentTypeId", documentType
173: .getDocumentTypeId());
174: }
175: if (docTypeParent != null) {
176: if (!"".equals(docTypeParent.getName())
177: && docTypeParent.getName() != null) {
178: Criteria parentCrit = new Criteria();
179: //addParentNameOrCriteria(docTypeParent.getName(), parentCrit);
180: addParentIdOrCriteria(
181: docTypeParent.getDocumentTypeId(), parentCrit);
182: if (climbHierarchy) {
183: assembleChildrenCriteria(docTypeParent
184: .getChildrenDocTypes(), parentCrit);
185: }
186: parentCrit.addEqualTo("currentInd", Boolean.TRUE);
187: crit.addAndCriteria(parentCrit);
188: }
189: } else {
190: if (documentType != null
191: && !Utilities.isEmpty(documentType.getName())) {
192: DocumentType searchDocumentType = findByName(documentType
193: .getName());
194: if ((searchDocumentType != null) && climbHierarchy) {
195: LOG.debug("searchDocumentType: "
196: + searchDocumentType);
197: Criteria criteria = new Criteria();
198: //addParentNameOrCriteria(searchDocumentType.getName(), criteria);
199: addParentIdOrCriteria(searchDocumentType
200: .getDocumentTypeId(), criteria);
201: assembleChildrenCriteria(searchDocumentType
202: .getChildrenDocTypes(), criteria);
203: criteria.addEqualTo("currentInd", Boolean.TRUE);
204: crit.addOrCriteria(criteria);
205: }
206: }
207: }
208: crit.addEqualTo("currentInd", Boolean.TRUE);
209: return this .getPersistenceBrokerTemplate()
210: .getCollectionByQuery(
211: new QueryByCriteria(DocumentType.class, crit));
212: }
213:
214: private void addParentIdOrCriteria(Long parentId,
215: Criteria mainCriteria) {
216: Criteria parentCriteria = new Criteria();
217: parentCriteria.addEqualTo("docTypeParentId", parentId);
218: mainCriteria.addOrCriteria(parentCriteria);
219: }
220:
221: private void assembleChildrenCriteria(Collection childrenDocTypes,
222: Criteria crit) {
223: if (childrenDocTypes != null) {
224: Iterator childrenDocTypesIter = childrenDocTypes.iterator();
225: while (childrenDocTypesIter.hasNext()) {
226: DocumentType child = (DocumentType) childrenDocTypesIter
227: .next();
228: addParentIdOrCriteria(child.getDocumentTypeId(), crit);
229: assembleChildrenCriteria(child.getChildrenDocTypes(),
230: crit);
231: }
232: }
233: }
234:
235: public DocumentType getMostRecentDocType(Long documentTypeId) {
236: Criteria crit = new Criteria();
237: crit.addEqualTo("documentTypeId", documentTypeId);
238: QueryByCriteria query = new QueryByCriteria(DocumentType.class,
239: crit);
240: query.addOrderByDescending("version");
241:
242: Iterator docTypes = this .getPersistenceBrokerTemplate()
243: .getCollectionByQuery(query).iterator();
244: while (docTypes.hasNext()) {
245: return (DocumentType) docTypes.next();
246: }
247: return null;
248: }
249:
250: public List findAllCurrentRootDocuments() {
251: Criteria crit = new Criteria();
252: crit.addIsNull("docTypeParentId");
253: crit.addEqualTo("currentInd", Boolean.TRUE);
254: return (List) this .getPersistenceBrokerTemplate()
255: .getCollectionByQuery(
256: new QueryByCriteria(DocumentType.class, crit));
257: }
258:
259: public List findAllCurrent() {
260: Criteria crit = new Criteria();
261: crit.addEqualTo("currentInd", Boolean.TRUE);
262: return (List) this .getPersistenceBrokerTemplate()
263: .getCollectionByQuery(
264: new QueryByCriteria(DocumentType.class, crit));
265: }
266:
267: public List findDocumentTypeAttributes(RuleAttribute ruleAttribute) {
268: Criteria crit = new Criteria();
269: crit.addEqualTo("ruleAttributeId", ruleAttribute
270: .getRuleAttributeId());
271: return (List) this .getPersistenceBrokerTemplate()
272: .getCollectionByQuery(
273: new QueryByCriteria(
274: DocumentTypeAttribute.class, crit));
275: }
276:
277: public Long findDocumentTypeIdByDocumentId(Long documentId) {
278: Criteria crit = new Criteria();
279: crit.addEqualTo("routeHeaderId", documentId);
280: ReportQueryByCriteria query = QueryFactory.newReportQuery(
281: DocumentRouteHeaderValue.class, crit);
282: query.setAttributes(new String[] { "documentTypeId" });
283: return (Long) this.getPersistenceBrokerTemplate()
284: .getObjectByQuery(query);
285: }
286:
287: }
|