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