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.earncode.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.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.apache.ojb.broker.query.ReportQueryByCriteria;
28  import org.kuali.hr.time.earncode.EarnCode;
29  import org.kuali.hr.time.util.TKUtils;
30  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
31  
32  public class EarnCodeDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements EarnCodeDao {
33  
34  	@SuppressWarnings("unused")
35  	private static final Logger LOG = Logger.getLogger(EarnCodeDaoSpringOjbImpl.class);
36  
37  	public void saveOrUpdate(EarnCode earnCode) {
38  		this.getPersistenceBrokerTemplate().store(earnCode);
39  	}
40  
41  	public void saveOrUpdate(List<EarnCode> earnCodeList) {
42  		if (earnCodeList != null) {
43  			for (EarnCode earnCode : earnCodeList) {
44  				this.getPersistenceBrokerTemplate().store(earnCode);
45  			}
46  		}
47  	}
48  
49  	public EarnCode getEarnCodeById(String earnCodeId) {
50  		Criteria crit = new Criteria();
51  		crit.addEqualTo("hrEarnCodeId", earnCodeId);
52  		return (EarnCode) this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(EarnCode.class, crit));
53  	}
54  
55  	@Override
56  	public EarnCode getEarnCode(String earnCode, Date asOfDate) {
57  		EarnCode ec = null;
58  
59  		Criteria root = new Criteria();
60  		Criteria effdt = new Criteria();
61  		Criteria timestamp = new Criteria();
62  
63  		// OJB's awesome sub query setup part 1
64  		effdt.addEqualToField("earnCode", Criteria.PARENT_QUERY_PREFIX + "earnCode");
65  		effdt.addLessOrEqualThan("effectiveDate", asOfDate);
66  //		effdt.addEqualTo("active", true);
67  		ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(EarnCode.class, effdt);
68  		effdtSubQuery.setAttributes(new String[] { "max(effdt)" });
69  
70  		// OJB's awesome sub query setup part 2
71  		timestamp.addEqualToField("earnCode", Criteria.PARENT_QUERY_PREFIX + "earnCode");
72  		timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
73  //		timestamp.addEqualTo("active", true);
74  		ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(EarnCode.class, timestamp);
75  		timestampSubQuery.setAttributes(new String[] { "max(timestamp)" });
76  
77  		root.addEqualTo("earnCode", earnCode);
78  		root.addEqualTo("effectiveDate", effdtSubQuery);
79  		root.addEqualTo("timestamp", timestampSubQuery);
80  //		root.addEqualTo("active", true);
81  		
82  		Criteria activeFilter = new Criteria(); // Inner Join For Activity
83  		activeFilter.addEqualTo("active", true);
84  		root.addAndCriteria(activeFilter);
85  		
86  		
87  		Query query = QueryFactory.newQuery(EarnCode.class, root);
88  		Object obj = this.getPersistenceBrokerTemplate().getObjectByQuery(query);
89  
90  		if (obj != null) {
91  			ec = (EarnCode) obj;
92  		}
93  
94  		return ec;
95  	}
96  
97  	@Override
98  	public List<EarnCode> getOvertimeEarnCodes(Date asOfDate) {
99  		Criteria root = new Criteria();
100 		Criteria effdt = new Criteria();
101 		Criteria timestamp = new Criteria();
102 
103 		// OJB's awesome sub query setup part 1
104 		effdt.addEqualToField("earnCode", Criteria.PARENT_QUERY_PREFIX + "earnCode");
105 		effdt.addLessOrEqualThan("effectiveDate", asOfDate);
106 //		effdt.addEqualTo("active", true);
107 		ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(EarnCode.class, effdt);
108 		effdtSubQuery.setAttributes(new String[] { "max(effdt)" });
109 
110 		// OJB's awesome sub query setup part 2
111 		timestamp.addEqualToField("earnCode", Criteria.PARENT_QUERY_PREFIX + "earnCode");
112 		timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
113 //		timestamp.addEqualTo("active", true);
114 		ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(EarnCode.class, timestamp);
115 		timestampSubQuery.setAttributes(new String[] { "max(timestamp)" });
116 
117 		root.addEqualTo("ovtEarnCode", "Y");
118 		root.addEqualTo("effectiveDate", effdtSubQuery);
119 		root.addEqualTo("timestamp", timestampSubQuery);
120 //		root.addEqualTo("active", true);
121 		
122 		Criteria activeFilter = new Criteria(); // Inner Join For Activity
123 		activeFilter.addEqualTo("active", true);
124 		root.addAndCriteria(activeFilter);
125 		
126 		
127 		Query query = QueryFactory.newQuery(EarnCode.class, root);
128 		List<EarnCode> ovtEarnCodes = (List<EarnCode>)this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
129 		return ovtEarnCodes;
130 	}
131 	
132 	@Override
133 	public int getEarnCodeCount(String earnCode) {
134 		Criteria crit = new Criteria();
135 		crit.addEqualTo("earnCode", earnCode);
136 		Query query = QueryFactory.newQuery(EarnCode.class, crit);
137 		return this.getPersistenceBrokerTemplate().getCount(query);
138 	}
139 	
140 	@Override
141 	public int getNewerEarnCodeCount(String earnCode, Date effdt) {
142 		Criteria crit = new Criteria();
143 		crit.addEqualTo("earnCode", earnCode);
144 		crit.addEqualTo("active", "Y");
145 		crit.addGreaterThan("effectiveDate", effdt);
146 		Query query = QueryFactory.newQuery(EarnCode.class, crit);
147        	return this.getPersistenceBrokerTemplate().getCount(query);
148 	}
149 
150 	@Override
151     @SuppressWarnings("unchecked")
152     public List<EarnCode> getEarnCodes(String earnCode, String ovtEarnCode, String descr, Date fromEffdt, Date toEffdt, String active, String showHistory) {
153         List<EarnCode> results = new ArrayList<EarnCode>();
154         
155         Criteria root = new Criteria();
156         
157         if (StringUtils.isNotBlank(earnCode)) {
158             root.addLike("earnCode", earnCode);
159         }
160         
161         if (StringUtils.isNotBlank(ovtEarnCode)) {
162             root.addEqualTo("ovtEarnCode", ovtEarnCode);
163         }
164         
165         if (StringUtils.isNotBlank(descr)) {
166             root.addLike("description", descr);
167         }
168         
169         Criteria effectiveDateFilter = new Criteria();
170         if (fromEffdt != null) {
171             effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt);
172         }
173         if (toEffdt != null) {
174             effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt);
175         }
176         if (fromEffdt == null && toEffdt == null) {
177             effectiveDateFilter.addLessOrEqualThan("effectiveDate", TKUtils.getCurrentDate());
178         }
179         root.addAndCriteria(effectiveDateFilter);
180         
181         if (StringUtils.isNotBlank(active)) {
182         	Criteria activeFilter = new Criteria();
183             if (StringUtils.equals(active, "Y")) {
184                 activeFilter.addEqualTo("active", true);
185             } else if (StringUtils.equals(active, "N")) {
186                 activeFilter.addEqualTo("active", false);
187             }
188             root.addAndCriteria(activeFilter);
189         }
190 
191         if (StringUtils.equals(showHistory, "N")) {
192             Criteria effdt = new Criteria();
193         	effdt.addEqualToField("earnCode", Criteria.PARENT_QUERY_PREFIX + "earnCode");
194         	effdt.addAndCriteria(effectiveDateFilter);
195         	ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(EarnCode.class, effdt);
196             effdtSubQuery.setAttributes(new String[]{"max(effectiveDate)"});
197             root.addEqualTo("effectiveDate", effdtSubQuery);
198             
199             Criteria timestamp = new Criteria();
200             timestamp.addEqualToField("earnCode", Criteria.PARENT_QUERY_PREFIX + "earnCode");
201             timestamp.addAndCriteria(effectiveDateFilter);
202             ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(EarnCode.class, timestamp);
203             timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
204             root.addEqualTo("timestamp", timestampSubQuery);
205         }
206         
207         Query query = QueryFactory.newQuery(EarnCode.class, root);
208         results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
209         
210         return results;
211     }
212 	
213 }