View Javadoc

1   /**
2    * Copyright 2011 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  
16  package org.kuali.mobility.shared.controllers;
17  
18  import org.apache.log4j.Logger;
19  import org.kuali.mobility.campus.entity.Campus;
20  import org.kuali.mobility.campus.service.CampusService;
21  import org.kuali.mobility.security.user.api.User;
22  import org.kuali.mobility.shared.Constants;
23  import org.springframework.beans.factory.annotation.Autowired;
24  import org.springframework.stereotype.Controller;
25  import org.springframework.ui.Model;
26  import org.springframework.web.bind.annotation.RequestMapping;
27  import org.springframework.web.bind.annotation.RequestMethod;
28  import org.springframework.web.bind.annotation.RequestParam;
29  
30  import javax.annotation.Resource;
31  import javax.servlet.http.Cookie;
32  import javax.servlet.http.HttpServletRequest;
33  import javax.servlet.http.HttpServletResponse;
34  import java.util.ArrayList;
35  import java.util.List;
36  import java.util.Properties;
37  
38  @Controller
39  @RequestMapping("/campus")
40  public class CampusController {
41  	
42  	private static final Logger LOG = Logger.getLogger(CampusController.class);
43  	
44  	@Autowired
45  	private CampusService campusService;
46  
47      @Resource(name="kmeProperties")
48      private Properties kmeProperties;
49  
50  	@RequestMapping(method = RequestMethod.GET)
51  	public String getList(HttpServletRequest request, Model uiModel,
52  			@RequestParam(required = true) String toolName) {
53  		List<Campus> campuses = getCampusService().findCampusesByTool(toolName);
54  		List<String> homeToolsList = null;
55  		homeToolsList = getToolsList();
56  		if (homeToolsList.contains(toolName)) {
57  			if (campuses == null || campuses.isEmpty()) {
58  				User user = (User) request.getSession().getAttribute(
59  						Constants.KME_USER_KEY);
60  				user.setViewCampus("ALL");
61  				return "redirect:/" + toolName;
62  			} else if (campuses.size() == 1) {
63  				User user = (User) request.getSession().getAttribute(
64  						Constants.KME_USER_KEY);
65  				user.setViewCampus(campuses.get(0).getCode());
66  				return "redirect:/" + toolName;
67  			}
68  			uiModel.addAttribute("campuses", campuses);
69  			uiModel.addAttribute("toolName", toolName);
70  			return "campus";
71  		} else {
72  			return "redirect:/home";
73  		}
74  	}
75  
76  	@RequestMapping(value = "/select", method = RequestMethod.GET)
77  	public String selectCampus(HttpServletRequest request,
78  			HttpServletResponse response, Model uiModel,
79  			@RequestParam(required = true) String campus,
80  			@RequestParam(required = true) String toolName) {
81  		List<String> homeToolsList = null;
82  		homeToolsList = getToolsList();
83  		if (homeToolsList.contains(toolName)) {
84  			User user = (User) request.getSession().getAttribute(
85  					Constants.KME_USER_KEY);
86  			user.setViewCampus(campus);
87  
88  			boolean useSecureCookie = Boolean.parseBoolean(getKmeProperties().getProperty("kme.secure.cookie", "false"));
89              Cookie cookie = new Cookie("campusSelection", campus);
90              cookie.setMaxAge(60 * 60 * 24 * 365); // one year
91              cookie.setPath(request.getContextPath());
92  			cookie.setSecure(useSecureCookie);
93              response.addCookie(cookie);
94  			return "redirect:/" + toolName;
95  		} else {
96  			return "redirect:/home";
97  		}
98  	}
99  
100 	public List<String> getToolsList() {
101 		List<String> toolsList = new ArrayList<String>();
102 		toolsList.add("dining");
103 		toolsList.add("maps");
104 		toolsList.add("conference");
105 		toolsList.add("events");
106 		toolsList.add("news");
107 		toolsList.add("grades");
108 		toolsList.add("computerlabs");
109 		toolsList.add("emergencycontacts");
110 		toolsList.add("weather");
111 		toolsList.add("reporting");
112 		toolsList.add("push");
113 		toolsList.add("publishing");
114 		toolsList.add("feedback");
115 		toolsList.add("calendar");
116 		return toolsList;
117 	}
118 
119 	public CampusService getCampusService() {
120 		return campusService;
121 	}
122 
123 	public void setCampusService(CampusService campusService) {
124 		this.campusService = campusService;
125 	}
126 
127 	public Properties getKmeProperties() {
128 		return kmeProperties;
129 	}
130 
131 	public void setKmeProperties(Properties kmeProperties) {
132 		this.kmeProperties = kmeProperties;
133 	}
134 }