001: /*
002: * Copyright 2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.kuali.module.purap.dao.ojb;
017:
018: import java.util.Iterator;
019:
020: import org.apache.ojb.broker.query.Criteria;
021: import org.apache.ojb.broker.query.ReportQueryByCriteria;
022: import org.kuali.core.dao.ojb.PlatformAwareDaoBaseOjb;
023: import org.kuali.core.util.TransactionalServiceUtils;
024: import org.kuali.kfs.KFSPropertyConstants;
025: import org.kuali.module.purap.PurapPropertyConstants;
026: import org.kuali.module.purap.dao.PurchaseOrderDao;
027: import org.kuali.module.purap.document.PurchaseOrderDocument;
028:
029: /**
030: * OJB implementation of PurchaseOrderDao.
031: */
032: public class PurchaseOrderDaoOjb extends PlatformAwareDaoBaseOjb
033: implements PurchaseOrderDao {
034: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
035: .getLogger(PurchaseOrderDaoOjb.class);
036:
037: /**
038: * @see org.kuali.module.purap.dao.PurchaseOrderDao#getDocumentNumberForPurchaseOrderId(java.lang.Integer)
039: */
040: public String getDocumentNumberForPurchaseOrderId(Integer id) {
041: Criteria criteria = new Criteria();
042: criteria.addEqualTo(PurapPropertyConstants.PURAP_DOC_ID, id);
043: return getDocumentNumberUsingPurchaseOrderCriteria(criteria);
044: }
045:
046: /**
047: * @see org.kuali.module.purap.dao.PurchaseOrderDao#getDocumentNumberForCurrentPurchaseOrder(java.lang.Integer)
048: */
049: public String getDocumentNumberForCurrentPurchaseOrder(Integer id) {
050: Criteria criteria = new Criteria();
051: criteria.addEqualTo(PurapPropertyConstants.PURAP_DOC_ID, id);
052: criteria
053: .addEqualTo(
054: PurapPropertyConstants.PURCHASE_ORDER_CURRENT_INDICATOR,
055: "Y");
056: return getDocumentNumberUsingPurchaseOrderCriteria(criteria);
057: }
058:
059: /**
060: * @see org.kuali.module.purap.dao.PurchaseOrderDao#getOldestPurchaseOrderDocumentNumber(java.lang.Integer)
061: */
062: public String getOldestPurchaseOrderDocumentNumber(Integer id) {
063: Criteria criteria = new Criteria();
064: criteria.addEqualTo(PurapPropertyConstants.PURAP_DOC_ID, id);
065: ReportQueryByCriteria rqbc = new ReportQueryByCriteria(
066: PurchaseOrderDocument.class, criteria);
067: rqbc
068: .setAttributes(new String[] { KFSPropertyConstants.DOCUMENT_NUMBER });
069: rqbc.addOrderByAscending(KFSPropertyConstants.DOCUMENT_NUMBER);
070: Iterator<Object[]> iter = getPersistenceBrokerTemplate()
071: .getReportQueryIteratorByQuery(rqbc);
072: String oldestDocumentNumber = null;
073: if (iter.hasNext()) {
074: oldestDocumentNumber = (String) (iter.next())[0];
075: }
076: return oldestDocumentNumber;
077: }
078:
079: /**
080: * Retrieves the document number of the purchase order returned by the passed in criteria.
081: *
082: * @param criteria - list of criteria to use in the retrieve
083: * @return Document number string if a valid purchase order is found, null if no purchase order is found
084: */
085: private String getDocumentNumberUsingPurchaseOrderCriteria(
086: Criteria criteria) {
087: Iterator<Object[]> iter = getDocumentNumbersUsingPurchaseOrderCriteria(criteria);
088: if (iter.hasNext()) {
089: Object[] cols = iter.next();
090: if (iter.hasNext()) {
091: // the iterator should have held only a single doc id of data but it holds 2 or more
092: String errorMsg = "Expected single document number for given criteria but multiple (at least 2) were returned";
093: LOG.error(errorMsg);
094: TransactionalServiceUtils.exhaustIterator(iter);
095: throw new RuntimeException(errorMsg);
096: }
097: // at this part of the code, we know there's no more elements in iterator
098: return (String) cols[0];
099: }
100: return null;
101: }
102:
103: /**
104: * Retrieves a list of document numbers of the purchase order returned by the passed in criteria.
105: *
106: * @param criteria - list of criteria to use in the retrieve
107: * @return Iterator of document numbers
108: */
109: private Iterator<Object[]> getDocumentNumbersUsingPurchaseOrderCriteria(
110: Criteria criteria) {
111: ReportQueryByCriteria rqbc = new ReportQueryByCriteria(
112: PurchaseOrderDocument.class, criteria);
113: rqbc
114: .setAttributes(new String[] { KFSPropertyConstants.DOCUMENT_NUMBER });
115: rqbc.addOrderByAscending(KFSPropertyConstants.DOCUMENT_NUMBER);
116: return getPersistenceBrokerTemplate()
117: .getReportQueryIteratorByQuery(rqbc);
118: }
119:
120: }
|