1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.kuali.mobility.events.controllers;
15
16 import java.text.DateFormat;
17 import java.text.ParseException;
18 import java.text.SimpleDateFormat;
19 import java.util.Date;
20 import java.util.List;
21 import java.util.Locale;
22 import java.util.TimeZone;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25 import javax.servlet.http.HttpServletRequest;
26 import org.codehaus.jettison.json.JSONArray;
27 import org.kuali.mobility.events.entity.GoogleCalendarSynchronizationStatus;
28 import org.kuali.mobility.events.entity.UserAccount;
29 import org.kuali.mobility.events.service.Event;
30 import org.kuali.mobility.events.service.EventsService;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.beans.factory.annotation.Qualifier;
33 import org.springframework.context.ApplicationContext;
34 import org.springframework.context.support.ClassPathXmlApplicationContext;
35 import org.springframework.stereotype.Controller;
36 import org.springframework.ui.Model;
37 import org.springframework.validation.BindingResult;
38 import org.springframework.web.bind.annotation.ModelAttribute;
39 import org.springframework.web.bind.annotation.RequestMapping;
40 import org.springframework.web.bind.annotation.RequestMethod;
41 import org.springframework.web.bind.annotation.RequestParam;
42
43 @Controller
44 @RequestMapping("/calendar")
45 public class CalendarController {
46
47 @Autowired
48 @Qualifier("eventService")
49 private EventsService eventsService;
50
51 @RequestMapping(method = RequestMethod.GET)
52 public String home(HttpServletRequest request, Model uiModel, @RequestParam(required = false) String date) throws Exception {
53 return week(request, uiModel, date);
54 }
55
56 @RequestMapping(value = "/week", method = RequestMethod.GET)
57 public String week(HttpServletRequest request, Model uiModel, @RequestParam(required = false) String date) {
58 return "calendar/week";
59 }
60
61 @RequestMapping(value = "/addEvent", method = RequestMethod.POST)
62 public void addEvent(Model uiModel, @ModelAttribute("event") Event event, BindingResult result, HttpServletRequest request) {
63 String user = getUser();
64 String startTime = event.getStartTime();
65 String endTime = event.getEndTime();
66 String estStartTime = getESTTimeFromLocalTime(startTime);
67 String estEndTime = getESTTimeFromLocalTime(endTime);
68
69 String timeZoneId = getTimeZoneID();
70 eventsService.addEvent(estStartTime, estEndTime,
71 event.getTitle(), event.getEventBody(), estStartTime, user, timeZoneId);
72 }
73
74 @RequestMapping(value = "/updateEvent", method = RequestMethod.POST)
75 public void updateEvent(Model uiModel, @ModelAttribute("event") Event event, BindingResult result, HttpServletRequest request) {
76 String user = getUser();
77 String startTime = event.getStartTime();
78 String endTime = event.getEndTime();
79 String estStartTime = getESTTimeFromLocalTime(startTime);
80 String estEndTime = getESTTimeFromLocalTime(endTime);
81 eventsService.updateEvent(event.getEventId(), estStartTime, estEndTime,
82 event.getTitle(), event.getEventBody(), estStartTime, user);
83 }
84
85 @RequestMapping(value = "/deleteEvent", method = RequestMethod.POST)
86 public void deleteEvent(Model uiModel, @ModelAttribute("event") Event event, BindingResult result, HttpServletRequest request) {
87 eventsService.deleteEvent(event.getEventId());
88 }
89
90 @RequestMapping(value = "/getEvents", method = RequestMethod.POST)
91 public String getEvents(Model uiModel, @ModelAttribute("event") Event event, BindingResult result, HttpServletRequest request) {
92 String startDateStr = request.getParameter("startdate");
93 String endDateStr = request.getParameter("enddate");
94 List eventList = eventsService.getEvents(startDateStr, endDateStr);
95 JSONArray arr = new JSONArray(eventList);
96 uiModel.addAttribute("eventList", arr);
97 return "calendar/getevent";
98 }
99
100 @RequestMapping(value = "/synchronizeWithGoogleeAccount", method = RequestMethod.POST)
101 public String synchronizeWithGoogleeAccount(Model uiModel, @ModelAttribute("userAccount") UserAccount userAccount,
102 BindingResult result, HttpServletRequest request) {
103 ApplicationContext ctx = new ClassPathXmlApplicationContext("EventsSpringBeans.xml");
104 String synchronizationStatus = eventsService.synchronizeWithGoogleAccount(userAccount.getEmailId(), userAccount.getPassword());
105
106
107
108 String synchronizationStatusMessage = ctx.getMessage(synchronizationStatus, null, " ", Locale.ENGLISH);
109
110 uiModel.addAttribute("synchronizationStatus", synchronizationStatusMessage);
111 return "redirect:/calendar";
112 }
113
114 public String getUser() {
115 String user = "nurul";
116 return user;
117 }
118
119 public String getESTTimeFromLocalTime(String time) {
120 String strFirst = time.substring(0, 28);
121 String strLast = time.substring(28, 33);
122 String startTime = strFirst + " " + strLast;
123
124 final String OLD_FORMAT = "EEE MMM dd yyyy HH:mm:ss z Z";
125 SimpleDateFormat sdf = new SimpleDateFormat(OLD_FORMAT);
126 Date eventDate = null;
127 try {
128 eventDate = sdf.parse(startTime);
129 } catch (ParseException ex) {
130 Logger.getLogger(CalendarController.class.getName()).log(Level.SEVERE, null, ex);
131 }
132 DateFormat formatter = new SimpleDateFormat(OLD_FORMAT);
133 formatter.setTimeZone(TimeZone.getTimeZone("EST"));
134 return formatter.format(eventDate);
135 }
136
137 public String getTimeZoneID() {
138 DateFormat estDf = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);
139 estDf.setTimeZone(TimeZone.getTimeZone("EST"));
140 return estDf.getTimeZone().getID();
141 }
142 }