View Javadoc
1   /*
2    * Copyright 2007 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.fp.document.dataaccess.impl;
17  
18  import java.util.ArrayList;
19  import java.util.Calendar;
20  import java.util.GregorianCalendar;
21  import java.util.Iterator;
22  import java.util.List;
23  
24  import org.apache.ojb.broker.query.Criteria;
25  import org.apache.ojb.broker.query.QueryByCriteria;
26  import org.apache.ojb.broker.query.QueryFactory;
27  import org.kuali.ole.fp.businessobject.CashieringItemInProcess;
28  import org.kuali.ole.fp.businessobject.CashieringTransaction;
29  import org.kuali.ole.fp.businessobject.Check;
30  import org.kuali.ole.fp.businessobject.CheckBase;
31  import org.kuali.ole.fp.businessobject.CoinDetail;
32  import org.kuali.ole.fp.businessobject.CurrencyDetail;
33  import org.kuali.ole.fp.document.dataaccess.CashManagementDao;
34  import org.kuali.ole.sys.OLEConstants;
35  import org.kuali.ole.sys.util.TransactionalServiceUtils;
36  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
37  import org.springframework.dao.DataAccessException;
38  
39  public class CashManagementDaoOjb extends PlatformAwareDaoBaseOjb implements CashManagementDao {
40      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(CashManagementDaoOjb.class);
41  
42      public CashManagementDaoOjb() {
43          super();
44      }
45  
46      /**
47       * @see org.kuali.ole.fp.document.dataaccess.CashManagementDao#findOpenItemsInProcessByCampusCode(java.lang.String)
48       */
49      public List<CashieringItemInProcess> findOpenItemsInProcessByCampusCode(String campusCode) throws DataAccessException {
50          List<CashieringItemInProcess> openItems = new ArrayList<CashieringItemInProcess>();
51          Criteria criteria = new Criteria();
52          criteria.addEqualTo("campusCode", campusCode);
53          criteria.addColumnIsNull("ITM_CLOSED_DT");
54  
55          QueryByCriteria openItemsQuery = QueryFactory.newQuery(CashieringItemInProcess.class, criteria);
56          return new ArrayList<CashieringItemInProcess>( getPersistenceBrokerTemplate().getCollectionByQuery(openItemsQuery) );
57      }
58  
59      /**
60       * @see org.kuali.ole.fp.document.dataaccess.CashManagementDao#findRecentlyClosedItemsInProcess(java.lang.String)
61       */
62      public List<CashieringItemInProcess> findRecentlyClosedItemsInProcess(String campusCode) {
63          List<CashieringItemInProcess> closedItems = new ArrayList<CashieringItemInProcess>();
64  
65          Criteria criteria = new Criteria();
66          criteria.addEqualTo("campusCode", campusCode);
67          criteria.addColumnNotNull("ITM_CLOSED_DT");
68          Calendar thirtyDaysAgo = new GregorianCalendar();
69          thirtyDaysAgo.add(Calendar.DAY_OF_YEAR, -30);
70          criteria.addGreaterThan("itemClosedDate", new java.sql.Date(thirtyDaysAgo.getTimeInMillis()));
71  
72          QueryByCriteria closedItemsQuery = QueryFactory.newQuery(CashieringItemInProcess.class, criteria);
73          Iterator iter = getPersistenceBrokerTemplate().getIteratorByQuery(closedItemsQuery);
74          while (iter.hasNext()) {
75              closedItems.add((CashieringItemInProcess) iter.next());
76          }
77          return closedItems;
78      }
79  
80      /**
81       * @see org.kuali.ole.fp.document.dataaccess.CashManagementDao#findCoinDetailByCashieringStatus(java.lang.String,
82       *      java.lang.String, java.lang.String)
83       */
84      public CoinDetail findCoinDetailByCashieringStatus(String documentNumber, String documentTypeCode, String cashieringStatus) {
85          return (CoinDetail) retrieveCashDetail(documentNumber, documentTypeCode, cashieringStatus, CoinDetail.class);
86      }
87  
88      /**
89       * @see org.kuali.ole.fp.document.dataaccess.CashManagementDao#findCurrencyDetailByCashieringStatus(java.lang.String,
90       *      java.lang.String, java.lang.String)
91       */
92      public CurrencyDetail findCurrencyDetailByCashieringStatus(String documentNumber, String documentTypeCode, String cashieringStatus) {
93          return (CurrencyDetail) retrieveCashDetail(documentNumber, documentTypeCode, cashieringStatus, CurrencyDetail.class);
94      }
95  
96      /**
97       * This takes the primary keys for a cash or currency detail record and returns an OJB criteria for it
98       * 
99       * @param documentNumber document number to retrieve
100      * @param documentTypeCode type code of the document
101      * @param cashieringStatus the cashiering status
102      * @return a criteria, based on all of the given information
103      */
104     protected Criteria getCashDetailCriteria(String documentNumber, String documentTypeCode, String cashieringStatus) {
105         Criteria criteria = new Criteria();
106         criteria.addEqualTo("documentNumber", documentNumber);
107         criteria.addEqualTo("financialDocumentTypeCode", documentTypeCode);
108         criteria.addEqualTo("cashieringStatus", cashieringStatus);
109         return criteria;
110     }
111 
112     /**
113      * This method retrieves a cash detail from the database
114      * 
115      * @param documentNumber the document number to retrieve from
116      * @param documentTypeCode the document type of the document the cash detail to look up is associated with
117      * @param cashieringStatus the cashiering status to look up from
118      * @param detailType the class of the cash detail type we want
119      * @return the cash detail type record
120      */
121     protected Object retrieveCashDetail(String documentNumber, String documentTypeCode, String cashieringStatus, Class detailType) {
122         QueryByCriteria cashDetailQuery = QueryFactory.newQuery(detailType, getCashDetailCriteria(documentNumber, documentTypeCode, cashieringStatus));
123         Iterator iter = getPersistenceBrokerTemplate().getIteratorByQuery(cashDetailQuery);
124         return (iter.hasNext() ? iter.next() : null);
125     }
126 
127     /**
128      * @see org.kuali.ole.fp.document.dataaccess.CashManagementDao#selectCashieringChecksForDeposit(java.lang.String, java.lang.Integer)
129      */
130     public List<Check> selectCashieringChecksForDeposit(String documentNumber, Integer depositLineNumber) {
131         QueryByCriteria depositedChecksQuery = QueryFactory.newQuery(CheckBase.class, createDepositedCashieringCheckCriteria(documentNumber, depositLineNumber));
132         return putResultsIntoCheckList(getPersistenceBrokerTemplate().getIteratorByQuery(depositedChecksQuery));
133     }
134 
135     /**
136      * This method creates a criteria to find the cashiering checks associated with a given deposit
137      * 
138      * @param documentNumber the document number the deposit is associated with
139      * @param depositLineNumber the line number of the deposit
140      * @return a criteria to find those checks
141      */
142     protected Criteria createDepositedCashieringCheckCriteria(String documentNumber, Integer depositLineNumber) {
143         Criteria criteria = getCashDetailCriteria(documentNumber, CashieringTransaction.DETAIL_DOCUMENT_TYPE, OLEConstants.CheckSources.CASH_MANAGEMENT);
144         criteria.addEqualTo("financialDocumentDepositLineNumber", depositLineNumber);
145         return criteria;
146     }
147 
148     /**
149      * This method puts the check elements of an iterator into a list
150      * 
151      * @param iter an iterator with checks results in it
152      * @return a list of checks
153      */
154     protected List<Check> putResultsIntoCheckList(Iterator iter) {
155         List<Check> result = new ArrayList<Check>();
156         while (iter.hasNext()) {
157             result.add((Check) iter.next());
158         }
159         return result;
160     }
161 
162     /**
163      * @see org.kuali.ole.fp.document.dataaccess.CashManagementDao#selectUndepositedCashieringChecks(java.lang.String)
164      */
165     public List<Check> selectUndepositedCashieringChecks(String documentNumber) {
166         QueryByCriteria undepositedChecksQuery = QueryFactory.newQuery(CheckBase.class, createUndepositedCashieringCheckCriteria(documentNumber));
167         return putResultsIntoCheckList(getPersistenceBrokerTemplate().getIteratorByQuery(undepositedChecksQuery));
168     }
169 
170     /**
171      * This method creates the criteria to find undeposited cashiering checks
172      * 
173      * @param documentNumber the document number undeposited checks are associated with
174      * @return a criteria to find them
175      */
176     protected Criteria createUndepositedCashieringCheckCriteria(String documentNumber) {
177         Criteria criteria = getCashDetailCriteria(documentNumber, CashieringTransaction.DETAIL_DOCUMENT_TYPE, OLEConstants.CheckSources.CASH_MANAGEMENT);
178         criteria.addColumnIsNull("FDOC_DPST_LN_NBR");
179         return criteria;
180     }
181 
182     /**
183      * @see org.kuali.ole.fp.document.dataaccess.CashManagementDao#selectDepositedCashieringChecks(java.lang.String)
184      */
185     public List<Check> selectDepositedCashieringChecks(String documentNumber) {
186         QueryByCriteria depositedChecksQuery = QueryFactory.newQuery(CheckBase.class, createDepositedCashieringCheckCriteria(documentNumber));
187         return putResultsIntoCheckList(getPersistenceBrokerTemplate().getIteratorByQuery(depositedChecksQuery));
188     }
189 
190     /**
191      * This method creates the criteria to find deposited checks
192      * 
193      * @param documentNumber the CM document the checks are associated with
194      * @return a criteria to find deposited checks
195      */
196     protected Criteria createDepositedCashieringCheckCriteria(String documentNumber) {
197         Criteria criteria = getCashDetailCriteria(documentNumber, CashieringTransaction.DETAIL_DOCUMENT_TYPE, OLEConstants.CheckSources.CASH_MANAGEMENT);
198         criteria.addColumnNotNull("FDOC_DPST_LN_NBR");
199         return criteria;
200     }
201 
202     /**
203      * This method retrieves all currency details associated with a cash management document
204      * 
205      * @param documentNumber the document number of the cash management document to get currency details for
206      * @return a list of currency details
207      */
208     public List<CurrencyDetail> getAllCurrencyDetails(String documentNumber) {
209         QueryByCriteria allCurrencyDetailsQuery = QueryFactory.newQuery(CurrencyDetail.class, getAllCashDetailCriteria(documentNumber));
210         List<CurrencyDetail> result = new ArrayList<CurrencyDetail>();
211         for (Iterator iter = getPersistenceBrokerTemplate().getIteratorByQuery(allCurrencyDetailsQuery); iter.hasNext();) {
212             result.add((CurrencyDetail) iter.next());
213         }
214         return result;
215     }
216 
217     /**
218      * This method gets all coin details for a particular document number, irregardless of cashiering record source
219      * 
220      * @param documentNumber the document number to find cash details for
221      * @return hopefully, a bunch of coin details
222      */
223     public List<CoinDetail> getAllCoinDetails(String documentNumber) {
224         QueryByCriteria allCoinDetailsQuery = QueryFactory.newQuery(CoinDetail.class, getAllCashDetailCriteria(documentNumber));
225         List<CoinDetail> result = new ArrayList<CoinDetail>();
226         for (Iterator iter = getPersistenceBrokerTemplate().getIteratorByQuery(allCoinDetailsQuery); iter.hasNext();) {
227             result.add((CoinDetail) iter.next());
228         }
229         return result;
230     }
231 
232     /**
233      * This method creates details for getting all of a certain cash detail, irregardless of cashiering record source
234      * 
235      * @param documentNumber the document number to get cash details for
236      * @return the criteria that will allow the retrieval of the right cash details
237      */
238     protected Criteria getAllCashDetailCriteria(String documentNumber) {
239         Criteria criteria = new Criteria();
240         criteria.addEqualTo("documentNumber", documentNumber);
241         criteria.addEqualTo("financialDocumentTypeCode", CashieringTransaction.DETAIL_DOCUMENT_TYPE);
242         return criteria;
243     }
244 
245     /**
246      * @see org.kuali.ole.fp.document.dataaccess.CashManagementDao#selectNextAvailableCheckLineNumber(java.lang.String)
247      */
248     public Integer selectNextAvailableCheckLineNumber(String documentNumber) {
249         if (documentNumber != null) {
250             // select all cashiering checks associated with document
251             Criteria criteria = new Criteria();
252             criteria.addEqualTo("documentNumber", documentNumber);
253             criteria.addEqualTo("cashieringStatus", OLEConstants.CheckSources.CASH_MANAGEMENT);
254             criteria.addEqualTo("financialDocumentTypeCode", CashieringTransaction.DETAIL_DOCUMENT_TYPE);
255 
256             QueryByCriteria cmChecksQuery = QueryFactory.newQuery(CheckBase.class, criteria);
257             cmChecksQuery.addOrderByDescending("sequenceId");
258             Iterator allChecksIter = getPersistenceBrokerTemplate().getIteratorByQuery(cmChecksQuery);
259             if (allChecksIter.hasNext()) {
260                 return new Integer((((Check) TransactionalServiceUtils.retrieveFirstAndExhaustIterator(allChecksIter)).getSequenceId()).intValue() + 1);
261             }
262             else {
263                 return new Integer(1);
264             }
265         }
266         else {
267             return null;
268         }
269     }
270 
271 }