View Javadoc

1   /**
2    * Copyright 2004-2013 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.hr.time.graceperiod.dao;
17  
18  import java.sql.Date;
19  import java.util.ArrayList;
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.apache.ojb.broker.query.ReportQueryByCriteria;
27  import org.kuali.hr.time.graceperiod.rule.GracePeriodRule;
28  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
29  
30  public class GracePeriodDaoImpl extends PlatformAwareDaoBaseOjb implements GracePeriodDao {
31  	public GracePeriodRule getGracePeriodRule(Date asOfDate){
32  		GracePeriodRule gracePeriodRule = null;
33  		
34  		Criteria root = new Criteria();
35  		Criteria effdt = new Criteria();
36  		Criteria timestamp = new Criteria();
37  
38  		// OJB's awesome sub query setup part 1
39  		effdt.addLessOrEqualThan("effectiveDate", asOfDate);
40  //		effdt.addEqualTo("active", true);
41  		ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(GracePeriodRule.class, effdt);
42  		effdtSubQuery.setAttributes(new String[] { "max(effdt)" });
43  
44  		// OJB's awesome sub query setup part 2
45  		timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
46  //		timestamp.addEqualTo("active", true);
47  		ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(GracePeriodRule.class, timestamp);
48  		timestampSubQuery.setAttributes(new String[] { "max(timestamp)" });
49  
50  		root.addEqualTo("effectiveDate", effdtSubQuery);
51  		root.addEqualTo("timestamp", timestampSubQuery);
52  
53  		Criteria activeFilter = new Criteria(); // Inner Join For Activity
54  		activeFilter.addEqualTo("active", true);
55  		root.addAndCriteria(activeFilter);
56  		
57  		
58  		Query query = QueryFactory.newQuery(GracePeriodRule.class, root);
59  		Object obj = this.getPersistenceBrokerTemplate().getObjectByQuery(query);
60  
61  		if (obj != null) {
62  			gracePeriodRule = (GracePeriodRule) obj;
63  		}
64  		
65  		return gracePeriodRule;
66  	}
67  
68  	@Override
69  	public GracePeriodRule getGracePeriodRule(String tkGracePeriodId) {
70  		Criteria crit = new Criteria();
71  		crit.addEqualTo("tkGracePeriodRuleId", tkGracePeriodId);
72  		
73  		Query query = QueryFactory.newQuery(GracePeriodRule.class, crit);
74  		return (GracePeriodRule)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
75  	}
76  
77  	@Override
78      @SuppressWarnings("unchecked")
79      public List<GracePeriodRule> getGracePeriodRules(String hourFactor, String active) {
80          List<GracePeriodRule> results = new ArrayList<GracePeriodRule>();
81          
82          Criteria root = new Criteria();
83  
84          if (StringUtils.isNotBlank(hourFactor)) {
85              root.addLike("hourFactor", hourFactor);
86          }
87          
88          if (StringUtils.isNotBlank(active)) {
89          	Criteria activeFilter = new Criteria();
90              if (StringUtils.equals(active, "Y")) {
91                  activeFilter.addEqualTo("active", true);
92              } else if (StringUtils.equals(active, "N")) {
93                  activeFilter.addEqualTo("active", false);
94              }
95              root.addAndCriteria(activeFilter);
96          }
97          
98          Query query = QueryFactory.newQuery(GracePeriodRule.class, root);
99          results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
100         
101         return results;
102     }
103 	
104 }