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 org.apache.log4j.Logger;
19  import org.kuali.mobility.admin.entity.HomeScreen;
20  import org.kuali.mobility.admin.entity.HomeTool;
21  import org.kuali.mobility.admin.entity.Tool;
22  import org.kuali.mobility.admin.service.AdminService;
23  import org.kuali.mobility.feedback.entity.Feedback;
24  import org.kuali.mobility.feedback.service.FeedbackService;
25  import org.kuali.mobility.security.authz.entity.AclExpression;
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  import org.springframework.web.servlet.LocaleResolver;
37  
38  import javax.annotation.Resource;
39  import javax.servlet.http.Cookie;
40  import javax.servlet.http.HttpServletRequest;
41  import java.sql.Timestamp;
42  import java.util.*;
43  
44  @Controller 
45  @RequestMapping("/feedback")
46  public class FeedbackController{
47  
48      private static final Logger LOG = Logger.getLogger(FeedbackController.class);
49  
50      @Autowired
51      private FeedbackService feedbackService;
52  
53      @Autowired
54      private AdminService adminService;
55      /**
56       */
57      public void setFeedbackService(FeedbackService feedbackService) {
58          this.feedbackService = feedbackService;
59      }
60      
61      @Resource(name="kmeProperties")
62      private Properties kmeProperties;
63  
64      /**
65       * A reference to the Spring Locale Resolver
66       */
67      @Resource(name="localeResolver")
68      private LocaleResolver localeResolver;
69  
70      private static final Map<String, String> deviceTypes;
71      
72      static {
73      	deviceTypes = new LinkedHashMap<String, String>();
74      	deviceTypes.put("feedback.default", "feedback.default");
75      	deviceTypes.put("feedback.computer", "feedback.computer");
76      	deviceTypes.put("feedback.android", "feedback.android");
77      	deviceTypes.put("feedback.blackberry", "feedback.blackberry");
78      	deviceTypes.put("feedback.ipad", "feedback.ipad");
79          deviceTypes.put("feedback.iphone", "feedback.iphone");
80          deviceTypes.put("feedback.ipod", "feedback.ipod");
81      	deviceTypes.put("feedback.windowsMobile", "feedback.windowsMobile");
82      	deviceTypes.put("feedback.other", "feedback.other");
83      }
84  
85      @RequestMapping(value = "/js/feedback.js")
86      public String getJavaScript(Model uiModel) {
87          return "ui3/feedback/js/feedback";
88      }
89  	
90      @RequestMapping(method = RequestMethod.GET)
91      public String getList(Model uiModel, HttpServletRequest request) {
92      	User user = (User) request.getSession().getAttribute(Constants.KME_USER_KEY);
93  
94     		uiModel.addAttribute("deviceTypes", deviceTypes);
95     		Feedback feedback = new Feedback();
96      	if ( user != null && user.getEmail()!=null) { //pre populate email
97              feedback.setEmail(user.getEmail());
98      	}
99     		uiModel.addAttribute("feedback", feedback);
100 
101         uiModel.addAttribute("tools", getHomeToolList(request));
102 
103    		if( "3".equalsIgnoreCase( getKmeProperties().getProperty("kme.uiVersion","classic") ) ) {
104             return "ui3/feedback/index";
105     	}
106 		
107     	return "feedback/form";
108     }
109     
110     @RequestMapping(method = RequestMethod.POST)
111     public String submitFeedback(Model uiModel, @ModelAttribute("feedback") Feedback feedback, BindingResult result, HttpServletRequest request) {
112     	//feedback.setPostedTimestamp(new Timestamp(System.currentTimeMillis()));
113         Locale locale = this.localeResolver.resolveLocale(request);
114 
115         User user = (User) request.getSession().getAttribute(Constants.KME_USER_KEY);
116         if ( user != null  ) {
117             if (user.getLoginName()!=null)
118                 feedback.setUserId(user.getLoginName());
119             if (feedback.getCampus()==null || feedback.getCampus().trim().isEmpty())
120                 feedback.setCampus(user.getViewCampus());
121         }
122 
123         if (isValidFeedback(feedback, result)) {
124             feedbackService.saveFeedback(feedback, locale);
125 
126        		if( "3".equalsIgnoreCase( getKmeProperties().getProperty("kme.uiVersion","classic") ) ) {
127                 return "ui3/feedback/index";
128         	}
129             
130             return "feedback/thanks";                	
131         } else {
132         	uiModel.addAttribute("deviceTypes", deviceTypes);
133         	
134        		if( "3".equalsIgnoreCase( getKmeProperties().getProperty("kme.uiVersion","classic") ) ) {
135                 return "ui3/feedback/index";
136         	}
137             
138         	return "feedback/form";    	
139         }
140     }
141     
142     private boolean isValidFeedback(Feedback f, BindingResult result) {
143     	boolean hasErrors = false;
144     	Errors errors = ((Errors) result);
145     	if (f.getNoteText() == null || "".equals(f.getNoteText().trim())) {
146     		errors.rejectValue("noteText", "", "Please type some feedback into the input box.");
147     		hasErrors = true;
148     	} else if (f.getNoteText().length() > 2000) {
149     		errors.rejectValue("noteText", "", "Error: Feedback must be less than 2000 characters.");
150     		hasErrors = true;    		
151     	}
152     	if (f.getDeviceType() == null || f.getDeviceType().equals("")) {
153      		errors.rejectValue("deviceType", "", "Please select a device type.");
154      		hasErrors = true;    		
155      	} else if (deviceTypes.get(f.getDeviceType()) == null) {
156      		errors.rejectValue("deviceType", "", "Error: Please select a valid device type.");
157      		hasErrors = true;    		
158      	}
159     	return !hasErrors;
160     }
161 
162     private List<HomeTool>  getHomeToolList(HttpServletRequest request) {
163         List<HomeTool> userTools = new ArrayList<HomeTool>();
164 
165         int myState = 0;
166         Cookie cks[] = request.getCookies();
167         if (cks != null) {
168             for (Cookie c : cks) {
169                 if (c.getName().equals("native")) {
170                     if ("yes".equals(c.getValue())) {
171                         myState |= Tool.NATIVE;
172                     } else {
173                         myState |= Tool.NON_NATIVE;
174                     }
175                 }
176                 if (c.getName().equals("platform")) {
177                     if ("iOS".equals(c.getValue())) {
178                         myState |= Tool.IOS;
179                     } else if ("Android".equals(c.getValue())) {
180                         myState |= Tool.ANDROID;
181                     } else if ("WindowsMobile".equals(c.getValue())) {
182                         myState |= Tool.WINDOWS_PHONE;
183                     } else if ("Blackberry".equals(c.getValue())) {
184                         myState |= Tool.BLACKBERRY;
185                     }
186                 }
187             }
188         }
189 
190         User user = (User) request.getSession().getAttribute(Constants.KME_USER_KEY);
191 
192         String alias = "PUBLIC";
193         if (request.getParameter("alias") != null) {
194             alias = request.getParameter("alias").toUpperCase();
195         }
196         if (user != null && user.getViewCampus() != null) {
197             alias = user.getViewCampus();
198         }
199 
200         HomeScreen home = getAdminService().getHomeScreenByAlias(alias);
201         if (home == null) {
202             LOG.debug("Home screen was null, getting PUBLIC screen.");
203             home = getAdminService().getHomeScreenByAlias("PUBLIC");
204         }
205 
206         List<HomeTool> copy;
207         if (home == null) {
208             LOG.error("Home screen object still null when it should not be.");
209             copy = new ArrayList<HomeTool>();
210         } else {
211             copy = new ArrayList<HomeTool>(home.getHomeTools());
212         }
213         Collections.sort(copy);
214 
215         for (HomeTool homeTool : copy) {
216             Tool tool = homeTool.getTool();
217 
218             AclExpression viewingPermission = tool.getViewingPermission();
219             if (viewingPermission != null && viewingPermission.getParsedExpression() != null && !viewingPermission.getParsedExpression().evaluate(user)) {
220                 continue;
221             }
222             // If a tools requisites is unset it will be treated as available to any, same as Tool.ANY.
223             if ((tool.getRequisites() & myState) == myState || (tool.getRequisites() == Tool.UNDEFINED_REQUISITES)) {
224                 userTools.add(homeTool);
225             } else {
226                 // LOG.info("--- HIDE TOOL: " + tool.getAlias());
227             }
228 
229         }
230 
231         return userTools;
232     }
233     
234     public Properties getKmeProperties() {
235         return kmeProperties;
236     }
237 
238     public void setKmeProperties(Properties kmeProperties) {
239         this.kmeProperties = kmeProperties;
240     }
241 
242     public AdminService getAdminService() {
243         return adminService;
244     }
245 
246     public void setAdminService(AdminService adminService) {
247         this.adminService = adminService;
248     }
249 
250 }