View Javadoc
1   /*
2    * Copyright 2008 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.module.purap.document.dataaccess.impl;
17  
18  import org.apache.log4j.Logger;
19  import org.apache.ojb.broker.query.Criteria;
20  import org.apache.ojb.broker.query.Query;
21  import org.apache.ojb.broker.query.QueryByCriteria;
22  import org.kuali.ole.module.purap.businessobject.ReceivingThreshold;
23  import org.kuali.ole.module.purap.document.dataaccess.ThresholdDao;
24  import org.kuali.ole.module.purap.util.ThresholdField;
25  import org.kuali.rice.core.api.util.type.KualiDecimal;
26  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
27  import org.springframework.transaction.annotation.Transactional;
28  
29  import java.util.Collection;
30  import java.util.HashMap;
31  import java.util.List;
32  import java.util.Map;
33  
34  @Transactional
35  public class ThresholdDaoOjb extends PlatformAwareDaoBaseOjb implements ThresholdDao {
36  
37      private static Logger LOG = Logger.getLogger(ThresholdDaoOjb.class);
38  
39      public Collection<ReceivingThreshold> findByChart(String chartCode) {
40          Map criteriaFields = new HashMap(1);
41          criteriaFields.put(ThresholdField.CHART_OF_ACCOUNTS_CODE, chartCode);
42          criteriaFields.put(ThresholdField.ACTIVE, Boolean.TRUE);
43          return getThresholdEnum(criteriaFields);
44      }
45  
46      public Collection<ReceivingThreshold> findByChartAndFund(String chartCode,
47                                                               String fund) {
48          Map criteriaFields = new HashMap(2);
49          criteriaFields.put(ThresholdField.CHART_OF_ACCOUNTS_CODE, chartCode);
50          criteriaFields.put(ThresholdField.ACCOUNT_TYPE_CODE, fund);
51          criteriaFields.put(ThresholdField.ACTIVE, Boolean.TRUE);
52          return getThresholdEnum(criteriaFields);
53      }
54  
55      public Collection<ReceivingThreshold> findByChartAndSubFund(String chartCode,
56                                                                  String subFund) {
57          Map criteriaFields = new HashMap(2);
58          criteriaFields.put(ThresholdField.CHART_OF_ACCOUNTS_CODE, chartCode);
59          criteriaFields.put(ThresholdField.SUBFUND_GROUP_CODE, subFund);
60          criteriaFields.put(ThresholdField.ACTIVE, Boolean.TRUE);
61          return getThresholdEnum(criteriaFields);
62      }
63  
64      public Collection<ReceivingThreshold> findByChartAndCommodity(String chartCode,
65                                                                    String commodityCode) {
66          Map criteriaFields = new HashMap(2);
67          criteriaFields.put(ThresholdField.CHART_OF_ACCOUNTS_CODE, chartCode);
68          criteriaFields.put(ThresholdField.COMMODITY_CODE, commodityCode);
69          criteriaFields.put(ThresholdField.ACTIVE, Boolean.TRUE);
70          return getThresholdEnum(criteriaFields);
71      }
72  
73      public Collection<ReceivingThreshold> findByChartAndObjectCode(String chartCode,
74                                                                     String objectCode) {
75          Map criteriaFields = new HashMap(2);
76          criteriaFields.put(ThresholdField.CHART_OF_ACCOUNTS_CODE, chartCode);
77          criteriaFields.put(ThresholdField.FINANCIAL_OBJECT_CODE, objectCode);
78          criteriaFields.put(ThresholdField.ACTIVE, Boolean.TRUE);
79          return getThresholdEnum(criteriaFields);
80      }
81  
82      public Collection<ReceivingThreshold> findByChartAndOrg(String chartCode,
83                                                              String org) {
84          Map criteriaFields = new HashMap(2);
85          criteriaFields.put(ThresholdField.CHART_OF_ACCOUNTS_CODE, chartCode);
86          criteriaFields.put(ThresholdField.ORGANIZATION_CODE, org);
87          criteriaFields.put(ThresholdField.ACTIVE, Boolean.TRUE);
88          return getThresholdEnum(criteriaFields);
89      }
90  
91      public Collection<ReceivingThreshold> findByChartAndVendor(String chartCode,
92                                                                 String vendorHeaderGeneratedIdentifier,
93                                                                 String vendorDetailAssignedIdentifier) {
94          Map criteriaFields = new HashMap(3);
95          criteriaFields.put(ThresholdField.CHART_OF_ACCOUNTS_CODE, chartCode);
96          criteriaFields.put(ThresholdField.VENDOR_HEADER_GENERATED_ID, vendorHeaderGeneratedIdentifier);
97          criteriaFields.put(ThresholdField.VENDOR_DETAIL_ASSIGNED_ID, vendorDetailAssignedIdentifier);
98          criteriaFields.put(ThresholdField.ACTIVE, Boolean.TRUE);
99          return getThresholdEnum(criteriaFields);
100     }
101 
102     protected Collection<ReceivingThreshold> getThresholdEnum(Map criteriaFields) {
103 
104         if (criteriaFields == null || criteriaFields.size() == 0) {
105             return null;
106         }
107 
108         Criteria criteria = new Criteria();
109         List<ThresholdField> allFields = ThresholdField.getEnumList();
110         for (int i = 0; i < allFields.size(); i++) {
111             if (allFields.get(i).isPersistedField()) {
112                 Object criteriaValue = criteriaFields.get(allFields.get(i));
113                 if (criteriaValue != null) {
114                     criteria.addEqualTo(allFields.get(i).getName(), criteriaValue);
115                 } else {
116                     criteria.addIsNull(allFields.get(i).getName());
117                 }
118             }
119         }
120         Query query = new QueryByCriteria(ReceivingThreshold.class, criteria);
121         Collection c = getPersistenceBrokerTemplate().getCollectionByQuery(query);
122         return c;
123     }
124 
125     /**
126      * Will return a mock object
127      */
128     protected ReceivingThreshold returnAMockObject() {
129         ReceivingThreshold newOne = new ReceivingThreshold();
130         newOne.setThresholdAmount(new KualiDecimal(10.00));
131         return newOne;
132     }
133 
134 }