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.module.ld.dataaccess.impl;
017    
018    import java.util.ArrayList;
019    import java.util.Collection;
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.QueryFactory;
027    import org.apache.ojb.broker.query.ReportQueryByCriteria;
028    import org.kuali.kfs.gl.Constant;
029    import org.kuali.kfs.gl.OJBUtility;
030    import org.kuali.kfs.module.ld.LaborConstants;
031    import org.kuali.kfs.module.ld.businessobject.AccountStatusBaseFunds;
032    import org.kuali.kfs.module.ld.businessobject.EmployeeFunding;
033    import org.kuali.kfs.module.ld.businessobject.LaborCalculatedSalaryFoundationTracker;
034    import org.kuali.kfs.module.ld.dataaccess.LaborCalculatedSalaryFoundationTrackerDao;
035    import org.kuali.kfs.module.ld.util.ConsolidationUtil;
036    import org.kuali.kfs.sys.KFSPropertyConstants;
037    import org.kuali.kfs.sys.ObjectUtil;
038    import org.kuali.rice.kns.dao.impl.PlatformAwareDaoBaseOjb;
039    
040    /**
041     * This is the data access object for calculated salary foundation tracker
042     * 
043     * @see org.kuali.kfs.module.ld.businessobject.CalculatedSalaryFoundationTracker
044     */
045    public class LaborCalculatedSalaryFoundationTrackerDaoOjb extends PlatformAwareDaoBaseOjb implements LaborCalculatedSalaryFoundationTrackerDao {
046        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(LaborCalculatedSalaryFoundationTrackerDaoOjb.class);
047    
048        /**
049         * @see org.kuali.kfs.module.ld.dataaccess.LaborBaseFundsDao#findCSFTrackers(java.util.Map, boolean)
050         */
051        public List<LaborCalculatedSalaryFoundationTracker> findCSFTrackers(Map fieldValues, boolean isConsolidated) {
052            LOG.info("Start findCSFTrackers()");
053    
054            List<LaborCalculatedSalaryFoundationTracker> csfTrackerCollection = new ArrayList<LaborCalculatedSalaryFoundationTracker>();
055            if (isConsolidated) {
056                List<String> groupByList = getGroupByList(isConsolidated);
057                List<String> attributeList = getAttributeListForCSFTracker(isConsolidated, false);
058    
059                Iterator<Object[]> queryResults = this.findConsolidatedCSFTrackerRawData(fieldValues, groupByList, attributeList);
060    
061                while (queryResults != null && queryResults.hasNext()) {
062                    csfTrackerCollection.add(this.marshalCSFTracker(queryResults.next()));
063                }
064            }
065            else {
066                csfTrackerCollection.addAll(findDetailedCSFTrackerRawData(fieldValues));
067            }
068            return csfTrackerCollection;
069        }
070    
071        /**
072         * @see org.kuali.kfs.module.ld.dataaccess.LaborBaseFundsDao#findCSFTrackersAsAccountStatusBaseFunds(java.util.Map, boolean)
073         */
074        public List<AccountStatusBaseFunds> findCSFTrackersAsAccountStatusBaseFunds(Map fieldValues, boolean isConsolidated) {
075            LOG.info("Start findCSFTrackersAsAccountStatusBaseFunds()");
076    
077            List<String> groupByList = getGroupByList(isConsolidated);
078            List<String> attributeList = getAttributeListForCSFTracker(isConsolidated, false);
079    
080            Iterator<Object[]> queryResults = this.findConsolidatedCSFTrackerRawData(fieldValues, groupByList, attributeList);
081            List<AccountStatusBaseFunds> baseFundsCollection = new ArrayList<AccountStatusBaseFunds>();
082            while (queryResults != null && queryResults.hasNext()) {
083                baseFundsCollection.add(this.marshalCSFTrackerAsAccountStatusBaseFunds(queryResults.next()));
084            }
085            return baseFundsCollection;
086        }
087    
088        /**
089         * @see org.kuali.kfs.module.ld.dataaccess.LaborCalculatedSalaryFoundationTrackerDao#findCSFTrackersAsEmployeeFunding(java.util.Map,
090         *      boolean)
091         */
092        public List<EmployeeFunding> findCSFTrackersAsEmployeeFunding(Map fieldValues, boolean isConsolidated) {
093            LOG.info("Start findCSFTrackersAsEmployeeFunding()");
094    
095            List<LaborCalculatedSalaryFoundationTracker> csfTrackerCollection = findCSFTrackers(fieldValues, isConsolidated);
096            List<EmployeeFunding> employeeFundingCollection = new ArrayList<EmployeeFunding>();
097            for (LaborCalculatedSalaryFoundationTracker csfTracker : csfTrackerCollection) {
098                EmployeeFunding employeeFunding = new EmployeeFunding();
099                ObjectUtil.buildObject(employeeFunding, csfTracker);
100                employeeFundingCollection.add(employeeFunding);
101            }
102            return employeeFundingCollection;
103        }
104    
105        // get the Consolidated CSF trackers according to the given criteria
106        protected Iterator<Object[]> findConsolidatedCSFTrackerRawData(Map fieldValues, List<String> groupByList, List<String> attributeList) {
107    
108            Criteria tempCriteria1 = new Criteria();
109            tempCriteria1.addEqualTo(KFSPropertyConstants.CSF_DELETE_CODE, LaborConstants.DASHES_DELETE_CODE);
110    
111            Criteria tempCriteria2 = new Criteria();
112            tempCriteria2.addIsNull(KFSPropertyConstants.CSF_DELETE_CODE);
113    
114            /* KFSPropertyConstants.CSF_DELETE_CODE = "-" OR is null */
115            tempCriteria2.addOrCriteria(tempCriteria1);
116    
117            Criteria criteria = OJBUtility.buildCriteriaFromMap(fieldValues, new LaborCalculatedSalaryFoundationTracker());
118            criteria.addAndCriteria(tempCriteria2);
119    
120            ReportQueryByCriteria query = QueryFactory.newReportQuery(LaborCalculatedSalaryFoundationTracker.class, criteria);
121    
122            String[] groupBy = (String[]) groupByList.toArray(new String[groupByList.size()]);
123            query.addGroupBy(groupBy);
124    
125            String[] attributes = (String[]) attributeList.toArray(new String[attributeList.size()]);
126            query.setAttributes(attributes);
127    
128            return getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(query);
129        }
130    
131        // get the detailed CSF trackers according to the given criteria
132        protected Collection<LaborCalculatedSalaryFoundationTracker> findDetailedCSFTrackerRawData(Map fieldValues) {
133    
134            Criteria tempCriteria1 = new Criteria();
135            tempCriteria1.addEqualTo(KFSPropertyConstants.CSF_DELETE_CODE, LaborConstants.DASHES_DELETE_CODE);
136    
137            Criteria tempCriteria2 = new Criteria();
138            tempCriteria2.addIsNull(KFSPropertyConstants.CSF_DELETE_CODE);
139    
140            /* KFSPropertyConstants.CSF_DELETE_CODE = "-" OR is null */
141            tempCriteria2.addOrCriteria(tempCriteria1);
142    
143            Criteria criteria = OJBUtility.buildCriteriaFromMap(fieldValues, new LaborCalculatedSalaryFoundationTracker());
144            criteria.addAndCriteria(tempCriteria2);
145    
146            Query query = QueryFactory.newQuery(LaborCalculatedSalaryFoundationTracker.class, criteria);
147            return getPersistenceBrokerTemplate().getCollectionByQuery(query);
148        }
149    
150        // marshal into CalculatedSalaryFoundationTracker from the query result
151        protected LaborCalculatedSalaryFoundationTracker marshalCSFTracker(Object[] queryResult) {
152            LaborCalculatedSalaryFoundationTracker CSFTracker = new LaborCalculatedSalaryFoundationTracker();
153            List<String> keyFields = this.getAttributeListForCSFTracker(false, true);
154    
155            ObjectUtil.buildObject(CSFTracker, queryResult, keyFields);
156            return CSFTracker;
157        }
158    
159        // marshal into AccountStatusBaseFunds from the query results
160        protected AccountStatusBaseFunds marshalCSFTrackerAsAccountStatusBaseFunds(Object[] queryResult) {
161            AccountStatusBaseFunds baseFunds = new AccountStatusBaseFunds();
162            List<String> keyFields = this.getAttributeListForCSFTracker(false, true);
163    
164            ObjectUtil.buildObject(baseFunds, queryResult, keyFields);
165            return baseFunds;
166        }
167    
168        // marshal into EmployeeFunding from the query results
169        protected EmployeeFunding marshalCSFTrackerAsEmployeeFunding(Object[] queryResult) {
170            EmployeeFunding employeeFunding = new EmployeeFunding();
171            List<String> keyFields = this.getAttributeListForCSFTracker(false, true);
172    
173            ObjectUtil.buildObject(employeeFunding, queryResult, keyFields);
174            return employeeFunding;
175        }
176    
177        // define a list of attributes that are used as the grouping criteria
178        protected List<String> getGroupByList(boolean isConsolidated) {
179            List<String> groupByList = new ArrayList<String>();
180            groupByList.add(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR);
181            groupByList.add(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE);
182            groupByList.add(KFSPropertyConstants.ACCOUNT_NUMBER);
183            groupByList.add(KFSPropertyConstants.FINANCIAL_OBJECT_CODE);
184    
185            if (!isConsolidated) {
186                groupByList.add(KFSPropertyConstants.SUB_ACCOUNT_NUMBER);
187                groupByList.add(KFSPropertyConstants.FINANCIAL_SUB_OBJECT_CODE);
188            }
189            return groupByList;
190        }
191    
192        // define the return attribute list
193        protected List<String> getAttributeList(boolean isConsolidated) {
194            List<String> attributeList = getGroupByList(isConsolidated);
195    
196            if (isConsolidated) {
197                attributeList.add("'" + Constant.CONSOLIDATED_SUB_ACCOUNT_NUMBER + "'");
198                attributeList.add("'" + Constant.CONSOLIDATED_SUB_OBJECT_CODE + "'");
199            }
200            return attributeList;
201        }
202    
203        // define the return attribute list for CSF traker query
204        protected List<String> getAttributeListForCSFTracker(boolean isConsolidated, boolean isAttributeNameNeeded) {
205            List<String> attributeList = getAttributeList(isConsolidated);
206    
207            if (!isAttributeNameNeeded) {
208                attributeList.add(ConsolidationUtil.sum(KFSPropertyConstants.CSF_FULL_TIME_EMPLOYMENT_QUANTITY));
209                attributeList.add(ConsolidationUtil.sum(KFSPropertyConstants.CSF_AMOUNT));
210            }
211            else {
212                attributeList.add(KFSPropertyConstants.CSF_FULL_TIME_EMPLOYMENT_QUANTITY);
213                attributeList.add(KFSPropertyConstants.CSF_AMOUNT);
214            }
215            return attributeList;
216        }
217    }