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.sql.Date;
019: import java.util.Arrays;
020: import java.util.Iterator;
021:
022: import org.apache.ojb.broker.query.Criteria;
023: import org.apache.ojb.broker.query.QueryByCriteria;
024: import org.apache.ojb.broker.query.ReportQueryByCriteria;
025: import org.kuali.core.dao.ojb.PlatformAwareDaoBaseOjb;
026: import org.kuali.core.util.KualiDecimal;
027: import org.kuali.core.util.TransactionalServiceUtils;
028: import org.kuali.kfs.KFSPropertyConstants;
029: import org.kuali.module.purap.PurapConstants;
030: import org.kuali.module.purap.PurapPropertyConstants;
031: import org.kuali.module.purap.PurapConstants.CreditMemoStatuses;
032: import org.kuali.module.purap.dao.CreditMemoDao;
033: import org.kuali.module.purap.document.CreditMemoDocument;
034: import org.kuali.module.purap.util.VendorGroupingHelper;
035:
036: /**
037: * OJB Implementation of CreditMemoDao. Provides persistence layer methods for the credit memo document.
038: */
039: public class CreditMemoDaoOjb extends PlatformAwareDaoBaseOjb implements
040: CreditMemoDao {
041: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
042: .getLogger(CreditMemoDaoOjb.class);
043:
044: /**
045: * @see org.kuali.module.purap.dao.CreditMemoDao#getCreditMemosToExtract(java.lang.String)
046: */
047: public Iterator<CreditMemoDocument> getCreditMemosToExtract(
048: String chartCode) {
049: LOG.debug("getCreditMemosToExtract() started");
050:
051: Criteria criteria = new Criteria();
052: criteria.addEqualTo("processingCampusCode", chartCode);
053: criteria
054: .addIn(
055: "statusCode",
056: Arrays
057: .asList(CreditMemoStatuses.STATUSES_ALLOWED_FOR_EXTRACTION));
058: criteria.addIsNull("extractedDate");
059: criteria.addEqualTo("holdIndicator", Boolean.FALSE);
060:
061: return getPersistenceBrokerTemplate()
062: .getIteratorByQuery(
063: new QueryByCriteria(CreditMemoDocument.class,
064: criteria));
065: }
066:
067: /**
068: * @see org.kuali.module.purap.dao.CreditMemoDao#getCreditMemosToExtractByVendor(java.lang.String, java.lang.Integer, java.lang.Integer)
069: */
070: public Iterator<CreditMemoDocument> getCreditMemosToExtractByVendor(
071: String chartCode, VendorGroupingHelper vendor) {
072: LOG.debug("getCreditMemosToExtractByVendor() started");
073:
074: Criteria criteria = new Criteria();
075: criteria.addEqualTo("processingCampusCode", chartCode);
076: criteria
077: .addIn(
078: "statusCode",
079: Arrays
080: .asList(CreditMemoStatuses.STATUSES_ALLOWED_FOR_EXTRACTION));
081: criteria.addIsNull("extractedDate");
082: criteria.addEqualTo("holdIndicator", Boolean.FALSE);
083: criteria.addEqualTo("vendorHeaderGeneratedIdentifier", vendor
084: .getVendorHeaderGeneratedIdentifier());
085: criteria.addEqualTo("vendorDetailAssignedIdentifier", vendor
086: .getVendorDetailAssignedIdentifier());
087: criteria.addEqualTo("vendorCountryCode", vendor
088: .getVendorCountry());
089: criteria.addEqualTo("vendorPostalCode", vendor
090: .getVendorPostalCode());
091:
092: return getPersistenceBrokerTemplate()
093: .getIteratorByQuery(
094: new QueryByCriteria(CreditMemoDocument.class,
095: criteria));
096: }
097:
098: /**
099: * @see edu.iu.uis.pur.cm.dao.CreditMemoDao#duplicateExists(java.lang.String, java.lang.String)
100: */
101: public boolean duplicateExists(Integer vendorNumberHeaderId,
102: Integer vendorNumberDetailId, String creditMemoNumber) {
103: LOG.debug("duplicateExists() started");
104:
105: // criteria: vendorNumberHeader AND vendorNumberDetail AND creditMemoNumber
106: Criteria criteria = new Criteria();
107: criteria.addEqualTo("vendorHeaderGeneratedIdentifier",
108: vendorNumberHeaderId);
109: criteria.addEqualTo("vendorDetailAssignedIdentifier",
110: vendorNumberDetailId);
111: criteria.addEqualTo("creditMemoNumber", creditMemoNumber);
112:
113: criteria.addNotIn(PurapPropertyConstants.STATUS_CODE,
114: PurapConstants.CreditMemoStatuses.CANCELLED_STATUSES);
115:
116: // use the criteria to do a Count against the DB, and return the resulting
117: // number. Any positive non-zero result means that a potential duplicate
118: // exists and we return true, otherwise, return false.
119: int cmCount = getPersistenceBrokerTemplate()
120: .getCount(
121: new QueryByCriteria(CreditMemoDocument.class,
122: criteria));
123: if (cmCount > 0) {
124: return true;
125: } else {
126: return false;
127: }
128: }
129:
130: /**
131: * @see edu.iu.uis.pur.cm.dao.CreditMemoDao#duplicateExists(java.lang.String, java.lang.String)
132: */
133: public boolean duplicateExists(Integer vendorNumberHeaderId,
134: Integer vendorNumberDetailId, Date date, KualiDecimal amount) {
135: LOG.debug("duplicateExists() started");
136:
137: // criteria: vendorNumberHeader AND vendorNumberDetail AND date AND amount
138: Criteria criteria = new Criteria();
139: criteria.addEqualTo("vendorHeaderGeneratedIdentifier",
140: vendorNumberHeaderId);
141: criteria.addEqualTo("vendorDetailAssignedIdentifier",
142: vendorNumberDetailId);
143: criteria.addEqualTo("creditMemoDate", date);
144: criteria.addEqualTo("creditMemoAmount", amount);
145:
146: criteria.addNotIn(PurapPropertyConstants.STATUS_CODE,
147: PurapConstants.CreditMemoStatuses.CANCELLED_STATUSES);
148:
149: // use the criteria to do a Count against the DB, and return the resulting
150: // number. Any positive non-zero result means that a potential duplicate
151: // exists and we return true, otherwise, return false.
152: int cmCount = getPersistenceBrokerTemplate()
153: .getCount(
154: new QueryByCriteria(CreditMemoDocument.class,
155: criteria));
156: if (cmCount > 0) {
157: return true;
158: } else {
159: return false;
160: }
161: }
162:
163: /**
164: * @see org.kuali.module.purap.dao.CreditMemoDao#getDocumentNumberByCreditMemoId(java.lang.Integer)
165: */
166: public String getDocumentNumberByCreditMemoId(Integer id) {
167: Criteria criteria = new Criteria();
168: criteria.addEqualTo(PurapPropertyConstants.PURAP_DOC_ID, id);
169: return getDocumentNumberOfCreditMemoByCriteria(criteria);
170: }
171:
172: /**
173: * Retrieves a document number for a credit memo by user defined criteria.
174: *
175: * @param criteria - holds field and value pairs defined by the calling method
176: * @return - document number
177: */
178: private String getDocumentNumberOfCreditMemoByCriteria(
179: Criteria criteria) {
180: LOG.debug("getDocumentNumberOfCreditMemoByCriteria() started");
181: Iterator<Object[]> iter = getDocumentNumbersOfCreditMemoByCriteria(
182: criteria, false);
183: if (iter.hasNext()) {
184: Object[] cols = (Object[]) iter.next();
185: if (iter.hasNext()) {
186: // the iterator should have held only a single doc id of data but it holds 2 or more
187: String errorMsg = "Expected single document number for given criteria but multiple (at least 2) were returned";
188: LOG.error(errorMsg);
189: TransactionalServiceUtils.exhaustIterator(iter);
190: throw new RuntimeException();
191: }
192: // at this part of the code, we know there's no more elements in iterator
193: return (String) cols[0];
194: }
195: return null;
196: }
197:
198: /**
199: * Retrieves a document number for a credit memo by user defined criteria and sorts the values ascending if orderByAscending
200: * parameter is true, descending otherwise.
201: *
202: * @param criteria - list of criteria to use in the retrieve
203: * @param orderByAscending - boolean indicating results should be sorted ascending, descending otherwise
204: * @return - Iterator of document numbers
205: */
206: private Iterator<Object[]> getDocumentNumbersOfCreditMemoByCriteria(
207: Criteria criteria, boolean orderByAscending) {
208: LOG.debug("getDocumentNumberOfCreditMemoByCriteria() started");
209: ReportQueryByCriteria rqbc = new ReportQueryByCriteria(
210: CreditMemoDocument.class, criteria);
211: rqbc
212: .setAttributes(new String[] { KFSPropertyConstants.DOCUMENT_NUMBER });
213: if (orderByAscending) {
214: rqbc
215: .addOrderByAscending(KFSPropertyConstants.DOCUMENT_NUMBER);
216: } else {
217: rqbc
218: .addOrderByDescending(KFSPropertyConstants.DOCUMENT_NUMBER);
219: }
220: return getPersistenceBrokerTemplate()
221: .getReportQueryIteratorByQuery(rqbc);
222: }
223:
224: }
|