1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
65
66
67
68
69
70
71
72
73
74
75
76
77 root.addEqualTo("hrCalendarId", hrCalendarId);
78
79 root.addLessOrEqualThan("beginPeriodDateTime", currentDate);
80 root.addGreaterThan("endPeriodDateTime", currentDate);
81
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 }