001/**
002 * Copyright 2004-2014 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 */
016package org.kuali.kpme.tklm.time.clocklog.dao;
017
018import java.util.List;
019
020import org.apache.log4j.Logger;
021import org.apache.ojb.broker.query.Criteria;
022import org.apache.ojb.broker.query.Query;
023import org.apache.ojb.broker.query.QueryFactory;
024import org.apache.ojb.broker.query.ReportQueryByCriteria;
025import org.kuali.kpme.core.calendar.entry.CalendarEntryBo;
026import org.kuali.kpme.tklm.time.clocklog.ClockLogBo;
027import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
028
029public class ClockLogDaoOjbImpl extends PlatformAwareDaoBaseOjb implements ClockLogDao {
030
031    @SuppressWarnings("unused")
032        private static final Logger LOG = Logger.getLogger(ClockLogDaoOjbImpl.class);
033    
034    public void saveOrUpdate(ClockLogBo clockLog) {
035        this.getPersistenceBrokerTemplate().store(clockLog);
036    }
037    
038    public void saveOrUpdate(List<ClockLogBo> clockLogList) {
039        if (clockLogList != null) {
040                for (ClockLogBo clockLog : clockLogList) {
041                        this.getPersistenceBrokerTemplate().store(clockLog);
042                }
043                }
044    }
045    
046    public ClockLogBo getClockLog(String tkClockLogId){
047        Criteria crit = new Criteria();
048        crit.addEqualTo("tkClockLogId", tkClockLogId);
049        Query query = QueryFactory.newQuery(ClockLogBo.class, crit);
050        return (ClockLogBo)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
051    }
052    
053    public ClockLogBo 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(ClockLogBo.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(ClockLogBo.class, timestampJoinCriteria);
070                timeStampSubQuery.setAttributes(new String[]{"max(timestamp)"});
071        
072                currentRecordCriteria.addEqualTo("timestamp", timeStampSubQuery);
073        
074                return (ClockLogBo)this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(ClockLogBo.class,currentRecordCriteria));
075    }
076    
077    @Override
078        public ClockLogBo 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(ClockLogBo.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(ClockLogBo.class, timestampJoinCriteria);
098        timeStampSubQuery.setAttributes(new String[]{"max(timestamp)"});
099        
100        currentRecordCriteria.addEqualTo("timestamp", timeStampSubQuery);
101        
102        return (ClockLogBo)this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(ClockLogBo.class,currentRecordCriteria));
103    }
104
105    @Override
106    public ClockLogBo getLastClockLog(String groupKeyCode, String principalId, String jobNumber, String workArea, String task, String timesheetId) {
107        Criteria criteria = new Criteria();
108        criteria.addEqualTo("groupKeyCode", groupKeyCode);
109        criteria.addEqualTo("principalId", principalId);
110        criteria.addEqualTo("jobNumber", jobNumber);
111        criteria.addEqualTo("workArea", workArea);
112        criteria.addEqualTo("task", task);
113        criteria.addEqualTo("documentId", timesheetId);
114        
115        Criteria clockTimeJoinCriteria = new Criteria();
116        clockTimeJoinCriteria.addEqualToField("groupKeyCode", Criteria.PARENT_QUERY_PREFIX + "groupKeyCode");
117        clockTimeJoinCriteria.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
118        clockTimeJoinCriteria.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber");
119        clockTimeJoinCriteria.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
120        clockTimeJoinCriteria.addEqualToField("task", Criteria.PARENT_QUERY_PREFIX + "task");
121        clockTimeJoinCriteria.addEqualToField("documentId", Criteria.PARENT_QUERY_PREFIX + "documentId");
122        ReportQueryByCriteria clockTimeSubQuery = QueryFactory.newReportQuery(ClockLogBo.class, clockTimeJoinCriteria);
123        clockTimeSubQuery.setAttributes(new String[]{"max(clockTimestamp)"});
124        criteria.addEqualTo("clockTimestamp", clockTimeSubQuery);
125
126        Criteria timestampJoinCriteria = new Criteria();
127        timestampJoinCriteria.addEqualToField("groupKeyCode", Criteria.PARENT_QUERY_PREFIX + "groupKeyCode");
128        timestampJoinCriteria.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
129        timestampJoinCriteria.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber");
130        timestampJoinCriteria.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
131        timestampJoinCriteria.addEqualToField("task", Criteria.PARENT_QUERY_PREFIX + "task");
132        timestampJoinCriteria.addEqualToField("documentId", Criteria.PARENT_QUERY_PREFIX + "documentId");
133        ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(ClockLogBo.class, timestampJoinCriteria);
134        timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
135        criteria.addEqualTo("timestamp", timestampSubQuery);
136
137        return (ClockLogBo) this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(ClockLogBo.class, criteria));
138    }
139
140    @Override
141        public ClockLogBo getLastClockLog(String principalId, String jobNumber, String workArea, String task, CalendarEntryBo calendarEntry) {
142        Criteria criteria = new Criteria();
143        criteria.addEqualTo("principalId", principalId);
144        criteria.addEqualTo("jobNumber", jobNumber);
145        criteria.addEqualTo("workArea", workArea);
146        criteria.addEqualTo("task", task);
147
148        Criteria clockTimeJoinCriteria = new Criteria();
149        clockTimeJoinCriteria.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
150        clockTimeJoinCriteria.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber");
151        clockTimeJoinCriteria.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
152        clockTimeJoinCriteria.addEqualToField("task", Criteria.PARENT_QUERY_PREFIX + "task");
153        clockTimeJoinCriteria.addBetween("clockTimestamp", calendarEntry.getBeginPeriodDate(), calendarEntry.getEndPeriodDate());
154        ReportQueryByCriteria clockTimeSubQuery = QueryFactory.newReportQuery(ClockLogBo.class, clockTimeJoinCriteria);
155        clockTimeSubQuery.setAttributes(new String[]{"max(clockTimestamp)"});
156        criteria.addEqualTo("clockTimestamp", clockTimeSubQuery);
157        
158        Criteria timestampJoinCriteria = new Criteria();
159        timestampJoinCriteria.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
160        timestampJoinCriteria.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber");
161        timestampJoinCriteria.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
162        timestampJoinCriteria.addEqualToField("task", Criteria.PARENT_QUERY_PREFIX + "task");
163        timestampJoinCriteria.addBetween("clockTimestamp", calendarEntry.getBeginPeriodDate(), calendarEntry.getEndPeriodDate());
164        ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(ClockLogBo.class, timestampJoinCriteria);
165        timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
166        criteria.addEqualTo("timestamp", timestampSubQuery);
167        
168        return (ClockLogBo) this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(ClockLogBo.class, criteria));
169    }
170    
171    @Override
172        public void deleteClockLogsForDocumentId(String documentId) {
173        Criteria crit = new Criteria();
174        crit.addEqualTo("documentId", documentId);
175        this.getPersistenceBrokerTemplate().deleteByQuery(QueryFactory.newQuery(ClockLogBo.class, crit));
176    }
177
178}