001 /**
002 * Copyright 2004-2013 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.hr.time.clock.location.dao;
017
018 import java.sql.Date;
019 import java.util.ArrayList;
020 import java.util.Collection;
021 import java.util.List;
022
023 import org.apache.commons.collections.CollectionUtils;
024 import org.apache.commons.lang.StringUtils;
025 import org.apache.ojb.broker.query.Criteria;
026 import org.apache.ojb.broker.query.Query;
027 import org.apache.ojb.broker.query.QueryFactory;
028 import org.apache.ojb.broker.query.ReportQueryByCriteria;
029 import org.kuali.hr.time.clock.location.ClockLocationRule;
030 import org.kuali.hr.time.clock.location.ClockLocationRuleIpAddress;
031 import org.kuali.hr.time.service.base.TkServiceLocator;
032 import org.kuali.hr.time.util.TKUtils;
033 import org.kuali.hr.time.workarea.WorkArea;
034 import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
035
036 public class ClockLocationDaoOjbImpl extends PlatformAwareDaoBaseOjb implements ClockLocationDao{
037 @SuppressWarnings("unchecked")
038 public List<ClockLocationRule> getClockLocationRule(String dept, Long workArea, String principalId, Long jobNumber, Date asOfDate){
039 Criteria root = new Criteria();
040 Criteria effdt = new Criteria();
041 Criteria timestamp = new Criteria();
042
043 effdt.addEqualToField("dept", Criteria.PARENT_QUERY_PREFIX + "dept");
044 effdt.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
045 effdt.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
046 effdt.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber");
047 effdt.addLessOrEqualThan("effectiveDate", asOfDate);
048 //effdt.addEqualTo("active", true);
049 ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(ClockLocationRule.class, effdt);
050 effdtSubQuery.setAttributes(new String[] { "max(effdt)" });
051
052 timestamp.addEqualToField("dept", Criteria.PARENT_QUERY_PREFIX + "dept");
053 timestamp.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
054 timestamp.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
055 timestamp.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber");
056 timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
057 //timestamp.addEqualTo("active", true);
058 ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(ClockLocationRule.class, timestamp);
059 timestampSubQuery.setAttributes(new String[] { "max(timestamp)" });
060
061 root.addEqualTo("dept", dept);
062 root.addEqualTo("workArea", workArea);
063 root.addEqualTo("principalId", principalId);
064 root.addEqualTo("jobNumber", jobNumber);
065 root.addEqualTo("effectiveDate", effdtSubQuery);
066 root.addEqualTo("timestamp", timestampSubQuery);
067 //root.addEqualTo("active", true);
068
069 Criteria activeFilter = new Criteria(); // Inner Join For Activity
070 activeFilter.addEqualTo("active", true);
071 root.addAndCriteria(activeFilter);
072
073 Query query = QueryFactory.newQuery(ClockLocationRule.class, root);
074 List<ClockLocationRule> clockLocationRules = (List<ClockLocationRule>)this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
075 if(clockLocationRules==null){
076 clockLocationRules = new ArrayList<ClockLocationRule>();
077 }
078 for(ClockLocationRule clr : clockLocationRules ) {
079 this.populateIPAddressesForCLR(clr);
080 }
081 return clockLocationRules;
082 }
083
084 @SuppressWarnings("unchecked")
085 @Override
086 public List<ClockLocationRule> getNewerVersionClockLocationRule(
087 String dept, Long workArea, String principalId, Long jobNumber,
088 Date asOfDate) {
089 Criteria root = new Criteria();
090 root.addEqualTo("dept", dept);
091 root.addEqualTo("workArea", workArea);
092 root.addEqualTo("principalId", principalId);
093 root.addEqualTo("jobNumber", jobNumber);
094 root.addGreaterThan("effectiveDate", asOfDate);
095
096 Criteria activeFilter = new Criteria(); // Inner Join For Activity
097 activeFilter.addEqualTo("active", true);
098 root.addAndCriteria(activeFilter);
099
100 Query query = QueryFactory.newQuery(ClockLocationRule.class, root);
101 List<ClockLocationRule> clockLocationRules = (List<ClockLocationRule>)this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
102 if(clockLocationRules==null){
103 clockLocationRules = new ArrayList<ClockLocationRule>();
104 }
105 for(ClockLocationRule clr : clockLocationRules ) {
106 this.populateIPAddressesForCLR(clr);
107 }
108 return clockLocationRules;
109 }
110
111 public ClockLocationRule getClockLocationRule(String tkClockLocationRuleId){
112 Criteria criteria = new Criteria();
113 criteria.addEqualTo("tkClockLocationRuleId", tkClockLocationRuleId);
114 ClockLocationRule clr = (ClockLocationRule) this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(
115 ClockLocationRule.class, criteria));
116 if(clr != null) {
117 this.populateIPAddressesForCLR(clr);
118 }
119 return clr;
120 }
121
122 // get ip address from tk_ip_addresses table for this ClockLocationRule
123 @SuppressWarnings("unchecked")
124 public void populateIPAddressesForCLR(ClockLocationRule clr) {
125 if(clr.getTkClockLocationRuleId() == null) {
126 return;
127 }
128 Criteria root = new Criteria();
129 root.addEqualTo("tkClockLocationRuleId", clr.getTkClockLocationRuleId().toString());
130 Query query = QueryFactory.newQuery(ClockLocationRuleIpAddress.class, root);
131 List<ClockLocationRuleIpAddress> ipAddresses = (List<ClockLocationRuleIpAddress>) this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
132 clr.setIpAddresses(ipAddresses);
133 }
134
135 @Override
136 @SuppressWarnings("unchecked")
137 public List<ClockLocationRule> getClockLocationRules(Date fromEffdt, Date toEffdt, String principalId, String jobNumber, String dept, String workArea,
138 String active, String showHistory) {
139
140 List<ClockLocationRule> results = new ArrayList<ClockLocationRule>();
141
142 Criteria root = new Criteria();
143
144 Criteria effectiveDateFilter = new Criteria();
145 if (fromEffdt != null) {
146 effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt);
147 }
148 if (toEffdt != null) {
149 effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt);
150 }
151 if (fromEffdt == null && toEffdt == null) {
152 effectiveDateFilter.addLessOrEqualThan("effectiveDate", TKUtils.getCurrentDate());
153 }
154 root.addAndCriteria(effectiveDateFilter);
155
156 if (StringUtils.isNotBlank(principalId)) {
157 root.addLike("principalId", principalId);
158 }
159
160 if (StringUtils.isNotBlank(dept)) {
161 root.addLike("dept", dept);
162 }
163
164 if (StringUtils.isNotBlank(jobNumber)) {
165 root.addLike("jobNumber", jobNumber);
166 }
167
168 if (StringUtils.isNotBlank(dept)) {
169 Criteria workAreaCriteria = new Criteria();
170 Date asOfDate = toEffdt != null ? toEffdt : TKUtils.getCurrentDate();
171 Collection<WorkArea> workAreasForDept = TkServiceLocator.getWorkAreaService().getWorkAreas(dept,asOfDate);
172 if (CollectionUtils.isNotEmpty(workAreasForDept)) {
173 List<Long> longWorkAreas = new ArrayList<Long>();
174 for(WorkArea cwa : workAreasForDept){
175 longWorkAreas.add(cwa.getWorkArea());
176 }
177 workAreaCriteria.addIn("workArea", longWorkAreas);
178 }
179 root.addAndCriteria(workAreaCriteria);
180 }
181
182 if (StringUtils.isNotBlank(workArea)) {
183 root.addLike("workArea", workArea);
184 }
185
186 if (StringUtils.isNotBlank(active)) {
187 Criteria activeFilter = new Criteria();
188 if (StringUtils.equals(active, "Y")) {
189 activeFilter.addEqualTo("active", true);
190 } else if (StringUtils.equals(active, "N")) {
191 activeFilter.addEqualTo("active", false);
192 }
193 root.addAndCriteria(activeFilter);
194 }
195
196 if (StringUtils.equals(showHistory, "N")) {
197 Criteria effdt = new Criteria();
198 effdt.addEqualToField("dept", Criteria.PARENT_QUERY_PREFIX + "dept");
199 effdt.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
200 effdt.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
201 effdt.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber");
202 effdt.addAndCriteria(effectiveDateFilter);
203 ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(ClockLocationRule.class, effdt);
204 effdtSubQuery.setAttributes(new String[] { "max(effectiveDate)" });
205 root.addEqualTo("effectiveDate", effdtSubQuery);
206
207 Criteria timestamp = new Criteria();
208 timestamp.addEqualToField("dept", Criteria.PARENT_QUERY_PREFIX + "dept");
209 timestamp.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
210 timestamp.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
211 timestamp.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber");
212 timestamp.addAndCriteria(effectiveDateFilter);
213 ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(ClockLocationRule.class, timestamp);
214 timestampSubQuery.setAttributes(new String[] { "max(timestamp)" });
215 root.addEqualTo("timestamp", timestampSubQuery);
216 }
217
218 Query query = QueryFactory.newQuery(ClockLocationRule.class, root);
219 results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
220
221 return results;
222 }
223
224 }