View Javadoc
1   /**
2    * Copyright 2004-2014 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.kpme.tklm.time.rules.graceperiod.dao;
17  
18  import java.util.ArrayList;
19  import java.util.Collections;
20  import java.util.List;
21  
22  import org.apache.commons.lang.StringUtils;
23  import org.apache.ojb.broker.query.Criteria;
24  import org.apache.ojb.broker.query.Query;
25  import org.apache.ojb.broker.query.QueryFactory;
26  import org.joda.time.LocalDate;
27  import org.kuali.kpme.core.util.OjbSubQueryUtil;
28  import org.kuali.kpme.tklm.time.rules.graceperiod.GracePeriodRule;
29  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
30  
31  public class GracePeriodDaoOjbImpl extends PlatformAwareDaoBaseOjb implements GracePeriodDao {
32  	public GracePeriodRule getGracePeriodRule(LocalDate asOfDate){
33  		GracePeriodRule gracePeriodRule = null;
34  		
35  		Criteria root = new Criteria();
36  
37          root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(GracePeriodRule.class, asOfDate, Collections.EMPTY_LIST, false));
38          root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(GracePeriodRule.class, Collections.EMPTY_LIST, false));
39  
40  		Criteria activeFilter = new Criteria(); // Inner Join For Activity
41  		activeFilter.addEqualTo("active", true);
42  		root.addAndCriteria(activeFilter);
43  		
44  		
45  		Query query = QueryFactory.newQuery(GracePeriodRule.class, root);
46  		Object obj = this.getPersistenceBrokerTemplate().getObjectByQuery(query);
47  
48  		if (obj != null) {
49  			gracePeriodRule = (GracePeriodRule) obj;
50  		}
51  		
52  		return gracePeriodRule;
53  	}
54  
55  	@Override
56  	public GracePeriodRule getGracePeriodRule(String tkGracePeriodId) {
57  		Criteria crit = new Criteria();
58  		crit.addEqualTo("tkGracePeriodRuleId", tkGracePeriodId);
59  		
60  		Query query = QueryFactory.newQuery(GracePeriodRule.class, crit);
61  		return (GracePeriodRule)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
62  	}
63  
64  	@Override
65      @SuppressWarnings("unchecked")
66      public List<GracePeriodRule> getGracePeriodRules(String hourFactor, String active, String showHistory) {
67          List<GracePeriodRule> results = new ArrayList<GracePeriodRule>();
68          
69          Criteria root = new Criteria();
70  
71          if (StringUtils.isNotBlank(hourFactor)) {
72              root.addLike("hourFactor", hourFactor);
73          }
74          
75          if (StringUtils.isNotBlank(active)) {
76          	Criteria activeFilter = new Criteria();
77              if (StringUtils.equals(active, "Y")) {
78                  activeFilter.addEqualTo("active", true);
79              } else if (StringUtils.equals(active, "N")) {
80                  activeFilter.addEqualTo("active", false);
81              }
82              root.addAndCriteria(activeFilter);
83          }
84  
85          if (StringUtils.equals(showHistory, "N")) {
86              root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithoutFilter(GracePeriodRule.class, Collections.EMPTY_LIST, false));
87              root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(GracePeriodRule.class, Collections.EMPTY_LIST, false));
88          }
89          
90          Query query = QueryFactory.newQuery(GracePeriodRule.class, root);
91          results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
92          
93          return results;
94      }
95  	
96  }