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.calendar.dao;
17  
18  import java.text.DateFormat;
19  import java.text.ParseException;
20  import java.text.SimpleDateFormat;
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.Date;
24  import java.util.List;
25  
26  import org.apache.ojb.broker.query.Criteria;
27  import org.apache.ojb.broker.query.Query;
28  import org.apache.ojb.broker.query.QueryByCriteria;
29  import org.apache.ojb.broker.query.QueryFactory;
30  import org.apache.ojb.broker.query.ReportQueryByCriteria;
31  import org.joda.time.DateTime;
32  import org.kuali.hr.time.calendar.CalendarEntries;
33  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
34  
35  public class CalendarEntriesDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements CalendarEntriesDao {
36  
37  
38      public void saveOrUpdate(CalendarEntries calendarEntries) {
39          this.getPersistenceBrokerTemplate().store(calendarEntries);
40      }
41  
42      public CalendarEntries getCalendarEntries(String hrCalendarEntriesId) {
43          Criteria currentRecordCriteria = new Criteria();
44          currentRecordCriteria.addEqualTo("hrCalendarEntriesId", hrCalendarEntriesId);
45  
46          return (CalendarEntries) this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(CalendarEntries.class, currentRecordCriteria));
47      }
48  
49      @Override
50      public CalendarEntries getCalendarEntriesByIdAndPeriodEndDate(String hrCalendarId, Date endPeriodDate) {
51          Criteria root = new Criteria();
52          root.addEqualTo("hrCalendarId", hrCalendarId);
53          root.addEqualTo("endPeriodDateTime", endPeriodDate);
54  
55          Query query = QueryFactory.newQuery(CalendarEntries.class, root);
56          CalendarEntries pce = (CalendarEntries) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
57          return pce;
58      }
59  
60      @Override
61      public CalendarEntries getCurrentCalendarEntriesByCalendarId(
62              String hrCalendarId, Date currentDate) {
63          Criteria root = new Criteria();
64  //		Criteria beginDate = new Criteria();
65  //		Criteria endDate = new Criteria();
66  
67  //		beginDate.addEqualToField("hrPyCalendarId", Criteria.PARENT_QUERY_PREFIX + "hrPyCalendarId");
68  //		beginDate.addLessOrEqualThan("beginPeriodDateTime", currentDate);
69  //		ReportQueryByCriteria beginDateSubQuery = QueryFactory.newReportQuery(CalendarEntries.class, beginDate);
70  //		beginDateSubQuery.setAttributes(new String[] { "max(beginPeriodDateTime)" });
71  
72  //		endDate.addEqualToField("hrPyCalendarId", Criteria.PARENT_QUERY_PREFIX + "hrPyCalendarId");
73  //		endDate.addGreaterOrEqualThan("endPeriodDateTime", currentDate);
74  //		ReportQueryByCriteria endDateSubQuery = QueryFactory.newReportQuery(CalendarEntries.class, endDate);
75  //		endDateSubQuery.setAttributes(new String[] { "min(endPeriodDateTime)" });
76  
77          root.addEqualTo("hrCalendarId", hrCalendarId);
78          //root.addEqualTo("beginPeriodDateTime", beginDateSubQuery);
79          root.addLessOrEqualThan("beginPeriodDateTime", currentDate);
80          root.addGreaterThan("endPeriodDateTime", currentDate);
81  //		root.addEqualTo("endPeriodDateTime", endDateSubQuery);
82  
83          Query query = QueryFactory.newQuery(CalendarEntries.class, root);
84  
85          CalendarEntries pce = (CalendarEntries) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
86          return pce;
87      }
88  
89      @Override
90      public CalendarEntries getCalendarEntriesByCalendarIdAndDateRange(
91              String hrCalendarId, Date beginDate, Date endDate) {
92          Criteria root = new Criteria();
93          root.addEqualTo("hrCalendarId", hrCalendarId);
94          //root.addEqualTo("beginPeriodDateTime", beginDateSubQuery);
95          root.addLessOrEqualThan("beginPeriodDateTime", endDate);
96          root.addGreaterThan("endPeriodDateTime", beginDate);
97  //		root.addEqualTo("endPeriodDateTime", endDateSubQuery);
98  
99          Query query = QueryFactory.newQuery(CalendarEntries.class, root);
100 
101         CalendarEntries pce = (CalendarEntries) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
102         return pce;
103     }
104 
105     @Override
106     public CalendarEntries getNextCalendarEntriesByCalendarId(String hrCalendarId, CalendarEntries calendarEntries) {
107         Criteria root = new Criteria();
108         Criteria beginDate = new Criteria();
109         Criteria endDate = new Criteria();
110 
111         beginDate.addEqualToField("hrCalendarId", Criteria.PARENT_QUERY_PREFIX + "hrCalendarId");
112         beginDate.addGreaterThan("beginPeriodDateTime", calendarEntries.getBeginPeriodDateTime());
113         ReportQueryByCriteria beginDateSubQuery = QueryFactory.newReportQuery(CalendarEntries.class, beginDate);
114         beginDateSubQuery.setAttributes(new String[]{"min(beginPeriodDateTime)"});
115 
116         endDate.addEqualToField("hrCalendarId", Criteria.PARENT_QUERY_PREFIX + "hrCalendarId");
117         endDate.addGreaterThan("endPeriodDateTime", calendarEntries.getEndPeriodDateTime());
118         ReportQueryByCriteria endDateSubQuery = QueryFactory.newReportQuery(CalendarEntries.class, endDate);
119         endDateSubQuery.setAttributes(new String[]{"min(endPeriodDateTime)"});
120 
121         root.addEqualTo("hrCalendarId", hrCalendarId);
122         root.addEqualTo("beginPeriodDateTime", beginDateSubQuery);
123         root.addEqualTo("endPeriodDateTime", endDateSubQuery);
124 
125         Query query = QueryFactory.newQuery(CalendarEntries.class, root);
126 
127         CalendarEntries pce = (CalendarEntries) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
128         return pce;
129     }
130 
131     @Override
132     public CalendarEntries getPreviousCalendarEntriesByCalendarId(String hrCalendarId, CalendarEntries calendarEntries) {
133         Criteria root = new Criteria();
134         Criteria beginDate = new Criteria();
135         Criteria endDate = new Criteria();
136 
137         beginDate.addEqualToField("hrCalendarId", Criteria.PARENT_QUERY_PREFIX + "hrCalendarId");
138         beginDate.addLessThan("beginPeriodDateTime", calendarEntries.getBeginPeriodDateTime());
139         ReportQueryByCriteria beginDateSubQuery = QueryFactory.newReportQuery(CalendarEntries.class, beginDate);
140         beginDateSubQuery.setAttributes(new String[]{"max(beginPeriodDateTime)"});
141 
142         endDate.addEqualToField("hrCalendarId", Criteria.PARENT_QUERY_PREFIX + "hrCalendarId");
143         endDate.addLessThan("endPeriodDateTime", calendarEntries.getEndPeriodDateTime());
144         ReportQueryByCriteria endDateSubQuery = QueryFactory.newReportQuery(CalendarEntries.class, endDate);
145         endDateSubQuery.setAttributes(new String[]{"max(endPeriodDateTime)"});
146 
147         root.addEqualTo("hrCalendarId", hrCalendarId);
148         root.addEqualTo("beginPeriodDateTime", beginDateSubQuery);
149         root.addEqualTo("endPeriodDateTime", endDateSubQuery);
150 
151         Query query = QueryFactory.newQuery(CalendarEntries.class, root);
152 
153         CalendarEntries pce = (CalendarEntries) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
154         return pce;
155     }
156 
157     public List<CalendarEntries> getCurrentCalendarEntryNeedsScheduled(int thresholdDays, Date asOfDate) {
158         DateTime current = new DateTime(asOfDate.getTime());
159         DateTime windowStart = current.minusDays(thresholdDays);
160         DateTime windowEnd = current.plusDays(thresholdDays);
161 
162         Criteria root = new Criteria();
163 
164         root.addGreaterOrEqualThan("beginPeriodDateTime", windowStart.toDate());
165         root.addLessOrEqualThan("beginPeriodDateTime", windowEnd.toDate());
166 
167         Query query = QueryFactory.newQuery(CalendarEntries.class, root);
168         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
169 
170         List<CalendarEntries> pce = new ArrayList<CalendarEntries>(c.size());
171         pce.addAll(c);
172 
173         return pce;
174     }
175 
176     public List<CalendarEntries> getFutureCalendarEntries(String hrCalendarId, Date currentDate, int numberOfEntries) {
177         Criteria root = new Criteria();
178         root.addEqualTo("hrCalendarId", hrCalendarId);
179         root.addGreaterOrEqualThan("beginPeriodDateTime", currentDate);
180         QueryByCriteria q = QueryFactory.newReportQuery(CalendarEntries.class, root);
181         q.addOrderByAscending("beginPeriodDateTime");
182         q.setStartAtIndex(1);
183         q.setEndAtIndex(numberOfEntries);
184         List<CalendarEntries> calendarEntries = new ArrayList<CalendarEntries>(this.getPersistenceBrokerTemplate().getCollectionByQuery(q));
185         return calendarEntries;
186     }
187     
188     @SuppressWarnings("unchecked")
189 	public List<CalendarEntries> getCalendarEntriesEndingBetweenBeginAndEndDate(String hrCalendarId, Date beginDate, Date endDate) {
190         List<CalendarEntries> results = new ArrayList<CalendarEntries>();
191     	
192     	Criteria root = new Criteria();
193         
194         root.addEqualTo("hrCalendarId", hrCalendarId);
195         root.addGreaterOrEqualThan("endPeriodDateTime", beginDate);
196         root.addLessOrEqualThan("endPeriodDateTime", endDate);
197         Query query = QueryFactory.newQuery(CalendarEntries.class, root);
198         
199         results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
200 
201         return results;
202     }
203     
204     public List<CalendarEntries> getAllCalendarEntriesForCalendarId(String hrCalendarId) {
205     	Criteria root = new Criteria();
206         root.addEqualTo("hrCalendarId", hrCalendarId);
207         Query query = QueryFactory.newQuery(CalendarEntries.class, root);
208         List<CalendarEntries> ceList = new ArrayList<CalendarEntries> (this.getPersistenceBrokerTemplate().getCollectionByQuery(query));
209         return ceList;
210     }
211     
212     public List<CalendarEntries> getAllCalendarEntriesForCalendarIdUpToCutOffTime(String hrCalendarId, Date cutOffTime) {
213     	Criteria root = new Criteria();
214         root.addEqualTo("hrCalendarId", hrCalendarId);
215         root.addLessOrEqualThan("endPeriodDateTime", cutOffTime);
216         Query query = QueryFactory.newQuery(CalendarEntries.class, root);
217         List<CalendarEntries> ceList = new ArrayList<CalendarEntries> (this.getPersistenceBrokerTemplate().getCollectionByQuery(query));
218         return ceList;
219     }
220     
221     public List<CalendarEntries> getAllCalendarEntriesForCalendarIdAndYear(String hrCalendarId, String year) {        
222         Criteria crit = new Criteria();
223         List<CalendarEntries> ceList = new ArrayList<CalendarEntries>();
224         try {
225 	    	 crit.addEqualTo("hrCalendarId", hrCalendarId);
226 	    	 DateFormat df = new SimpleDateFormat("yyyy");
227 	    	 java.util.Date cYear = df.parse(year);
228 	    	 String nextYear = Integer.toString((Integer.parseInt(year) + 1));
229 	    	 java.util.Date nYear = df.parse(nextYear);
230 	    	 
231 	    	 crit.addGreaterOrEqualThan("beginPeriodDateTime", cYear);
232 	    	 crit.addLessThan("beginPeriodDateTime", nYear );
233 	    	 QueryByCriteria query = new QueryByCriteria(CalendarEntries.class, crit);
234 	    	 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
235 	    	 if (c != null) {
236 		    	ceList.addAll(c);
237 	    	 }
238 		  } catch (ParseException e) {
239 				e.printStackTrace();
240 		  }
241 		  return ceList;
242     }
243 
244 
245 }