001/** 002 * Copyright 2011 The Kuali Foundation Licensed under the 003 * Educational Community License, Version 2.0 (the "License"); you may 004 * not use this file except in compliance with the License. You may 005 * obtain a copy of the License at 006 * 007 * http://www.osedu.org/licenses/ECL-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, 010 * software distributed under the License is distributed on an "AS IS" 011 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 012 * or implied. See the License for the specific language governing 013 * permissions and limitations under the License. 014 */ 015 016package org.kuali.mobility.feedback.controllers; 017 018import java.sql.Timestamp; 019import java.util.LinkedHashMap; 020import java.util.Map; 021 022import javax.servlet.http.HttpServletRequest; 023 024import org.kuali.mobility.feedback.entity.Feedback; 025import org.kuali.mobility.feedback.service.FeedbackService; 026import org.kuali.mobility.security.user.api.User; 027import org.kuali.mobility.shared.Constants; 028import org.springframework.beans.factory.annotation.Autowired; 029import org.springframework.stereotype.Controller; 030import org.springframework.ui.Model; 031import org.springframework.validation.BindingResult; 032import org.springframework.validation.Errors; 033import org.springframework.web.bind.annotation.ModelAttribute; 034import org.springframework.web.bind.annotation.RequestMapping; 035import org.springframework.web.bind.annotation.RequestMethod; 036 037@Controller 038@RequestMapping("/feedback") 039public class FeedbackController{ 040 041 @Autowired 042 private FeedbackService feedbackService; 043 044 private static final Map<String, String> deviceTypes; 045 046 static { 047 deviceTypes = new LinkedHashMap<String, String>(); 048 deviceTypes.put("", "feedback.default"); 049 deviceTypes.put("computer", "feedback.computer"); 050 deviceTypes.put("an", "feedback.android"); 051 deviceTypes.put("bb", "feedback.blackberry"); 052 deviceTypes.put("ip", "feedback.iphone"); 053 deviceTypes.put("ie", "feedback.windowsMobile"); 054 deviceTypes.put("other", "feedback.other"); 055 } 056 057 @RequestMapping(method = RequestMethod.GET) 058 public String getList(Model uiModel, HttpServletRequest request) { 059 User user = (User) request.getSession().getAttribute(Constants.KME_USER_KEY); 060 uiModel.addAttribute("feedback", new Feedback()); 061 uiModel.addAttribute("deviceTypes", deviceTypes); 062 Feedback feedback = new Feedback(); 063 if (user != null && user.getEmail() != null) { 064 feedback.setEmail(user.getEmail()); 065 } 066 uiModel.addAttribute("feedback", feedback); 067 return "feedback/form"; 068 } 069 070 @RequestMapping(method = RequestMethod.POST) 071 public String submitFeedback(Model uiModel, @ModelAttribute("feedback") Feedback feedback, BindingResult result) { 072 feedback.setPostedTimestamp(new Timestamp(System.currentTimeMillis())); 073 if (isValidFeedback(feedback, result)) { 074 feedbackService.saveFeedback(feedback); 075 return "feedback/thanks"; 076 } else { 077 uiModel.addAttribute("deviceTypes", deviceTypes); 078 return "feedback/form"; 079 } 080 } 081 082 private boolean isValidFeedback(Feedback f, BindingResult result) { 083 boolean hasErrors = false; 084 Errors errors = ((Errors) result); 085 if (f.getNoteText() == null || "".equals(f.getNoteText().trim())) { 086 errors.rejectValue("noteText", "", "Please type some feedback into the input box."); 087 hasErrors = true; 088 } else if (f.getNoteText().length() > 2000) { 089 errors.rejectValue("noteText", "", "Error: Feedback must be less than 2000 characters."); 090 hasErrors = true; 091 } 092 if (f.getDeviceType() == null || f.getDeviceType().equals("")) { 093 errors.rejectValue("deviceType", "", "Please select a device type."); 094 hasErrors = true; 095 } else if (deviceTypes.get(f.getDeviceType()) == null) { 096 errors.rejectValue("deviceType", "", "Error: Please select a valid device type."); 097 hasErrors = true; 098 } 099 return !hasErrors; 100 } 101 102}