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 com.google.common.collect.ImmutableList;
024 import org.apache.commons.collections.CollectionUtils;
025 import org.apache.commons.lang.StringUtils;
026 import org.apache.ojb.broker.query.Criteria;
027 import org.apache.ojb.broker.query.Query;
028 import org.apache.ojb.broker.query.QueryFactory;
029 import org.apache.ojb.broker.query.ReportQueryByCriteria;
030 import org.kuali.hr.core.util.OjbSubQueryUtil;
031 import org.kuali.hr.time.clock.location.ClockLocationRule;
032 import org.kuali.hr.time.clock.location.ClockLocationRuleIpAddress;
033 import org.kuali.hr.time.service.base.TkServiceLocator;
034 import org.kuali.hr.time.util.TKUtils;
035 import org.kuali.hr.time.workarea.WorkArea;
036 import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
037
038 public class ClockLocationDaoOjbImpl extends PlatformAwareDaoBaseOjb implements ClockLocationDao{
039 private static final ImmutableList<String> EQUAL_TO_FIELDS = new ImmutableList.Builder<String>()
040 .add("dept")
041 .add("workArea")
042 .add("jobNumber")
043 .add("principalId")
044 .build();
045
046 public List<ClockLocationRule> getClockLocationRule(String dept, Long workArea, String principalId, Long jobNumber, Date asOfDate){
047 Criteria root = new Criteria();
048
049 root.addEqualTo("dept", dept);
050 root.addEqualTo("workArea", workArea);
051 root.addEqualTo("principalId", principalId);
052 root.addEqualTo("jobNumber", jobNumber);
053 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(ClockLocationRule.class, asOfDate, EQUAL_TO_FIELDS, false));
054 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(ClockLocationRule.class, EQUAL_TO_FIELDS, false));
055 //root.addEqualTo("active", true);
056
057 Criteria activeFilter = new Criteria(); // Inner Join For Activity
058 activeFilter.addEqualTo("active", true);
059 root.addAndCriteria(activeFilter);
060
061 Query query = QueryFactory.newQuery(ClockLocationRule.class, root);
062 List<ClockLocationRule> clockLocationRules = (List<ClockLocationRule>)this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
063 if(clockLocationRules==null){
064 clockLocationRules = new ArrayList<ClockLocationRule>();
065 }
066 for(ClockLocationRule clr : clockLocationRules ) {
067 this.populateIPAddressesForCLR(clr);
068 }
069 return clockLocationRules;
070 }
071
072 @SuppressWarnings("unchecked")
073 @Override
074 public List<ClockLocationRule> getNewerVersionClockLocationRule(
075 String dept, Long workArea, String principalId, Long jobNumber,
076 Date asOfDate) {
077 Criteria root = new Criteria();
078 root.addEqualTo("dept", dept);
079 root.addEqualTo("workArea", workArea);
080 root.addEqualTo("principalId", principalId);
081 root.addEqualTo("jobNumber", jobNumber);
082 root.addGreaterThan("effectiveDate", asOfDate);
083
084 Criteria activeFilter = new Criteria(); // Inner Join For Activity
085 activeFilter.addEqualTo("active", true);
086 root.addAndCriteria(activeFilter);
087
088 Query query = QueryFactory.newQuery(ClockLocationRule.class, root);
089 List<ClockLocationRule> clockLocationRules = (List<ClockLocationRule>)this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
090 if(clockLocationRules==null){
091 clockLocationRules = new ArrayList<ClockLocationRule>();
092 }
093 for(ClockLocationRule clr : clockLocationRules ) {
094 this.populateIPAddressesForCLR(clr);
095 }
096 return clockLocationRules;
097 }
098
099 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 root.addLike("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 }