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.core.earncode.dao;
17  
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.List;
21  
22  import org.apache.commons.lang.StringUtils;
23  import org.apache.log4j.Logger;
24  import org.apache.ojb.broker.query.Criteria;
25  import org.apache.ojb.broker.query.Query;
26  import org.apache.ojb.broker.query.QueryFactory;
27  import org.joda.time.LocalDate;
28  import org.kuali.kpme.core.earncode.EarnCode;
29  import org.kuali.kpme.core.util.OjbSubQueryUtil;
30  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
31  
32  public class EarnCodeDaoOjbImpl extends PlatformAwareDaoBaseOjb implements EarnCodeDao {
33     private static final Logger LOG = Logger.getLogger(EarnCodeDaoOjbImpl.class);
34  
35  	public void saveOrUpdate(EarnCode earnCode) {
36  		this.getPersistenceBrokerTemplate().store(earnCode);
37  	}
38  
39  	public void saveOrUpdate(List<EarnCode> earnCodeList) {
40  		if (earnCodeList != null) {
41  			for (EarnCode earnCode : earnCodeList) {
42  				this.getPersistenceBrokerTemplate().store(earnCode);
43  			}
44  		}
45  	}
46  
47  	public EarnCode getEarnCodeById(String earnCodeId) {
48  		Criteria crit = new Criteria();
49  		crit.addEqualTo("hrEarnCodeId", earnCodeId);
50  		return (EarnCode) this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(EarnCode.class, crit));
51  	}
52  
53  	@Override
54  	public EarnCode getEarnCode(String earnCode, LocalDate asOfDate) {
55  		EarnCode ec = null;
56  
57  		Criteria root = new Criteria();
58  
59  		root.addEqualTo("earnCode", earnCode);
60          root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(EarnCode.class, asOfDate, EarnCode.EQUAL_TO_FIELDS, false));
61          root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(EarnCode.class, EarnCode.EQUAL_TO_FIELDS, false));
62  
63  		Criteria activeFilter = new Criteria(); // Inner Join For Activity
64  		activeFilter.addEqualTo("active", true);
65  		root.addAndCriteria(activeFilter);
66  		
67  		
68  		Query query = QueryFactory.newQuery(EarnCode.class, root);
69  		Object obj = this.getPersistenceBrokerTemplate().getObjectByQuery(query);
70  
71  		if (obj != null) {
72  			ec = (EarnCode) obj;
73  		}
74  
75  		return ec;
76  	}
77  
78  	@Override
79  	public List<EarnCode> getOvertimeEarnCodes(LocalDate asOfDate) {
80  		Criteria root = new Criteria();
81  
82  		root.addEqualTo("ovtEarnCode", "Y");
83          root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(EarnCode.class, asOfDate, EarnCode.EQUAL_TO_FIELDS, false));
84          root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(EarnCode.class, EarnCode.EQUAL_TO_FIELDS, false));
85  //		root.addEqualTo("active", true);
86  		
87  		Criteria activeFilter = new Criteria(); // Inner Join For Activity
88  		activeFilter.addEqualTo("active", true);
89  		root.addAndCriteria(activeFilter);
90  		
91  		
92  		Query query = QueryFactory.newQuery(EarnCode.class, root);
93  		List<EarnCode> ovtEarnCodes = (List<EarnCode>)this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
94  		return ovtEarnCodes;
95  	}
96  	
97  	@Override
98  	public int getEarnCodeCount(String earnCode) {
99  		Criteria crit = new Criteria();
100 		crit.addEqualTo("earnCode", earnCode);
101 		Query query = QueryFactory.newQuery(EarnCode.class, crit);
102 		return this.getPersistenceBrokerTemplate().getCount(query);
103 	}
104 	
105 	@Override
106 	public int getNewerEarnCodeCount(String earnCode, LocalDate effdt) {
107 		Criteria crit = new Criteria();
108 		crit.addEqualTo("earnCode", earnCode);
109 		crit.addEqualTo("active", "Y");
110 		if(effdt != null) {
111 			crit.addGreaterThan("effectiveDate", effdt.toDate());
112 		}
113 		Query query = QueryFactory.newQuery(EarnCode.class, crit);
114        	return this.getPersistenceBrokerTemplate().getCount(query);
115 	}
116 
117 	@Override
118 	public List<EarnCode> getEarnCodes(String leavePlan, LocalDate asOfDate) {
119 		List<EarnCode> earnCodes = new ArrayList<EarnCode>();
120 		Criteria root = new Criteria();
121 
122         List<String> fields = new ArrayList<String>();
123         fields.add("earnCode");
124         fields.add("leavePlan");
125 		root.addEqualTo("leavePlan", leavePlan);
126         root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(EarnCode.class, asOfDate, fields, false));
127         root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(EarnCode.class, fields, false));
128 		
129 		Criteria activeFilter = new Criteria(); // Inner Join For Activity
130 		activeFilter.addEqualTo("active", true);
131 		root.addAndCriteria(activeFilter);
132 		
133 		
134 		Query query = QueryFactory.newQuery(EarnCode.class, root);
135 		Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
136 
137 		if (c != null) {
138 			earnCodes.addAll(c);
139 		}	
140 		return earnCodes;
141 	}
142 
143 	@Override
144     @SuppressWarnings("unchecked")
145     public List<EarnCode> getEarnCodes(String earnCode, String ovtEarnCode, String descr, String leavePlan, String accrualCategory, LocalDate fromEffdt, LocalDate toEffdt, String active, String showHistory) {
146         List<EarnCode> results = new ArrayList<EarnCode>();
147         
148         Criteria root = new Criteria();
149         
150         if (StringUtils.isNotBlank(earnCode)) {
151             root.addLike("earnCode", earnCode);
152         }
153         
154         if (StringUtils.isNotBlank(ovtEarnCode)) {
155             root.addEqualTo("ovtEarnCode", ovtEarnCode);
156         }
157         
158         if (StringUtils.isNotBlank(descr)) {
159             root.addLike("UPPER(`descr`)", descr.toUpperCase()); // KPME-2695
160         }
161 
162         if (StringUtils.isNotBlank(leavePlan)) {
163         	root.addLike("UPPER(`leave_plan`)", leavePlan.toUpperCase()); // KPME-2695 
164         }
165         
166         if (StringUtils.isNotBlank(accrualCategory)) {
167         	root.addLike("UPPER(`accrual_category`)", accrualCategory.toUpperCase()); // KPME-2695 
168         }
169         
170         Criteria effectiveDateFilter = new Criteria();
171         if (fromEffdt != null) {
172             effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt.toDate());
173         }
174         if (toEffdt != null) {
175             effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt.toDate());
176         }
177         if (fromEffdt == null && toEffdt == null) {
178             effectiveDateFilter.addLessOrEqualThan("effectiveDate", LocalDate.now().toDate());
179         }
180         root.addAndCriteria(effectiveDateFilter);
181         
182         if (StringUtils.isNotBlank(active)) {
183         	Criteria activeFilter = new Criteria();
184             if (StringUtils.equals(active, "Y")) {
185                 activeFilter.addEqualTo("active", true);
186             } else if (StringUtils.equals(active, "N")) {
187                 activeFilter.addEqualTo("active", false);
188             }
189             root.addAndCriteria(activeFilter);
190         }
191 
192         if (StringUtils.equals(showHistory, "N")) {
193             root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithFilter(EarnCode.class, effectiveDateFilter, EarnCode.EQUAL_TO_FIELDS, false));
194             root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(EarnCode.class, EarnCode.EQUAL_TO_FIELDS, false));
195         }
196         
197         Query query = QueryFactory.newQuery(EarnCode.class, root);
198         results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
199         
200         return results;
201     }
202 	
203 }