001 /** 002 * Copyright 2004-2012 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.timecollection.rule.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.lang.StringUtils; 024 import org.apache.ojb.broker.query.Criteria; 025 import org.apache.ojb.broker.query.Query; 026 import org.apache.ojb.broker.query.QueryFactory; 027 import org.apache.ojb.broker.query.ReportQueryByCriteria; 028 import org.kuali.hr.time.collection.rule.TimeCollectionRule; 029 import org.kuali.hr.time.util.TKUtils; 030 import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb; 031 032 public class TimeCollectionRuleDaoServiceImpl extends PlatformAwareDaoBaseOjb implements TimeCollectionRuleDaoService { 033 034 /* 035 * Returns valid TimeCollectionRule based on dept,workArea, and asOfDate 036 * dept and work area are wildcardable values 037 * @see org.kuali.hr.time.timecollection.rule.dao.TimeCollectionRuleDaoService#getTimeCollectionRule(java.lang.String dept, 038 * java.lang.Long workArea, java.sql.Date asOfDate) 039 */ 040 041 @Override 042 public TimeCollectionRule getTimeCollectionRule(String dept, Long workArea, Date asOfDate) { 043 044 045 TimeCollectionRule timeCollectionRule = new TimeCollectionRule(); 046 047 //First call confirm no exact match 048 timeCollectionRule = getTimeCollectionRuleWildCarded(dept, workArea, asOfDate); 049 if (timeCollectionRule != null) { 050 return timeCollectionRule; 051 } 052 //Try with dept wildcarded * 053 timeCollectionRule = getTimeCollectionRuleWildCarded("%", workArea, asOfDate); 054 if (timeCollectionRule != null) { 055 return timeCollectionRule; 056 } 057 058 //Try with work area wildcarded 059 timeCollectionRule = getTimeCollectionRuleWildCarded(dept, -1L, asOfDate); 060 if (timeCollectionRule != null) { 061 return timeCollectionRule; 062 } 063 064 //Try with everything wildcarded 065 timeCollectionRule = getTimeCollectionRuleWildCarded("%", -1L, asOfDate); 066 if (timeCollectionRule != null) { 067 return timeCollectionRule; 068 } 069 070 //Return default time collection rule 071 timeCollectionRule = new TimeCollectionRule(); 072 timeCollectionRule.setActive(true); 073 timeCollectionRule.setClockUserFl(true); 074 timeCollectionRule.setDept(dept); 075 timeCollectionRule.setWorkArea(workArea); 076 077 return timeCollectionRule; 078 } 079 080 private TimeCollectionRule getTimeCollectionRuleWildCarded(String dept, Long workArea, Date asOfDate) { 081 Criteria root = new Criteria(); 082 Criteria effdt = new Criteria(); 083 Criteria timestamp = new Criteria(); 084 085 effdt.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea"); 086 effdt.addLessOrEqualThan("effectiveDate", asOfDate); 087 // effdt.addEqualTo("active", true); 088 effdt.addEqualTo("dept", dept); 089 ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(TimeCollectionRule.class, effdt); 090 effdtSubQuery.setAttributes(new String[]{"max(effdt)"}); 091 092 timestamp.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea"); 093 timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate"); 094 // timestamp.addEqualTo("active", true); 095 timestamp.addEqualTo("dept", dept); 096 ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(TimeCollectionRule.class, timestamp); 097 timestampSubQuery.setAttributes(new String[]{"max(timestamp)"}); 098 099 root.addEqualTo("dept", dept); 100 root.addEqualTo("workArea", workArea); 101 root.addEqualTo("effectiveDate", effdtSubQuery); 102 root.addEqualTo("timestamp", timestampSubQuery); 103 // root.addEqualTo("active", true); 104 105 Criteria activeFilter = new Criteria(); // Inner Join For Activity 106 activeFilter.addEqualTo("active", true); 107 root.addAndCriteria(activeFilter); 108 109 110 Query query = QueryFactory.newQuery(TimeCollectionRule.class, root); 111 return (TimeCollectionRule) this.getPersistenceBrokerTemplate().getObjectByQuery(query); 112 113 } 114 115 @Override 116 public TimeCollectionRule getTimeCollectionRule(String tkTimeCollectionRuleId) { 117 Criteria crit = new Criteria(); 118 crit.addEqualTo("tkTimeCollectionRuleId", tkTimeCollectionRuleId); 119 120 Query query = QueryFactory.newQuery(TimeCollectionRule.class, crit); 121 return (TimeCollectionRule) this.getPersistenceBrokerTemplate().getObjectByQuery(query); 122 123 } 124 125 /* Jira 1152 126 * Returns valid TimeCollectionRule based on dept, workArea, payType, and asOfDate 127 * dept, work area, and payType can be wildcardable values 128 */ 129 @Override 130 public TimeCollectionRule getTimeCollectionRule(String dept, Long workArea, String payType, Date asOfDate) { 131 132 133 TimeCollectionRule timeCollectionRule = new TimeCollectionRule(); 134 135 //First call confirm no exact match 136 timeCollectionRule = getTimeCollectionRuleWildCarded(dept, workArea, payType, asOfDate); 137 if (timeCollectionRule != null) { 138 return timeCollectionRule; 139 } 140 //Try with dept wildcarded * 141 timeCollectionRule = getTimeCollectionRuleWildCarded("%", workArea, payType, asOfDate); 142 if (timeCollectionRule != null) { 143 return timeCollectionRule; 144 } 145 146 //Try with work area wildcarded 147 timeCollectionRule = getTimeCollectionRuleWildCarded(dept, -1L, payType, asOfDate); 148 if (timeCollectionRule != null) { 149 return timeCollectionRule; 150 } 151 152 //Try with payType wildcarded 153 timeCollectionRule = getTimeCollectionRuleWildCarded(dept, workArea, "%", asOfDate); 154 if (timeCollectionRule != null) { 155 return timeCollectionRule; 156 } 157 158 //Try with dept and workArea wildcarded 159 timeCollectionRule = getTimeCollectionRuleWildCarded("%", -1L, payType, asOfDate); 160 if (timeCollectionRule != null) { 161 return timeCollectionRule; 162 } 163 164 //Try with dept and payType wildcarded 165 timeCollectionRule = getTimeCollectionRuleWildCarded("%", workArea, "%", asOfDate); 166 if (timeCollectionRule != null) { 167 return timeCollectionRule; 168 } 169 170 //Try with workArea and payType wildcarded 171 timeCollectionRule = getTimeCollectionRuleWildCarded(dept, -1L, "%", asOfDate); 172 if (timeCollectionRule != null) { 173 return timeCollectionRule; 174 } 175 176 //Try with everything wildcarded 177 timeCollectionRule = getTimeCollectionRuleWildCarded("%", -1L, "%", asOfDate); 178 if (timeCollectionRule != null) { 179 return timeCollectionRule; 180 } 181 182 //Return default time collection rule 183 timeCollectionRule = new TimeCollectionRule(); 184 timeCollectionRule.setActive(true); 185 timeCollectionRule.setClockUserFl(true); 186 timeCollectionRule.setDept(dept); 187 timeCollectionRule.setWorkArea(workArea); 188 189 return timeCollectionRule; 190 } 191 192 private TimeCollectionRule getTimeCollectionRuleWildCarded(String dept, Long workArea, String payType, Date asOfDate) { 193 Criteria root = new Criteria(); 194 Criteria effdt = new Criteria(); 195 Criteria timestamp = new Criteria(); 196 197 effdt.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea"); 198 effdt.addLessOrEqualThan("effectiveDate", asOfDate); 199 // effdt.addEqualTo("active", true); 200 effdt.addEqualTo("dept", dept); 201 effdt.addEqualTo("payType", payType); 202 ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(TimeCollectionRule.class, effdt); 203 effdtSubQuery.setAttributes(new String[]{"max(effdt)"}); 204 205 timestamp.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea"); 206 timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate"); 207 // timestamp.addEqualTo("active", true); 208 timestamp.addEqualTo("dept", dept); 209 timestamp.addEqualTo("payType", payType); 210 ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(TimeCollectionRule.class, timestamp); 211 timestampSubQuery.setAttributes(new String[]{"max(timestamp)"}); 212 213 root.addEqualTo("dept", dept); 214 root.addEqualTo("workArea", workArea); 215 root.addEqualTo("payType", payType); 216 root.addEqualTo("effectiveDate", effdtSubQuery); 217 root.addEqualTo("timestamp", timestampSubQuery); 218 // root.addEqualTo("active", true); 219 220 Criteria activeFilter = new Criteria(); // Inner Join For Activity 221 activeFilter.addEqualTo("active", true); 222 root.addAndCriteria(activeFilter); 223 224 225 Query query = QueryFactory.newQuery(TimeCollectionRule.class, root); 226 return (TimeCollectionRule) this.getPersistenceBrokerTemplate().getObjectByQuery(query); 227 228 } 229 230 @Override 231 public List<TimeCollectionRule> getTimeCollectionRules(String dept, Long workArea, String payType, String active, String showHistory) { 232 233 List<TimeCollectionRule> results = new ArrayList<TimeCollectionRule>(); 234 235 Criteria root = new Criteria(); 236 Criteria effdt = new Criteria(); 237 Criteria timestamp = new Criteria(); 238 if (workArea != null) { 239 effdt.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea"); 240 } else { 241 effdt.addIsNull(Criteria.PARENT_QUERY_PREFIX + "workArea"); 242 } 243 effdt.addLessOrEqualThan("effectiveDate", TKUtils.getCurrentDate()); 244 if (StringUtils.isNotBlank(dept)) { 245 effdt.addEqualTo("dept", dept); 246 } 247 if (StringUtils.isNotBlank(payType)) { 248 effdt.addEqualTo("payType", payType); 249 } 250 ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(TimeCollectionRule.class, effdt); 251 effdtSubQuery.setAttributes(new String[]{"max(effdt)"}); 252 if (workArea != null) { 253 timestamp.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea"); 254 } else { 255 timestamp.addIsNull(Criteria.PARENT_QUERY_PREFIX + "workArea"); 256 } 257 timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate"); 258 if (StringUtils.isNotBlank(dept)) { 259 timestamp.addEqualTo("dept", dept); 260 } 261 if (StringUtils.isNotBlank(payType)) { 262 timestamp.addEqualTo("payType", payType); 263 } 264 ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(TimeCollectionRule.class, timestamp); 265 timestampSubQuery.setAttributes(new String[]{"max(timestamp)"}); 266 267 if (StringUtils.isNotBlank(dept)) { 268 root.addLike("dept", dept); 269 } 270 271 if (workArea != null) { 272 root.addLike("workArea", workArea); 273 } 274 if (StringUtils.isNotBlank(payType)) { 275 root.addLike("payType", payType); 276 } 277 //root.addEqualTo("effectiveDate", effdtSubQuery); 278 //root.addEqualTo("timestamp", timestampSubQuery); 279 280 if (StringUtils.equals(active, "Y")) { 281 Criteria activeFilter = new Criteria(); // Inner Join For Activity 282 activeFilter.addEqualTo("active", true); 283 root.addAndCriteria(activeFilter); 284 } else if (StringUtils.equals(active, "N")) { 285 Criteria activeFilter = new Criteria(); // Inner Join For Activity 286 activeFilter.addEqualTo("active", false); 287 root.addAndCriteria(activeFilter); 288 } 289 290 Query query = QueryFactory.newQuery(TimeCollectionRule.class, root); 291 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query); 292 results.addAll(c); 293 294 return results; 295 } 296 }