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: package org.kuali.module.gl.dao.ojb;
017:
018: import java.util.Iterator;
019: import java.util.List;
020: import java.util.Map;
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.AccountBalance;
029: import org.kuali.module.gl.bo.Transaction;
030: import org.kuali.module.gl.dao.AccountBalanceConsolidationDao;
031: import org.kuali.module.gl.dao.AccountBalanceDao;
032: import org.kuali.module.gl.dao.AccountBalanceLevelDao;
033: import org.kuali.module.gl.dao.AccountBalanceObjectDao;
034: import org.kuali.module.gl.util.OJBUtility;
035:
036: /**
037: * An OJB implmentation of the AccountBalanceDao
038: */
039: public class AccountBalanceDaoOjb extends PlatformAwareDaoBaseOjb
040: implements AccountBalanceDao {
041: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
042: .getLogger(AccountBalanceDaoOjb.class);
043:
044: private AccountBalanceConsolidationDao accountBalanceConsolidationDao;
045: private AccountBalanceLevelDao accountBalanceLevelDao;
046: private AccountBalanceObjectDao accountBalanceObjectDao;
047:
048: static final private String OBJ_TYP_CD = "financialObject.financialObjectTypeCode";
049:
050: /**
051: * Given a transaction, finds a matching account balance in the database
052: *
053: * @param t a transaction to find an appropriate related account balance for
054: * @return an appropriate account balance
055: * @see org.kuali.module.gl.dao.AccountBalanceDao#getByTransaction(org.kuali.module.gl.bo.Transaction)
056: */
057: public AccountBalance getByTransaction(Transaction t) {
058: LOG.debug("getByTransaction() started");
059:
060: Criteria crit = new Criteria();
061: crit.addEqualTo(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR, t
062: .getUniversityFiscalYear());
063: crit.addEqualTo(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE, t
064: .getChartOfAccountsCode());
065: crit.addEqualTo(KFSPropertyConstants.ACCOUNT_NUMBER, t
066: .getAccountNumber());
067: crit.addEqualTo(KFSPropertyConstants.SUB_ACCOUNT_NUMBER, t
068: .getSubAccountNumber());
069: crit.addEqualTo(KFSPropertyConstants.OBJECT_CODE, t
070: .getFinancialObjectCode());
071: crit.addEqualTo(KFSPropertyConstants.SUB_OBJECT_CODE, t
072: .getFinancialSubObjectCode());
073:
074: QueryByCriteria qbc = QueryFactory.newQuery(
075: AccountBalance.class, crit);
076: return (AccountBalance) getPersistenceBrokerTemplate()
077: .getObjectByQuery(qbc);
078: }
079:
080: /**
081: * Saves an account balance to the database
082: *
083: * @param ab an account balance to save
084: * @see org.kuali.module.gl.dao.AccountBalanceDao#save(org.kuali.module.gl.bo.AccountBalance)
085: */
086: public void save(AccountBalance ab) {
087: LOG.debug("save() started");
088:
089: getPersistenceBrokerTemplate().store(ab);
090: }
091:
092: /**
093: * This method finds the available account balances according to input fields and values
094: *
095: * @param fieldValues the input fields and values
096: * @return the summary records of account balance entries
097: * @see org.kuali.module.gl.dao.AccountBalanceDao#findAvailableAccountBalance(java.util.Map, boolean)
098: */
099: public Iterator findConsolidatedAvailableAccountBalance(
100: Map fieldValues) {
101: LOG.debug("findConsolidatedAvailableAccountBalance() started");
102:
103: Criteria criteria = OJBUtility.buildCriteriaFromMap(
104: fieldValues, new AccountBalance());
105: ReportQueryByCriteria query = QueryFactory.newReportQuery(
106: AccountBalance.class, criteria);
107:
108: String[] attributes = new String[] {
109: KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR,
110: KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE,
111: KFSPropertyConstants.ACCOUNT_NUMBER,
112: KFSPropertyConstants.OBJECT_CODE, OBJ_TYP_CD,
113: "sum(currentBudgetLineBalanceAmount)",
114: "sum(accountLineActualsBalanceAmount)",
115: "sum(accountLineEncumbranceBalanceAmount)" };
116:
117: String[] groupBy = new String[] {
118: KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR,
119: KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE,
120: KFSPropertyConstants.ACCOUNT_NUMBER,
121: KFSPropertyConstants.OBJECT_CODE, OBJ_TYP_CD };
122:
123: query.setAttributes(attributes);
124: query.addGroupBy(groupBy);
125: OJBUtility.limitResultSize(query);
126:
127: return getPersistenceBrokerTemplate()
128: .getReportQueryIteratorByQuery(query);
129: }
130:
131: /**
132: * This method finds the available account balances according to input fields and values
133: *
134: * @param fieldValues the input fields and values
135: * @return account balance entries
136: * @see org.kuali.module.gl.dao.AccountBalanceDao#findAvailableAccountBalance(java.util.Map)
137: */
138: public Iterator findAvailableAccountBalance(Map fieldValues) {
139: LOG.debug("findAvailableAccountBalance(Map) started");
140:
141: Criteria criteria = OJBUtility.buildCriteriaFromMap(
142: fieldValues, new AccountBalance());
143: QueryByCriteria query = QueryFactory.newReportQuery(
144: AccountBalance.class, criteria);
145: OJBUtility.limitResultSize(query);
146:
147: return getPersistenceBrokerTemplate().getIteratorByQuery(query);
148: }
149:
150: /**
151: * Get available balances by consolidation for specific object types
152: *
153: * @param objectTypes the object types that reported account balances must have
154: * @param universityFiscalYear the university fiscal year of account balances to find
155: * @param chartOfAccountsCode the chart of accounts of account balances to find
156: * @param accountNumber the account number of account balances to find
157: * @param isExcludeCostShare whether cost share entries should be excluded from this inquiry
158: * @param isConsolidated whether the results of this should be consolidated or not
159: * @param pendingEntriesCode whether to include no pending entries, approved pending entries, or all pending entries
160: * @return a List of Maps with the appropriate query results
161: * @see org.kuali.module.gl.dao.AccountBalanceDao#findAccountBalanceByConsolidationByObjectTypes(java.lang.String[],
162: * java.lang.Integer, java.lang.String, java.lang.String, boolean, boolean, int)
163: */
164: public List findAccountBalanceByConsolidationByObjectTypes(
165: String[] objectTypes, Integer universityFiscalYear,
166: String chartOfAccountsCode, String accountNumber,
167: boolean isExcludeCostShare, boolean isConsolidated,
168: int pendingEntriesCode) {
169: LOG
170: .debug("findAccountBalanceByConsolidationByObjectTypes() started");
171:
172: // This is in a new object just to make each class smaller and easier to read
173: try {
174: return accountBalanceConsolidationDao
175: .findAccountBalanceByConsolidationObjectTypes(
176: objectTypes, universityFiscalYear,
177: chartOfAccountsCode, accountNumber,
178: isExcludeCostShare, isConsolidated,
179: pendingEntriesCode);
180: } catch (Exception e) {
181: LOG.error("findAccountBalanceByConsolidation() "
182: + e.getMessage(), e);
183: throw new RuntimeException(e.getMessage(), e);
184: }
185: }
186:
187: /**
188: * Get available balances by level
189: *
190: * @param universityFiscalYear the university fiscal year of account balances to find
191: * @param chartOfAccountsCode the chart of accounts of account balances to find
192: * @param accountNumber the account number of account balances to find
193: * @param financialConsolidationObjectCode the consolidation code of account balances to find
194: * @param isCostShareExcluded whether cost share entries should be excluded from this inquiry
195: * @param isConsolidated whether the results of this should be consolidated or not
196: * @return a List of Mapswith the appropriate query results
197: * @see org.kuali.module.gl.dao.AccountBalanceDao#findAccountBalanceByLevel(java.lang.Integer, java.lang.String,
198: * java.lang.String, java.lang.String, boolean, boolean, int)
199: */
200: public List findAccountBalanceByLevel(Integer universityFiscalYear,
201: String chartOfAccountsCode, String accountNumber,
202: String financialConsolidationObjectCode,
203: boolean isCostShareExcluded, boolean isConsolidated,
204: int pendingEntriesCode) {
205: LOG.debug("findAccountBalanceByLevel() started");
206:
207: // This is in a new object just to make each class smaller and easier to read
208: try {
209: return accountBalanceLevelDao.findAccountBalanceByLevel(
210: universityFiscalYear, chartOfAccountsCode,
211: accountNumber, financialConsolidationObjectCode,
212: isCostShareExcluded, isConsolidated,
213: pendingEntriesCode);
214: } catch (Exception ex) {
215: LOG.error("findAccountBalanceByLevel() " + ex.getMessage(),
216: ex);
217: throw new RuntimeException(
218: "error executing findAccountBalanceByLevel()", ex);
219: }
220: }
221:
222: /**
223: * Get available balances by object
224: *
225: * @param universityFiscalYear the university fiscal year of account balances to find
226: * @param chartOfAccountsCode the chart of accounts of account balances to find
227: * @param accountNumber the account number of account balances to find
228: * @param financialObjectLevelCode the object level code of account balances to find
229: * @param financialReportingSortCode
230: * @param isCostShareExcluded whether cost share entries should be excluded from this inquiry
231: * @param isConsolidated whether the results of this should be consolidated or not
232: * @return a List of Maps with the appropriate query results
233: * @see org.kuali.module.gl.dao.AccountBalanceDao#findAccountBalanceByObject(java.lang.Integer, java.lang.String,
234: * java.lang.String, java.lang.String, java.lang.String, boolean, boolean, int)
235: */
236: public List findAccountBalanceByObject(
237: Integer universityFiscalYear, String chartOfAccountsCode,
238: String accountNumber, String financialObjectLevelCode,
239: String financialReportingSortCode,
240: boolean isCostShareExcluded, boolean isConsolidated,
241: int pendingEntriesCode) {
242: LOG.debug("findAccountBalanceByObject() started");
243:
244: // This is in a new object just to make each class smaller and easier to read
245: try {
246: return accountBalanceObjectDao.findAccountBalanceByObject(
247: universityFiscalYear, chartOfAccountsCode,
248: accountNumber, financialObjectLevelCode,
249: financialReportingSortCode, isCostShareExcluded,
250: isConsolidated, pendingEntriesCode);
251: } catch (Exception ex) {
252: LOG.error(
253: "findAccountBalanceByObject() " + ex.getMessage(),
254: ex);
255: throw new RuntimeException(ex.getMessage(), ex);
256: }
257: }
258:
259: /**
260: * Purge an entire fiscal year for a single chart.
261: *
262: * @param chartOfAccountsCode the chart of accounts code of account balances to purge
263: * @param year the fiscal year of account balances to purge
264: * @see org.kuali.module.gl.dao.AccountBalanceDao#purgeYearByChart(java.lang.String, int)
265: */
266: public void purgeYearByChart(String chartOfAccountsCode, int year) {
267: LOG.debug("purgeYearByChart() started");
268:
269: Criteria criteria = new Criteria();
270: criteria.addEqualTo(
271: KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE,
272: chartOfAccountsCode);
273: criteria.addLessThan(
274: KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR,
275: new Integer(year));
276:
277: getPersistenceBrokerTemplate().deleteByQuery(
278: new QueryByCriteria(AccountBalance.class, criteria));
279:
280: // This is required because if any deleted account balances are in the cache, deleteByQuery doesn't
281: // remove them from the cache so a future select will retrieve these deleted account balances from
282: // the cache and return them. Clearing the cache forces OJB to go to the database again.
283: getPersistenceBrokerTemplate().clearCache();
284: }
285:
286: public AccountBalanceConsolidationDao getAccountBalanceConsolidationDao() {
287: return accountBalanceConsolidationDao;
288: }
289:
290: public void setAccountBalanceConsolidationDao(
291: AccountBalanceConsolidationDao accountBalanceConsolidationDao) {
292: this .accountBalanceConsolidationDao = accountBalanceConsolidationDao;
293: }
294:
295: public AccountBalanceLevelDao getAccountBalanceLevelDao() {
296: return accountBalanceLevelDao;
297: }
298:
299: public void setAccountBalanceLevelDao(
300: AccountBalanceLevelDao accountBalanceLevelDao) {
301: this .accountBalanceLevelDao = accountBalanceLevelDao;
302: }
303:
304: public AccountBalanceObjectDao getAccountBalanceObjectDao() {
305: return accountBalanceObjectDao;
306: }
307:
308: public void setAccountBalanceObjectDao(
309: AccountBalanceObjectDao accountBalanceObjectDao) {
310: this.accountBalanceObjectDao = accountBalanceObjectDao;
311: }
312: }
|