1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.kuali.mobility.bus.controllers;
16
17 import net.sf.json.JSONArray;
18 import net.sf.json.JSONObject;
19 import net.sf.json.JSONSerializer;
20 import org.apache.commons.lang.StringUtils;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23 import org.kuali.mobility.bus.entity.Bus;
24 import org.kuali.mobility.bus.entity.BusAlert;
25 import org.kuali.mobility.bus.entity.BusRoute;
26 import org.kuali.mobility.bus.entity.BusStop;
27 import org.kuali.mobility.bus.service.BusService;
28 import org.kuali.mobility.bus.util.BusConstants;
29 import org.kuali.mobility.security.user.api.User;
30 import org.kuali.mobility.shared.Constants;
31 import org.springframework.context.ApplicationContext;
32 import org.springframework.context.ApplicationContextAware;
33 import org.springframework.stereotype.Controller;
34 import org.springframework.ui.Model;
35 import org.springframework.web.bind.annotation.*;
36
37 import javax.annotation.Resource;
38 import javax.servlet.http.HttpServletRequest;
39 import javax.servlet.http.HttpSession;
40 import java.util.*;
41
42
43
44
45
46 @Controller
47 @RequestMapping("/bus")
48 public class BusController implements ApplicationContextAware {
49
50 private static Logger LOG = LoggerFactory.getLogger(BusController.class);
51 @Resource(name="busService")
52 private BusService service;
53 @Resource(name="busProperties")
54 private Properties busProperties;
55 @Resource(name="kmeProperties")
56 private Properties kmeProperties;
57
58 private ApplicationContext applicationContext;
59
60 private static final Map<String,String> URL_MAP;
61 static {
62 Map<String, String> aMap = new HashMap<String,String>();
63 aMap.put(BusConstants.MAP.toString(), "/viewBusTracking");
64 aMap.put(BusConstants.STOPS.toString(), "/viewStops");
65 aMap.put(BusConstants.ROUTES.toString(), "");
66 URL_MAP = Collections.unmodifiableMap(aMap);
67 }
68
69 @RequestMapping(method = RequestMethod.GET)
70 public String index(HttpServletRequest request, Model uiModel) {
71 User user = (User) request.getSession().getAttribute(Constants.KME_USER_KEY);
72 String campus = null;
73 String viewName = null;
74 if (user.getViewCampus() == null) {
75
76 viewName = "redirect:/campus?toolName=bus";
77 } else {
78 campus = user.getViewCampus();
79
80
81
82
83
84 uiModel.addAttribute("campus", campus);
85
86
87
88 HttpSession mySession = request.getSession();
89 mySession.setAttribute(BusConstants.URL_MAP.toString(), URL_MAP);
90 mySession.setAttribute(BusConstants.TAB_ORDER.toString(), getTabOrder());
91
92 uiModel.addAttribute("currentTime", System.currentTimeMillis());
93 uiModel.addAttribute("routeId", null);
94 uiModel.addAttribute("stopId", null);
95 Properties kmeProperties = (Properties)getApplicationContext().getBean("kmeProperties");
96 uiModel.addAttribute("initialLatitude",kmeProperties.getProperty("maps.center.lat", "0.0"));
97 uiModel.addAttribute("initialLongitude",kmeProperties.getProperty("maps.center.lon", "0.0"));
98 if( "3".equalsIgnoreCase( getKmeProperties().getProperty("kme.uiVersion","classic") ) ) {
99 viewName = "ui3/bus/index";
100 } else {
101 viewName = "bus/index";
102 }
103 }
104 return viewName;
105 }
106
107 @RequestMapping(value="/templates/{key}")
108 public String getAngularTemplates(
109 @PathVariable("key") String key,
110 HttpServletRequest request,
111 Model uiModel ) {
112 uiModel.addAttribute("currentTime", System.currentTimeMillis());
113 uiModel.addAttribute("initialLatitude", getKmeProperties().getProperty("maps.center.lat", "0.0"));
114 uiModel.addAttribute("initialLongitude", getKmeProperties().getProperty("maps.center.lon", "0.0"));
115 uiModel.addAttribute("stopId", null);
116 uiModel.addAttribute("routeId", null);
117 uiModel.addAttribute("tabOrder",getBusProperties().getProperty("bus.taborder","map,nearbystops,favorites,routes").split(","));
118 return "ui3/bus/templates/"+key;
119 }
120
121 @RequestMapping(value = "/js/{key}.js")
122 public String getJavaScript(
123 @PathVariable("key") String key,
124 Model uiModel,
125 HttpServletRequest request) {
126 String selectedCampus = null;
127 User user = (User) request.getSession().getAttribute(Constants.KME_USER_KEY);
128 selectedCampus = user.getViewCampus();
129 uiModel.addAttribute("campus",selectedCampus);
130 uiModel.addAttribute("tabOrder", getBusProperties().getProperty("bus.taborder","routes,favorites,nearbystops,map"));
131 uiModel.addAttribute("distanceUnits", getBusProperties().getProperty("bus.distance.units","imperial"));
132 uiModel.addAttribute("favoriteDisabled", getBusProperties().getProperty("bus.favorite.color.disabled","white"));
133 uiModel.addAttribute("favoriteEnabled", getBusProperties().getProperty("bus.favorite.color.enabled","yellow"));
134 uiModel.addAttribute("stopRefresh", getBusProperties().getProperty("bus.stop.refresh","10"));
135 uiModel.addAttribute("initialLatitude", getKmeProperties().getProperty("maps.center.lat", "0.0"));
136 uiModel.addAttribute("initialLongitude", getKmeProperties().getProperty("maps.center.lon", "0.0"));
137 uiModel.addAttribute("initialZoom", getKmeProperties().getProperty("maps.google.default.zoom", "8"));
138
139 uiModel.addAttribute("defaultTab",getBusProperties().getProperty("bus.taborder","map,nearbystops,favorites,routes").split(",")[0]);
140
141 return "ui3/bus/js/"+key;
142 }
143
144 @RequestMapping(value = "/viewStops")
145 public String viewStops(HttpServletRequest request, Model uiModel) {
146 HttpSession mySession = request.getSession();
147 mySession.setAttribute(BusConstants.URL_MAP.toString(), URL_MAP);
148 mySession.setAttribute(BusConstants.TAB_ORDER.toString(), getTabOrder());
149 User user = (User) request.getSession().getAttribute(Constants.KME_USER_KEY);
150 String campus = null;
151 if (user.getViewCampus() == null) {
152
153 return "redirect:/campus?toolName=bus";
154 } else {
155 campus = user.getViewCampus();
156 }
157 LOG.debug("viewStops:" + campus);
158 List<? extends BusRoute> routes = getService().getRoutes(campus);
159 JSONArray jsonRoutes = (JSONArray) JSONSerializer.toJSON(routes);
160 uiModel.addAttribute("routes", jsonRoutes);
161 List<? extends BusStop> stops = getService().getStops(campus);
162 JSONArray jsonStops = (JSONArray) JSONSerializer.toJSON(stops);
163 LOG.debug("Found " + stops.size() + " bus stops via local service for campus " + campus);
164 uiModel.addAttribute("stops", jsonStops);
165 uiModel.addAttribute("campus", campus);
166 return "bus/viewStops";
167 }
168
169 @RequestMapping(value = "/viewBus")
170 public String viewBus(HttpServletRequest request, Model uiModel, @RequestParam(required = true) String routeId, @RequestParam(required = true) String busId, @RequestParam(required = false) String campus) {
171 HttpSession mySession = request.getSession();
172 mySession.setAttribute(BusConstants.URL_MAP.toString(), URL_MAP);
173 mySession.setAttribute(BusConstants.TAB_ORDER.toString(), getTabOrder());
174 BusRoute route = getService().getRoute(campus, Long.parseLong(routeId));
175 JSONObject jsonRoutes = (JSONObject) JSONSerializer.toJSON(route);
176 uiModel.addAttribute("busId", busId);
177 uiModel.addAttribute("route", jsonRoutes);
178 return "bus/viewBus";
179 }
180
181 @RequestMapping(value = "/viewNearByStops")
182 public String viewNearByStops(HttpServletRequest request, Model uiModel, @RequestParam(required = false) String latitude, @RequestParam(required = false) String longitude, @RequestParam(required = false) String radius) {
183
184 User user = (User) request.getSession().getAttribute(Constants.KME_USER_KEY);
185 String campus = null;
186 if (user.getViewCampus() == null) {
187
188 return "redirect:/campus?toolName=bus";
189 } else {
190 campus = user.getViewCampus();
191 }
192 if ((latitude != null) && (longitude != null) && (radius != null)) {
193 LOG.debug("latitude: " + latitude);
194 LOG.debug("longitude: " + longitude);
195 LOG.debug("radius: " + radius);
196 List<? extends BusStop> stopsnear = getService().getNearbyStops(Double.parseDouble(latitude), Double.parseDouble(longitude), Double.parseDouble(radius));
197
198 if (stopsnear != null) {
199 LOG.debug("Found nearby stops " + stopsnear.size());
200 } else {
201 LOG.debug("Unable to find stop.");
202 }
203
204 uiModel.addAttribute("stopsnear", stopsnear);
205 uiModel.addAttribute("campus", campus);
206 }
207
208 return "bus/viewNearByStops";
209 }
210
211 @RequestMapping(value = "/viewRoute", method = RequestMethod.GET)
212 public String viewRoute(HttpServletRequest request, Model uiModel, @RequestParam(required = true) String routeId, @RequestParam(required = false) String campus) {
213 BusRoute route = getService().getRoute(campus, Long.parseLong(routeId));
214
215 uiModel.addAttribute("route", route);
216 uiModel.addAttribute("campus", campus);
217
218 return "bus/viewRoute";
219 }
220
221 @RequestMapping(value = "/favoriteStops")
222 public String viewFavoriteStops(HttpServletRequest request, Model uiModel) {
223 User user = (User) request.getSession().getAttribute(Constants.KME_USER_KEY);
224 String campus = null;
225 if (user.getViewCampus() == null) {
226
227 return "redirect:/campus?toolName=bus";
228 } else {
229 campus = user.getViewCampus();
230 }
231 uiModel.addAttribute("campus", campus);
232 return "bus/favoriteStops";
233 }
234
235 @SuppressWarnings("unchecked")
236 @RequestMapping(value = "/viewStop")
237 public String viewStop(HttpServletRequest request, Model uiModel, @RequestParam(required = true) String routeId, @RequestParam(required = true) String stopId, @RequestParam(required = false) String campus) {
238 HttpSession mySession = request.getSession();
239 mySession.setAttribute(BusConstants.URL_MAP.toString(), URL_MAP);
240 mySession.setAttribute(BusConstants.TAB_ORDER.toString(), getTabOrder());
241 BusStop stop = null;
242
243 stop = getService().getStop(campus, Long.parseLong(stopId));
244 List<BusRoute> routes = (List<BusRoute>) getService().getRoutes(campus);
245 uiModel.addAttribute("routes", routes);
246 uiModel.addAttribute("routeId", routeId);
247 uiModel.addAttribute("stop", stop);
248 uiModel.addAttribute("campus", campus);
249
250 return "bus/viewStop";
251 }
252
253 @RequestMapping(value = "/viewRouteTracking", method = RequestMethod.GET)
254 public String viewRouteTracking(HttpServletRequest request, Model uiModel) {
255 User user = (User) request.getSession().getAttribute(Constants.KME_USER_KEY);
256 String campus = null;
257 if (user.getViewCampus() == null) {
258
259 return "redirect:/campus?toolName=bus";
260 } else {
261 campus = user.getViewCampus();
262 }
263 List<? extends BusRoute> routes = getService().getRoutes(campus);
264 LOG.debug("Found " + routes.size() + " bus routes via local service for campus " + campus);
265 uiModel.addAttribute("routes", routes);
266 uiModel.addAttribute("campus", campus);
267 return "bus/viewRouteTracking";
268 }
269
270 @RequestMapping(value = "/viewBusTracking", method = RequestMethod.GET)
271 public String viewBusTracking(HttpServletRequest request, Model uiModel, @RequestParam(required = false) String routeId, @RequestParam(required = false) String stopId) {
272 User user = (User) request.getSession().getAttribute(Constants.KME_USER_KEY);
273 String campus = null;
274 if (user.getViewCampus() == null) {
275
276 return "redirect:/campus?toolName=bus";
277 } else {
278 campus = user.getViewCampus();
279 }
280 uiModel.addAttribute("campus", campus);
281 Properties kmeProperties = (Properties)getApplicationContext().getBean("kmeProperties");
282
283 uiModel.addAttribute("currentTime", System.currentTimeMillis());
284 uiModel.addAttribute("initialLatitude",kmeProperties.getProperty("maps.center.lat", "0.0"));
285 uiModel.addAttribute("initialLongitude",kmeProperties.getProperty("maps.center.lon", "0.0"));
286 if (StringUtils.isNotBlank(stopId)) {
287 uiModel.addAttribute("stopId", stopId);
288 } else {
289 uiModel.addAttribute("stopId", null);
290 }
291 if (StringUtils.isNotBlank(routeId)) {
292 uiModel.addAttribute("routeId", routeId);
293 } else {
294 uiModel.addAttribute("routeId", null);
295 }
296 return "bus/viewBusTracking";
297 }
298
299 @Deprecated
300 @RequestMapping(value = "/busLocations", method = RequestMethod.GET)
301 @ResponseBody
302 public String busLocations(Model uiModel) {
303 List<Bus> buses = this.getService().getDao().getBuses();
304 JSONArray jsonBuses;
305 if (buses != null) {
306 jsonBuses = (JSONArray) JSONSerializer.toJSON(buses);
307 } else {
308 return null;
309 }
310 return jsonBuses.toString();
311 }
312
313 @Deprecated
314 @RequestMapping(value = "/busRoutes", method = RequestMethod.GET)
315 @ResponseBody
316 public String busRoutes(Model uiModel) {
317 List<BusRoute> routes = this.getService().getDao().getBusRoutes();
318 JSONArray jsonRoutes;
319 if (routes != null) {
320 jsonRoutes = (JSONArray) JSONSerializer.toJSON(routes);
321 } else {
322 return null;
323 }
324 return jsonRoutes.toString();
325 }
326
327 @Deprecated
328 @RequestMapping(value = "/busAlerts", method = RequestMethod.GET)
329 @ResponseBody
330 public String busAlerts(Model uiModel) {
331 List<BusAlert> alerts = this.getService().getDao().getBusAlerts();
332 JSONArray jsonAlerts;
333 if (alerts != null) {
334 jsonAlerts = (JSONArray) JSONSerializer.toJSON(alerts);
335 } else {
336 return null;
337 }
338 return jsonAlerts.toString();
339 }
340
341 @Deprecated
342 @RequestMapping(value = "/routesByDistance", method = RequestMethod.GET)
343 @ResponseBody
344 public String routesByDistance(Model uiModel, @RequestParam(required = true) String latitude, @RequestParam(required = true) String longitude, @RequestParam(required = true) String radius) {
345 LOG.debug("latitude: " + latitude);
346 LOG.debug("longitude: " + longitude);
347 LOG.debug("radius: " + radius);
348 List<? extends BusRoute> distanceRoutes = getService().getRoutesWithDistance(Double.parseDouble(latitude), Double.parseDouble(longitude), Double.parseDouble(radius));
349
350
351
352 JSONArray jsonStops = (JSONArray) JSONSerializer.toJSON(distanceRoutes);
353 return jsonStops.toString();
354 }
355
356 @Deprecated
357 @SuppressWarnings("unchecked")
358 @RequestMapping(value = "/busETAs/{stopId}", method = RequestMethod.GET)
359 @ResponseBody
360 public String busstop(@PathVariable("stopId") int stopId, Model uiModel) {
361 List<BusStop> busStops = (List<BusStop>) this.getService().getStops("");
362 BusStop foundStop = null;
363 for (BusStop stop : busStops) {
364 if (stopId == stop.getId()) {
365 foundStop = stop;
366 break;
367 }
368 }
369 if (foundStop != null) {
370 JSONObject jsonStop = (JSONObject) JSONSerializer.toJSON(foundStop);
371 return jsonStop.toString();
372 }
373 return null;
374 }
375
376 private List<String> getTabOrder() {
377 List<String> tabOrder = new ArrayList<String>();
378 for( String s : getBusProperties().getProperty("bus." + BusConstants.TAB_ORDER).split(",") ) {
379 if( "true".equalsIgnoreCase( getBusProperties().getProperty("bus." + s + ".enabled", "false"))) {
380 tabOrder.add(s);
381 }
382 }
383 return tabOrder;
384 }
385
386
387
388
389 public BusService getService() {
390 return service;
391 }
392
393
394
395
396 public void setService(BusService service) {
397 this.service = service;
398 }
399
400
401
402
403 public Properties getBusProperties() {
404 return busProperties;
405 }
406
407
408
409
410 public void setBusProperties(Properties busProperties) {
411 this.busProperties = busProperties;
412 }
413
414 public ApplicationContext getApplicationContext() {
415 return applicationContext;
416 }
417
418 public void setApplicationContext(ApplicationContext applicationContext) {
419 this.applicationContext = applicationContext;
420 }
421
422 public Properties getKmeProperties() {
423 return kmeProperties;
424 }
425
426 public void setKmeProperties(Properties kmeProperties) {
427 this.kmeProperties = kmeProperties;
428 }
429 }