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