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.timeblock.TimeBlock;
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        @Override
106            public ClockLog getLastClockLog(String principalId, String jobNumber, String workArea, String task, CalendarEntries calendarEntry) {
107            Criteria criteria = new Criteria();
108            criteria.addEqualTo("principalId", principalId);
109            criteria.addEqualTo("jobNumber", jobNumber);
110            criteria.addEqualTo("workArea", workArea);
111            criteria.addEqualTo("task", task);
112    
113            Criteria clockTimeJoinCriteria = new Criteria();
114            clockTimeJoinCriteria.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
115            clockTimeJoinCriteria.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber");
116            clockTimeJoinCriteria.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
117            clockTimeJoinCriteria.addEqualToField("task", Criteria.PARENT_QUERY_PREFIX + "task");
118            clockTimeJoinCriteria.addBetween("clockTimestamp", calendarEntry.getBeginPeriodDate(), calendarEntry.getEndPeriodDate());
119            ReportQueryByCriteria clockTimeSubQuery = QueryFactory.newReportQuery(ClockLog.class, clockTimeJoinCriteria);
120            clockTimeSubQuery.setAttributes(new String[]{"max(clockTimestamp)"});
121            criteria.addEqualTo("clockTimestamp", clockTimeSubQuery);
122            
123            Criteria timestampJoinCriteria = new Criteria();
124            timestampJoinCriteria.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
125            timestampJoinCriteria.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber");
126            timestampJoinCriteria.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
127            timestampJoinCriteria.addEqualToField("task", Criteria.PARENT_QUERY_PREFIX + "task");
128            timestampJoinCriteria.addBetween("clockTimestamp", calendarEntry.getBeginPeriodDate(), calendarEntry.getEndPeriodDate());
129            ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(ClockLog.class, timestampJoinCriteria);
130            timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
131            criteria.addEqualTo("timestamp", timestampSubQuery);
132            
133            return (ClockLog) this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(ClockLog.class, criteria));
134        }
135        
136        @Override
137        public void deleteClockLogsForDocumentId(String documentId) {
138            Criteria crit = new Criteria();
139            crit.addEqualTo("documentId", documentId);
140            this.getPersistenceBrokerTemplate().deleteByQuery(QueryFactory.newQuery(ClockLog.class, crit));
141        }
142    
143    }