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.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 getNextCalendarEntriesByCalendarId(String hrCalendarId, CalendarEntries calendarEntries) {
91          Criteria root = new Criteria();
92          Criteria beginDate = new Criteria();
93          Criteria endDate = new Criteria();
94  
95          beginDate.addEqualToField("hrCalendarId", Criteria.PARENT_QUERY_PREFIX + "hrCalendarId");
96          beginDate.addGreaterThan("beginPeriodDateTime", calendarEntries.getBeginPeriodDateTime());
97          ReportQueryByCriteria beginDateSubQuery = QueryFactory.newReportQuery(CalendarEntries.class, beginDate);
98          beginDateSubQuery.setAttributes(new String[]{"min(beginPeriodDateTime)"});
99  
100         endDate.addEqualToField("hrCalendarId", Criteria.PARENT_QUERY_PREFIX + "hrCalendarId");
101         endDate.addGreaterThan("endPeriodDateTime", calendarEntries.getEndPeriodDateTime());
102         ReportQueryByCriteria endDateSubQuery = QueryFactory.newReportQuery(CalendarEntries.class, endDate);
103         endDateSubQuery.setAttributes(new String[]{"min(endPeriodDateTime)"});
104 
105         root.addEqualTo("hrCalendarId", hrCalendarId);
106         root.addEqualTo("beginPeriodDateTime", beginDateSubQuery);
107         root.addEqualTo("endPeriodDateTime", endDateSubQuery);
108 
109         Query query = QueryFactory.newQuery(CalendarEntries.class, root);
110 
111         CalendarEntries pce = (CalendarEntries) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
112         return pce;
113     }
114 
115     @Override
116     public CalendarEntries getPreviousCalendarEntriesByCalendarId(String hrCalendarId, CalendarEntries calendarEntries) {
117         Criteria root = new Criteria();
118         Criteria beginDate = new Criteria();
119         Criteria endDate = new Criteria();
120 
121         beginDate.addEqualToField("hrCalendarId", Criteria.PARENT_QUERY_PREFIX + "hrCalendarId");
122         beginDate.addLessThan("beginPeriodDateTime", calendarEntries.getBeginPeriodDateTime());
123         ReportQueryByCriteria beginDateSubQuery = QueryFactory.newReportQuery(CalendarEntries.class, beginDate);
124         beginDateSubQuery.setAttributes(new String[]{"max(beginPeriodDateTime)"});
125 
126         endDate.addEqualToField("hrCalendarId", Criteria.PARENT_QUERY_PREFIX + "hrCalendarId");
127         endDate.addLessThan("endPeriodDateTime", calendarEntries.getEndPeriodDateTime());
128         ReportQueryByCriteria endDateSubQuery = QueryFactory.newReportQuery(CalendarEntries.class, endDate);
129         endDateSubQuery.setAttributes(new String[]{"max(endPeriodDateTime)"});
130 
131         root.addEqualTo("hrCalendarId", hrCalendarId);
132         root.addEqualTo("beginPeriodDateTime", beginDateSubQuery);
133         root.addEqualTo("endPeriodDateTime", endDateSubQuery);
134 
135         Query query = QueryFactory.newQuery(CalendarEntries.class, root);
136 
137         CalendarEntries pce = (CalendarEntries) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
138         return pce;
139     }
140 
141     public List<CalendarEntries> getCurrentCalendarEntryNeedsScheduled(int thresholdDays, Date asOfDate) {
142         DateTime current = new DateTime(asOfDate.getTime());
143         DateTime windowStart = current.minusDays(thresholdDays);
144         DateTime windowEnd = current.plusDays(thresholdDays);
145 
146         Criteria root = new Criteria();
147 
148         root.addGreaterOrEqualThan("beginPeriodDateTime", windowStart.toDate());
149         root.addLessOrEqualThan("beginPeriodDateTime", windowEnd.toDate());
150 
151         Query query = QueryFactory.newQuery(CalendarEntries.class, root);
152         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
153 
154         List<CalendarEntries> pce = new ArrayList<CalendarEntries>(c.size());
155         pce.addAll(c);
156 
157         return pce;
158     }
159 
160     public List<CalendarEntries> getFutureCalendarEntries(String hrCalendarId, Date currentDate, int numberOfEntries) {
161         Criteria root = new Criteria();
162         root.addEqualTo("hrCalendarId", hrCalendarId);
163         root.addGreaterOrEqualThan("beginPeriodDateTime", currentDate);
164         QueryByCriteria q = QueryFactory.newReportQuery(CalendarEntries.class, root);
165         q.addOrderByAscending("beginPeriodDateTime");
166         q.setStartAtIndex(1);
167         q.setEndAtIndex(numberOfEntries);
168         List<CalendarEntries> calendarEntries = new ArrayList<CalendarEntries>(this.getPersistenceBrokerTemplate().getCollectionByQuery(q));
169         return calendarEntries;
170     }
171 
172     public CalendarEntries getCalendarEntriesByBeginAndEndDate(Date beginPeriodDate, Date endPeriodDate) {
173         Criteria root = new Criteria();
174         root.addEqualTo("beginPeriodDateTime", beginPeriodDate);
175         root.addEqualTo("endPeriodDateTime", endPeriodDate);
176         Query query = QueryFactory.newQuery(CalendarEntries.class, root);
177 
178         return (CalendarEntries) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
179     }
180     
181     public List<CalendarEntries> getAllCalendarEntriesForCalendarId(String hrCalendarId) {
182     	Criteria root = new Criteria();
183         root.addEqualTo("hrCalendarId", hrCalendarId);
184         Query query = QueryFactory.newQuery(CalendarEntries.class, root);
185         List<CalendarEntries> ceList = new ArrayList<CalendarEntries> (this.getPersistenceBrokerTemplate().getCollectionByQuery(query));
186         return ceList;
187     }
188     
189     public List<CalendarEntries> getAllCalendarEntriesForCalendarIdAndYear(String hrCalendarId, String year) {        
190         Criteria crit = new Criteria();
191         List<CalendarEntries> ceList = new ArrayList<CalendarEntries>();
192         try {
193 	    	 crit.addEqualTo("hrCalendarId", hrCalendarId);
194 	    	 DateFormat df = new SimpleDateFormat("yyyy");
195 	    	 java.util.Date cYear = df.parse(year);
196 	    	 String nextYear = Integer.toString((Integer.parseInt(year) + 1));
197 	    	 java.util.Date nYear = df.parse(nextYear);
198 	    	 
199 	    	 crit.addGreaterOrEqualThan("beginPeriodDateTime", cYear);
200 	    	 crit.addLessThan("beginPeriodDateTime", nYear );
201 	    	 QueryByCriteria query = new QueryByCriteria(CalendarEntries.class, crit);
202 	    	 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
203 	    	 if (c != null) {
204 		    	ceList.addAll(c);
205 	    	 }
206 		  } catch (ParseException e) {
207 				e.printStackTrace();
208 		  }
209 		  return ceList;
210     }
211 
212 
213 }