1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.kuali.mobility.events.service;
16
17 import flexjson.JSONSerializer;
18 import org.apache.commons.collections.CollectionUtils;
19 import org.apache.commons.lang.time.DateUtils;
20 import org.kuali.mobility.events.dao.EventsDao;
21 import org.kuali.mobility.events.entity.Category;
22 import org.kuali.mobility.events.entity.CategoryImpl;
23 import org.kuali.mobility.events.entity.Event;
24 import org.kuali.mobility.events.entity.EventImpl;
25 import org.kuali.mobility.events.util.*;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.springframework.stereotype.Service;
29
30 import javax.annotation.Resource;
31 import javax.jws.WebService;
32 import javax.ws.rs.GET;
33 import javax.ws.rs.Path;
34 import javax.ws.rs.PathParam;
35 import javax.ws.rs.QueryParam;
36 import java.text.Format;
37 import java.text.ParseException;
38 import java.text.SimpleDateFormat;
39 import java.util.*;
40
41 @Service
42 @WebService(endpointInterface = "org.kuali.mobility.events.service.EventsService")
43 public class EventsServiceImpl implements EventsService {
44
45 private static final Logger LOG = LoggerFactory.getLogger(EventsServiceImpl.class);
46
47 private static String[] DATE_PATTERNS = {
48 "YYYY-MM-DD",
49 "YYYY-MMM-DD",
50 "DD-MM-YYYY",
51 "DD-MMM-YYYY",
52 "DD MMMM YYYY",
53 "MMMM DD, YYYY",
54 "DD/MM/YYYY",
55 "YYYYMMDD"
56 };
57
58 @Resource(name="eventDao")
59 private EventsDao dao;
60
61
62
63 @GET
64 @Path("/findEvent")
65 @Override
66 public EventImpl getEvent(@QueryParam(value = "campus") String campus, @QueryParam(value = "categoryId") String categoryId,
67 @QueryParam(value = "eventId") String eventId) {
68 Event event;
69
70 getDao().initData(campus, categoryId);
71
72 List<Event> events = (List<Event>) CollectionUtils.select(getDao().getEvents(), new EventPredicate(campus, categoryId, eventId));
73
74 if (events == null || events.isEmpty()) {
75 LOG.info("No events matched the criteria.");
76 event = null;
77 } else if (events.size() > 1) {
78 LOG.info("Multiple events found that match the criteria.");
79 event = events.get(0);
80 } else {
81 event = events.get(0);
82 }
83
84 return (EventImpl)(new EventTransform().transform(event));
85 }
86
87 @GET
88 @Path("/byCategory/{categoryId}")
89 @Override
90 public List<EventImpl> getAllEvents(@QueryParam(value = "campus") String campus,@PathParam(value = "categoryId") String categoryId) {
91 getDao().initData(campus, categoryId);
92 List<Event> events = (List<Event>) CollectionUtils.select(getDao().getEvents(), new EventPredicate(campus, categoryId, null));
93 Collections.sort(events, new EventComparator());
94
95 List<EventImpl> eventList = new ArrayList<EventImpl>();
96 CollectionUtils.collect(events, new EventTransform(),eventList);
97
98 return eventList;
99 }
100
101 @GET
102 @Path("/categories")
103 @Override
104 public List<CategoryImpl> getCategoriesByCampus(@QueryParam(value = "campus") String campus) {
105 List<Category> categories = new ArrayList<Category>();
106
107 LOG.debug( "Loading categories for campus ["+campus+"]" );
108 getDao().initData(campus);
109
110 categories = (List<Category>) CollectionUtils.select(getDao().getCategories(), new CategoryPredicate(campus, null));
111
112 LOG.debug( "Number of categories for campus ["+campus+"] = "+categories.size() );
113
114 List<CategoryImpl> categoryObjs = new ArrayList<CategoryImpl>();
115 for(int i = 0; i < categories.size(); i++) {
116 Collection events = CollectionUtils.select(getDao().getEvents(), new EventPredicate(campus, categories.get(i).getCategoryId(), null));
117 if (events.size() > 0) {
118 categories.get(i).setHasEvents(true);
119 } else {
120 categories.get(i).setHasEvents(false);
121 }
122 }
123 CollectionUtils.collect(categories, new CategoryTransform(), categoryObjs);
124
125 return categoryObjs;
126 }
127
128 @GET
129 @Path("/findByDateRange")
130 public List<EventImpl> getEventsByDateRange(
131 @QueryParam(value="campus") String campus,
132 @QueryParam(value="fromDate") String fromDate,
133 @QueryParam(value="toDate") String toDate,
134 @QueryParam(value="categoryId") String categoryId
135 ) {
136 Date startDate,endDate;
137 List<EventImpl> events = new ArrayList<EventImpl>();
138 try {
139 startDate = DateUtils.parseDateStrictly(fromDate, DATE_PATTERNS);
140 endDate = DateUtils.parseDateStrictly(toDate,DATE_PATTERNS);
141 } catch( ParseException pe ) {
142 LOG.error(pe.getLocalizedMessage(),pe);
143 startDate = new Date();
144 endDate = new Date();
145 }
146 if( null == categoryId || categoryId.isEmpty() ) {
147 events = getEventsForDateRange(fromDate,toDate);
148 } else {
149 events = getAllEventsByDateFromTo(campus, categoryId, startDate, endDate);
150 }
151 return events;
152 }
153
154 @GET
155 @Deprecated
156 @Path("/getEventsForDateRange")
157 public List<EventImpl> getEventsForDateRange(@QueryParam(value = "fromDate") String fromDateStr, @QueryParam(value = "toDate") String toDateStr) {
158 List<EventImpl> filteredEvents = new ArrayList<EventImpl>();
159 List<EventImpl> eventList = new ArrayList<EventImpl>();
160 List<EventImpl> allEvents = new ArrayList<EventImpl>();
161 List<CategoryImpl> categories = new ArrayList<CategoryImpl>();
162 String campus = "ALL";
163 categories = getCategoriesByCampus(campus);
164
165 Date fromDate = null;
166 Date toDate = null;
167 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
168 try {
169 fromDate = sdf.parse(fromDateStr);
170 toDate = sdf.parse(toDateStr);
171 } catch (ParseException e) {
172 e.printStackTrace();
173 }
174
175 for(Category category : categories) {
176 eventList = getAllEvents(campus, category.getCategoryId());
177 if(!eventList.isEmpty() && eventList != null) {
178 allEvents.addAll(eventList);
179 }
180 }
181 boolean hasEndDate = true;
182 Iterator it = allEvents.iterator();
183 while(it.hasNext()) {
184 EventImpl obj = (EventImpl) it.next();
185 if(obj.getEndDate() != null) {
186 hasEndDate = !(obj.getEndDate().after(toDate));
187 }
188 if (!obj.getStartDate().before(fromDate) && hasEndDate) {
189 filteredEvents.add(obj);
190 }
191 hasEndDate = false;
192 }
193 Collections.sort(filteredEvents, new Comparator<EventImpl>() {
194 public int compare(EventImpl e1, EventImpl e2) {
195 return e1.getStartDate().compareTo(e2.getStartDate());
196 }
197 });
198 return filteredEvents;
199 }
200
201
202 public List<EventImpl> getAllEvents() {
203 List<EventImpl> allEvents = new ArrayList<EventImpl>();
204 List<EventImpl> eventList = new ArrayList<EventImpl>();
205 List<CategoryImpl> categories = new ArrayList<CategoryImpl>();
206 String campus = "ALL";
207 categories = getCategoriesByCampus(campus);
208 for(CategoryImpl category : categories) {
209 eventList = getAllEvents(campus, category.getCategoryId());
210 allEvents.addAll(eventList);
211 }
212 return allEvents;
213 }
214
215
216 @GET
217 @Path("/onDate")
218 public List<EventImpl> getEventsForCurrentDate(@QueryParam(value = "date") String date) {
219 List<EventImpl> filteredEvents = new ArrayList<EventImpl>();
220 List<EventImpl> allEvents = getAllEvents();
221 String campus = "ALL";
222 Iterator it = allEvents.iterator();
223 while(it.hasNext()) {
224 EventImpl obj = (EventImpl) it.next();
225 Date eventDate = obj.getStartDate();
226 Format formatter = new SimpleDateFormat("yyyy-MM-dd");
227 String eventDateStr = formatter.format(eventDate);
228 if(eventDateStr.equals(date)) {
229 filteredEvents.add(obj);
230 }
231 }
232 return filteredEvents;
233 }
234
235
236 @GET
237 @Path("/nextDay")
238 public String getNextDay(@QueryParam(value = "currentDate") String currentDate) {
239 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
240 Date eventDate = null;
241 Date nextDay = null;
242 try {
243 eventDate = sdf.parse(currentDate);
244 nextDay = addDays(eventDate, 1);
245 } catch (ParseException e) {
246 e.printStackTrace();
247 }
248 String nextDayStr = sdf.format(nextDay);
249 return nextDayStr;
250 }
251
252
253 @GET
254 @Path("/previousDay")
255 public String getPreviousDay(@QueryParam(value = "currentDate") String currentDate) {
256 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
257 Date eventDate = null;
258 Date previousDay = null;
259 try {
260 eventDate = sdf.parse(currentDate);
261 previousDay = addDays(eventDate, -1);
262 } catch (ParseException e) {
263 e.printStackTrace();
264 }
265 String previousDayStr = sdf.format(previousDay);
266 return previousDayStr;
267 }
268
269
270 public Date addDays(Date date, int days) {
271 Calendar cal = Calendar.getInstance();
272 cal.setTime(date);
273 cal.add(Calendar.DATE, days);
274 return cal.getTime();
275 }
276
277
278 @GET
279 @Path("/category/{categoryId}")
280 @Override
281 public CategoryImpl getCategory(@QueryParam(value = "campus") String campus, @PathParam(value = "categoryId") String categoryId) {
282
283 if (getDao().getCategories() == null || getDao().getCategories().isEmpty()) {
284 getDao().initData(campus);
285 }
286
287 Category category = (Category) CollectionUtils.find(getDao().getCategories(), new CategoryPredicate(campus, categoryId));
288
289 return (CategoryImpl)(new CategoryTransform().transform(category));
290 }
291
292
293
294
295 public EventsDao getDao() {
296 return dao;
297 }
298
299
300
301
302 public void setDao(EventsDao dao) {
303 this.dao = dao;
304 }
305
306 public String getEventJson(final String eventId) {
307 return new JSONSerializer().exclude("*.class").deepSerialize(this.getEvent(null, null, eventId));
308 }
309
310
311
312 @GET
313 @Deprecated
314 @Path("/current/{campus}/{categoryId}")
315 @Override
316 public List<EventImpl> getAllEventsByDateCurrent(@QueryParam(value = "campus") String campus, @QueryParam(value = "categoryId") String categoryId,
317 @QueryParam (value = "mon-dd-yy") Date dateCurrent) {
318
319 List<EventImpl> events = getAllEvents(campus, categoryId);
320 List<Event> resultEvents = new ArrayList<Event>();
321 Iterator it = events.iterator();
322 while (it.hasNext()) {
323 Event obj = (Event) it.next();
324 Date obtainedDate = obj.getStartDate();
325 if (obtainedDate.equals(dateCurrent)) {
326 resultEvents.add(obj);
327 }
328
329 }
330
331 List<EventImpl> resultEventObjs = new ArrayList<EventImpl>();
332 CollectionUtils.collect(resultEvents,new EventTransform(),resultEventObjs);
333
334 return resultEventObjs;
335 }
336
337 @GET
338 @Path("/between/{campus}/{categoryId}")
339 @Deprecated
340 @Override
341 public List<EventImpl> getAllEventsByDateFromTo(@QueryParam(value = "campus") String campus, @QueryParam(value = "categoryId") String categoryId,
342 @QueryParam (value = "from-mon-dd-yy") Date from, @QueryParam (value = "to-mon-dd-yy") Date to) {
343
344 List<EventImpl> events = getAllEvents(campus, categoryId);
345 List<Event> resultEvents = new ArrayList<Event>();
346 Iterator it = events.iterator();
347 while (it.hasNext()) {
348 Event obj = (Event) it.next();
349 if (!obj.getStartDate().before(from) || !obj.getEndDate().after(to)) {
350 resultEvents.add(obj);
351 }
352
353 }
354
355 List<EventImpl> resultEventObjs = new ArrayList<EventImpl>();
356 CollectionUtils.collect(resultEvents,new EventTransform(),resultEventObjs);
357
358 return resultEventObjs;
359
360 }
361
362 @GET
363 @Path("/fordate/{campus}/{categoryId}")
364 @Deprecated
365 @Override
366 public List<EventImpl> getAllEventsByDateSpecific(@QueryParam(value = "campus") String campus, @QueryParam(value = "categoryId") String categoryId,
367 @QueryParam (value = "yyyy-mm-dd") String specific) {
368 List<? extends Event> events = getAllEvents(campus, categoryId);
369 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
370 List<Event> resultEvents = new ArrayList<Event>();
371 try {
372 for( Event e : events ) {
373 String startDate = sdf.format(e.getStartDate());
374 if( specific.equalsIgnoreCase( startDate ) ) {
375 LOG.debug( "Found match for event date ["+startDate+"] to ["+specific+"]." );
376 resultEvents.add(e);
377 }
378 }
379 } catch (Exception ex) {
380 LOG.info("Failure from getAllEventsByDateSpecific method");
381 ex.printStackTrace();
382 }
383 LOG.debug( "Number of events matching date: "+resultEvents.size() );
384 List<EventImpl> resultEventObjs = new ArrayList<EventImpl>();
385 CollectionUtils.collect(resultEvents,new EventTransform(),resultEventObjs);
386 LOG.debug( "Number of events after transform: "+resultEventObjs.size() );
387 return resultEventObjs;
388 }
389
390 }