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.kpme.tklm.time.rules.clocklocation.dao;
17  
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.List;
21  
22  import org.apache.commons.collections.CollectionUtils;
23  import org.apache.commons.lang.StringUtils;
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.service.HrServiceLocator;
29  import org.kuali.kpme.core.util.OjbSubQueryUtil;
30  import org.kuali.kpme.core.workarea.WorkArea;
31  import org.kuali.kpme.tklm.time.rules.clocklocation.ClockLocationRule;
32  import org.kuali.kpme.tklm.time.rules.clocklocation.ClockLocationRuleIpAddress;
33  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
34  
35  public class ClockLocationDaoOjbImpl extends PlatformAwareDaoBaseOjb implements ClockLocationDao{
36       public List<ClockLocationRule> getClockLocationRule(String dept, Long workArea, String principalId, Long jobNumber, LocalDate asOfDate){
37  		Criteria root = new Criteria();
38  
39  		root.addEqualTo("dept", dept);
40  		root.addEqualTo("workArea", workArea);
41  		root.addEqualTo("principalId", principalId);
42  		root.addEqualTo("jobNumber", jobNumber);
43  		root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(ClockLocationRule.class, asOfDate, ClockLocationRule.EQUAL_TO_FIELDS, false));
44  		root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(ClockLocationRule.class, ClockLocationRule.EQUAL_TO_FIELDS, false));
45  		//root.addEqualTo("active", true);
46  		
47  		Criteria activeFilter = new Criteria(); // Inner Join For Activity
48  		activeFilter.addEqualTo("active", true);
49  		root.addAndCriteria(activeFilter);
50  		
51  		Query query = QueryFactory.newQuery(ClockLocationRule.class, root);
52  		List<ClockLocationRule> clockLocationRules = (List<ClockLocationRule>)this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
53  		if(clockLocationRules==null){
54  			clockLocationRules = new ArrayList<ClockLocationRule>();
55  		}
56  		for(ClockLocationRule clr : clockLocationRules ) {
57  			this.populateIPAddressesForCLR(clr);
58  		}
59  		return clockLocationRules;
60  	}
61  
62  	@SuppressWarnings("unchecked")
63  	@Override
64  	public List<ClockLocationRule> getNewerVersionClockLocationRule(
65  			String dept, Long workArea, String principalId, Long jobNumber,
66  			LocalDate asOfDate) {
67  		Criteria root = new Criteria();
68  		root.addEqualTo("dept", dept);
69  		root.addEqualTo("workArea", workArea);
70  		root.addEqualTo("principalId", principalId);
71  		root.addEqualTo("jobNumber", jobNumber);
72  		root.addGreaterThan("effectiveDate", asOfDate.toDate());
73  		
74  		Criteria activeFilter = new Criteria(); // Inner Join For Activity
75  		activeFilter.addEqualTo("active", true);
76  		root.addAndCriteria(activeFilter);
77  		
78  		Query query = QueryFactory.newQuery(ClockLocationRule.class, root);
79  		List<ClockLocationRule> clockLocationRules = (List<ClockLocationRule>)this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
80  		if(clockLocationRules==null){
81  			clockLocationRules = new ArrayList<ClockLocationRule>();
82  		}
83  		for(ClockLocationRule clr : clockLocationRules ) {
84  			this.populateIPAddressesForCLR(clr);
85  		}
86  		return clockLocationRules;
87  	}
88  	
89  	public ClockLocationRule getClockLocationRule(String tkClockLocationRuleId){
90  	        Criteria criteria = new Criteria();
91  	        criteria.addEqualTo("tkClockLocationRuleId", tkClockLocationRuleId);
92  	        ClockLocationRule clr = (ClockLocationRule) this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(
93  	        							ClockLocationRule.class, criteria));
94  	        if(clr != null) {
95  	        	this.populateIPAddressesForCLR(clr);
96  	        }
97  	        return clr;
98  	}
99  	
100 	// get ip address from tk_ip_addresses table for this ClockLocationRule
101 	@SuppressWarnings("unchecked")
102 	public void populateIPAddressesForCLR(ClockLocationRule clr) {
103 		if(clr.getTkClockLocationRuleId() == null) {
104 			return;
105 		}
106 		Criteria root = new Criteria();
107 		root.addEqualTo("tkClockLocationRuleId", clr.getTkClockLocationRuleId().toString());
108 		Query query = QueryFactory.newQuery(ClockLocationRuleIpAddress.class, root);
109 		List<ClockLocationRuleIpAddress> ipAddresses = (List<ClockLocationRuleIpAddress>) this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
110 		clr.setIpAddresses(ipAddresses);
111 	}
112 
113 	@Override
114     @SuppressWarnings("unchecked")
115     public List<ClockLocationRule> getClockLocationRules(LocalDate fromEffdt, LocalDate toEffdt, String principalId, String jobNumber, String dept, String workArea, 
116     													 String active, String showHistory) {
117 
118         List<ClockLocationRule> results = new ArrayList<ClockLocationRule>();
119         
120         Criteria root = new Criteria();
121 
122         Criteria effectiveDateFilter = new Criteria();
123         if (fromEffdt != null) {
124             effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt.toDate());
125         }
126         if (toEffdt != null) {
127             effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt.toDate());
128         }
129         if (fromEffdt == null && toEffdt == null) {
130             effectiveDateFilter.addLessOrEqualThan("effectiveDate", LocalDate.now().toDate());
131         }
132         root.addAndCriteria(effectiveDateFilter);
133 
134         if (StringUtils.isNotBlank(principalId)) {
135             root.addLike("UPPER(`principal_id`)", principalId.toUpperCase()); // KPME-2695 in case principal id is not a number
136         }
137 
138         if (StringUtils.isNotBlank(dept)) {
139             root.addLike("UPPER(`dept`)", dept.toUpperCase()); // KPME-2695
140         }
141 
142 //        if (StringUtils.isNotBlank(jobNumber)) {
143 //            root.addLike("jobNumber", jobNumber);
144 //        }
145         
146         if (StringUtils.isNotBlank(jobNumber)) {
147             OjbSubQueryUtil.addNumericCriteria(root, "jobNumber", jobNumber);
148         }
149 
150         if (StringUtils.isNotBlank(dept)) {
151             Criteria workAreaCriteria = new Criteria();
152             LocalDate asOfDate = toEffdt != null ? toEffdt : LocalDate.now();
153             Collection<WorkArea> workAreasForDept = HrServiceLocator.getWorkAreaService().getWorkAreas(dept,asOfDate);
154             if (CollectionUtils.isNotEmpty(workAreasForDept)) {
155                 List<Long> longWorkAreas = new ArrayList<Long>();
156                 for(WorkArea cwa : workAreasForDept){
157                     longWorkAreas.add(cwa.getWorkArea());
158                 }
159                 workAreaCriteria.addIn("workArea", longWorkAreas);
160             }
161             root.addAndCriteria(workAreaCriteria);
162         }
163 
164         if (StringUtils.isNotBlank(workArea)) {
165             OjbSubQueryUtil.addNumericCriteria(root, "workArea", workArea);
166         }
167         
168         if (StringUtils.isNotBlank(active)) {
169         	Criteria activeFilter = new Criteria();
170             if (StringUtils.equals(active, "Y")) {
171                 activeFilter.addEqualTo("active", true);
172             } else if (StringUtils.equals(active, "N")) {
173                 activeFilter.addEqualTo("active", false);
174             }
175             root.addAndCriteria(activeFilter);
176         }
177 
178         if (StringUtils.equals(showHistory, "N")) {
179     		root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithFilter(ClockLocationRule.class, effectiveDateFilter, ClockLocationRule.EQUAL_TO_FIELDS, false));
180     		root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(ClockLocationRule.class, ClockLocationRule.EQUAL_TO_FIELDS, false));
181         }
182         
183         Query query = QueryFactory.newQuery(ClockLocationRule.class, root);
184         results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
185         
186         return results;
187     }
188 
189 }