View Javadoc
1   /**
2    * Copyright 2004-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.kpme.tklm.time.clocklog.dao;
17  
18  import java.util.List;
19  
20  import org.apache.log4j.Logger;
21  import org.apache.ojb.broker.query.Criteria;
22  import org.apache.ojb.broker.query.Query;
23  import org.apache.ojb.broker.query.QueryFactory;
24  import org.apache.ojb.broker.query.ReportQueryByCriteria;
25  import org.kuali.kpme.core.calendar.entry.CalendarEntryBo;
26  import org.kuali.kpme.tklm.time.clocklog.ClockLogBo;
27  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
28  
29  public class ClockLogDaoOjbImpl extends PlatformAwareDaoBaseOjb implements ClockLogDao {
30  
31      private static final Logger LOG = Logger.getLogger(ClockLogDaoOjbImpl.class);
32      
33      public void saveOrUpdate(ClockLogBo clockLog) {
34  	this.getPersistenceBrokerTemplate().store(clockLog);
35      }
36      
37      public void saveOrUpdate(List<ClockLogBo> clockLogList) {
38      	if (clockLogList != null) {
39  	    	for (ClockLogBo clockLog : clockLogList) {
40  	    		this.getPersistenceBrokerTemplate().store(clockLog);
41  	    	}
42  		}
43      }
44      
45      public ClockLogBo getClockLog(String tkClockLogId){
46      	Criteria crit = new Criteria();
47      	crit.addEqualTo("tkClockLogId", tkClockLogId);
48      	Query query = QueryFactory.newQuery(ClockLogBo.class, crit);
49      	return (ClockLogBo)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
50      }
51      
52      public ClockLogBo getLastClockLog(String principalId){
53          Criteria currentRecordCriteria = new Criteria();
54          currentRecordCriteria.addEqualTo("principalId", principalId);
55  
56          Criteria clockTimeJoinCriteria = new Criteria();
57          clockTimeJoinCriteria.addEqualToField("principalId",Criteria.PARENT_QUERY_PREFIX +"principalId");
58  
59          ReportQueryByCriteria clockTimeSubQuery = QueryFactory.newReportQuery(ClockLogBo.class, clockTimeJoinCriteria);
60          clockTimeSubQuery.setAttributes(new String[]{"max(clockTimestamp)"});
61  
62          currentRecordCriteria.addEqualTo("clockTimestamp", clockTimeSubQuery);
63  
64          Criteria timestampJoinCriteria = new Criteria();
65          timestampJoinCriteria.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
66          timestampJoinCriteria.addEqualToField("clockTimestamp", Criteria.PARENT_QUERY_PREFIX + "clockTimestamp");
67  	
68  		ReportQueryByCriteria timeStampSubQuery = QueryFactory.newReportQuery(ClockLogBo.class, timestampJoinCriteria);
69  		timeStampSubQuery.setAttributes(new String[]{"max(timestamp)"});
70  	
71  		currentRecordCriteria.addEqualTo("timestamp", timeStampSubQuery);
72  	
73  		return (ClockLogBo)this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(ClockLogBo.class,currentRecordCriteria));
74      }
75      
76      @Override
77  	public ClockLogBo getLastClockLog(String principalId, String clockAction){
78      	Criteria currentRecordCriteria = new Criteria();
79      	currentRecordCriteria.addEqualTo("principalId", principalId);
80      	currentRecordCriteria.addEqualTo("clockAction", clockAction);
81      	
82      	Criteria clockTimeJoinCriteria = new Criteria();
83      	clockTimeJoinCriteria.addEqualToField("principalId",Criteria.PARENT_QUERY_PREFIX +"principalId");
84      	clockTimeJoinCriteria.addEqualToField("clockAction",Criteria.PARENT_QUERY_PREFIX +"clockAction");
85      	
86      	ReportQueryByCriteria clockTimeSubQuery = QueryFactory.newReportQuery(ClockLogBo.class, clockTimeJoinCriteria);
87      	clockTimeSubQuery.setAttributes(new String[]{"max(clockTimestamp)"});
88      	
89      	currentRecordCriteria.addEqualTo("clockTimestamp", clockTimeSubQuery);
90      	
91      	Criteria timestampJoinCriteria = new Criteria();
92      	timestampJoinCriteria.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
93      	timestampJoinCriteria.addEqualToField("clockAction", Criteria.PARENT_QUERY_PREFIX + "clockAction");
94      	timestampJoinCriteria.addEqualToField("clockTimestamp", Criteria.PARENT_QUERY_PREFIX + "clockTimestamp");
95      	
96      	ReportQueryByCriteria timeStampSubQuery = QueryFactory.newReportQuery(ClockLogBo.class, timestampJoinCriteria);
97      	timeStampSubQuery.setAttributes(new String[]{"max(timestamp)"});
98      	
99      	currentRecordCriteria.addEqualTo("timestamp", timeStampSubQuery);
100     	
101     	return (ClockLogBo)this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(ClockLogBo.class,currentRecordCriteria));
102     }
103 
104     @Override
105     public ClockLogBo getLastClockLog(String principalId, String jobNumber, String workArea, String task, String timesheetId) {
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         criteria.addEqualTo("documentId", timesheetId);
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.addEqualToField("documentId", Criteria.PARENT_QUERY_PREFIX + "documentId");
118         ReportQueryByCriteria clockTimeSubQuery = QueryFactory.newReportQuery(ClockLogBo.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.addEqualToField("documentId", Criteria.PARENT_QUERY_PREFIX + "documentId");
128         ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(ClockLogBo.class, timestampJoinCriteria);
129         timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
130         criteria.addEqualTo("timestamp", timestampSubQuery);
131 
132         return (ClockLogBo) this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(ClockLogBo.class, criteria));
133     }
134 
135     @Override
136 	public ClockLogBo getLastClockLog(String principalId, String jobNumber, String workArea, String task, CalendarEntryBo calendarEntry) {
137     	Criteria criteria = new Criteria();
138     	criteria.addEqualTo("principalId", principalId);
139     	criteria.addEqualTo("jobNumber", jobNumber);
140     	criteria.addEqualTo("workArea", workArea);
141     	criteria.addEqualTo("task", task);
142 
143     	Criteria clockTimeJoinCriteria = new Criteria();
144     	clockTimeJoinCriteria.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
145     	clockTimeJoinCriteria.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber");
146     	clockTimeJoinCriteria.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
147     	clockTimeJoinCriteria.addEqualToField("task", Criteria.PARENT_QUERY_PREFIX + "task");
148     	clockTimeJoinCriteria.addBetween("clockTimestamp", calendarEntry.getBeginPeriodDate(), calendarEntry.getEndPeriodDate());
149     	ReportQueryByCriteria clockTimeSubQuery = QueryFactory.newReportQuery(ClockLogBo.class, clockTimeJoinCriteria);
150     	clockTimeSubQuery.setAttributes(new String[]{"max(clockTimestamp)"});
151     	criteria.addEqualTo("clockTimestamp", clockTimeSubQuery);
152     	
153     	Criteria timestampJoinCriteria = new Criteria();
154     	timestampJoinCriteria.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
155     	timestampJoinCriteria.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber");
156     	timestampJoinCriteria.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
157     	timestampJoinCriteria.addEqualToField("task", Criteria.PARENT_QUERY_PREFIX + "task");
158     	timestampJoinCriteria.addBetween("clockTimestamp", calendarEntry.getBeginPeriodDate(), calendarEntry.getEndPeriodDate());
159     	ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(ClockLogBo.class, timestampJoinCriteria);
160     	timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
161     	criteria.addEqualTo("timestamp", timestampSubQuery);
162     	
163     	return (ClockLogBo) this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(ClockLogBo.class, criteria));
164     }
165     
166     @Override
167 	public void deleteClockLogsForDocumentId(String documentId) {
168     	Criteria crit = new Criteria();
169     	crit.addEqualTo("documentId", documentId);
170     	this.getPersistenceBrokerTemplate().deleteByQuery(QueryFactory.newQuery(ClockLogBo.class, crit));
171     }
172 
173 }