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.feedback.controllers;
17  
18  import java.sql.Timestamp;
19  import java.util.LinkedHashMap;
20  import java.util.Map;
21  
22  import javax.servlet.http.HttpServletRequest;
23  
24  import org.kuali.mobility.feedback.entity.Feedback;
25  import org.kuali.mobility.feedback.service.FeedbackService;
26  import org.kuali.mobility.security.user.api.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.validation.BindingResult;
32  import org.springframework.validation.Errors;
33  import org.springframework.web.bind.annotation.ModelAttribute;
34  import org.springframework.web.bind.annotation.RequestMapping;
35  import org.springframework.web.bind.annotation.RequestMethod;
36  
37  @Controller 
38  @RequestMapping("/feedback")
39  public class FeedbackController{
40      
41      @Autowired
42      private FeedbackService feedbackService;
43      
44      private static final Map<String, String> deviceTypes;
45      
46      static {
47      	deviceTypes = new LinkedHashMap<String, String>();
48      	deviceTypes.put("", "feedback.default");
49      	deviceTypes.put("computer", "feedback.computer");
50      	deviceTypes.put("an", "feedback.android");
51      	deviceTypes.put("bb", "feedback.blackberry");
52      	deviceTypes.put("ip", "feedback.iphone");
53      	deviceTypes.put("ie", "feedback.windowsMobile");
54      	deviceTypes.put("other", "feedback.other");
55      }
56  
57      @RequestMapping(method = RequestMethod.GET)
58      public String getList(Model uiModel, HttpServletRequest request) {
59      	User user = (User) request.getSession().getAttribute(Constants.KME_USER_KEY);
60     		uiModel.addAttribute("feedback", new Feedback());
61     		uiModel.addAttribute("deviceTypes", deviceTypes);
62     		Feedback feedback = new Feedback();
63      	if (user != null && user.getEmail() != null) {
64      		feedback.setEmail(user.getEmail());
65      	}
66     		uiModel.addAttribute("feedback", feedback);
67      	return "feedback/form";
68      }
69      
70      @RequestMapping(method = RequestMethod.POST)
71      public String submitFeedback(Model uiModel, @ModelAttribute("feedback") Feedback feedback, BindingResult result) {
72      	feedback.setPostedTimestamp(new Timestamp(System.currentTimeMillis()));
73          if (isValidFeedback(feedback, result)) {
74              feedbackService.saveFeedback(feedback);
75              return "feedback/thanks";                	
76          } else {
77          	uiModel.addAttribute("deviceTypes", deviceTypes);
78          	return "feedback/form";    	
79          }
80      }
81      
82      private boolean isValidFeedback(Feedback f, BindingResult result) {
83      	boolean hasErrors = false;
84      	Errors errors = ((Errors) result);
85      	if (f.getNoteText() == null || "".equals(f.getNoteText().trim())) {
86      		errors.rejectValue("noteText", "", "Please type some feedback into the input box.");
87      		hasErrors = true;
88      	} else if (f.getNoteText().length() > 2000) {
89      		errors.rejectValue("noteText", "", "Error: Feedback must be less than 2000 characters.");
90      		hasErrors = true;    		
91      	}
92      	if (f.getDeviceType() == null || f.getDeviceType().equals("")) {
93       		errors.rejectValue("deviceType", "", "Please select a device type.");
94       		hasErrors = true;    		
95       	} else if (deviceTypes.get(f.getDeviceType()) == null) {
96       		errors.rejectValue("deviceType", "", "Error: Please select a valid device type.");
97       		hasErrors = true;    		
98       	}
99      	return !hasErrors;
100     }
101     
102 }