View Javadoc

1   /**
2    * Copyright 2004-2013 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.hr.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.hr.time.calendar.CalendarEntries;
26  import org.kuali.hr.time.clocklog.ClockLog;
27  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
28  
29  public class ClockLogDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements ClockLogDao {
30  
31      private static final Logger LOG = Logger.getLogger(ClockLogDaoSpringOjbImpl.class);
32      
33      public void saveOrUpdate(ClockLog clockLog) {
34  	this.getPersistenceBrokerTemplate().store(clockLog);
35      }
36      
37      public void saveOrUpdate(List<ClockLog> clockLogList) {
38      	if (clockLogList != null) {
39  	    	for (ClockLog clockLog : clockLogList) {
40  	    		this.getPersistenceBrokerTemplate().store(clockLog);
41  	    	}
42  		}
43      }
44      
45      public ClockLog getClockLog(String tkClockLogId){
46      	Criteria crit = new Criteria();
47      	crit.addEqualTo("tkClockLogId", tkClockLogId);
48      	Query query = QueryFactory.newQuery(ClockLog.class, crit);
49      	return (ClockLog)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
50      }
51      
52      public ClockLog 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(ClockLog.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(ClockLog.class, timestampJoinCriteria);
69  		timeStampSubQuery.setAttributes(new String[]{"max(timestamp)"});
70  	
71  		currentRecordCriteria.addEqualTo("timestamp", timeStampSubQuery);
72  	
73  		return (ClockLog)this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(ClockLog.class,currentRecordCriteria));
74      }
75      
76      @Override
77  	public ClockLog 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(ClockLog.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(ClockLog.class, timestampJoinCriteria);
97      	timeStampSubQuery.setAttributes(new String[]{"max(timestamp)"});
98      	
99      	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 }