001: /*
002: * Copyright 2005-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.gl.dao.ojb;
017:
018: import java.math.BigDecimal;
019: import java.util.Date;
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.QueryFactory;
025: import org.apache.ojb.broker.query.ReportQueryByCriteria;
026: import org.kuali.core.dao.ojb.PlatformAwareDaoBaseOjb;
027: import org.kuali.kfs.KFSPropertyConstants;
028: import org.kuali.module.gl.bo.Entry;
029: import org.kuali.module.gl.bo.Transaction;
030: import org.kuali.module.gl.dao.EntryDao;
031:
032: /**
033: * An OJB implementation of EntryDao
034: */
035: public class EntryDaoOjb extends PlatformAwareDaoBaseOjb implements
036: EntryDao {
037: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
038: .getLogger(EntryDaoOjb.class);
039:
040: private final static String UNIVERISITY_FISCAL_YEAR = "universityFiscalYear";
041: private final static String CHART_OF_ACCOUNTS_CODE = "chartOfAccountsCode";
042: private final static String ACCOUNT_NUMBER = "accountNumber";
043: private final static String SUB_ACCOUNT_NUMBER = "subAccountNumber";
044: private final static String FINANCIAL_OBJECT_CODE = "financialObjectCode";
045: private final static String FINANCIAL_SUB_OBJECT_CODE = "financialSubObjectCode";
046: private final static String FINANCIAL_BALANCE_TYPE_CODE = "financialBalanceTypeCode";
047: private final static String FINANCIAL_OBJECT_TYPE_CODE = "financialObjectTypeCode";
048: private final static String UNIVERISTY_FISCAL_PERIOD_CODE = "universityFiscalPeriodCode";
049: private final static String FINANCIAL_DOCUMENT_TYPE_CODE = "financialDocumentTypeCode";
050: private final static String FINANCIAL_SYSTEM_ORIGINATION_CODE = "financialSystemOriginationCode";
051: private final static String MAX_CONSTANT = "max(documentNumber)";
052:
053: /**
054: * Constructs a EntryDaoOjb instance
055: */
056: public EntryDaoOjb() {
057: super ();
058: }
059:
060: /**
061: * Turns the given transaction into an entry and then saves that entry in the database
062: *
063: * @param t the transaction to save
064: * @param postDate the officially reported posting date
065: * @see org.kuali.module.gl.dao.EntryDao#addEntry(org.kuali.module.gl.bo.Transaction, java.util.Date)
066: */
067: public void addEntry(Transaction t, Date postDate) {
068: LOG.debug("addEntry() started");
069:
070: Entry e = new Entry(t, postDate);
071:
072: getPersistenceBrokerTemplate().store(e);
073: }
074:
075: /**
076: * Find the maximum transactionLedgerEntrySequenceNumber in the entry table for a specific transaction. This is used to make
077: * sure that rows added have a unique primary key.
078: *
079: * @param t the transaction to check
080: * @return the max sequence number
081: */
082: public int getMaxSequenceNumber(Transaction t) {
083: LOG.debug("getSequenceNumber() ");
084:
085: Criteria crit = new Criteria();
086: crit.addEqualTo(UNIVERISITY_FISCAL_YEAR, t
087: .getUniversityFiscalYear());
088: crit.addEqualTo(CHART_OF_ACCOUNTS_CODE, t
089: .getChartOfAccountsCode());
090: crit.addEqualTo(ACCOUNT_NUMBER, t.getAccountNumber());
091: crit.addEqualTo(SUB_ACCOUNT_NUMBER, t.getSubAccountNumber());
092: crit.addEqualTo(FINANCIAL_OBJECT_CODE, t
093: .getFinancialObjectCode());
094: crit.addEqualTo(FINANCIAL_SUB_OBJECT_CODE, t
095: .getFinancialSubObjectCode());
096: crit.addEqualTo(FINANCIAL_BALANCE_TYPE_CODE, t
097: .getFinancialBalanceTypeCode());
098: crit.addEqualTo(FINANCIAL_OBJECT_TYPE_CODE, t
099: .getFinancialObjectTypeCode());
100: crit.addEqualTo(UNIVERISTY_FISCAL_PERIOD_CODE, t
101: .getUniversityFiscalPeriodCode());
102: crit.addEqualTo(FINANCIAL_DOCUMENT_TYPE_CODE, t
103: .getFinancialDocumentTypeCode());
104: crit.addEqualTo(FINANCIAL_SYSTEM_ORIGINATION_CODE, t
105: .getFinancialSystemOriginationCode());
106: crit.addEqualTo(KFSPropertyConstants.DOCUMENT_NUMBER, t
107: .getDocumentNumber());
108:
109: ReportQueryByCriteria q = QueryFactory.newReportQuery(
110: Entry.class, crit);
111: q
112: .setAttributes(new String[] { "max(transactionLedgerEntrySequenceNumber)" });
113:
114: Iterator iter = getPersistenceBrokerTemplate()
115: .getReportQueryIteratorByQuery(q);
116: // would this work better? max = (BigDecimal) getPersistenceBrokerTemplate().getObjectByQuery(q);
117: BigDecimal max = null;
118: while (iter.hasNext()) {
119: Object[] data = (Object[]) iter.next();
120: max = (BigDecimal) data[0]; // Don't know why OJB returns a BigDecimal, but it does
121: }
122: if (max == null) {
123: return 0;
124: } else {
125: return max.intValue();
126: }
127: }
128:
129: /**
130: * Purge the entry table by chart/year
131: *
132: * @param chart the chart of accounts code of entries to purge
133: * @param year the university fiscal year of entries to purge
134: */
135: public void purgeYearByChart(String chartOfAccountsCode, int year) {
136: LOG.debug("purgeYearByChart() started");
137:
138: Criteria criteria = new Criteria();
139: criteria
140: .addEqualTo(CHART_OF_ACCOUNTS_CODE, chartOfAccountsCode);
141: criteria
142: .addLessThan(UNIVERISITY_FISCAL_YEAR, new Integer(year));
143:
144: getPersistenceBrokerTemplate().deleteByQuery(
145: new QueryByCriteria(Entry.class, criteria));
146:
147: // This is required because if any deleted rows are in the cache, deleteByQuery doesn't
148: // remove them from the cache so a future select will retrieve these deleted account balances from
149: // the cache and return them. Clearing the cache forces OJB to go to the database again.
150: getPersistenceBrokerTemplate().clearCache();
151: }
152: }
|