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.purap.document.dataaccess.impl;
017    
018    import java.util.Collection;
019    import java.util.HashMap;
020    import java.util.List;
021    import java.util.Map;
022    
023    import org.apache.log4j.Logger;
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.kuali.kfs.module.purap.businessobject.ReceivingThreshold;
028    import org.kuali.kfs.module.purap.document.dataaccess.ThresholdDao;
029    import org.kuali.kfs.module.purap.util.ThresholdField;
030    import org.kuali.rice.kns.dao.impl.PlatformAwareDaoBaseOjb;
031    import org.kuali.rice.kns.util.KualiDecimal;
032    
033    public class ThresholdDaoOjb extends PlatformAwareDaoBaseOjb implements ThresholdDao {
034    
035        private static Logger LOG = Logger.getLogger(ThresholdDaoOjb.class);
036        
037        public Collection<ReceivingThreshold> findByChart(String chartCode) {
038            Map criteriaFields = new HashMap(1);
039            criteriaFields.put(ThresholdField.CHART_OF_ACCOUNTS_CODE,chartCode);
040            criteriaFields.put(ThresholdField.ACTIVE,Boolean.TRUE);
041            return getThresholdEnum(criteriaFields);
042        }
043    
044        public Collection<ReceivingThreshold> findByChartAndFund(String chartCode, 
045                                                        String fund) {
046            Map criteriaFields = new HashMap(2);
047            criteriaFields.put(ThresholdField.CHART_OF_ACCOUNTS_CODE,chartCode);
048            criteriaFields.put(ThresholdField.ACCOUNT_TYPE_CODE,fund);
049            criteriaFields.put(ThresholdField.ACTIVE,Boolean.TRUE);
050            return getThresholdEnum(criteriaFields);
051        }
052    
053        public Collection<ReceivingThreshold> findByChartAndSubFund(String chartCode, 
054                                                           String subFund) {
055            Map criteriaFields = new HashMap(2);
056            criteriaFields.put(ThresholdField.CHART_OF_ACCOUNTS_CODE,chartCode);
057            criteriaFields.put(ThresholdField.SUBFUND_GROUP_CODE,subFund);
058            criteriaFields.put(ThresholdField.ACTIVE,Boolean.TRUE);
059            return getThresholdEnum(criteriaFields);
060        }
061    
062        public Collection<ReceivingThreshold> findByChartAndCommodity(String chartCode, 
063                                                             String commodityCode) {
064            Map criteriaFields = new HashMap(2);
065            criteriaFields.put(ThresholdField.CHART_OF_ACCOUNTS_CODE,chartCode);
066            criteriaFields.put(ThresholdField.COMMODITY_CODE,commodityCode);
067            criteriaFields.put(ThresholdField.ACTIVE,Boolean.TRUE);
068            return getThresholdEnum(criteriaFields);
069        }
070    
071        public Collection<ReceivingThreshold> findByChartAndObjectCode(String chartCode, 
072                                                              String objectCode) {
073            Map criteriaFields = new HashMap(2);
074            criteriaFields.put(ThresholdField.CHART_OF_ACCOUNTS_CODE,chartCode);
075            criteriaFields.put(ThresholdField.FINANCIAL_OBJECT_CODE,objectCode);
076            criteriaFields.put(ThresholdField.ACTIVE,Boolean.TRUE);
077            return getThresholdEnum(criteriaFields);
078        }
079    
080        public Collection<ReceivingThreshold> findByChartAndOrg(String chartCode, 
081                                                       String org) {
082            Map criteriaFields = new HashMap(2);
083            criteriaFields.put(ThresholdField.CHART_OF_ACCOUNTS_CODE,chartCode);
084            criteriaFields.put(ThresholdField.ORGANIZATION_CODE,org);
085            criteriaFields.put(ThresholdField.ACTIVE,Boolean.TRUE);
086            return getThresholdEnum(criteriaFields);
087        }
088    
089        public Collection<ReceivingThreshold> findByChartAndVendor(String chartCode, 
090                                                          String vendorHeaderGeneratedIdentifier,
091                                                          String vendorDetailAssignedIdentifier) {
092            Map criteriaFields = new HashMap(3);
093            criteriaFields.put(ThresholdField.CHART_OF_ACCOUNTS_CODE,chartCode);
094            criteriaFields.put(ThresholdField.VENDOR_HEADER_GENERATED_ID,vendorHeaderGeneratedIdentifier);
095            criteriaFields.put(ThresholdField.VENDOR_DETAIL_ASSIGNED_ID,vendorDetailAssignedIdentifier);
096            criteriaFields.put(ThresholdField.ACTIVE,Boolean.TRUE);
097            return getThresholdEnum(criteriaFields);
098        }
099        
100        protected Collection<ReceivingThreshold> getThresholdEnum(Map criteriaFields){
101            
102            if (criteriaFields == null || criteriaFields.size() == 0){
103                return null;
104            }
105    
106            Criteria criteria = new Criteria();
107            List<ThresholdField> allFields = ThresholdField.getEnumList();
108            for (int i = 0; i < allFields.size(); i++) {
109                if (allFields.get(i).isPersistedField()){
110                    Object criteriaValue = criteriaFields.get(allFields.get(i));
111                    if (criteriaValue != null){
112                        criteria.addEqualTo(allFields.get(i).getName(), criteriaValue);
113                    }else{
114                         criteria.addIsNull(allFields.get(i).getName());
115                    }
116                }
117            }
118            Query query = new QueryByCriteria(ReceivingThreshold.class, criteria);
119            Collection c = getPersistenceBrokerTemplate().getCollectionByQuery(query);
120            return c;
121        }
122        
123        /**
124         * Will return a mock object
125         */
126        protected ReceivingThreshold returnAMockObject(){
127            ReceivingThreshold newOne = new ReceivingThreshold();
128            newOne.setThresholdAmount(new KualiDecimal(10.00));
129            return newOne;
130        }
131    
132    }