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.dept.lunch.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.dept.lunch.DeptLunchRule; 032 import org.kuali.hr.time.service.base.TkServiceLocator; 033 import org.kuali.hr.time.util.TKUtils; 034 import org.kuali.hr.time.workarea.WorkArea; 035 import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb; 036 037 public class DepartmentLunchRuleDaoImpl extends PlatformAwareDaoBaseOjb implements DepartmentLunchRuleDao { 038 private static final ImmutableList<String> EQUAL_TO_FIELDS = new ImmutableList.Builder<String>() 039 .add("dept") 040 .add("workArea") 041 .add("principalId") 042 .add("jobNumber") 043 .build(); 044 045 @Override 046 public DeptLunchRule getDepartmentLunchRule(String dept, Long workArea, String principalId, 047 Long jobNumber, Date asOfDate) { 048 Criteria root = new Criteria(); 049 050 root.addEqualTo("dept", dept); 051 root.addEqualTo("workArea", workArea); 052 root.addEqualTo("principalId", principalId); 053 root.addEqualTo("jobNumber", jobNumber); 054 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(DeptLunchRule.class, asOfDate, EQUAL_TO_FIELDS, false)); 055 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(DeptLunchRule.class, EQUAL_TO_FIELDS, false)); 056 // root.addEqualTo("active", true); 057 058 Criteria activeFilter = new Criteria(); // Inner Join For Activity 059 activeFilter.addEqualTo("active", true); 060 root.addAndCriteria(activeFilter); 061 062 063 Query query = QueryFactory.newQuery(DeptLunchRule.class, root); 064 return (DeptLunchRule)this.getPersistenceBrokerTemplate().getObjectByQuery(query); 065 066 067 } 068 069 @Override 070 public DeptLunchRule getDepartmentLunchRule(String tkDeptLunchRuleId) { 071 Criteria crit = new Criteria(); 072 crit.addEqualTo("tkDeptLunchRuleId", tkDeptLunchRuleId); 073 074 Query query = QueryFactory.newQuery(DeptLunchRule.class, crit); 075 return (DeptLunchRule)this.getPersistenceBrokerTemplate().getObjectByQuery(query); 076 } 077 078 @Override 079 @SuppressWarnings("unchecked") 080 public List<DeptLunchRule> getDepartmentLunchRules(String dept, String workArea, String principalId, String jobNumber, 081 Date fromEffdt, Date toEffdt, String active, String showHistory) { 082 List<DeptLunchRule> results = new ArrayList<DeptLunchRule>(); 083 084 Criteria root = new Criteria(); 085 086 if (StringUtils.isNotBlank(dept)) { 087 root.addLike("dept", dept); 088 } 089 090 if (StringUtils.isNotBlank(principalId)) { 091 root.addLike("principalId", principalId); 092 } 093 094 if (StringUtils.isNotBlank(jobNumber)) { 095 root.addLike("jobNumber", jobNumber); 096 } 097 098 if (StringUtils.isNotBlank(dept)) { 099 Criteria workAreaCriteria = new Criteria(); 100 Date asOfDate = TKUtils.getCurrentDate(); 101 Collection<WorkArea> workAreasForDept = TkServiceLocator.getWorkAreaService().getWorkAreas(dept,asOfDate); 102 if (CollectionUtils.isNotEmpty(workAreasForDept)) { 103 List<Long> longWorkAreas = new ArrayList<Long>(); 104 for(WorkArea cwa : workAreasForDept){ 105 longWorkAreas.add(cwa.getWorkArea()); 106 } 107 workAreaCriteria.addIn("workArea", longWorkAreas); 108 } 109 root.addAndCriteria(workAreaCriteria); 110 } 111 112 if (StringUtils.isNotBlank(workArea)) { 113 OjbSubQueryUtil.addNumericCriteria(root, "workArea", workArea); 114 } 115 116 if (StringUtils.isNotBlank(active)) { 117 Criteria activeFilter = new Criteria(); 118 if (StringUtils.equals(active, "Y")) { 119 activeFilter.addEqualTo("active", true); 120 } else if (StringUtils.equals(active, "N")) { 121 activeFilter.addEqualTo("active", false); 122 } 123 root.addAndCriteria(activeFilter); 124 } 125 126 Criteria effectiveDateFilter = new Criteria(); 127 if (fromEffdt != null) { 128 effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt); 129 } 130 if (toEffdt != null) { 131 effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt); 132 } 133 if (fromEffdt == null && toEffdt == null) { 134 effectiveDateFilter.addLessOrEqualThan("effectiveDate", TKUtils.getCurrentDate()); 135 } 136 137 if (StringUtils.equals(showHistory, "N")) { 138 root.addAndCriteria(effectiveDateFilter); 139 } 140 if (StringUtils.equals(showHistory, "N")) { 141 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithFilter(DeptLunchRule.class, effectiveDateFilter, EQUAL_TO_FIELDS, false)); 142 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(DeptLunchRule.class, EQUAL_TO_FIELDS, false)); 143 } 144 145 Query query = QueryFactory.newQuery(DeptLunchRule.class, root); 146 results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query)); 147 148 return results; 149 } 150 151 }