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.clocklog.dao; 017 018 import java.util.List; 019 020 import org.apache.log4j.Logger; 021 import org.apache.ojb.broker.query.Criteria; 022 import org.apache.ojb.broker.query.Query; 023 import org.apache.ojb.broker.query.QueryFactory; 024 import org.apache.ojb.broker.query.ReportQueryByCriteria; 025 import org.kuali.hr.time.calendar.CalendarEntries; 026 import org.kuali.hr.time.clocklog.ClockLog; 027 import org.kuali.hr.time.util.TkConstants; 028 import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb; 029 030 public class ClockLogDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements ClockLogDao { 031 032 private static final Logger LOG = Logger.getLogger(ClockLogDaoSpringOjbImpl.class); 033 034 public void saveOrUpdate(ClockLog clockLog) { 035 this.getPersistenceBrokerTemplate().store(clockLog); 036 } 037 038 public void saveOrUpdate(List<ClockLog> clockLogList) { 039 if (clockLogList != null) { 040 for (ClockLog clockLog : clockLogList) { 041 this.getPersistenceBrokerTemplate().store(clockLog); 042 } 043 } 044 } 045 046 public ClockLog getClockLog(String tkClockLogId){ 047 Criteria crit = new Criteria(); 048 crit.addEqualTo("tkClockLogId", tkClockLogId); 049 Query query = QueryFactory.newQuery(ClockLog.class, crit); 050 return (ClockLog)this.getPersistenceBrokerTemplate().getObjectByQuery(query); 051 } 052 053 public ClockLog getLastClockLog(String principalId){ 054 Criteria currentRecordCriteria = new Criteria(); 055 currentRecordCriteria.addEqualTo("principalId", principalId); 056 057 Criteria clockTimeJoinCriteria = new Criteria(); 058 clockTimeJoinCriteria.addEqualToField("principalId",Criteria.PARENT_QUERY_PREFIX +"principalId"); 059 060 ReportQueryByCriteria clockTimeSubQuery = QueryFactory.newReportQuery(ClockLog.class, clockTimeJoinCriteria); 061 clockTimeSubQuery.setAttributes(new String[]{"max(clockTimestamp)"}); 062 063 currentRecordCriteria.addEqualTo("clockTimestamp", clockTimeSubQuery); 064 065 Criteria timestampJoinCriteria = new Criteria(); 066 timestampJoinCriteria.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId"); 067 timestampJoinCriteria.addEqualToField("clockTimestamp", Criteria.PARENT_QUERY_PREFIX + "clockTimestamp"); 068 069 ReportQueryByCriteria timeStampSubQuery = QueryFactory.newReportQuery(ClockLog.class, timestampJoinCriteria); 070 timeStampSubQuery.setAttributes(new String[]{"max(timestamp)"}); 071 072 currentRecordCriteria.addEqualTo("timestamp", timeStampSubQuery); 073 074 return (ClockLog)this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(ClockLog.class,currentRecordCriteria)); 075 } 076 077 @Override 078 public ClockLog getLastClockLog(String principalId, String clockAction){ 079 Criteria currentRecordCriteria = new Criteria(); 080 currentRecordCriteria.addEqualTo("principalId", principalId); 081 currentRecordCriteria.addEqualTo("clockAction", clockAction); 082 083 Criteria clockTimeJoinCriteria = new Criteria(); 084 clockTimeJoinCriteria.addEqualToField("principalId",Criteria.PARENT_QUERY_PREFIX +"principalId"); 085 clockTimeJoinCriteria.addEqualToField("clockAction",Criteria.PARENT_QUERY_PREFIX +"clockAction"); 086 087 ReportQueryByCriteria clockTimeSubQuery = QueryFactory.newReportQuery(ClockLog.class, clockTimeJoinCriteria); 088 clockTimeSubQuery.setAttributes(new String[]{"max(clockTimestamp)"}); 089 090 currentRecordCriteria.addEqualTo("clockTimestamp", clockTimeSubQuery); 091 092 Criteria timestampJoinCriteria = new Criteria(); 093 timestampJoinCriteria.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId"); 094 timestampJoinCriteria.addEqualToField("clockAction", Criteria.PARENT_QUERY_PREFIX + "clockAction"); 095 timestampJoinCriteria.addEqualToField("clockTimestamp", Criteria.PARENT_QUERY_PREFIX + "clockTimestamp"); 096 097 ReportQueryByCriteria timeStampSubQuery = QueryFactory.newReportQuery(ClockLog.class, timestampJoinCriteria); 098 timeStampSubQuery.setAttributes(new String[]{"max(timestamp)"}); 099 100 currentRecordCriteria.addEqualTo("timestamp", timeStampSubQuery); 101 102 return (ClockLog)this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(ClockLog.class,currentRecordCriteria)); 103 } 104 105 @SuppressWarnings("unchecked") 106 public List<ClockLog> getOpenClockLogs(CalendarEntries payCalendarEntry){ 107 Criteria criteria = new Criteria(); 108 criteria.addIn("clockAction", TkConstants.ON_THE_CLOCK_CODES); 109 110 Criteria clockTimeJoinCriteria = new Criteria(); 111 clockTimeJoinCriteria.addBetween("clockTimestamp", payCalendarEntry.getBeginPeriodDate(), payCalendarEntry.getEndPeriodDate()); 112 clockTimeJoinCriteria.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId"); 113 ReportQueryByCriteria clockTimeSubQuery = QueryFactory.newReportQuery(ClockLog.class, clockTimeJoinCriteria); 114 clockTimeSubQuery.setAttributes(new String[] {new StringBuffer("max(").append("clockTimestamp").append(")").toString() }); 115 116 criteria.addEqualTo("clockTimestamp", clockTimeSubQuery); 117 118 Criteria clockTimestampJoinCriteria = new Criteria(); 119 clockTimestampJoinCriteria.addBetween("clockTimestamp", payCalendarEntry.getBeginPeriodDate(), payCalendarEntry.getEndPeriodDate()); 120 clockTimestampJoinCriteria.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId"); 121 ReportQueryByCriteria clockTimestampSubQuery = QueryFactory.newReportQuery(ClockLog.class, clockTimestampJoinCriteria); 122 clockTimestampSubQuery.setAttributes(new String[] { new StringBuffer("max(").append("timestamp").append(")").toString()}); 123 124 criteria.addEqualTo("timestamp", clockTimestampSubQuery); 125 return (List<ClockLog>)this.getPersistenceBrokerTemplate().getCollectionByQuery((QueryFactory.newQuery(ClockLog.class, criteria))); 126 } 127 128 }