View Javadoc
1   /**
2    * Copyright 2011-2013 The Kuali Foundation Licensed under the Educational
3    * Community License, Version 2.0 (the "License"); you may not use this file
4    * except in compliance with the License. You may obtain a copy of the License
5    * at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12   * License for the specific language governing permissions and limitations under
13   * the License.
14   */
15  package org.kuali.mobility.events.controllers;
16  
17  import org.kuali.mobility.events.entity.Category;
18  import org.kuali.mobility.events.entity.CategoryImpl;
19  import org.kuali.mobility.events.entity.Event;
20  import org.kuali.mobility.events.entity.EventImpl;
21  import org.kuali.mobility.events.service.EventsService;
22  import org.kuali.mobility.events.util.CategoryComparator;
23  import org.kuali.mobility.security.user.api.User;
24  import org.kuali.mobility.shared.Constants;
25  import org.springframework.stereotype.Controller;
26  import org.springframework.ui.Model;
27  import org.springframework.web.bind.annotation.*;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  import javax.annotation.Resource;
31  import javax.servlet.http.HttpServletRequest;
32  import java.text.ParseException;
33  import java.text.SimpleDateFormat;
34  import java.util.*;
35  
36  /**
37   * @author Kuali Mobility Team (mobility.collab@kuali.org)
38   */
39  @Controller
40  @RequestMapping("/events")
41  public class EventsController {
42  
43  	private static final Logger LOG = LoggerFactory.getLogger(EventsController.class);
44  	@Resource(name="eventService")
45  	private EventsService eventsService;
46  
47      @Resource(name="kmeProperties")
48      private Properties kmeProperties;
49  
50  	@Resource(name="eventsProperties")
51  	private Properties eventsProperties;
52  
53      private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
54  
55  	@RequestMapping(method = RequestMethod.GET)
56  	public String homePage(HttpServletRequest request, Model uiModel, @RequestParam(required = false) String date, @RequestParam(required=false) String direction ) throws ParseException {
57  		User user = (User) request.getSession().getAttribute(Constants.KME_USER_KEY);
58  		String viewName = null;
59  		if (user.getViewCampus() == null) {
60  			viewName = "redirect:/campus?toolName=events";
61  		}
62          else if( "3".equalsIgnoreCase( getKmeProperties().getProperty("kme.uiVersion","classic") ) ) {
63              viewName = "ui3/events/index";
64          }
65          else {
66  			String campus = null;
67  			campus = user.getViewCampus();
68  
69  			List<Category> categories = (List<Category>) getEventsService().getCategoriesByCampus(campus);
70  			LOG.debug("Found " + categories.size() + " categories via local service for campus " + campus);
71  			uiModel.addAttribute("categories", categories);
72  			uiModel.addAttribute("campus", campus);
73  
74  			//code for making the new home page of events
75  			HashMap<Category, List<EventImpl>> mObj = new HashMap<Category, List<EventImpl>>();
76  
77  			if (null == date || date.isEmpty()) {
78  				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
79  				Date d = new Date();
80  				date = sdf.format(new Date());
81  			} else {
82  				int MILLIS_IN_DAY = 1000 * 60 * 60 * 24;
83  				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
84  				Date tempDate = sdf.parse(date);
85  
86  				String nextDate;
87  				if( "previous".equalsIgnoreCase(direction) ) {
88  					nextDate = sdf.format(tempDate.getTime() - MILLIS_IN_DAY);
89  				} else if( "next".equalsIgnoreCase(direction) ) {
90  					nextDate = sdf.format(tempDate.getTime() + MILLIS_IN_DAY);
91  				} else {
92  					nextDate = date;
93  				}
94  				date = nextDate;
95  			}
96  
97  			uiModel.addAttribute("displayDate", date);
98  
99  			for (Category c : categories) {
100 				List<EventImpl> eventList = (List<EventImpl>) getEventsService().getAllEventsByDateSpecific(campus, c.getCategoryId(), date);
101 
102 				if( eventList != null && !eventList.isEmpty() ) {
103 					mObj.put(c, eventList);
104 				}
105 
106 				LOG.debug("Found " + eventList.size() + " events for category " + c.getTitle());
107 			}
108 			uiModel.addAttribute("categoryMap", mObj);
109 
110 			List<Category> categoriesWithEvents = new ArrayList<Category>();
111 			categoriesWithEvents.addAll( mObj.keySet() );
112 
113 			Collections.sort( categoriesWithEvents, new CategoryComparator() );
114 
115 			uiModel.addAttribute("categoryList", categoriesWithEvents );
116 
117 			uiModel.addAttribute("showCategoryTab", getEventsProperties().getProperty("events.showCategoryTab", "true"));
118 			uiModel.addAttribute("showDateRangeTab", getEventsProperties().getProperty("events.showDateRangeTab", "true"));
119                         if ("true".equalsIgnoreCase(getEventsProperties().getProperty("events.enableHistoricalDate", "false"))) {
120                             uiModel.addAttribute("showPreviousDate", "true");
121                         }
122                         else {
123                             // check if selected date is after Today
124                             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
125                             Date selectedDate = sdf.parse(date);
126                             if ( selectedDate.before(new Date()) ) {
127                                 LOG.debug(selectedDate + " is before Today");
128                                 uiModel.addAttribute("showPreviousDate", "false");
129                             }
130                             else {
131                                 LOG.debug(selectedDate + " is after Today");
132                                 uiModel.addAttribute("showPreviousDate", "true");
133                             }
134                         }
135                         
136 			viewName = "events/index";
137 		}
138 		return viewName;
139 	}
140 
141 	@RequestMapping(value = "/byCategory", method = RequestMethod.GET)
142 	public String homeByCategory(HttpServletRequest request, Model uiModel) throws ParseException {
143 		User user = (User) request.getSession().getAttribute(Constants.KME_USER_KEY);
144 		String campus = null;
145 		if (user.getViewCampus() == null) {
146 			return "redirect:/campus?toolName=events";
147 		} else {
148 			campus = user.getViewCampus();
149 		}
150 		List<? extends Category> categories = getEventsService().getCategoriesByCampus(campus);
151 		uiModel.addAttribute("categories", categories);
152 		uiModel.addAttribute("campus", campus);
153 
154 		uiModel.addAttribute("showCategoryTab", getEventsProperties().getProperty("events.showCategoryTab", "true"));
155 		uiModel.addAttribute("showDateRangeTab", getEventsProperties().getProperty("events.showDateRangeTab", "true"));
156 
157 		return "events/category";
158 	}
159 
160 	@RequestMapping(value = "/byDateRange", method = RequestMethod.GET)
161 	public String homeByDateRange(HttpServletRequest request, Model uiModel) throws ParseException {
162 		uiModel.addAttribute("showCategoryTab", getEventsProperties().getProperty("events.showCategoryTab", "true"));
163 		uiModel.addAttribute("showDateRangeTab", getEventsProperties().getProperty("events.showDateRangeTab", "true"));
164 
165 		return "events/dateRange";
166 	}
167 
168     @RequestMapping(value = "/eventstForDateRange", method = RequestMethod.GET)
169     public String eventsListForDateRange(HttpServletRequest request, Model uiModel, @RequestParam(required = true) String from, @RequestParam(required = true) String to) throws ParseException {
170         String campus = "ALL";
171         List<EventImpl> eventList = null;
172         Category category = null;
173         SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
174         Date fromDate = null;
175         Date toDate = null;
176         try {
177             fromDate = sdf.parse(from);
178             toDate = sdf.parse(to);
179         } catch (ParseException e) {
180             e.printStackTrace();
181         }
182         if(toDate.compareTo(fromDate)>=0) {
183                 eventList = getEventsService().getEventsForDateRange(from, to);
184 
185                 uiModel.addAttribute("events", eventList);
186 
187                 if (eventList != null && eventList.size() > 0) {
188                     category = eventList.get(0).getCategory();
189                 } else {
190                     category = getEventsService().getCategory(campus, category.getCategoryId());
191                 }
192                 if (category == null) {
193                     LOG.error("Couldn't find category for categoryId - " + category.getCategoryId());
194                     category = new CategoryImpl();
195                     category.setCategoryId(category.getCategoryId());
196                     category.setTitle(category.getCategoryId());
197                 }
198             uiModel.addAttribute("errorMsg", "");
199         }
200         else {
201             uiModel.addAttribute("errorMsg", getEventsProperties().get("events.errorMsg"));
202         }
203         uiModel.addAttribute("category", category);
204         uiModel.addAttribute("campus", "ALL");
205         return "events/eventsForDateRange";
206     }
207 
208 	@RequestMapping(value = "/viewEvents", method = RequestMethod.GET)
209 	public String viewEvents(HttpServletRequest request, Model uiModel, @RequestParam(required = true) String categoryId, @RequestParam(required = false) String campus) throws Exception {
210         String filteredCategoryId = (String)request.getSession().getAttribute("categoryId");
211         String filteredCampus = (String)request.getSession().getAttribute("campus");
212         List<Event> eventList = (List<Event>) getEventsService().getAllEvents(filteredCampus, filteredCategoryId);
213 		uiModel.addAttribute("events", eventList);
214 		Category category;
215 		if (eventList != null && eventList.size() > 0) {
216 			category = eventList.get(0).getCategory();
217 		} else {
218 			category = getEventsService().getCategory(campus, filteredCategoryId);
219 		}
220 		if (category == null) {
221 			LOG.error("Couldn't find category for categoryId - " + filteredCategoryId);
222 			category = new CategoryImpl();
223 			category.setCategoryId(filteredCategoryId);
224 			category.setTitle(filteredCategoryId);
225 		}
226 		uiModel.addAttribute("category", category);
227 		uiModel.addAttribute("campus", campus);
228 		return "events/eventsList";
229 	}
230 
231 	@RequestMapping(value = "/viewEvent", method = RequestMethod.GET)
232 	public String viewEvent(HttpServletRequest request, Model uiModel, @RequestParam(required = true) String categoryId, @RequestParam(required = false) String campus, @RequestParam(required = true) String eventId) throws Exception {
233 		//Event event = eventsService.getEvent(campus, categoryId, eventId);
234 		//uiModel.addAttribute("event", event);
235         String filteredCategoryId = (String)request.getSession().getAttribute("categoryId");
236         String filteredCampus = (String)request.getSession().getAttribute("campus");
237         String filteredEventId = (String)request.getSession().getAttribute("eventId");
238 		uiModel.addAttribute("categoryId", filteredCategoryId);
239 		uiModel.addAttribute("campus", filteredCampus);
240         uiModel.addAttribute("event", filteredEventId);
241 		return "events/detail";
242 	}
243 
244 	@RequestMapping(value = "/viewEvent", method = RequestMethod.GET, headers = "Accept=application/json")
245 	@ResponseBody
246 	public String getEventJson(@RequestParam(required = true) String eventId) {
247 		return getEventsService().getEventJson(eventId);
248 	}
249 
250 	@RequestMapping(value = "/viewEventsByDateFromTo", method = RequestMethod.GET)
251 	public String viewEventsByDateFromTo(HttpServletRequest request, Model uiModel, @RequestParam(required = true) String dateFrom, @RequestParam(required = true) String dateTo) throws Exception {
252 
253 		SortedMap<String, HashMap<Category, List<Event>>> mObjFT = new TreeMap<String, HashMap<Category, List<Event>>>();
254 		List<String> listOfDates = new ArrayList<String>();
255 		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
256 		Date dateF = sdf.parse(dateFrom);
257 		Date dateT = sdf.parse(dateTo);
258 		//checking if the start date is smaller than the end date
259 		if (dateF.before(dateT)) {
260 			//getting the array list of dates from the range
261 			listOfDates = listOfDates(dateFrom, dateTo);
262 			Iterator ite = listOfDates.iterator();
263 			while (ite.hasNext()) {
264 				String fetchedDate = (String) ite.next();
265 				System.out.println(fetchedDate);
266 				//storing into map, each date as key and "list of events grouped by category" as value pair
267 //                mObjFT.put(fetchedDate, supportViewEventsDateSpecific(fetchedDate, categories));
268 			}
269 			uiModel.addAttribute("eventDateFromTo", mObjFT);
270 		}
271 
272 		return "events/eventsListDateRange";
273 	}
274 
275 	public List<String> listOfDates(String from, String to) throws Exception {
276 		List<String> lst = new ArrayList<String>();
277 		int MILLIS_IN_DAY = 1000 * 60 * 60 * 24;
278 		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
279 
280 		Date dateF = sdf.parse(from);
281 		Date dateT = sdf.parse(to);
282 
283 		while (dateF.equals(dateT) || dateF.before(dateT)) {
284 			lst.add(from);
285 			from = sdf.format(dateF.getTime() + MILLIS_IN_DAY);
286 			dateF = sdf.parse(from);
287 		}
288 
289 		return lst;
290 	}
291 
292     @RequestMapping(value="/templates/{key}")
293     public String getAngularTemplates(
294             @PathVariable("key") String key,
295             HttpServletRequest request,
296             Model uiModel ) {
297         uiModel.addAttribute("showCategoryTab",getEventsProperties().getProperty("events.showCategoryTab","true"));
298         uiModel.addAttribute("showDateRangeTab",getEventsProperties().getProperty("events.showDateRangeTab","true"));
299         return "ui3/events/templates/"+key;
300     }
301 
302     @RequestMapping(value = "/js/events.js")
303     public String getJavaScript(Model uiModel, HttpServletRequest request) {
304         uiModel.addAttribute("today",DATE_FORMAT.format(GregorianCalendar.getInstance().getTime()));
305         return "ui3/events/js/events";
306     }
307 
308 
309     public Properties getKmeProperties() {
310         return kmeProperties;
311     }
312 
313     public void setKmeProperties(Properties kmeProperties) {
314         this.kmeProperties = kmeProperties;
315     }
316 
317     public EventsService getEventsService() {
318         return eventsService;
319     }
320 
321     public void setEventsService(EventsService eventsService) {
322         this.eventsService = eventsService;
323     }
324 
325     public Properties getEventsProperties() {
326         return eventsProperties;
327     }
328 
329     public void setEventsProperties(Properties eventsProperties) {
330         this.eventsProperties = eventsProperties;
331     }
332 }