View Javadoc

1   /**
2    * Copyright 2004-2012 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.hr.time.util.TkConstants;
28  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
29  
30  public class ClockLogDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements ClockLogDao {
31  
32      private static final Logger LOG = Logger.getLogger(ClockLogDaoSpringOjbImpl.class);
33      
34      public void saveOrUpdate(ClockLog clockLog) {
35  	this.getPersistenceBrokerTemplate().store(clockLog);
36      }
37      
38      public void saveOrUpdate(List<ClockLog> clockLogList) {
39      	if (clockLogList != null) {
40  	    	for (ClockLog clockLog : clockLogList) {
41  	    		this.getPersistenceBrokerTemplate().store(clockLog);
42  	    	}
43  		}
44      }
45      
46      public ClockLog getClockLog(String tkClockLogId){
47      	Criteria crit = new Criteria();
48      	crit.addEqualTo("tkClockLogId", tkClockLogId);
49      	Query query = QueryFactory.newQuery(ClockLog.class, crit);
50      	return (ClockLog)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
51      }
52      
53      public ClockLog getLastClockLog(String principalId){
54  	Criteria currentRecordCriteria = new Criteria();
55  	currentRecordCriteria.addEqualTo("principalId", principalId);
56  	
57  	Criteria clockTimeJoinCriteria = new Criteria();
58  	clockTimeJoinCriteria.addEqualToField("principalId",Criteria.PARENT_QUERY_PREFIX +"principalId");
59  	
60  	ReportQueryByCriteria clockTimeSubQuery = QueryFactory.newReportQuery(ClockLog.class, clockTimeJoinCriteria);
61  	clockTimeSubQuery.setAttributes(new String[]{"max(clockTimestamp)"});
62  	
63  	currentRecordCriteria.addEqualTo("clockTimestamp", clockTimeSubQuery);
64  	
65  	Criteria timestampJoinCriteria = new Criteria();
66  	timestampJoinCriteria.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
67  	timestampJoinCriteria.addEqualToField("clockTimestamp", Criteria.PARENT_QUERY_PREFIX + "clockTimestamp");
68  	
69  		ReportQueryByCriteria timeStampSubQuery = QueryFactory.newReportQuery(ClockLog.class, timestampJoinCriteria);
70  		timeStampSubQuery.setAttributes(new String[]{"max(timestamp)"});
71  	
72  		currentRecordCriteria.addEqualTo("timestamp", timeStampSubQuery);
73  	
74  		return (ClockLog)this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(ClockLog.class,currentRecordCriteria));
75      }
76      
77      @Override
78  	public ClockLog getLastClockLog(String principalId, String clockAction){
79      	Criteria currentRecordCriteria = new Criteria();
80      	currentRecordCriteria.addEqualTo("principalId", principalId);
81      	currentRecordCriteria.addEqualTo("clockAction", clockAction);
82      	
83      	Criteria clockTimeJoinCriteria = new Criteria();
84      	clockTimeJoinCriteria.addEqualToField("principalId",Criteria.PARENT_QUERY_PREFIX +"principalId");
85      	clockTimeJoinCriteria.addEqualToField("clockAction",Criteria.PARENT_QUERY_PREFIX +"clockAction");
86      	
87      	ReportQueryByCriteria clockTimeSubQuery = QueryFactory.newReportQuery(ClockLog.class, clockTimeJoinCriteria);
88      	clockTimeSubQuery.setAttributes(new String[]{"max(clockTimestamp)"});
89      	
90      	currentRecordCriteria.addEqualTo("clockTimestamp", clockTimeSubQuery);
91      	
92      	Criteria timestampJoinCriteria = new Criteria();
93      	timestampJoinCriteria.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
94      	timestampJoinCriteria.addEqualToField("clockAction", Criteria.PARENT_QUERY_PREFIX + "clockAction");
95      	timestampJoinCriteria.addEqualToField("clockTimestamp", Criteria.PARENT_QUERY_PREFIX + "clockTimestamp");
96      	
97      	ReportQueryByCriteria timeStampSubQuery = QueryFactory.newReportQuery(ClockLog.class, timestampJoinCriteria);
98      	timeStampSubQuery.setAttributes(new String[]{"max(timestamp)"});
99      	
100     	currentRecordCriteria.addEqualTo("timestamp", timeStampSubQuery);
101     	
102     	return (ClockLog)this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(ClockLog.class,currentRecordCriteria));
103     }
104     
105     @SuppressWarnings("unchecked")
106 	public List<ClockLog> getOpenClockLogs(CalendarEntries payCalendarEntry){
107     	Criteria criteria = new Criteria();
108     	criteria.addIn("clockAction", TkConstants.ON_THE_CLOCK_CODES);
109 
110     	Criteria clockTimeJoinCriteria = new Criteria();
111         clockTimeJoinCriteria.addBetween("clockTimestamp", payCalendarEntry.getBeginPeriodDate(), payCalendarEntry.getEndPeriodDate());
112     	clockTimeJoinCriteria.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
113     	ReportQueryByCriteria clockTimeSubQuery = QueryFactory.newReportQuery(ClockLog.class, clockTimeJoinCriteria);
114     	clockTimeSubQuery.setAttributes(new String[] {new StringBuffer("max(").append("clockTimestamp").append(")").toString() });
115     	
116     	criteria.addEqualTo("clockTimestamp", clockTimeSubQuery);
117     	
118     	Criteria clockTimestampJoinCriteria = new Criteria();
119         clockTimestampJoinCriteria.addBetween("clockTimestamp", payCalendarEntry.getBeginPeriodDate(), payCalendarEntry.getEndPeriodDate());
120     	clockTimestampJoinCriteria.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
121     	ReportQueryByCriteria clockTimestampSubQuery = QueryFactory.newReportQuery(ClockLog.class, clockTimestampJoinCriteria);
122     	clockTimestampSubQuery.setAttributes(new String[] { new StringBuffer("max(").append("timestamp").append(")").toString()});
123     	
124     	criteria.addEqualTo("timestamp", clockTimestampSubQuery);
125     	return (List<ClockLog>)this.getPersistenceBrokerTemplate().getCollectionByQuery((QueryFactory.newQuery(ClockLog.class, criteria)));
126     }
127 
128 }