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.cab.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.commons.lang.StringUtils;
025    import org.apache.ojb.broker.query.Criteria;
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.module.cab.CabConstants;
031    import org.kuali.kfs.module.cab.CabPropertyConstants;
032    import org.kuali.kfs.module.cab.businessobject.GeneralLedgerEntry;
033    import org.kuali.kfs.module.cab.businessobject.PurchasingAccountsPayableDocument;
034    import org.kuali.kfs.module.cab.dataaccess.PurchasingAccountsPayableReportDao;
035    import org.kuali.kfs.sys.KFSConstants;
036    import org.kuali.rice.kns.dao.impl.PlatformAwareDaoBaseOjb;
037    
038    public class PurchasingAccountsPayableReportDaoOjb extends PlatformAwareDaoBaseOjb implements PurchasingAccountsPayableReportDao {
039        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(PurchasingAccountsPayableReportDaoOjb.class);
040    
041        /**
042         * @see org.kuali.kfs.module.cab.dataaccess.PurchasingAccountsPayableReportDao#findPurchasingAccountsPayableDocuments(java.util.Map)
043         */
044        public Collection findPurchasingAccountsPayableDocuments(Map fieldValues) {
045            Criteria criteria = OJBUtility.buildCriteriaFromMap(fieldValues, new PurchasingAccountsPayableDocument());
046            QueryByCriteria query = QueryFactory.newQuery(PurchasingAccountsPayableDocument.class, criteria);
047            OJBUtility.limitResultSize(query);
048    
049            return getPersistenceBrokerTemplate().getCollectionByQuery(query);
050        }
051    
052    
053        /**
054         * @see org.kuali.kfs.module.cab.dataaccess.PurchasingAccountsPayableReportDao#findGeneralLedgers(java.util.Map)
055         */
056        public Iterator findGeneralLedgers(Map fieldValues) {
057            LOG.debug("findGeneralLedgers started...");
058            ReportQueryByCriteria query = getGeneralLedgerReportQuery(fieldValues);
059            OJBUtility.limitResultSize(query);
060            return getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(query);
061        }
062    
063        /**
064         * Generate Query object for GeneralLedgerEntry search.
065         * 
066         * @param fieldValues
067         * @return
068         */
069        protected ReportQueryByCriteria getGeneralLedgerReportQuery(Map fieldValues) {
070            Collection docTypeCodes = getDocumentType(fieldValues);
071            Collection activityStatusCodes = getActivityStatusCode(fieldValues);
072    
073            Criteria criteria = OJBUtility.buildCriteriaFromMap(fieldValues, new GeneralLedgerEntry());
074    
075            // set document type code criteria
076            if (!docTypeCodes.isEmpty()) {
077                criteria.addIn(CabPropertyConstants.GeneralLedgerEntry.FINANCIAL_DOCUMENT_TYPE_CODE, docTypeCodes);
078            }
079            // set activity status code criteria
080            if (!activityStatusCodes.isEmpty()) {
081                criteria.addIn(CabPropertyConstants.GeneralLedgerEntry.ACTIVITY_STATUS_CODE, activityStatusCodes);
082            }
083            ReportQueryByCriteria query = QueryFactory.newReportQuery(GeneralLedgerEntry.class, criteria);
084    
085            List attributeList = buildAttributeList(false);
086    
087            // set the selection attributes
088            String[] attributes = (String[]) attributeList.toArray(new String[attributeList.size()]);
089            query.setAttributes(attributes);
090            return query;
091        }
092    
093    
094        /**
095         * Get activity_statu_code
096         * 
097         * @param fieldValues
098         * @return
099         */
100        protected Collection getActivityStatusCode(Map fieldValues) {
101            Collection activityStatusCodes = new ArrayList<String>();
102    
103            if (fieldValues.containsKey(CabPropertyConstants.GeneralLedgerEntry.ACTIVITY_STATUS_CODE)) {
104                String fieldValue = (String) fieldValues.get(CabPropertyConstants.GeneralLedgerEntry.ACTIVITY_STATUS_CODE);
105                if (KFSConstants.NON_ACTIVE_INDICATOR.equalsIgnoreCase(fieldValue)) {
106                    // when selected as 'N', search for active lines as 'M'-modified by CAB user,'N'- new
107                    activityStatusCodes.add(CabConstants.ActivityStatusCode.NEW);
108                    activityStatusCodes.add(CabConstants.ActivityStatusCode.MODIFIED);
109                }
110                fieldValues.remove(CabPropertyConstants.GeneralLedgerEntry.ACTIVITY_STATUS_CODE);
111            }
112            return activityStatusCodes;
113        }
114    
115    
116        /**
117         * Get Document type code selection
118         * 
119         * @param fieldValues
120         * @return
121         */
122        protected Collection getDocumentType(Map fieldValues) {
123            Collection docTypeCodes = new ArrayList<String>();
124    
125            if (fieldValues.containsKey(CabPropertyConstants.GeneralLedgerEntry.FINANCIAL_DOCUMENT_TYPE_CODE)) {
126                String fieldValue = (String) fieldValues.get(CabPropertyConstants.GeneralLedgerEntry.FINANCIAL_DOCUMENT_TYPE_CODE);
127                if (StringUtils.isEmpty(fieldValue)) {
128                    docTypeCodes.add(CabConstants.PREQ);
129                    docTypeCodes.add(CabConstants.CM);
130                }
131                else {
132                    docTypeCodes.add(fieldValue);
133                }
134                // truncate the non-property filed
135                fieldValues.remove(CabPropertyConstants.GeneralLedgerEntry.FINANCIAL_DOCUMENT_TYPE_CODE);
136            }
137    
138            return docTypeCodes;
139        }
140    
141    
142        /**
143         * Build attribute list for select clause.
144         * 
145         * @param isExtended
146         * @return
147         */
148        protected List<String> buildAttributeList(boolean isExtended) {
149            List attributeList = new ArrayList();
150    
151            attributeList.add(CabPropertyConstants.GeneralLedgerEntry.UNIVERSITY_FISCAL_YEAR);
152            attributeList.add(CabPropertyConstants.GeneralLedgerEntry.UNIVERSITY_FISCAL_PERIOD_CODE);
153            attributeList.add(CabPropertyConstants.GeneralLedgerEntry.CHART_OF_ACCOUNTS_CODE);
154            attributeList.add(CabPropertyConstants.GeneralLedgerEntry.ACCOUNT_NUMBER);
155            attributeList.add(CabPropertyConstants.GeneralLedgerEntry.FINANCIAL_OBJECT_CODE);
156            attributeList.add(CabPropertyConstants.GeneralLedgerEntry.FINANCIAL_DOCUMENT_TYPE_CODE);
157            attributeList.add(CabPropertyConstants.GeneralLedgerEntry.DOCUMENT_NUMBER);
158            attributeList.add(CabPropertyConstants.GeneralLedgerEntry.TRANSACTION_DEBIT_CREDIT_CODE);
159            attributeList.add(CabPropertyConstants.GeneralLedgerEntry.TRANSACTION_LEDGER_ENTRY_AMOUNT);
160            attributeList.add(CabPropertyConstants.GeneralLedgerEntry.REFERENCE_FINANCIAL_DOCUMENT_NUMBER);
161            attributeList.add(CabPropertyConstants.GeneralLedgerEntry.TRANSACTION_DATE);
162            attributeList.add(CabPropertyConstants.GeneralLedgerEntry.TRANSACTION_LEDGER_SUBMIT_AMOUNT);
163            attributeList.add(CabPropertyConstants.GeneralLedgerEntry.ACTIVITY_STATUS_CODE);
164            return attributeList;
165        }
166    
167    }