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.shiftdiff.rule.dao;
17  
18  import com.google.common.collect.ImmutableList;
19  import org.apache.commons.lang.StringUtils;
20  import org.apache.ojb.broker.query.Criteria;
21  import org.apache.ojb.broker.query.Query;
22  import org.apache.ojb.broker.query.QueryFactory;
23  import org.kuali.hr.core.util.OjbSubQueryUtil;
24  import org.kuali.hr.time.shiftdiff.rule.ShiftDifferentialRule;
25  import org.kuali.hr.time.util.TKUtils;
26  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
27  
28  import java.sql.Date;
29  import java.util.ArrayList;
30  import java.util.Collection;
31  import java.util.List;
32  
33  public class ShiftDifferentialRuleDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements ShiftDifferentialRuleDao {
34  	
35  	@Override
36  	public ShiftDifferentialRule findShiftDifferentialRule(String id) {
37  		Object o = this.getPersistenceBrokerTemplate().getObjectById(ShiftDifferentialRule.class, id);
38  		
39  		return (ShiftDifferentialRule)o;
40  	}
41  
42  	@SuppressWarnings("unchecked")
43  	@Override
44  	public List<ShiftDifferentialRule> findShiftDifferentialRules(String location, String hrSalGroup, String payGrade, String pyCalendarGroup, Date asOfDate) {
45  		List<ShiftDifferentialRule> list = new ArrayList<ShiftDifferentialRule>();
46  
47  		Criteria root = new Criteria();
48  
49  		root.addEqualTo("location", location);
50  		root.addEqualTo("hrSalGroup", hrSalGroup);
51  		root.addEqualTo("payGrade", payGrade);
52  		root.addEqualTo("pyCalendarGroup", pyCalendarGroup);
53  
54          ImmutableList<String> fields = new ImmutableList.Builder<String>()
55                  .add("location")
56                  .add("hrSalGroup")
57                  .add("payGrade")
58                  .add("pyCalendarGroup")
59                  .build();
60          root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(ShiftDifferentialRule.class, asOfDate, fields, false));
61          root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(ShiftDifferentialRule.class, fields, false));
62  
63  
64  		Criteria activeFilter = new Criteria(); // Inner Join For Activity
65  		activeFilter.addEqualTo("active", true);
66  		root.addAndCriteria(activeFilter);
67  		
68  		Query query = QueryFactory.newQuery(ShiftDifferentialRule.class, root);
69  		Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
70  
71  		if (c != null) {
72  			list.addAll(c);
73  		}
74  		
75  		return list;
76  	}
77  
78  	public void saveOrUpdate(ShiftDifferentialRule shiftDifferentialRule) {
79  		this.getPersistenceBrokerTemplate().store(shiftDifferentialRule);
80  	}
81  
82  	public void saveOrUpdate(List<ShiftDifferentialRule> shiftDifferentialRules) {
83  		for (ShiftDifferentialRule sdr : shiftDifferentialRules) {
84  			saveOrUpdate(sdr);
85  		}
86  	}
87  
88  	@Override
89      @SuppressWarnings("unchecked")
90      public List<ShiftDifferentialRule> getShiftDifferentialRules(String location, String hrSalGroup, String payGrade, Date fromEffdt, Date toEffdt, 
91      															 String active, String showHistory) {
92      	
93  		List<ShiftDifferentialRule> results = new ArrayList<ShiftDifferentialRule>();
94      	
95      	Criteria root = new Criteria();
96  
97          if (StringUtils.isNotBlank(location)) {
98              root.addLike("location", location);
99          }
100         
101         if (StringUtils.isNotBlank(hrSalGroup)) {
102             root.addLike("hrSalGroup", hrSalGroup);
103         }
104         
105         if (StringUtils.isNotBlank(payGrade)) {
106             root.addLike("payGrade", payGrade);
107         }
108 
109         Criteria effectiveDateFilter = new Criteria();
110         if (fromEffdt != null) {
111             effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt);
112         }
113         if (toEffdt != null) {
114             effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt);
115         }
116         if (fromEffdt == null && toEffdt == null) {
117             effectiveDateFilter.addLessOrEqualThan("effectiveDate", TKUtils.getCurrentDate());
118         }
119 
120         
121         if (StringUtils.isNotBlank(active)) {
122         	Criteria activeFilter = new Criteria();
123             if (StringUtils.equals(active, "Y")) {
124                 activeFilter.addEqualTo("active", true);
125             } else if (StringUtils.equals(active, "N")) {
126                 activeFilter.addEqualTo("active", false);
127             }
128             root.addAndCriteria(activeFilter);
129         }
130 
131         if (StringUtils.equals(showHistory, "N")) {
132             root.addAndCriteria(effectiveDateFilter);
133         }
134         
135         Query query = QueryFactory.newQuery(ShiftDifferentialRule.class, root);
136         results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
137 
138         return results;
139     }
140 
141 }