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