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 getNextCalendarEntriesByCalendarId(String hrCalendarId, CalendarEntries calendarEntries) { 091 Criteria root = new Criteria(); 092 Criteria beginDate = new Criteria(); 093 Criteria endDate = new Criteria(); 094 095 beginDate.addEqualToField("hrCalendarId", Criteria.PARENT_QUERY_PREFIX + "hrCalendarId"); 096 beginDate.addGreaterThan("beginPeriodDateTime", calendarEntries.getBeginPeriodDateTime()); 097 ReportQueryByCriteria beginDateSubQuery = QueryFactory.newReportQuery(CalendarEntries.class, beginDate); 098 beginDateSubQuery.setAttributes(new String[]{"min(beginPeriodDateTime)"}); 099 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 }