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 java.util.List;
19  
20  import javax.servlet.http.Cookie;
21  import javax.servlet.http.HttpServletRequest;
22  import javax.servlet.http.HttpServletResponse;
23  
24  import org.kuali.mobility.campus.entity.Campus;
25  import org.kuali.mobility.campus.service.CampusService;
26  import org.kuali.mobility.security.authn.entity.User;
27  import org.kuali.mobility.shared.Constants;
28  import org.springframework.beans.factory.annotation.Autowired;
29  import org.springframework.stereotype.Controller;
30  import org.springframework.ui.Model;
31  import org.springframework.web.bind.annotation.RequestMapping;
32  import org.springframework.web.bind.annotation.RequestMethod;
33  import org.springframework.web.bind.annotation.RequestParam;
34  
35  @Controller
36  @RequestMapping("/campus")
37  public class CampusController {
38  
39  	@Autowired
40  	private CampusService campusService;
41  
42  	@RequestMapping(method = RequestMethod.GET)
43  	public String getList(HttpServletRequest request, Model uiModel, @RequestParam(required = true) String toolName) {
44  		List<Campus> campuses = campusService.findCampusesByTool(toolName);
45  		if (campuses == null || campuses.isEmpty()) {
46  			User user = (User) request.getSession().getAttribute(Constants.KME_USER_KEY);
47  			user.setViewCampus("ALL");
48  			return "redirect:/" + toolName;
49  		} else if (campuses.size() == 1) {
50  			User user = (User) request.getSession().getAttribute(Constants.KME_USER_KEY);
51  			user.setViewCampus(campuses.get(0).getCode());
52  			return "redirect:/" + toolName;
53  		}
54  		uiModel.addAttribute("campuses", campuses);
55  		uiModel.addAttribute("toolName", toolName);
56  		return "campus";
57  	}
58  
59  	@RequestMapping(value = "/select", method = RequestMethod.GET)
60  	public String selectCampus(HttpServletRequest request, HttpServletResponse response, Model uiModel, @RequestParam(required = true) String campus, @RequestParam(required = true) String toolName) {
61  		User user = (User) request.getSession().getAttribute(Constants.KME_USER_KEY);
62  		user.setViewCampus(campus);
63  		
64  		Cookie cookie = new Cookie("campusSelection", campus);
65  		cookie.setMaxAge(60*60*24*365); //one year
66  		cookie.setPath(request.getContextPath());
67  		response.addCookie(cookie);
68  		
69  		return "redirect:/" + toolName;
70  	}
71  
72  }