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.financial.dao.ojb;
017:
018: import java.util.ArrayList;
019: import java.util.Calendar;
020: import java.util.GregorianCalendar;
021: import java.util.Iterator;
022: import java.util.List;
023:
024: import org.apache.ojb.broker.query.Criteria;
025: import org.apache.ojb.broker.query.QueryByCriteria;
026: import org.apache.ojb.broker.query.QueryFactory;
027: import org.kuali.core.dao.ojb.PlatformAwareDaoBaseOjb;
028: import org.kuali.core.util.TransactionalServiceUtils;
029: import org.kuali.kfs.KFSConstants;
030: import org.kuali.module.financial.bo.CashieringItemInProcess;
031: import org.kuali.module.financial.bo.CashieringTransaction;
032: import org.kuali.module.financial.bo.Check;
033: import org.kuali.module.financial.bo.CheckBase;
034: import org.kuali.module.financial.bo.CoinDetail;
035: import org.kuali.module.financial.bo.CurrencyDetail;
036: import org.kuali.module.financial.dao.CashManagementDao;
037: import org.springframework.dao.DataAccessException;
038:
039: public class CashManagementDaoOjb extends PlatformAwareDaoBaseOjb
040: implements CashManagementDao {
041: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
042: .getLogger(CashManagementDaoOjb.class);
043:
044: public CashManagementDaoOjb() {
045: super ();
046: }
047:
048: /**
049: * @see org.kuali.module.financial.dao.CashManagementDao#findOpenItemsInProcessByWorkgroupName(java.lang.String)
050: */
051: public List<CashieringItemInProcess> findOpenItemsInProcessByWorkgroupName(
052: String wrkgrpName) throws DataAccessException {
053: List<CashieringItemInProcess> openItems = new ArrayList<CashieringItemInProcess>();
054: Criteria criteria = new Criteria();
055: criteria.addEqualTo("workgroupName", wrkgrpName);
056: criteria.addColumnIsNull("ITM_CLOSED_DT");
057:
058: QueryByCriteria openItemsQuery = QueryFactory.newQuery(
059: CashieringItemInProcess.class, criteria);
060: Iterator iter = getPersistenceBrokerTemplate()
061: .getIteratorByQuery(openItemsQuery);
062: while (iter.hasNext()) {
063: openItems.add((CashieringItemInProcess) iter.next());
064: }
065: return openItems;
066: }
067:
068: /**
069: * @see org.kuali.module.financial.dao.CashManagementDao#findRecentlyClosedItemsInProcess(java.lang.String)
070: */
071: public List<CashieringItemInProcess> findRecentlyClosedItemsInProcess(
072: String workgroupName) {
073: List<CashieringItemInProcess> closedItems = new ArrayList<CashieringItemInProcess>();
074:
075: Criteria criteria = new Criteria();
076: criteria.addEqualTo("workgroupName", workgroupName);
077: criteria.addColumnNotNull("ITM_CLOSED_DT");
078: Calendar thirtyDaysAgo = new GregorianCalendar();
079: thirtyDaysAgo.add(Calendar.DAY_OF_YEAR, -30);
080: criteria.addGreaterThan("itemClosedDate", new java.sql.Date(
081: thirtyDaysAgo.getTimeInMillis()));
082:
083: QueryByCriteria closedItemsQuery = QueryFactory.newQuery(
084: CashieringItemInProcess.class, criteria);
085: Iterator iter = getPersistenceBrokerTemplate()
086: .getIteratorByQuery(closedItemsQuery);
087: while (iter.hasNext()) {
088: closedItems.add((CashieringItemInProcess) iter.next());
089: }
090: return closedItems;
091: }
092:
093: /**
094: * @see org.kuali.module.financial.dao.CashManagementDao#findCoinDetailByCashieringRecordSource(java.lang.String,
095: * java.lang.String, java.lang.String)
096: */
097: public CoinDetail findCoinDetailByCashieringRecordSource(
098: String documentNumber, String documentTypeCode,
099: String cashieringRecordSource) {
100: return (CoinDetail) retrieveCashDetail(documentNumber,
101: documentTypeCode, cashieringRecordSource,
102: CoinDetail.class);
103: }
104:
105: /**
106: * @see org.kuali.module.financial.dao.CashManagementDao#findCurrencyDetailByCashieringRecordSource(java.lang.String,
107: * java.lang.String, java.lang.String)
108: */
109: public CurrencyDetail findCurrencyDetailByCashieringRecordSource(
110: String documentNumber, String documentTypeCode,
111: String cashieringRecordSource) {
112: return (CurrencyDetail) retrieveCashDetail(documentNumber,
113: documentTypeCode, cashieringRecordSource,
114: CurrencyDetail.class);
115: }
116:
117: /**
118: * This takes the primary keys for a cash or currency detail record and returns an OJB criteria for it
119: *
120: * @param documentNumber document number to retrieve
121: * @param documentTypeCode type code of the document
122: * @param cashieringRecordSource the cashiering record source
123: * @return a criteria, based on all of the given information
124: */
125: private Criteria getCashDetailCriteria(String documentNumber,
126: String documentTypeCode, String cashieringRecordSource) {
127: Criteria criteria = new Criteria();
128: criteria.addEqualTo("documentNumber", documentNumber);
129: criteria.addEqualTo("financialDocumentTypeCode",
130: documentTypeCode);
131: criteria.addEqualTo("cashieringRecordSource",
132: cashieringRecordSource);
133: return criteria;
134: }
135:
136: /**
137: * This method retrieves a cash detail from the database
138: *
139: * @param documentNumber the document number to retrieve from
140: * @param documentTypeCode the document type of the document the cash detail to look up is associated with
141: * @param cashieringRecordSource the cashiering record source to look up from
142: * @param detailType the class of the cash detail type we want
143: * @return the cash detail type record
144: */
145: private Object retrieveCashDetail(String documentNumber,
146: String documentTypeCode, String cashieringRecordSource,
147: Class detailType) {
148: QueryByCriteria cashDetailQuery = QueryFactory.newQuery(
149: detailType, getCashDetailCriteria(documentNumber,
150: documentTypeCode, cashieringRecordSource));
151: Iterator iter = getPersistenceBrokerTemplate()
152: .getIteratorByQuery(cashDetailQuery);
153: return (iter.hasNext() ? iter.next() : null);
154: }
155:
156: /**
157: * @see org.kuali.module.financial.dao.CashManagementDao#selectCashieringChecksForDeposit(java.lang.String, java.lang.Integer)
158: */
159: public List<Check> selectCashieringChecksForDeposit(
160: String documentNumber, Integer depositLineNumber) {
161: QueryByCriteria depositedChecksQuery = QueryFactory.newQuery(
162: CheckBase.class,
163: createDepositedCashieringCheckCriteria(documentNumber,
164: depositLineNumber));
165: return putResultsIntoCheckList(getPersistenceBrokerTemplate()
166: .getIteratorByQuery(depositedChecksQuery));
167: }
168:
169: /**
170: * This method creates a criteria to find the cashiering checks associated with a given deposit
171: *
172: * @param documentNumber the document number the deposit is associated with
173: * @param depositLineNumber the line number of the deposit
174: * @return a criteria to find those checks
175: */
176: private Criteria createDepositedCashieringCheckCriteria(
177: String documentNumber, Integer depositLineNumber) {
178: Criteria criteria = getCashDetailCriteria(documentNumber,
179: CashieringTransaction.DETAIL_DOCUMENT_TYPE,
180: KFSConstants.CheckSources.CASH_MANAGEMENT);
181: criteria.addEqualTo("financialDocumentDepositLineNumber",
182: depositLineNumber);
183: return criteria;
184: }
185:
186: /**
187: * This method puts the check elements of an iterator into a list
188: *
189: * @param iter an iterator with checks results in it
190: * @return a list of checks
191: */
192: private List<Check> putResultsIntoCheckList(Iterator iter) {
193: List<Check> result = new ArrayList<Check>();
194: while (iter.hasNext()) {
195: result.add((Check) iter.next());
196: }
197: return result;
198: }
199:
200: /**
201: * @see org.kuali.module.financial.dao.CashManagementDao#selectUndepositedCashieringChecks(java.lang.String)
202: */
203: public List<Check> selectUndepositedCashieringChecks(
204: String documentNumber) {
205: QueryByCriteria undepositedChecksQuery = QueryFactory
206: .newQuery(
207: CheckBase.class,
208: createUndepositedCashieringCheckCriteria(documentNumber));
209: return putResultsIntoCheckList(getPersistenceBrokerTemplate()
210: .getIteratorByQuery(undepositedChecksQuery));
211: }
212:
213: /**
214: * This method creates the criteria to find undeposited cashiering checks
215: *
216: * @param documentNumber the document number undeposited checks are associated with
217: * @return a criteria to find them
218: */
219: private Criteria createUndepositedCashieringCheckCriteria(
220: String documentNumber) {
221: Criteria criteria = getCashDetailCriteria(documentNumber,
222: CashieringTransaction.DETAIL_DOCUMENT_TYPE,
223: KFSConstants.CheckSources.CASH_MANAGEMENT);
224: criteria.addColumnIsNull("FDOC_DPST_LN_NBR");
225: return criteria;
226: }
227:
228: /**
229: * @see org.kuali.module.financial.dao.CashManagementDao#selectDepositedCashieringChecks(java.lang.String)
230: */
231: public List<Check> selectDepositedCashieringChecks(
232: String documentNumber) {
233: QueryByCriteria depositedChecksQuery = QueryFactory.newQuery(
234: CheckBase.class,
235: createDepositedCashieringCheckCriteria(documentNumber));
236: return putResultsIntoCheckList(getPersistenceBrokerTemplate()
237: .getIteratorByQuery(depositedChecksQuery));
238: }
239:
240: /**
241: * This method creates the criteria to find deposited checks
242: *
243: * @param documentNumber the CM document the checks are associated with
244: * @return a criteria to find deposited checks
245: */
246: private Criteria createDepositedCashieringCheckCriteria(
247: String documentNumber) {
248: Criteria criteria = getCashDetailCriteria(documentNumber,
249: CashieringTransaction.DETAIL_DOCUMENT_TYPE,
250: KFSConstants.CheckSources.CASH_MANAGEMENT);
251: criteria.addColumnNotNull("FDOC_DPST_LN_NBR");
252: return criteria;
253: }
254:
255: /**
256: * This method retrieves all currency details associated with a cash management document
257: *
258: * @param documentNumber the document number of the cash management document to get currency details for
259: * @return a list of currency details
260: */
261: public List<CurrencyDetail> getAllCurrencyDetails(
262: String documentNumber) {
263: QueryByCriteria allCurrencyDetailsQuery = QueryFactory
264: .newQuery(CurrencyDetail.class,
265: getAllCashDetailCriteria(documentNumber));
266: List<CurrencyDetail> result = new ArrayList<CurrencyDetail>();
267: for (Iterator iter = getPersistenceBrokerTemplate()
268: .getIteratorByQuery(allCurrencyDetailsQuery); iter
269: .hasNext();) {
270: result.add((CurrencyDetail) iter.next());
271: }
272: return result;
273: }
274:
275: /**
276: * This method gets all coin details for a particular document number, irregardless of cashiering record source
277: *
278: * @param documentNumber the document number to find cash details for
279: * @return hopefully, a bunch of coin details
280: */
281: public List<CoinDetail> getAllCoinDetails(String documentNumber) {
282: QueryByCriteria allCoinDetailsQuery = QueryFactory.newQuery(
283: CoinDetail.class,
284: getAllCashDetailCriteria(documentNumber));
285: List<CoinDetail> result = new ArrayList<CoinDetail>();
286: for (Iterator iter = getPersistenceBrokerTemplate()
287: .getIteratorByQuery(allCoinDetailsQuery); iter
288: .hasNext();) {
289: result.add((CoinDetail) iter.next());
290: }
291: return result;
292: }
293:
294: /**
295: * This method creates details for getting all of a certain cash detail, irregardless of cashiering record source
296: *
297: * @param documentNumber the document number to get cash details for
298: * @return the criteria that will allow the retrieval of the right cash details
299: */
300: private Criteria getAllCashDetailCriteria(String documentNumber) {
301: Criteria criteria = new Criteria();
302: criteria.addEqualTo("documentNumber", documentNumber);
303: criteria.addEqualTo("financialDocumentTypeCode",
304: CashieringTransaction.DETAIL_DOCUMENT_TYPE);
305: return criteria;
306: }
307:
308: /**
309: * @see org.kuali.module.financial.dao.CashManagementDao#selectNextAvailableCheckLineNumber(java.lang.String)
310: */
311: public Integer selectNextAvailableCheckLineNumber(
312: String documentNumber) {
313: if (documentNumber != null) {
314: // select all cashiering checks associated with document
315: Criteria criteria = new Criteria();
316: criteria.addEqualTo("documentNumber", documentNumber);
317: criteria.addEqualTo("cashieringRecordSource",
318: KFSConstants.CheckSources.CASH_MANAGEMENT);
319: criteria.addEqualTo("financialDocumentTypeCode",
320: CashieringTransaction.DETAIL_DOCUMENT_TYPE);
321:
322: QueryByCriteria cmChecksQuery = QueryFactory.newQuery(
323: CheckBase.class, criteria);
324: cmChecksQuery.addOrderByDescending("sequenceId");
325: Iterator allChecksIter = getPersistenceBrokerTemplate()
326: .getIteratorByQuery(cmChecksQuery);
327: if (allChecksIter.hasNext()) {
328: return new Integer(
329: (((Check) TransactionalServiceUtils
330: .retrieveFirstAndExhaustIterator(allChecksIter))
331: .getSequenceId()).intValue() + 1);
332: } else {
333: return new Integer(1);
334: }
335: } else {
336: return null;
337: }
338: }
339:
340: }
|