View Javadoc
1   /*
2    * Copyright 2006 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.gl.dataaccess.impl;
17  
18  import java.util.Iterator;
19  import java.util.List;
20  import java.util.Map;
21  
22  import org.apache.ojb.broker.query.Criteria;
23  import org.apache.ojb.broker.query.QueryByCriteria;
24  import org.apache.ojb.broker.query.QueryFactory;
25  import org.apache.ojb.broker.query.ReportQueryByCriteria;
26  import org.kuali.ole.gl.OJBUtility;
27  import org.kuali.ole.gl.businessobject.AccountBalance;
28  import org.kuali.ole.gl.businessobject.Transaction;
29  import org.kuali.ole.gl.dataaccess.AccountBalanceConsolidationDao;
30  import org.kuali.ole.gl.dataaccess.AccountBalanceDao;
31  import org.kuali.ole.gl.dataaccess.AccountBalanceLevelDao;
32  import org.kuali.ole.gl.dataaccess.AccountBalanceObjectDao;
33  import org.kuali.ole.sys.OLEPropertyConstants;
34  import org.kuali.ole.sys.businessobject.SystemOptions;
35  import org.kuali.ole.sys.businessobject.UniversityDate;
36  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
37  
38  /**
39   * An OJB implmentation of the AccountBalanceDao
40   */
41  public class AccountBalanceDaoOjb extends PlatformAwareDaoBaseOjb implements AccountBalanceDao {
42      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AccountBalanceDaoOjb.class);
43  
44      private AccountBalanceConsolidationDao accountBalanceConsolidationDao;
45      private AccountBalanceLevelDao accountBalanceLevelDao;
46      private AccountBalanceObjectDao accountBalanceObjectDao;
47  
48      static final private String OBJ_TYP_CD = "financialObject.financialObjectTypeCode";
49  
50      /**
51       * Given a transaction, finds a matching account balance in the database
52       * 
53       * @param t a transaction to find an appropriate related account balance for
54       * @return an appropriate account balance
55       * @see org.kuali.ole.gl.dataaccess.AccountBalanceDao#getByTransaction(org.kuali.ole.gl.businessobject.Transaction)
56       */
57      public AccountBalance getByTransaction(Transaction t) {
58          LOG.debug("getByTransaction() started");
59  
60          Criteria crit = new Criteria();
61          crit.addEqualTo(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR, t.getUniversityFiscalYear());
62          crit.addEqualTo(OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, t.getChartOfAccountsCode());
63          crit.addEqualTo(OLEPropertyConstants.ACCOUNT_NUMBER, t.getAccountNumber());
64          crit.addEqualTo(OLEPropertyConstants.SUB_ACCOUNT_NUMBER, t.getSubAccountNumber());
65          crit.addEqualTo(OLEPropertyConstants.OBJECT_CODE, t.getFinancialObjectCode());
66          crit.addEqualTo(OLEPropertyConstants.SUB_OBJECT_CODE, t.getFinancialSubObjectCode());
67  
68          QueryByCriteria qbc = QueryFactory.newQuery(AccountBalance.class, crit);
69          return (AccountBalance) getPersistenceBrokerTemplate().getObjectByQuery(qbc);
70      }
71  
72      /**
73       * This method finds the available account balances according to input fields and values
74       * 
75       * @param fieldValues the input fields and values
76       * @return the summary records of account balance entries
77       * @see org.kuali.ole.gl.dataaccess.AccountBalanceDao#findAvailableAccountBalance(java.util.Map, boolean)
78       */
79      public Iterator findConsolidatedAvailableAccountBalance(Map fieldValues) {
80          LOG.debug("findConsolidatedAvailableAccountBalance() started");
81  
82          Criteria criteria = OJBUtility.buildCriteriaFromMap(fieldValues, new AccountBalance());
83          ReportQueryByCriteria query = QueryFactory.newReportQuery(AccountBalance.class, criteria);
84  
85          String[] attributes = new String[] { OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR, OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, OLEPropertyConstants.ACCOUNT_NUMBER, OLEPropertyConstants.OBJECT_CODE, OBJ_TYP_CD, "sum(currentBudgetLineBalanceAmount)", "sum(accountLineActualsBalanceAmount)", "sum(accountLineEncumbranceBalanceAmount)" };
86  
87          String[] groupBy = new String[] { OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR, OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, OLEPropertyConstants.ACCOUNT_NUMBER, OLEPropertyConstants.OBJECT_CODE, OBJ_TYP_CD };
88  
89          query.setAttributes(attributes);
90          query.addGroupBy(groupBy);
91          OJBUtility.limitResultSize(query);
92  
93          return getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(query);
94      }
95  
96      /**
97       * This method finds the available account balances according to input fields and values
98       * 
99       * @param fieldValues the input fields and values
100      * @return account balance entries
101      * @see org.kuali.ole.gl.dataaccess.AccountBalanceDao#findAvailableAccountBalance(java.util.Map)
102      */
103     public Iterator findAvailableAccountBalance(Map fieldValues) {
104         LOG.debug("findAvailableAccountBalance(Map) started");
105 
106         Criteria criteria = OJBUtility.buildCriteriaFromMap(fieldValues, new AccountBalance());
107         QueryByCriteria query = QueryFactory.newReportQuery(AccountBalance.class, criteria);
108         OJBUtility.limitResultSize(query);
109 
110         return getPersistenceBrokerTemplate().getIteratorByQuery(query);
111     }
112 
113     /**
114      * Get available balances by consolidation for specific object types
115      * 
116      * @param objectTypes the object types that reported account balances must have
117      * @param universityFiscalYear the university fiscal year of account balances to find
118      * @param chartOfAccountsCode the chart of accounts of account balances to find
119      * @param accountNumber the account number of account balances to find
120      * @param isExcludeCostShare whether cost share entries should be excluded from this inquiry
121      * @param isConsolidated whether the results of this should be consolidated or not
122      * @param pendingEntriesCode whether to include no pending entries, approved pending entries, or all pending entries
123      * @return a List of Maps with the appropriate query results
124      * @see org.kuali.ole.gl.dataaccess.AccountBalanceDao#findAccountBalanceByConsolidationByObjectTypes(java.lang.String[],
125      *      java.lang.Integer, java.lang.String, java.lang.String, boolean, boolean, int)
126      */
127     public List findAccountBalanceByConsolidationByObjectTypes(String[] objectTypes, Integer universityFiscalYear, String chartOfAccountsCode, String accountNumber, boolean isExcludeCostShare, boolean isConsolidated, int pendingEntriesCode, SystemOptions options, UniversityDate today) {
128         LOG.debug("findAccountBalanceByConsolidationByObjectTypes() started");
129 
130         // This is in a new object just to make each class smaller and easier to read
131         try {
132             return accountBalanceConsolidationDao.findAccountBalanceByConsolidationObjectTypes(objectTypes, universityFiscalYear, chartOfAccountsCode, accountNumber, isExcludeCostShare, isConsolidated, pendingEntriesCode, options, today);
133         }
134         catch (Exception e) {
135             LOG.error("findAccountBalanceByConsolidation() " + e.getMessage(), e);
136             throw new RuntimeException(e.getMessage(), e);
137         }
138     }
139 
140     /**
141      * @see org.kuali.ole.gl.dataaccess.AccountBalanceDao#findAccountBalanceByLevel(java.lang.Integer, java.lang.String,
142      *      java.lang.String, java.lang.String, boolean, boolean, int, org.kuali.ole.sys.businessobject.UniversityDate)
143      */
144     public List findAccountBalanceByLevel(Integer universityFiscalYear, String chartOfAccountsCode, String accountNumber, String financialConsolidationObjectCode, boolean isCostShareExcluded, boolean isConsolidated, int pendingEntriesCode, UniversityDate today, SystemOptions options) {
145         LOG.debug("findAccountBalanceByLevel() started");
146 
147         // This is in a new object just to make each class smaller and easier to read
148         try {
149             return accountBalanceLevelDao.findAccountBalanceByLevel(universityFiscalYear, chartOfAccountsCode, accountNumber, financialConsolidationObjectCode, isCostShareExcluded, isConsolidated, pendingEntriesCode, today, options);
150         }
151         catch (Exception ex) {
152             LOG.error("findAccountBalanceByLevel() " + ex.getMessage(), ex);
153             throw new RuntimeException("error executing findAccountBalanceByLevel()", ex);
154         }
155     }
156 
157     /**
158      * @see org.kuali.ole.gl.dataaccess.AccountBalanceDao#findAccountBalanceByObject(java.lang.Integer, java.lang.String,
159      *      java.lang.String, java.lang.String, java.lang.String, boolean, boolean, int,
160      *      org.kuali.ole.sys.businessobject.UniversityDate)
161      */
162     public List findAccountBalanceByObject(Integer universityFiscalYear, String chartOfAccountsCode, String accountNumber, String financialObjectLevelCode, String financialReportingSortCode, boolean isCostShareExcluded, boolean isConsolidated, int pendingEntriesCode, UniversityDate today, SystemOptions options) {
163         LOG.debug("findAccountBalanceByObject() started");
164 
165         // This is in a new object just to make each class smaller and easier to read
166         try {
167             return accountBalanceObjectDao.findAccountBalanceByObject(universityFiscalYear, chartOfAccountsCode, accountNumber, financialObjectLevelCode, financialReportingSortCode, isCostShareExcluded, isConsolidated, pendingEntriesCode, today, options);
168         }
169         catch (Exception ex) {
170             LOG.error("findAccountBalanceByObject() " + ex.getMessage(), ex);
171             throw new RuntimeException(ex.getMessage(), ex);
172         }
173     }
174 
175     /**
176      * Purge an entire fiscal year for a single chart.
177      * 
178      * @param chartOfAccountsCode the chart of accounts code of account balances to purge
179      * @param year the fiscal year of account balances to purge
180      * @see org.kuali.ole.gl.dataaccess.AccountBalanceDao#purgeYearByChart(java.lang.String, int)
181      */
182     public void purgeYearByChart(String chartOfAccountsCode, int year) {
183         LOG.debug("purgeYearByChart() started");
184 
185         Criteria criteria = new Criteria();
186         criteria.addEqualTo(OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, chartOfAccountsCode);
187         criteria.addLessThan(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR, new Integer(year));
188 
189         getPersistenceBrokerTemplate().deleteByQuery(new QueryByCriteria(AccountBalance.class, criteria));
190 
191         // This is required because if any deleted account balances are in the cache, deleteByQuery doesn't
192         // remove them from the cache so a future select will retrieve these deleted account balances from
193         // the cache and return them. Clearing the cache forces OJB to go to the database again.
194         getPersistenceBrokerTemplate().clearCache();
195     }
196 
197     /**
198      * @see org.kuali.ole.gl.dataaccess.AccountBalanceDao#findCountGreaterOrEqualThan(java.lang.Integer)
199      */
200     public Integer findCountGreaterOrEqualThan(Integer year) {
201         Criteria criteria = new Criteria();
202         criteria.addGreaterOrEqualThan(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR, year);
203 
204         ReportQueryByCriteria query = QueryFactory.newReportQuery(AccountBalance.class, criteria);
205 
206         return getPersistenceBrokerTemplate().getCount(query);
207     }
208 
209     public AccountBalanceConsolidationDao getAccountBalanceConsolidationDao() {
210         return accountBalanceConsolidationDao;
211     }
212 
213     public void setAccountBalanceConsolidationDao(AccountBalanceConsolidationDao accountBalanceConsolidationDao) {
214         this.accountBalanceConsolidationDao = accountBalanceConsolidationDao;
215     }
216 
217     public AccountBalanceLevelDao getAccountBalanceLevelDao() {
218         return accountBalanceLevelDao;
219     }
220 
221     public void setAccountBalanceLevelDao(AccountBalanceLevelDao accountBalanceLevelDao) {
222         this.accountBalanceLevelDao = accountBalanceLevelDao;
223     }
224 
225     public AccountBalanceObjectDao getAccountBalanceObjectDao() {
226         return accountBalanceObjectDao;
227     }
228 
229     public void setAccountBalanceObjectDao(AccountBalanceObjectDao accountBalanceObjectDao) {
230         this.accountBalanceObjectDao = accountBalanceObjectDao;
231     }
232 }