001/*
002 * Copyright 2005-2006 The Kuali Foundation
003 * 
004 * Licensed under the Educational Community License, Version 2.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/ecl2.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 */
016package org.kuali.ole.gl.dataaccess.impl;
017
018import java.util.ArrayList;
019import java.util.Arrays;
020import java.util.Iterator;
021import java.util.List;
022import java.util.Map;
023
024import org.apache.ojb.broker.query.Criteria;
025import org.apache.ojb.broker.query.Query;
026import org.apache.ojb.broker.query.QueryByCriteria;
027import org.apache.ojb.broker.query.QueryFactory;
028import org.apache.ojb.broker.query.ReportQueryByCriteria;
029import org.kuali.ole.gl.OJBUtility;
030import org.kuali.ole.gl.businessobject.Encumbrance;
031import org.kuali.ole.gl.businessobject.Transaction;
032import org.kuali.ole.gl.dataaccess.EncumbranceDao;
033import org.kuali.ole.sys.OLEConstants;
034import org.kuali.ole.sys.OLEPropertyConstants;
035import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
036
037/**
038 * An OJB implementation of the EncumbranceDao
039 */
040public class EncumbranceDaoOjb extends PlatformAwareDaoBaseOjb implements EncumbranceDao {
041    private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(EncumbranceDaoOjb.class);
042
043    /**
044     * Returns an encumbrance that would be affected by the given transaction
045     * 
046     * @param t the transaction to find the affected encumbrance for
047     * @return an Encumbrance that would be affected by the posting of the transaction, or null
048     * @see org.kuali.ole.gl.dataaccess.EncumbranceDao#getEncumbranceByTransaction(org.kuali.ole.gl.businessobject.Transaction)
049     */
050    public Encumbrance getEncumbranceByTransaction(Transaction t) {
051        LOG.debug("getEncumbranceByTransaction() started");
052
053        Criteria crit = new Criteria();
054        crit.addEqualTo(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR, t.getUniversityFiscalYear());
055        crit.addEqualTo(OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, t.getChartOfAccountsCode());
056        crit.addEqualTo(OLEPropertyConstants.ACCOUNT_NUMBER, t.getAccountNumber());
057        crit.addEqualTo(OLEPropertyConstants.SUB_ACCOUNT_NUMBER, t.getSubAccountNumber());
058        crit.addEqualTo(OLEPropertyConstants.OBJECT_CODE, t.getFinancialObjectCode());
059        crit.addEqualTo(OLEPropertyConstants.SUB_OBJECT_CODE, t.getFinancialSubObjectCode());
060        crit.addEqualTo(OLEPropertyConstants.BALANCE_TYPE_CODE, t.getFinancialBalanceTypeCode());
061        crit.addEqualTo(OLEPropertyConstants.ENCUMBRANCE_DOCUMENT_TYPE_CODE, t.getFinancialDocumentTypeCode());
062        crit.addEqualTo(OLEPropertyConstants.ORIGIN_CODE, t.getFinancialSystemOriginationCode());
063        crit.addEqualTo(OLEPropertyConstants.DOCUMENT_NUMBER, t.getDocumentNumber());
064
065        QueryByCriteria qbc = QueryFactory.newQuery(Encumbrance.class, crit);
066        return (Encumbrance) getPersistenceBrokerTemplate().getObjectByQuery(qbc);
067    }
068
069    /**
070     * Returns an Iterator of all encumbrances that need to be closed for the fiscal year
071     * 
072     * @param fiscalYear a fiscal year to find encumbrances for
073     * @return an Iterator of encumbrances to close
074     * @see org.kuali.ole.gl.dataaccess.EncumbranceDao#getEncumbrancesToClose(java.lang.Integer)
075     */
076    public Iterator getEncumbrancesToClose(Integer fiscalYear) {
077
078        Criteria criteria = new Criteria();
079        criteria.addEqualTo(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR, fiscalYear);
080
081        QueryByCriteria query = new QueryByCriteria(Encumbrance.class, criteria);
082        query.addOrderByAscending(OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE);
083        query.addOrderByAscending(OLEPropertyConstants.ACCOUNT_NUMBER);
084        query.addOrderByAscending(OLEPropertyConstants.SUB_ACCOUNT_NUMBER);
085        query.addOrderByAscending(OLEPropertyConstants.OBJECT_CODE);
086        query.addOrderByAscending(OLEPropertyConstants.SUB_OBJECT_CODE);
087        query.addOrderByAscending(OLEPropertyConstants.BALANCE_TYPE_CODE);
088
089        return getPersistenceBrokerTemplate().getIteratorByQuery(query);
090    }
091
092    /**
093     * Purges the database of all those encumbrances with the given chart and year 
094     * 
095     * @param chartOfAccountsCode the chart of accounts code purged encumbrances will have
096     * @param year the university fiscal year purged encumbrances will have
097     * @see org.kuali.ole.gl.dataaccess.EncumbranceDao#purgeYearByChart(java.lang.String, int)
098     */
099    public void purgeYearByChart(String chartOfAccountsCode, int year) {
100        LOG.debug("purgeYearByChart() started");
101
102        Criteria criteria = new Criteria();
103        criteria.addEqualTo(OLEPropertyConstants.CHART, chartOfAccountsCode);
104        criteria.addLessThan(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR, new Integer(year));
105
106        getPersistenceBrokerTemplate().deleteByQuery(new QueryByCriteria(Encumbrance.class, criteria));
107
108        // This is required because if any deleted account balances are in the cache, deleteByQuery
109        // doesn't
110        // remove them from the cache so a future select will retrieve these deleted account
111        // balances from
112        // the cache and return them. Clearing the cache forces OJB to go to the database again.
113        getPersistenceBrokerTemplate().clearCache();
114    }
115
116    /**
117     * fetch all encumbrance records from GL open encumbrance table
118     * 
119     * @return an Iterator with all encumbrances currently in the database
120     * @see org.kuali.ole.gl.dataaccess.EncumbranceDao#getAllEncumbrances()
121     */
122    public Iterator getAllEncumbrances() {
123        Criteria criteria = new Criteria();
124        QueryByCriteria query = QueryFactory.newQuery(Encumbrance.class, criteria);
125        return getPersistenceBrokerTemplate().getIteratorByQuery(query);
126    }
127
128    /**
129     * group all encumbrances with/without the given document type code by fiscal year, chart, account, sub-account, object code,
130     * sub object code, and balance type code, and summarize the encumbrance amount and the encumbrance close amount.
131     * 
132     * @param documentTypeCode the given document type code
133     * @param included indicate if all encumbrances with the given document type are included in the results or not
134     * @return an Iterator of arrays of java.lang.Objects holding summarization data about qualifying encumbrances 
135     * @see org.kuali.ole.gl.dataaccess.EncumbranceDao#getSummarizedEncumbrances(String, boolean)
136     */
137    public Iterator getSummarizedEncumbrances(String documentTypeCode, boolean included) {
138        Criteria criteria = new Criteria();
139
140        if (included) {
141            criteria.addEqualTo(OLEPropertyConstants.ENCUMBRANCE_DOCUMENT_TYPE_CODE, documentTypeCode);
142        }
143        else {
144            criteria.addNotEqualTo(OLEPropertyConstants.ENCUMBRANCE_DOCUMENT_TYPE_CODE, documentTypeCode);
145        }
146
147        ReportQueryByCriteria query = QueryFactory.newReportQuery(Encumbrance.class, criteria);
148
149        // set the selection attributes
150        List attributeList = buildAttributeList();
151        String[] attributes = (String[]) attributeList.toArray(new String[attributeList.size()]);
152        query.setAttributes(attributes);
153
154        // add the group criteria into the selection statement
155        List groupByList = buildGroupByList();
156        String[] groupBy = (String[]) groupByList.toArray(new String[groupByList.size()]);
157        query.addGroupBy(groupBy);
158
159        return getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(query);
160    }
161
162    /**
163     * Queries the database to find all open encumbrances that qualify by the given keys
164     * 
165     * @param fieldValues the input fields and values
166     * @return a collection of open encumbrances
167     * @see org.kuali.ole.gl.dataaccess.EncumbranceDao#findOpenEncumbrance(java.util.Map)
168     */
169    public Iterator findOpenEncumbrance(Map fieldValues, boolean includeZeroEncumbrances) {
170        LOG.debug("findOpenEncumbrance() started");
171
172        Query query = this.getOpenEncumbranceQuery(fieldValues, includeZeroEncumbrances);
173        OJBUtility.limitResultSize(query);
174        return getPersistenceBrokerTemplate().getIteratorByQuery(query);
175    }
176
177    /**
178     * Counts the number of open encumbrances that have the keys given in the map
179     * 
180     * @param fieldValues the input fields and values
181     * @return the number of the open encumbrances
182     * @see org.kuali.ole.gl.dataaccess.EncumbranceDao#getOpenEncumbranceRecordCount(java.util.Map)
183     */
184    public Integer getOpenEncumbranceRecordCount(Map fieldValues, boolean includeZeroEncumbrances) {
185        LOG.debug("getOpenEncumbranceRecordCount() started");
186
187        Query query = this.getOpenEncumbranceQuery(fieldValues, includeZeroEncumbrances);
188        return getPersistenceBrokerTemplate().getCount(query);
189    }
190
191    /**
192     * build the query for encumbrance search
193     * 
194     * @param fieldValues a Map of values to use as keys for the query
195     * @param includeZeroEncumbrances should the query include encumbrances which have zeroed out?
196     * @return an OJB query
197     */
198    protected Query getOpenEncumbranceQuery(Map fieldValues, boolean includeZeroEncumbrances) {
199        Criteria criteria = OJBUtility.buildCriteriaFromMap(fieldValues, new Encumbrance());
200        criteria.addIn(OLEPropertyConstants.BALANCE_TYPE_CODE, Arrays.asList(OLEConstants.ENCUMBRANCE_BALANCE_TYPE));
201        if (!includeZeroEncumbrances) {
202            Criteria nonZeroEncumbranceCriteria = new Criteria();
203            nonZeroEncumbranceCriteria.addNotEqualToField(OLEPropertyConstants.ACCOUNT_LINE_ENCUMBRANCE_AMOUNT, OLEPropertyConstants.ACCOUNT_LINE_ENCUMBRANCE_CLOSED_AMOUNT);
204            criteria.addAndCriteria(nonZeroEncumbranceCriteria);
205        }
206        return QueryFactory.newQuery(Encumbrance.class, criteria);
207    }
208
209    /**
210     * This method builds the atrribute list used by balance searching
211     * 
212     * @return a List of encumbrance attributes that need to be summed
213     */
214    protected List buildAttributeList() {
215        List attributeList = this.buildGroupByList();
216
217        attributeList.add("sum(" + OLEPropertyConstants.ACCOUNT_LINE_ENCUMBRANCE_AMOUNT + ")");
218        attributeList.add("sum(" + OLEPropertyConstants.ACCOUNT_LINE_ENCUMBRANCE_CLOSED_AMOUNT + ")");
219
220        return attributeList;
221    }
222
223    /**
224     * This method builds group by attribute list used by balance searching
225     * 
226     * @return a List of encumbrance attributes to search on
227     */
228    protected List buildGroupByList() {
229        List attributeList = new ArrayList();
230
231        attributeList.add(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR);
232        attributeList.add(OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE);
233        attributeList.add(OLEPropertyConstants.ACCOUNT_NUMBER);
234        attributeList.add(OLEPropertyConstants.SUB_ACCOUNT_NUMBER);
235        attributeList.add(OLEPropertyConstants.OBJECT_CODE);
236        attributeList.add(OLEPropertyConstants.SUB_OBJECT_CODE);
237        attributeList.add(OLEPropertyConstants.BALANCE_TYPE_CODE);
238
239        return attributeList;
240    }
241    
242    /**
243     * @see org.kuali.ole.gl.dataaccess.EncumbranceDao#findCountGreaterOrEqualThan(java.lang.Integer)
244     */
245    public Integer findCountGreaterOrEqualThan(Integer year) {
246        Criteria criteria = new Criteria();
247        criteria.addGreaterOrEqualThan(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR, year);
248        
249        ReportQueryByCriteria query = QueryFactory.newReportQuery(Encumbrance.class, criteria);
250        
251        return getPersistenceBrokerTemplate().getCount(query);
252    }
253}