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.principal.dao; 017 018 import java.util.ArrayList; 019 import java.util.Collection; 020 import java.util.Date; 021 import java.util.List; 022 023 import org.apache.commons.collections.CollectionUtils; 024 import org.apache.commons.lang.StringUtils; 025 import org.apache.ojb.broker.query.Criteria; 026 import org.apache.ojb.broker.query.Query; 027 import org.apache.ojb.broker.query.QueryFactory; 028 import org.apache.ojb.broker.query.ReportQueryByCriteria; 029 import org.kuali.hr.time.principal.PrincipalHRAttributes; 030 import org.kuali.hr.time.util.TKUtils; 031 import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb; 032 033 public class PrincipalHRAttributesDaoImpl extends PlatformAwareDaoBaseOjb implements PrincipalHRAttributesDao { 034 035 @Override 036 public PrincipalHRAttributes getPrincipalCalendar(String principalId, 037 Date asOfDate) { 038 PrincipalHRAttributes pc = null; 039 040 Criteria root = new Criteria(); 041 Criteria effdt = new Criteria(); 042 Criteria timestamp = new Criteria(); 043 044 effdt.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId"); 045 effdt.addLessOrEqualThan("effectiveDate", asOfDate); 046 ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(PrincipalHRAttributes.class, effdt); 047 effdtSubQuery.setAttributes(new String[] { "max(effdt)" }); 048 049 timestamp.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId"); 050 timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate"); 051 ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(PrincipalHRAttributes.class, timestamp); 052 timestampSubQuery.setAttributes(new String[] { "max(timestamp)" }); 053 054 root.addEqualTo("principalId", principalId); 055 root.addEqualTo("effectiveDate", effdtSubQuery); 056 root.addEqualTo("timestamp", timestampSubQuery); 057 058 Criteria activeFilter = new Criteria(); // Inner Join For Activity 059 activeFilter.addEqualTo("active", true); 060 root.addAndCriteria(activeFilter); 061 062 Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root); 063 Object obj = this.getPersistenceBrokerTemplate().getObjectByQuery(query); 064 065 if (obj != null) { 066 pc = (PrincipalHRAttributes) obj; 067 } 068 069 return pc; 070 } 071 072 @Override 073 public void saveOrUpdate(PrincipalHRAttributes principalCalendar) { 074 this.getPersistenceBrokerTemplate().store(principalCalendar); 075 076 } 077 078 @Override 079 public void saveOrUpdate(List<PrincipalHRAttributes> lstPrincipalCalendar) { 080 if(lstPrincipalCalendar != null){ 081 for(PrincipalHRAttributes principalCal : lstPrincipalCalendar){ 082 this.getPersistenceBrokerTemplate().store(principalCal); 083 } 084 } 085 086 } 087 088 089 // @Override 090 // public PrincipalHRAttributes getPrincipalHRAttributes(String principalId) { 091 // Criteria crit = new Criteria(); 092 // crit.addEqualTo("principalId", principalId); 093 // Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, crit); 094 // return (PrincipalHRAttributes)this.getPersistenceBrokerTemplate().getObjectByQuery(query); 095 // } 096 097 @Override 098 public PrincipalHRAttributes getInactivePrincipalHRAttributes(String principalId, Date asOfDate) { 099 PrincipalHRAttributes pc = null; 100 101 Criteria root = new Criteria(); 102 Criteria effdt = new Criteria(); 103 Criteria timestamp = new Criteria(); 104 105 effdt.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId"); 106 effdt.addGreaterOrEqualThan("effectiveDate", asOfDate); 107 ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(PrincipalHRAttributes.class, effdt); 108 effdtSubQuery.setAttributes(new String[] { "max(effdt)" }); 109 110 timestamp.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId"); 111 timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate"); 112 ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(PrincipalHRAttributes.class, timestamp); 113 timestampSubQuery.setAttributes(new String[] { "max(timestamp)" }); 114 115 root.addEqualTo("principalId", principalId); 116 root.addEqualTo("effectiveDate", effdtSubQuery); 117 root.addEqualTo("timestamp", timestampSubQuery); 118 119 Criteria activeFilter = new Criteria(); // Inner Join For Activity 120 activeFilter.addEqualTo("active", false); 121 root.addAndCriteria(activeFilter); 122 123 Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root); 124 Object obj = this.getPersistenceBrokerTemplate().getObjectByQuery(query); 125 126 if (obj != null) { 127 pc = (PrincipalHRAttributes) obj; 128 } 129 130 return pc; 131 } 132 133 @Override 134 public PrincipalHRAttributes getPrincipalHRAttributes(String hrPrincipalAttributeId) { 135 Criteria crit = new Criteria(); 136 crit.addEqualTo("hrPrincipalAttributeId", hrPrincipalAttributeId); 137 138 Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, crit); 139 return (PrincipalHRAttributes)this.getPersistenceBrokerTemplate().getObjectByQuery(query); 140 } 141 142 @Override 143 public List<PrincipalHRAttributes> getAllActivePrincipalHrAttributesForPrincipalId(String principalId, Date asOfDate) { 144 145 List<PrincipalHRAttributes> phaList = new ArrayList<PrincipalHRAttributes>(); 146 Criteria root = new Criteria(); 147 root.addEqualTo("principalId", principalId); 148 root.addLessOrEqualThan("effectiveDate", asOfDate); 149 root.addEqualTo("active", true); 150 Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root); 151 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query); 152 if (c != null) { 153 phaList.addAll(c); 154 } 155 return phaList; 156 } 157 158 @Override 159 public List<PrincipalHRAttributes> getAllInActivePrincipalHrAttributesForPrincipalId(String principalId, Date asOfDate) { 160 List<PrincipalHRAttributes> phaList = new ArrayList<PrincipalHRAttributes>(); 161 Criteria root = new Criteria(); 162 root.addEqualTo("principalId", principalId); 163 root.addLessOrEqualThan("effectiveDate", asOfDate); 164 root.addEqualTo("active", false); 165 166 Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root); 167 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query); 168 if (c != null) { 169 phaList.addAll(c); 170 } 171 return phaList; 172 } 173 @Override 174 public PrincipalHRAttributes getMaxTimeStampPrincipalHRAttributes(String principalId) { 175 Criteria root = new Criteria(); 176 Criteria crit = new Criteria(); 177 178 crit.addEqualTo("principalId", principalId); 179 ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(PrincipalHRAttributes.class, crit); 180 timestampSubQuery.setAttributes(new String[]{"max(timestamp)"}); 181 182 root.addEqualTo("principalId", principalId); 183 root.addEqualTo("timestamp", timestampSubQuery); 184 185 Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root); 186 return (PrincipalHRAttributes) this.getPersistenceBrokerTemplate().getObjectByQuery(query); 187 } 188 189 @Override 190 public List<PrincipalHRAttributes> getActivePrincipalHrAttributesForRange(String principalId, Date startDate, Date endDate) { 191 List<PrincipalHRAttributes> activeList = new ArrayList<PrincipalHRAttributes>(); 192 Criteria root = new Criteria(); 193 root.addEqualTo("principalId", principalId); 194 root.addGreaterOrEqualThan("effectiveDate", startDate); 195 root.addLessOrEqualThan("effectiveDate", endDate); 196 root.addEqualTo("active", true); 197 Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root); 198 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query); 199 if (c != null) { 200 activeList.addAll(c); 201 } 202 List<PrincipalHRAttributes> aList = new ArrayList<PrincipalHRAttributes>(); 203 aList.addAll(activeList); 204 for(PrincipalHRAttributes aPha : aList) { 205 List<PrincipalHRAttributes> inactivePhas = this.getInactivePrincipalHRAttributesForRange(principalId, aPha.getEffectiveDate(), endDate); 206 if(CollectionUtils.isNotEmpty(inactivePhas)) { 207 for(PrincipalHRAttributes inactive : inactivePhas) { 208 if(inactive.getTimestamp().after(aPha.getTimestamp())) { 209 activeList.remove(aPha); 210 } 211 } 212 } 213 } 214 215 return activeList; 216 } 217 218 @Override 219 public List<PrincipalHRAttributes> getInactivePrincipalHRAttributesForRange(String principalId, Date startDate, Date endDate) { 220 List<PrincipalHRAttributes> inactiveList = new ArrayList<PrincipalHRAttributes>(); 221 Criteria root = new Criteria(); 222 root.addEqualTo("principalId", principalId); 223 root.addGreaterOrEqualThan("effectiveDate", startDate); 224 root.addLessOrEqualThan("effectiveDate", endDate); 225 root.addEqualTo("active", false); 226 Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root); 227 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query); 228 if (c != null) { 229 inactiveList.addAll(c); 230 } 231 return inactiveList; 232 } 233 234 @Override 235 @SuppressWarnings("unchecked") 236 public List<PrincipalHRAttributes> getPrincipalHrAtributes(String principalId, java.sql.Date fromEffdt, java.sql.Date toEffdt, String active, String showHistory) { 237 List<PrincipalHRAttributes> results = new ArrayList<PrincipalHRAttributes>(); 238 239 Criteria root = new Criteria(); 240 241 if (StringUtils.isNotBlank(principalId)) { 242 root.addLike("principalId", principalId); 243 } 244 245 Criteria effectiveDateFilter = new Criteria(); 246 if (fromEffdt != null) { 247 effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt); 248 } 249 if (toEffdt != null) { 250 effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt); 251 } 252 if (fromEffdt == null && toEffdt == null) { 253 effectiveDateFilter.addLessOrEqualThan("effectiveDate", TKUtils.getCurrentDate()); 254 } 255 root.addAndCriteria(effectiveDateFilter); 256 257 if (StringUtils.isNotBlank(active)) { 258 Criteria activeFilter = new Criteria(); 259 if (StringUtils.equals(active, "Y")) { 260 activeFilter.addEqualTo("active", true); 261 } else if (StringUtils.equals(active, "N")) { 262 activeFilter.addEqualTo("active", false); 263 } 264 root.addAndCriteria(activeFilter); 265 } 266 267 if (StringUtils.equals(showHistory, "N")) { 268 Criteria effdt = new Criteria(); 269 effdt.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId"); 270 effdt.addAndCriteria(effectiveDateFilter); 271 ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(PrincipalHRAttributes.class, effdt); 272 effdtSubQuery.setAttributes(new String[]{"max(effectiveDate)"}); 273 root.addEqualTo("effectiveDate", effdtSubQuery); 274 275 Criteria timestamp = new Criteria(); 276 timestamp.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId"); 277 timestamp.addAndCriteria(effectiveDateFilter); 278 ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(PrincipalHRAttributes.class, timestamp); 279 timestampSubQuery.setAttributes(new String[]{"max(timestamp)"}); 280 root.addEqualTo("timestamp", timestampSubQuery); 281 } 282 283 Query query = QueryFactory.newQuery(PrincipalHRAttributes.class, root); 284 results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query)); 285 286 return results; 287 } 288 289 }