001    /**
002     * Copyright 2004-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.hr.time.calendar.dao;
017    
018    import java.text.DateFormat;
019    import java.text.ParseException;
020    import java.text.SimpleDateFormat;
021    import java.util.ArrayList;
022    import java.util.Collection;
023    import java.util.Date;
024    import java.util.List;
025    
026    import org.apache.ojb.broker.query.Criteria;
027    import org.apache.ojb.broker.query.Query;
028    import org.apache.ojb.broker.query.QueryByCriteria;
029    import org.apache.ojb.broker.query.QueryFactory;
030    import org.apache.ojb.broker.query.ReportQueryByCriteria;
031    import org.joda.time.DateTime;
032    import org.kuali.hr.time.calendar.CalendarEntries;
033    import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
034    
035    public class CalendarEntriesDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements CalendarEntriesDao {
036    
037    
038        public void saveOrUpdate(CalendarEntries calendarEntries) {
039            this.getPersistenceBrokerTemplate().store(calendarEntries);
040        }
041    
042        public CalendarEntries getCalendarEntries(String hrCalendarEntriesId) {
043            Criteria currentRecordCriteria = new Criteria();
044            currentRecordCriteria.addEqualTo("hrCalendarEntriesId", hrCalendarEntriesId);
045    
046            return (CalendarEntries) this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(CalendarEntries.class, currentRecordCriteria));
047        }
048    
049        @Override
050        public CalendarEntries getCalendarEntriesByIdAndPeriodEndDate(String hrCalendarId, Date endPeriodDate) {
051            Criteria root = new Criteria();
052            root.addEqualTo("hrCalendarId", hrCalendarId);
053            root.addEqualTo("endPeriodDateTime", endPeriodDate);
054    
055            Query query = QueryFactory.newQuery(CalendarEntries.class, root);
056            CalendarEntries pce = (CalendarEntries) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
057            return pce;
058        }
059    
060        @Override
061        public CalendarEntries getCurrentCalendarEntriesByCalendarId(
062                String hrCalendarId, Date currentDate) {
063            Criteria root = new Criteria();
064    //              Criteria beginDate = new Criteria();
065    //              Criteria endDate = new Criteria();
066    
067    //              beginDate.addEqualToField("hrPyCalendarId", Criteria.PARENT_QUERY_PREFIX + "hrPyCalendarId");
068    //              beginDate.addLessOrEqualThan("beginPeriodDateTime", currentDate);
069    //              ReportQueryByCriteria beginDateSubQuery = QueryFactory.newReportQuery(CalendarEntries.class, beginDate);
070    //              beginDateSubQuery.setAttributes(new String[] { "max(beginPeriodDateTime)" });
071    
072    //              endDate.addEqualToField("hrPyCalendarId", Criteria.PARENT_QUERY_PREFIX + "hrPyCalendarId");
073    //              endDate.addGreaterOrEqualThan("endPeriodDateTime", currentDate);
074    //              ReportQueryByCriteria endDateSubQuery = QueryFactory.newReportQuery(CalendarEntries.class, endDate);
075    //              endDateSubQuery.setAttributes(new String[] { "min(endPeriodDateTime)" });
076    
077            root.addEqualTo("hrCalendarId", hrCalendarId);
078            //root.addEqualTo("beginPeriodDateTime", beginDateSubQuery);
079            root.addLessOrEqualThan("beginPeriodDateTime", currentDate);
080            root.addGreaterThan("endPeriodDateTime", currentDate);
081    //              root.addEqualTo("endPeriodDateTime", endDateSubQuery);
082    
083            Query query = QueryFactory.newQuery(CalendarEntries.class, root);
084    
085            CalendarEntries pce = (CalendarEntries) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
086            return pce;
087        }
088    
089        @Override
090        public CalendarEntries getCalendarEntriesByCalendarIdAndDateRange(
091                String hrCalendarId, Date beginDate, Date endDate) {
092            Criteria root = new Criteria();
093            root.addEqualTo("hrCalendarId", hrCalendarId);
094            //root.addEqualTo("beginPeriodDateTime", beginDateSubQuery);
095            root.addLessOrEqualThan("beginPeriodDateTime", endDate);
096            root.addGreaterThan("endPeriodDateTime", beginDate);
097    //              root.addEqualTo("endPeriodDateTime", endDateSubQuery);
098    
099            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    }