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