001: /*
002: * Copyright 2006-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: /*
017: * Created on Jan 11, 2006
018: *
019: */
020: package org.kuali.module.gl.dao.ojb;
021:
022: import java.math.BigDecimal;
023: import java.util.Date;
024: import java.util.Iterator;
025:
026: import org.apache.ojb.broker.query.Criteria;
027: import org.apache.ojb.broker.query.QueryByCriteria;
028: import org.apache.ojb.broker.query.QueryFactory;
029: import org.apache.ojb.broker.query.ReportQueryByCriteria;
030: import org.kuali.core.dao.ojb.PlatformAwareDaoBaseOjb;
031: import org.kuali.core.util.TransactionalServiceUtils;
032: import org.kuali.kfs.KFSPropertyConstants;
033: import org.kuali.module.gl.bo.Entry;
034: import org.kuali.module.gl.bo.Reversal;
035: import org.kuali.module.gl.bo.Transaction;
036: import org.kuali.module.gl.dao.ReversalDao;
037:
038: /**
039: * An OJB implementation of the Reversal DAO
040: */
041: public class ReversalDaoOjb extends PlatformAwareDaoBaseOjb implements
042: ReversalDao {
043: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
044: .getLogger(ReversalDaoOjb.class);
045:
046: private final static String UNIVERISITY_FISCAL_YEAR = "universityFiscalYear";
047: private final static String CHART_OF_ACCOUNTS_CODE = "chartOfAccountsCode";
048: private final static String ACCOUNT_NUMBER = "accountNumber";
049: private final static String SUB_ACCOUNT_NUMBER = "subAccountNumber";
050: private final static String FINANCIAL_OBJECT_CODE = "financialObjectCode";
051: private final static String FINANCIAL_SUB_OBJECT_CODE = "financialSubObjectCode";
052: private final static String FINANCIAL_BALANCE_TYPE_CODE = "financialBalanceTypeCode";
053: private final static String FINANCIAL_OBJECT_TYPE_CODE = "financialObjectTypeCode";
054: private final static String UNIVERISTY_FISCAL_PERIOD_CODE = "universityFiscalPeriodCode";
055: private final static String FINANCIAL_DOCUMENT_TYPE_CODE = "financialDocumentTypeCode";
056: private final static String FINANCIAL_SYSTEM_ORIGINATION_CODE = "financialSystemOriginationCode";
057: private final static String MAX_CONSTANT = "max(documentNumber)";
058:
059: /**
060: * Constructs a ReversalDaoOjb instance
061: */
062: public ReversalDaoOjb() {
063: super ();
064: }
065:
066: /**
067: * Find the maximum transactionLedgerEntrySequenceNumber in the entry table for a specific transaction. This is used to make
068: * sure that rows added have a unique primary key.
069: *
070: * @param t a transaction to find the maximum sequence number for
071: * @return the max sequence number for the given transaction
072: */
073: public int getMaxSequenceNumber(Transaction t) {
074: LOG.debug("getSequenceNumber() ");
075:
076: Criteria crit = new Criteria();
077: crit.addEqualTo(UNIVERISITY_FISCAL_YEAR, t
078: .getUniversityFiscalYear());
079: crit.addEqualTo(CHART_OF_ACCOUNTS_CODE, t
080: .getChartOfAccountsCode());
081: crit.addEqualTo(ACCOUNT_NUMBER, t.getAccountNumber());
082: crit.addEqualTo(SUB_ACCOUNT_NUMBER, t.getSubAccountNumber());
083: crit.addEqualTo(FINANCIAL_OBJECT_CODE, t
084: .getFinancialObjectCode());
085: crit.addEqualTo(FINANCIAL_SUB_OBJECT_CODE, t
086: .getFinancialSubObjectCode());
087: crit.addEqualTo(FINANCIAL_BALANCE_TYPE_CODE, t
088: .getFinancialBalanceTypeCode());
089: crit.addEqualTo(FINANCIAL_OBJECT_TYPE_CODE, t
090: .getFinancialObjectTypeCode());
091: crit.addEqualTo(UNIVERISTY_FISCAL_PERIOD_CODE, t
092: .getUniversityFiscalPeriodCode());
093: crit.addEqualTo(FINANCIAL_DOCUMENT_TYPE_CODE, t
094: .getFinancialDocumentTypeCode());
095: crit.addEqualTo(FINANCIAL_SYSTEM_ORIGINATION_CODE, t
096: .getFinancialSystemOriginationCode());
097: crit.addEqualTo(KFSPropertyConstants.DOCUMENT_NUMBER, t
098: .getDocumentNumber());
099:
100: ReportQueryByCriteria q = QueryFactory.newReportQuery(
101: Entry.class, crit);
102: q
103: .setAttributes(new String[] { "max(transactionLedgerEntrySequenceNumber)" });
104:
105: Iterator iter = getPersistenceBrokerTemplate()
106: .getReportQueryIteratorByQuery(q);
107: if (iter.hasNext()) {
108: Object[] data = (Object[]) TransactionalServiceUtils
109: .retrieveFirstAndExhaustIterator(iter);
110: BigDecimal max = (BigDecimal) data[0]; // Don't know why OJB returns a BigDecimal, but it does
111:
112: if (max == null) {
113: return 0;
114: } else {
115: return max.intValue();
116: }
117: } else {
118: return 0;
119: }
120: }
121:
122: /**
123: * Fetches the reversal record that would affected by the posting of the given transaction
124: *
125: * @param t the transaction to find the related reversal for
126: * @return the reversal affected by the given transaction
127: * @see org.kuali.module.gl.dao.ReversalDao#getByTransaction(org.kuali.module.gl.bo.Transaction)
128: */
129: public Reversal getByTransaction(Transaction t) {
130: LOG.debug("getByTransaction() started");
131:
132: Criteria crit = new Criteria();
133: crit.addEqualTo(
134: KFSPropertyConstants.FINANCIAL_DOCUMENT_REVERSAL_DATE,
135: t.getFinancialDocumentReversalDate());
136: crit.addEqualTo(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR, t
137: .getUniversityFiscalYear());
138: crit.addEqualTo(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE, t
139: .getChartOfAccountsCode());
140: crit.addEqualTo(KFSPropertyConstants.ACCOUNT_NUMBER, t
141: .getAccountNumber());
142: crit.addEqualTo(KFSPropertyConstants.SUB_ACCOUNT_NUMBER, t
143: .getSubAccountNumber());
144: crit.addEqualTo(KFSPropertyConstants.FINANCIAL_OBJECT_CODE, t
145: .getFinancialObjectCode());
146: crit.addEqualTo(KFSPropertyConstants.FINANCIAL_SUB_OBJECT_CODE,
147: t.getFinancialSubObjectCode());
148: crit.addEqualTo(
149: KFSPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE, t
150: .getFinancialBalanceTypeCode());
151: crit.addEqualTo(
152: KFSPropertyConstants.FINANCIAL_OBJECT_TYPE_CODE, t
153: .getFinancialObjectTypeCode());
154: crit.addEqualTo(
155: KFSPropertyConstants.UNIVERSITY_FISCAL_PERIOD_CODE, t
156: .getUniversityFiscalPeriodCode());
157: crit.addEqualTo(
158: KFSPropertyConstants.FINANCIAL_DOCUMENT_TYPE_CODE, t
159: .getFinancialDocumentTypeCode());
160: crit.addEqualTo(
161: KFSPropertyConstants.FINANCIAL_SYSTEM_ORIGINATION_CODE,
162: t.getFinancialSystemOriginationCode());
163: crit.addEqualTo(KFSPropertyConstants.DOCUMENT_NUMBER, t
164: .getDocumentNumber());
165: crit.addEqualTo(
166: KFSPropertyConstants.TRANSACTION_ENTRY_SEQUENCE_NUMBER,
167: t.getTransactionLedgerEntrySequenceNumber());
168:
169: QueryByCriteria qbc = QueryFactory.newQuery(Reversal.class,
170: crit);
171: return (Reversal) getPersistenceBrokerTemplate()
172: .getObjectByQuery(qbc);
173: }
174:
175: /**
176: * Saves a reversal record
177: *
178: * @param re a reversal record to save
179: * @see org.kuali.module.gl.dao.ReversalDao#save(org.kuali.module.gl.bo.Reversal)
180: */
181: public void save(Reversal re) {
182: LOG.debug("save() started");
183:
184: getPersistenceBrokerTemplate().store(re);
185: }
186:
187: /**
188: * Fetches all reversals that have been set to reverse on or before the given date - that is to say,
189: * returns all the reversal records ready to be reversed!
190: *
191: * @param before the date that reversals must reverse on or before
192: * @return an Iterator of reversal records to reverse
193: * @see org.kuali.module.gl.dao.ReversalDao#getByDate(java.util.Date)
194: */
195: public Iterator getByDate(Date before) {
196: LOG.debug("getByDate() started");
197:
198: Criteria crit = new Criteria();
199: crit.addLessOrEqualThan(
200: KFSPropertyConstants.FINANCIAL_DOCUMENT_REVERSAL_DATE,
201: new java.sql.Date(before.getTime()));
202:
203: QueryByCriteria qbc = QueryFactory.newQuery(Reversal.class,
204: crit);
205: return getPersistenceBrokerTemplate().getIteratorByQuery(qbc);
206: }
207:
208: /**
209: * Deletes a reversal record
210: *
211: * @param re reversal to delete
212: * @see org.kuali.module.gl.dao.ReversalDao#delete(org.kuali.module.gl.bo.Reversal)
213: */
214: public void delete(Reversal re) {
215: LOG.debug("delete() started");
216:
217: getPersistenceBrokerTemplate().delete(re);
218: }
219: }
|