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.job.dao; 017 018 import java.util.ArrayList; 019 import java.util.Collection; 020 import java.util.Date; 021 import java.util.LinkedList; 022 import java.util.List; 023 024 import org.apache.commons.lang.StringUtils; 025 import org.apache.log4j.Logger; 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.job.Job; 031 import org.kuali.hr.time.util.TKUtils; 032 import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb; 033 034 /** 035 * Represents an implementation of {@link JobDao}. 036 */ 037 public class JobDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements JobDao { 038 039 @SuppressWarnings("unused") 040 private static final Logger LOG = Logger.getLogger(JobDaoSpringOjbImpl.class); 041 042 public void saveOrUpdate(Job job) { 043 this.getPersistenceBrokerTemplate().store(job); 044 } 045 046 public void saveOrUpdate(List<Job> jobList) { 047 if (jobList != null) { 048 for (Job job : jobList) { 049 this.saveOrUpdate(job); 050 } 051 } 052 } 053 054 public Job getPrimaryJob(String principalId, Date payPeriodEndDate) { 055 Criteria root = new Criteria(); 056 Criteria effdt = new Criteria(); 057 Criteria timestamp = new Criteria(); 058 059 effdt.addEqualTo("principalId", principalId); 060 effdt.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber"); 061 effdt.addLessOrEqualThan("effectiveDate", payPeriodEndDate); 062 ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(Job.class, effdt); 063 effdtSubQuery.setAttributes(new String[]{"max(effectiveDate)"}); 064 065 timestamp.addEqualTo("principalId", principalId); 066 timestamp.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber"); 067 timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate"); 068 ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(Job.class, timestamp); 069 timestampSubQuery.setAttributes(new String[]{"max(timestamp)"}); 070 071 root.addEqualTo("principalId", principalId); 072 root.addEqualTo("effectiveDate", effdtSubQuery); 073 root.addEqualTo("timestamp", timestampSubQuery); 074 root.addEqualTo("primaryIndicator", true); 075 076 Criteria activeFilter = new Criteria(); // Inner Join For Activity 077 activeFilter.addEqualTo("active", true); 078 root.addAndCriteria(activeFilter); 079 080 Query query = QueryFactory.newQuery(Job.class, root); 081 082 return (Job) this.getPersistenceBrokerTemplate().getObjectByQuery(query); 083 } 084 085 @SuppressWarnings({"unchecked", "rawtypes"}) 086 @Override 087 public List<Job> getJobs(String principalId, Date payPeriodEndDate) { 088 List<Job> jobs = new LinkedList<Job>(); 089 Criteria root = new Criteria(); 090 Criteria effdt = new Criteria(); 091 Criteria timestamp = new Criteria(); 092 093 effdt.addEqualTo("principalId", principalId); 094 effdt.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber"); 095 effdt.addLessOrEqualThan("effectiveDate", payPeriodEndDate); 096 ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(Job.class, effdt); 097 effdtSubQuery.setAttributes(new String[]{"max(effectiveDate)"}); 098 099 timestamp.addEqualTo("principalId", principalId); 100 timestamp.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber"); 101 timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate"); 102 ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(Job.class, timestamp); 103 timestampSubQuery.setAttributes(new String[]{"max(timestamp)"}); 104 105 root.addEqualTo("principalId", principalId); 106 root.addEqualTo("effectiveDate", effdtSubQuery); 107 root.addEqualTo("timestamp", timestampSubQuery); 108 109 Criteria activeFilter = new Criteria(); // Inner Join For Activity 110 activeFilter.addEqualTo("active", true); 111 root.addAndCriteria(activeFilter); 112 113 114 Query query = QueryFactory.newQuery(Job.class, root); 115 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query); 116 117 if (c != null) { 118 jobs.addAll(c); 119 } 120 121 122 return jobs; 123 } 124 125 public Job getJob(String principalId, Long jobNumber, Date asOfDate) { 126 Criteria root = new Criteria(); 127 Criteria effdt = new Criteria(); 128 Criteria timestamp = new Criteria(); 129 130 effdt.addEqualTo("principalId", principalId); 131 effdt.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber"); 132 effdt.addLessOrEqualThan("effectiveDate", asOfDate); 133 ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(Job.class, effdt); 134 effdtSubQuery.setAttributes(new String[]{"max(effectiveDate)"}); 135 136 timestamp.addEqualTo("principalId", principalId); 137 timestamp.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber"); 138 timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate"); 139 ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(Job.class, timestamp); 140 timestampSubQuery.setAttributes(new String[]{"max(timestamp)"}); 141 142 root.addEqualTo("principalId", principalId); 143 root.addEqualTo("jobNumber", jobNumber); 144 root.addEqualTo("effectiveDate", effdtSubQuery); 145 root.addEqualTo("timestamp", timestampSubQuery); 146 147 Criteria activeFilter = new Criteria(); // Inner Join For Activity 148 activeFilter.addEqualTo("active", true); 149 root.addAndCriteria(activeFilter); 150 151 Query query = QueryFactory.newQuery(Job.class, root); 152 Job job = (Job) this.getPersistenceBrokerTemplate().getObjectByQuery(query); 153 return job; 154 } 155 156 @SuppressWarnings("unchecked") 157 public List<Job> getActiveJobsForPosition(String positionNbr, Date asOfDate) { 158 Criteria root = new Criteria(); 159 Criteria effdt = new Criteria(); 160 Criteria timestamp = new Criteria(); 161 162 // OJB's awesome sub query setup part 1 163 effdt.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId"); 164 effdt.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber"); 165 effdt.addLessOrEqualThan("effectiveDate", asOfDate); 166 ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(Job.class, effdt); 167 effdtSubQuery.setAttributes(new String[]{"max(effectiveDate)"}); 168 169 // OJB's awesome sub query setup part 2 170 timestamp.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId"); 171 timestamp.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber"); 172 timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate"); 173 ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(Job.class, timestamp); 174 timestampSubQuery.setAttributes(new String[]{"max(timestamp)"}); 175 176 root.addEqualTo("positionNumber", positionNbr); 177 root.addEqualTo("effectiveDate", effdtSubQuery); 178 root.addEqualTo("timestamp", timestampSubQuery); 179 180 Criteria activeFilter = new Criteria(); // Inner Join For Activity 181 activeFilter.addEqualTo("active", true); 182 root.addAndCriteria(activeFilter); 183 184 Query query = QueryFactory.newQuery(Job.class, root); 185 return (List<Job>) this.getPersistenceBrokerTemplate().getCollectionByQuery(query); 186 } 187 188 @SuppressWarnings("unchecked") 189 public List<Job> getActiveJobsForPayType(String hrPayType, Date asOfDate) { 190 Criteria root = new Criteria(); 191 Criteria effdt = new Criteria(); 192 Criteria timestamp = new Criteria(); 193 194 // OJB's awesome sub query setup part 1 195 effdt.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId"); 196 effdt.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber"); 197 effdt.addLessOrEqualThan("effectiveDate", asOfDate); 198 ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(Job.class, effdt); 199 effdtSubQuery.setAttributes(new String[]{"max(effectiveDate)"}); 200 201 // OJB's awesome sub query setup part 2 202 timestamp.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId"); 203 timestamp.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber"); 204 timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate"); 205 ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(Job.class, timestamp); 206 timestampSubQuery.setAttributes(new String[]{"max(timestamp)"}); 207 208 root.addEqualTo("hrPayType", hrPayType); 209 root.addEqualTo("effectiveDate", effdtSubQuery); 210 root.addEqualTo("timestamp", timestampSubQuery); 211 212 Criteria activeFilter = new Criteria(); // Inner Join For Activity 213 activeFilter.addEqualTo("active", true); 214 root.addAndCriteria(activeFilter); 215 216 Query query = QueryFactory.newQuery(Job.class, root); 217 return (List<Job>) this.getPersistenceBrokerTemplate().getCollectionByQuery(query); 218 } 219 220 @Override 221 public Job getJob(String hrJobId) { 222 Criteria crit = new Criteria(); 223 crit.addEqualTo("hrJobId", hrJobId); 224 225 Query query = QueryFactory.newQuery(Job.class, crit); 226 return (Job) this.getPersistenceBrokerTemplate().getObjectByQuery(query); 227 } 228 229 @Override 230 public Job getMaxJob(String principalId) { 231 Criteria root = new Criteria(); 232 Criteria crit = new Criteria(); 233 crit.addEqualTo("principalId", principalId); 234 ReportQueryByCriteria jobNumberSubQuery = QueryFactory.newReportQuery(Job.class, crit); 235 jobNumberSubQuery.setAttributes(new String[]{"max(jobNumber)"}); 236 237 crit.addEqualTo("principalId", principalId); 238 root.addEqualTo("jobNumber", jobNumberSubQuery); 239 240 Query query = QueryFactory.newQuery(Job.class, root); 241 return (Job) this.getPersistenceBrokerTemplate().getObjectByQuery(query); 242 } 243 244 @Override 245 @SuppressWarnings("unchecked") 246 public List<Job> getJobs(String principalId, String jobNumber, String dept, String positionNumber, String hrPayType, Date fromEffdt, Date toEffdt, 247 String active, String showHistory) { 248 249 List<Job> results = new ArrayList<Job>(); 250 251 Criteria root = new Criteria(); 252 253 if (StringUtils.isNotBlank(principalId)) { 254 root.addLike("principalId", principalId); 255 } 256 257 if (StringUtils.isNotBlank(jobNumber)) { 258 root.addLike("jobNumber", jobNumber); 259 } 260 261 if (StringUtils.isNotBlank(dept)) { 262 root.addLike("dept", dept); 263 } 264 265 if (StringUtils.isNotBlank(positionNumber)) { 266 root.addLike("positionNumber", positionNumber); 267 } 268 269 if (StringUtils.isNotBlank(hrPayType)) { 270 root.addLike("hrPayType", hrPayType); 271 } 272 273 Criteria effectiveDateFilter = new Criteria(); 274 if (fromEffdt != null) { 275 effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt); 276 } 277 if (toEffdt != null) { 278 effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt); 279 } 280 if (fromEffdt == null && toEffdt == null) { 281 effectiveDateFilter.addLessOrEqualThan("effectiveDate", TKUtils.getCurrentDate()); 282 } 283 root.addAndCriteria(effectiveDateFilter); 284 285 if (StringUtils.isNotBlank(active)) { 286 Criteria activeFilter = new Criteria(); 287 if (StringUtils.equals(active, "Y")) { 288 activeFilter.addEqualTo("active", true); 289 } else if (StringUtils.equals(active, "N")) { 290 activeFilter.addEqualTo("active", false); 291 } 292 root.addAndCriteria(activeFilter); 293 } 294 295 if (StringUtils.equals(showHistory, "N")) { 296 Criteria effdt = new Criteria(); 297 effdt.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId"); 298 effdt.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber"); 299 effdt.addEqualToField("dept", Criteria.PARENT_QUERY_PREFIX + "dept"); 300 effdt.addEqualToField("positionNumber", Criteria.PARENT_QUERY_PREFIX + "positionNumber"); 301 effdt.addEqualToField("hrPayType", Criteria.PARENT_QUERY_PREFIX + "hrPayType"); 302 effdt.addAndCriteria(effectiveDateFilter); 303 ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(Job.class, effdt); 304 effdtSubQuery.setAttributes(new String[]{"max(effectiveDate)"}); 305 root.addEqualTo("effectiveDate", effdtSubQuery); 306 307 Criteria timestamp = new Criteria(); 308 timestamp.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId"); 309 timestamp.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber"); 310 timestamp.addEqualToField("dept", Criteria.PARENT_QUERY_PREFIX + "dept"); 311 timestamp.addEqualToField("positionNumber", Criteria.PARENT_QUERY_PREFIX + "positionNumber"); 312 timestamp.addEqualToField("hrPayType", Criteria.PARENT_QUERY_PREFIX + "hrPayType"); 313 timestamp.addAndCriteria(effectiveDateFilter); 314 ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(Job.class, timestamp); 315 timestampSubQuery.setAttributes(new String[]{"max(timestamp)"}); 316 root.addEqualTo("timestamp", timestampSubQuery); 317 } 318 319 Query query = QueryFactory.newQuery(Job.class, root); 320 results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query)); 321 322 return results; 323 } 324 325 @Override 326 public int getJobCount(String principalId, Long jobNumber, String dept) { 327 Criteria crit = new Criteria(); 328 crit.addEqualTo("jobNumber", jobNumber); 329 if(principalId != null) { 330 crit.addEqualTo("principalId", principalId); 331 } 332 if(dept != null) { 333 crit.addEqualTo("dept", dept); 334 } 335 Query query = QueryFactory.newQuery(Job.class, crit); 336 return this.getPersistenceBrokerTemplate().getCount(query); 337 } 338 }