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.earncodesec.dao;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.log4j.Logger;
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.apache.ojb.broker.query.ReportQueryByCriteria;
24  import org.kuali.hr.earncodesec.EarnCodeSecurity;
25  import org.kuali.hr.time.util.TKUtils;
26  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
27  
28  import java.util.*;
29  
30  public class EarnCodeSecurityDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements EarnCodeSecurityDao {
31  
32  	@SuppressWarnings("unused")
33  	private static final Logger LOG = Logger.getLogger(EarnCodeSecurityDaoSpringOjbImpl.class);
34  
35  	public void saveOrUpdate(EarnCodeSecurity earnCodeSec) {
36  		this.getPersistenceBrokerTemplate().store(earnCodeSec);
37  	}
38  
39  	public void saveOrUpdate(List<EarnCodeSecurity> ernCdSecList) {
40  		if (ernCdSecList != null) {
41  			for (EarnCodeSecurity ernCdSec : ernCdSecList) {
42  				this.getPersistenceBrokerTemplate().store(ernCdSec);
43  			}
44  		}
45  	}
46  
47  	@SuppressWarnings({ "unchecked", "deprecation" })
48  	@Override
49  	public List<EarnCodeSecurity> getEarnCodeSecurities(String department, String hrSalGroup, String location, Date asOfDate) {
50  		List<EarnCodeSecurity> decs = new LinkedList<EarnCodeSecurity>();
51  
52  		Criteria root = new Criteria();
53  		Criteria effdt = new Criteria();
54  		Criteria timestamp = new Criteria();
55  		
56  		Criteria deptCrit = new Criteria();
57  		Criteria salGroupCrit = new Criteria();
58  		Criteria locationCrit = new Criteria();
59  		
60  		deptCrit.addEqualTo("dept", "%");
61  		salGroupCrit.addEqualTo("hrSalGroup", "%");
62  		locationCrit.addEqualTo("location", "%");
63  		
64  		Criteria deptCrit2 = new Criteria();
65  		deptCrit2.addEqualTo("dept", department);
66  		deptCrit2.addOrCriteria(deptCrit);
67  		root.addAndCriteria(deptCrit2);
68  		
69  		Criteria salGroupCrit2 = new Criteria();
70  		salGroupCrit2.addEqualTo("hrSalGroup", hrSalGroup);
71  		salGroupCrit2.addOrCriteria(salGroupCrit);
72  		root.addAndCriteria(salGroupCrit2);
73  		
74  		Criteria locationCrit2 = new Criteria();
75  		if ( !location.trim().isEmpty() ){
76  			locationCrit2.addEqualTo("location", location);
77  			locationCrit2.addOrCriteria(locationCrit);
78  			root.addAndCriteria(locationCrit2);
79  		}
80  		
81  		Criteria activeFilter = new Criteria(); // Inner Join For Activity
82  		activeFilter.addEqualTo("active", true);
83  		root.addAndCriteria(activeFilter);
84  		
85  		// OJB's awesome sub query setup part 1
86  		effdt.addEqualToField("dept", Criteria.PARENT_QUERY_PREFIX + "dept");
87  		effdt.addEqualToField("hrSalGroup", Criteria.PARENT_QUERY_PREFIX + "hrSalGroup");
88  		effdt.addEqualToField("earnCode", Criteria.PARENT_QUERY_PREFIX + "earnCode");
89  		effdt.addLessOrEqualThan("effectiveDate", asOfDate);
90  		
91  		if ( !location.trim().isEmpty() ){
92  			effdt.addAndCriteria(locationCrit2);
93  			effdt.addEqualToField("location", Criteria.PARENT_QUERY_PREFIX + "location");
94  		}
95  		
96  		// KPME-856, commented out the following line, when geting max(effdt) for each earnCode, do not need to limit to active entries.
97  		//effdt.addEqualTo("active", true);
98  		ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(EarnCodeSecurity.class, effdt);
99  		effdtSubQuery.setAttributes(new String[] { "max(effdt)" });
100 
101 		// OJB's awesome sub query setup part 2
102 		timestamp.addEqualToField("dept", Criteria.PARENT_QUERY_PREFIX + "dept");
103 		timestamp.addEqualToField("hrSalGroup", Criteria.PARENT_QUERY_PREFIX + "hrSalGroup");
104 		timestamp.addEqualToField("earnCode", Criteria.PARENT_QUERY_PREFIX + "earnCode");
105 		if ( !location.trim().isEmpty() ){
106 			timestamp.addEqualToField("location", Criteria.PARENT_QUERY_PREFIX + "location");
107 		}
108 		timestamp.addEqualTo("active", true);
109 		timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
110 		ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(EarnCodeSecurity.class, timestamp);
111 		timestampSubQuery.setAttributes(new String[]{ "max(timestamp)" });
112 		
113 		root.addEqualTo("effectiveDate", effdtSubQuery);
114 		root.addEqualTo("timestamp", timestampSubQuery);
115 		
116 		root.addOrderBy("earnCode", true);
117 		root.addOrderBy("dept",false);
118 		root.addOrderBy("hrSalGroup",false);
119 		
120 		
121 		Query query = QueryFactory.newQuery(EarnCodeSecurity.class, root);
122 		
123 		Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
124 		
125 		if (c != null) {
126 			decs.addAll(c);
127 		}
128 		
129 		//Now we can have duplicates so remove any that match more than once
130 		Set<String> aSet = new HashSet<String>();
131 		List<EarnCodeSecurity> aList = new ArrayList<EarnCodeSecurity>();
132 		for(EarnCodeSecurity dec : decs){
133 			if(!aSet.contains(dec.getEarnCode())){
134 				aList.add(dec);
135 				aSet.add(dec.getEarnCode());
136 			} 
137 		}
138 		return aList;
139 	}
140 
141 	@Override
142 	public EarnCodeSecurity getEarnCodeSecurity(String hrEarnCodeSecId) {
143 		Criteria crit = new Criteria();
144 		crit.addEqualTo("hrEarnCodeSecurityId", hrEarnCodeSecId);
145 		
146 		Query query = QueryFactory.newQuery(EarnCodeSecurity.class, crit);
147 		return (EarnCodeSecurity)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
148 	}
149 	
150 	@Override
151 	@SuppressWarnings("unchecked")
152 	public List<EarnCodeSecurity> searchEarnCodeSecurities(String dept, String salGroup, String earnCode, String location, Date fromEffdt, Date toEffdt, 
153 														   String active, String showHistory) {
154 		
155 		List<EarnCodeSecurity> results = new ArrayList<EarnCodeSecurity>();
156 
157         Criteria root = new Criteria();
158 
159         if (StringUtils.isNotBlank(dept)) {
160             root.addLike("dept", dept);
161         }
162 
163         if (StringUtils.isNotBlank(salGroup)) {
164             root.addLike("hrSalGroup", salGroup);
165         }
166 
167         if (StringUtils.isNotBlank(earnCode)) {
168             root.addLike("earnCode", earnCode);
169         }
170 
171         if (StringUtils.isNotBlank(location)) {
172             root.addLike("location", location);
173         }
174 
175         Criteria effectiveDateFilter = new Criteria();
176         if (fromEffdt != null) {
177             effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt);
178         }
179         if (toEffdt != null) {
180             effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt);
181         }
182         if (fromEffdt == null && toEffdt == null) {
183             effectiveDateFilter.addLessOrEqualThan("effectiveDate", TKUtils.getCurrentDate());
184         }
185         root.addAndCriteria(effectiveDateFilter);
186 
187         if (StringUtils.isNotBlank(active)) {
188         	Criteria activeFilter = new Criteria();
189             if (StringUtils.equals(active, "Y")) {
190                 activeFilter.addEqualTo("active", true);
191             } else if (StringUtils.equals(active, "N")) {
192                 activeFilter.addEqualTo("active", false);
193             }
194             root.addAndCriteria(activeFilter);
195         }
196 
197         if (StringUtils.equals(showHistory, "N")) {
198             Criteria effdt = new Criteria();
199             effdt.addEqualToField("dept", Criteria.PARENT_QUERY_PREFIX + "dept");
200             effdt.addEqualToField("hrSalGroup", Criteria.PARENT_QUERY_PREFIX + "hrSalGroup");
201             effdt.addEqualToField("earnCode", Criteria.PARENT_QUERY_PREFIX + "earnCode");
202             effdt.addEqualToField("location", Criteria.PARENT_QUERY_PREFIX + "location");
203             effdt.addAndCriteria(effectiveDateFilter);
204             ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(EarnCodeSecurity.class, effdt);
205             effdtSubQuery.setAttributes(new String[]{"max(effectiveDate)"});
206             root.addEqualTo("effectiveDate", effdtSubQuery);
207             
208             Criteria timestamp = new Criteria();
209             timestamp.addEqualToField("dept", Criteria.PARENT_QUERY_PREFIX + "dept");
210             timestamp.addEqualToField("hrSalGroup", Criteria.PARENT_QUERY_PREFIX + "hrSalGroup");
211             timestamp.addEqualToField("earnCode", Criteria.PARENT_QUERY_PREFIX + "earnCode");
212             timestamp.addEqualToField("location", Criteria.PARENT_QUERY_PREFIX + "location");
213             timestamp.addAndCriteria(effectiveDateFilter);
214             ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(EarnCodeSecurity.class, timestamp);
215             timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
216             root.addEqualTo("timestamp", timestampSubQuery);
217         }
218         
219         Query query = QueryFactory.newQuery(EarnCodeSecurity.class, root);
220         results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
221 
222         return results;
223 	}
224 	
225 	@Override
226 	public int getEarnCodeSecurityCount(String dept, String salGroup, String earnCode, String employee, String approver, String location,
227 			String active, java.sql.Date effdt,String hrDeptEarnCodeId) {
228 		Criteria crit = new Criteria();
229       crit.addEqualTo("dept", dept);
230       crit.addEqualTo("hrSalGroup", salGroup);
231       crit.addEqualTo("earnCode", earnCode);
232       crit.addEqualTo("employee", employee);
233       crit.addEqualTo("approver", approver);
234       crit.addEqualTo("location", location);
235       crit.addEqualTo("active", active);
236       crit.addEqualTo("effectiveDate", effdt);
237       if(hrDeptEarnCodeId != null) {
238     	  crit.addEqualTo("hrEarnCodeSecurityId", hrDeptEarnCodeId);
239       }
240       Query query = QueryFactory.newQuery(EarnCodeSecurity.class, crit);
241       return this.getPersistenceBrokerTemplate().getCount(query);
242 	}
243 	@Override
244 	public int getNewerEarnCodeSecurityCount(String earnCode, Date effdt) {
245 		Criteria crit = new Criteria();
246 		crit.addEqualTo("earnCode", earnCode);
247 		crit.addEqualTo("active", "Y");
248 		crit.addGreaterThan("effectiveDate", effdt);
249 		Query query = QueryFactory.newQuery(EarnCodeSecurity.class, crit);
250        	return this.getPersistenceBrokerTemplate().getCount(query);
251 	}
252 }