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.lm.earncodesec.dao;
17  
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.Date;
21  import java.util.HashSet;
22  import java.util.LinkedList;
23  import java.util.List;
24  import java.util.Set;
25  
26  import com.google.common.collect.ImmutableList;
27  import org.apache.commons.lang.StringUtils;
28  import org.apache.log4j.Logger;
29  import org.apache.ojb.broker.query.Criteria;
30  import org.apache.ojb.broker.query.Query;
31  import org.apache.ojb.broker.query.QueryFactory;
32  import org.apache.ojb.broker.query.ReportQueryByCriteria;
33  import org.kuali.hr.core.util.OjbSubQueryUtil;
34  import org.kuali.hr.lm.earncodesec.EarnCodeSecurity;
35  import org.kuali.hr.time.util.TKUtils;
36  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
37  
38  public class EarnCodeSecurityDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements EarnCodeSecurityDao {
39      private static final ImmutableList<String> EQUAL_TO_FIELDS = new ImmutableList.Builder<String>()
40              .add("dept")
41              .add("hrSalGroup")
42              .add("earnCode")
43              .add("location")
44              .build();
45  
46  	@SuppressWarnings("unused")
47  	private static final Logger LOG = Logger.getLogger(EarnCodeSecurityDaoSpringOjbImpl.class);
48  
49  	public void saveOrUpdate(EarnCodeSecurity earnCodeSec) {
50  		this.getPersistenceBrokerTemplate().store(earnCodeSec);
51  	}
52  
53  	public void saveOrUpdate(List<EarnCodeSecurity> ernCdSecList) {
54  		if (ernCdSecList != null) {
55  			for (EarnCodeSecurity ernCdSec : ernCdSecList) {
56  				this.getPersistenceBrokerTemplate().store(ernCdSec);
57  			}
58  		}
59  	}
60  
61  	@SuppressWarnings({ "unchecked", "deprecation" })
62  	@Override
63  	public List<EarnCodeSecurity> getEarnCodeSecurities(String department, String hrSalGroup, String location, Date asOfDate) {
64  		List<EarnCodeSecurity> decs = new LinkedList<EarnCodeSecurity>();
65  
66  		Criteria root = new Criteria();
67  		
68  		Criteria deptCrit = new Criteria();
69  		Criteria salGroupCrit = new Criteria();
70  		Criteria locationCrit = new Criteria();
71  		
72  		deptCrit.addEqualTo("dept", "%");
73  		salGroupCrit.addEqualTo("hrSalGroup", "%");
74  		locationCrit.addEqualTo("location", "%");
75  		
76  		Criteria deptCrit2 = new Criteria();
77  		deptCrit2.addEqualTo("dept", department);
78  		deptCrit2.addOrCriteria(deptCrit);
79  		root.addAndCriteria(deptCrit2);
80  		
81  		Criteria salGroupCrit2 = new Criteria();
82  		salGroupCrit2.addEqualTo("hrSalGroup", hrSalGroup);
83  		salGroupCrit2.addOrCriteria(salGroupCrit);
84  		root.addAndCriteria(salGroupCrit2);
85  		
86  		Criteria locationCrit2 = new Criteria();
87  		if ( !location.trim().isEmpty() ){
88  			locationCrit2.addEqualTo("location", location);
89  			locationCrit2.addOrCriteria(locationCrit);
90  			root.addAndCriteria(locationCrit2);
91  		}
92  		
93  		Criteria activeFilter = new Criteria(); // Inner Join For Activity
94  		activeFilter.addEqualTo("active", true);
95  		root.addAndCriteria(activeFilter);
96          ImmutableList.Builder<String> fields = new ImmutableList.Builder<String>()
97                  .add("dept")
98                  .add("hrSalGroup")
99                  .add("earnCode");
100         if ( !location.trim().isEmpty() ){
101             fields.add("location");
102         }
103         java.sql.Date effDate = null;
104         if (asOfDate != null) {
105             effDate = new java.sql.Date(asOfDate.getTime());
106         }
107         root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(EarnCodeSecurity.class, effDate, fields.build(), false));
108         root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(EarnCodeSecurity.class, fields.build(), false));
109 		
110 		root.addOrderBy("earnCode", true);
111 		root.addOrderBy("dept",false);
112 		root.addOrderBy("hrSalGroup",false);
113 		
114 		
115 		Query query = QueryFactory.newQuery(EarnCodeSecurity.class, root);
116 		
117 		Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
118 		
119 		if (c != null) {
120 			decs.addAll(c);
121 		}
122 		
123 		//Now we can have duplicates so remove any that match more than once
124 		Set<String> aSet = new HashSet<String>();
125 		List<EarnCodeSecurity> aList = new ArrayList<EarnCodeSecurity>();
126 		for(EarnCodeSecurity dec : decs){
127 			if(!aSet.contains(dec.getEarnCode())){
128 				aList.add(dec);
129 				aSet.add(dec.getEarnCode());
130 			} 
131 		}
132 		return aList;
133 	}
134 
135 	@Override
136 	public EarnCodeSecurity getEarnCodeSecurity(String hrEarnCodeSecId) {
137 		Criteria crit = new Criteria();
138 		crit.addEqualTo("hrEarnCodeSecurityId", hrEarnCodeSecId);
139 		
140 		Query query = QueryFactory.newQuery(EarnCodeSecurity.class, crit);
141 		return (EarnCodeSecurity)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
142 	}
143 	
144 	@Override
145 	@SuppressWarnings("unchecked")
146 	public List<EarnCodeSecurity> searchEarnCodeSecurities(String dept, String salGroup, String earnCode, String location, Date fromEffdt, Date toEffdt, 
147 														   String active, String showHistory) {
148 		
149 		List<EarnCodeSecurity> results = new ArrayList<EarnCodeSecurity>();
150 
151         Criteria root = new Criteria();
152 
153         if (StringUtils.isNotBlank(dept)) {
154             root.addLike("dept", dept);
155         }
156 
157         if (StringUtils.isNotBlank(salGroup)) {
158             root.addLike("hrSalGroup", salGroup);
159         }
160 
161         if (StringUtils.isNotBlank(earnCode)) {
162             root.addLike("earnCode", earnCode);
163         }
164 
165         if (StringUtils.isNotBlank(location)) {
166             root.addLike("location", location);
167         }
168 
169         Criteria effectiveDateFilter = new Criteria();
170         if (fromEffdt != null) {
171             effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt);
172         }
173 
174         if (toEffdt != null) {
175             effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt);
176         }
177         if (fromEffdt == null && toEffdt == null) {
178             effectiveDateFilter.addLessOrEqualThan("effectiveDate", TKUtils.getCurrentDate());
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(EarnCodeSecurity.class, effectiveDateFilter, EQUAL_TO_FIELDS, false));
194             root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(EarnCodeSecurity.class, EQUAL_TO_FIELDS, false));
195         }
196         
197         Query query = QueryFactory.newQuery(EarnCodeSecurity.class, root);
198         results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
199 
200         return results;
201 	}
202 	
203 	@Override
204 	public int getEarnCodeSecurityCount(String dept, String salGroup, String earnCode, String employee, String approver, String location,
205 			String active, java.sql.Date effdt,String hrDeptEarnCodeId) {
206 		Criteria crit = new Criteria();
207       crit.addEqualTo("dept", dept);
208       crit.addEqualTo("hrSalGroup", salGroup);
209       crit.addEqualTo("earnCode", earnCode);
210       crit.addEqualTo("employee", employee);
211       crit.addEqualTo("approver", approver);
212       crit.addEqualTo("location", location);
213       crit.addEqualTo("active", active);
214       crit.addEqualTo("effectiveDate", effdt);
215       if(hrDeptEarnCodeId != null) {
216     	  crit.addEqualTo("hrEarnCodeSecurityId", hrDeptEarnCodeId);
217       }
218       Query query = QueryFactory.newQuery(EarnCodeSecurity.class, crit);
219       return this.getPersistenceBrokerTemplate().getCount(query);
220 	}
221 	@Override
222 	public int getNewerEarnCodeSecurityCount(String earnCode, Date effdt) {
223 		Criteria crit = new Criteria();
224 		crit.addEqualTo("earnCode", earnCode);
225 		crit.addEqualTo("active", "Y");
226 		crit.addGreaterThan("effectiveDate", effdt);
227 		Query query = QueryFactory.newQuery(EarnCodeSecurity.class, crit);
228        	return this.getPersistenceBrokerTemplate().getCount(query);
229 	}
230 }