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.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
028    
029    public class ClockLogDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements ClockLogDao {
030    
031        private static final Logger LOG = Logger.getLogger(ClockLogDaoSpringOjbImpl.class);
032        
033        public void saveOrUpdate(ClockLog clockLog) {
034            this.getPersistenceBrokerTemplate().store(clockLog);
035        }
036        
037        public void saveOrUpdate(List<ClockLog> clockLogList) {
038            if (clockLogList != null) {
039                    for (ClockLog clockLog : clockLogList) {
040                            this.getPersistenceBrokerTemplate().store(clockLog);
041                    }
042                    }
043        }
044        
045        public ClockLog getClockLog(String tkClockLogId){
046            Criteria crit = new Criteria();
047            crit.addEqualTo("tkClockLogId", tkClockLogId);
048            Query query = QueryFactory.newQuery(ClockLog.class, crit);
049            return (ClockLog)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
050        }
051        
052        public ClockLog getLastClockLog(String principalId){
053            Criteria currentRecordCriteria = new Criteria();
054            currentRecordCriteria.addEqualTo("principalId", principalId);
055            
056            Criteria clockTimeJoinCriteria = new Criteria();
057            clockTimeJoinCriteria.addEqualToField("principalId",Criteria.PARENT_QUERY_PREFIX +"principalId");
058            
059            ReportQueryByCriteria clockTimeSubQuery = QueryFactory.newReportQuery(ClockLog.class, clockTimeJoinCriteria);
060            clockTimeSubQuery.setAttributes(new String[]{"max(clockTimestamp)"});
061            
062            currentRecordCriteria.addEqualTo("clockTimestamp", clockTimeSubQuery);
063            
064            Criteria timestampJoinCriteria = new Criteria();
065            timestampJoinCriteria.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
066            timestampJoinCriteria.addEqualToField("clockTimestamp", Criteria.PARENT_QUERY_PREFIX + "clockTimestamp");
067            
068                    ReportQueryByCriteria timeStampSubQuery = QueryFactory.newReportQuery(ClockLog.class, timestampJoinCriteria);
069                    timeStampSubQuery.setAttributes(new String[]{"max(timestamp)"});
070            
071                    currentRecordCriteria.addEqualTo("timestamp", timeStampSubQuery);
072            
073                    return (ClockLog)this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(ClockLog.class,currentRecordCriteria));
074        }
075        
076        @Override
077            public ClockLog getLastClockLog(String principalId, String clockAction){
078            Criteria currentRecordCriteria = new Criteria();
079            currentRecordCriteria.addEqualTo("principalId", principalId);
080            currentRecordCriteria.addEqualTo("clockAction", clockAction);
081            
082            Criteria clockTimeJoinCriteria = new Criteria();
083            clockTimeJoinCriteria.addEqualToField("principalId",Criteria.PARENT_QUERY_PREFIX +"principalId");
084            clockTimeJoinCriteria.addEqualToField("clockAction",Criteria.PARENT_QUERY_PREFIX +"clockAction");
085            
086            ReportQueryByCriteria clockTimeSubQuery = QueryFactory.newReportQuery(ClockLog.class, clockTimeJoinCriteria);
087            clockTimeSubQuery.setAttributes(new String[]{"max(clockTimestamp)"});
088            
089            currentRecordCriteria.addEqualTo("clockTimestamp", clockTimeSubQuery);
090            
091            Criteria timestampJoinCriteria = new Criteria();
092            timestampJoinCriteria.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
093            timestampJoinCriteria.addEqualToField("clockAction", Criteria.PARENT_QUERY_PREFIX + "clockAction");
094            timestampJoinCriteria.addEqualToField("clockTimestamp", Criteria.PARENT_QUERY_PREFIX + "clockTimestamp");
095            
096            ReportQueryByCriteria timeStampSubQuery = QueryFactory.newReportQuery(ClockLog.class, timestampJoinCriteria);
097            timeStampSubQuery.setAttributes(new String[]{"max(timestamp)"});
098            
099            currentRecordCriteria.addEqualTo("timestamp", timeStampSubQuery);
100            
101            return (ClockLog)this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(ClockLog.class,currentRecordCriteria));
102        }
103        
104        @Override
105            public ClockLog getLastClockLog(String principalId, String jobNumber, String workArea, String task, CalendarEntries calendarEntry) {
106            Criteria criteria = new Criteria();
107            criteria.addEqualTo("principalId", principalId);
108            criteria.addEqualTo("jobNumber", jobNumber);
109            criteria.addEqualTo("workArea", workArea);
110            criteria.addEqualTo("task", task);
111    
112            Criteria clockTimeJoinCriteria = new Criteria();
113            clockTimeJoinCriteria.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
114            clockTimeJoinCriteria.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber");
115            clockTimeJoinCriteria.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
116            clockTimeJoinCriteria.addEqualToField("task", Criteria.PARENT_QUERY_PREFIX + "task");
117            clockTimeJoinCriteria.addBetween("clockTimestamp", calendarEntry.getBeginPeriodDate(), calendarEntry.getEndPeriodDate());
118            ReportQueryByCriteria clockTimeSubQuery = QueryFactory.newReportQuery(ClockLog.class, clockTimeJoinCriteria);
119            clockTimeSubQuery.setAttributes(new String[]{"max(clockTimestamp)"});
120            criteria.addEqualTo("clockTimestamp", clockTimeSubQuery);
121            
122            Criteria timestampJoinCriteria = new Criteria();
123            timestampJoinCriteria.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
124            timestampJoinCriteria.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber");
125            timestampJoinCriteria.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
126            timestampJoinCriteria.addEqualToField("task", Criteria.PARENT_QUERY_PREFIX + "task");
127            timestampJoinCriteria.addBetween("clockTimestamp", calendarEntry.getBeginPeriodDate(), calendarEntry.getEndPeriodDate());
128            ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(ClockLog.class, timestampJoinCriteria);
129            timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
130            criteria.addEqualTo("timestamp", timestampSubQuery);
131            
132            return (ClockLog) this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(ClockLog.class, criteria));
133        }
134    
135    }