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