Advertisement
Guest User

Untitled

a guest
Jan 17th, 2013
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.31 KB | None | 0 0
  1. /**
  2. * The contents of this file are subject to the OpenMRS Public License
  3. * Version 1.0 (the "License"); you may not use this file except in
  4. * compliance with the License. You may obtain a copy of the License at
  5. *
  6. * Software distributed under the License is distributed on an "AS IS"
  7. * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
  8. * License for the specific language governing rights and limitations
  9. * under the License.
  10. *
  11. * Copyright (C) OpenMRS, LLC. All Rights Reserved.
  12. */
  13. package org.openmrs.validator;
  14.  
  15. import org.junit.Assert;
  16. import org.junit.Test;
  17. import org.openmrs.notification.Alert;
  18. import org.springframework.validation.BindException;
  19. import org.springframework.validation.Errors;
  20.  
  21. /**
  22. * Tests methods on the {@link AlertValidator} class.
  23. *
  24. * @since 1.10
  25. */
  26. public class AlertValidatorTest {
  27. /**
  28. * @see AlertValidator#validate(Object,Errors)
  29. * @verifies fail validation if Alert Text is null or empty or whitespace
  30. */
  31. @Test
  32. public void validate_shouldFailValidationIfAlertTextIsNullOrEmptyOrWhitespace()
  33. throws Exception {
  34. Alert alert = new Alert();
  35. alert.setText(null);
  36.  
  37. Errors errors = new BindException(alert, "alert");
  38. new AlertValidator().validate(alert, errors);
  39. Assert.assertTrue(errors.hasFieldErrors("text"));
  40.  
  41. alert.setText("");
  42. errors = new BindException(alert, "alert");
  43. new AlertValidator().validate(alert, errors);
  44. Assert.assertTrue(errors.hasFieldErrors("text"));
  45.  
  46. alert.setText(" ");
  47. errors = new BindException(alert, "alert");
  48. new AlertValidator().validate(alert, errors);
  49. Assert.assertTrue(errors.hasFieldErrors("text"));
  50. }
  51. }
  52.  
  53.  
  54.  
  55.  
  56.  
  57. /**
  58. * The contents of this file are subject to the OpenMRS Public License
  59. * Version 1.0 (the "License"); you may not use this file except in
  60. * compliance with the License. You may obtain a copy of the License at
  61. *
  62. * Software distributed under the License is distributed on an "AS IS"
  63. * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
  64. * License for the specific language governing rights and limitations
  65. * under the License.
  66. *
  67. * Copyright (C) OpenMRS, LLC. All Rights Reserved.
  68. */
  69. package org.openmrs.validator;
  70.  
  71. import org.apache.commons.logging.Log;
  72. import org.apache.commons.logging.LogFactory;
  73. import org.openmrs.notification.Alert;
  74. import org.openmrs.annotation.Handler;
  75. import org.springframework.validation.Errors;
  76. import org.springframework.validation.ValidationUtils;
  77. import org.springframework.validation.Validator;
  78.  
  79. /**
  80. * Validates attributes on the {@link Alert} object.
  81. *
  82. * @since 1.10
  83. */
  84. @Handler(supports = { Alert.class }, order = 50)
  85. public class AlertValidator implements Validator {
  86. /** Log for this class and subclasses */
  87. protected final Log log = LogFactory.getLog(getClass());
  88.  
  89. /**
  90. * Determines if the command object being submitted is a valid type
  91. *
  92. * @see org.springframework.validation.Validator#supports(java.lang.Class)
  93. */
  94. @SuppressWarnings("unchecked")
  95. public boolean supports(Class c) {
  96. return c.equals(Alert.class);
  97. }
  98. /**
  99. * @see org.springframework.validation.Validator#validate(java.lang.Object, org.springframework.validation.Errors)
  100. * @should fail validation if Alert Text is null or empty or whitespace
  101. */
  102. public void validate(Object obj, Errors errors) {
  103. Alert alert = (Alert) obj;
  104. if (alert == null) {
  105. errors.rejectValue("alert", "error.general");
  106. }
  107. else {
  108. ValidationUtils.rejectIfEmptyOrWhitespace(errors, "text", "Alert.text.required");
  109. }
  110. }
  111. }
  112.  
  113.  
  114.  
  115.  
  116.  
  117. /**
  118. * The contents of this file are subject to the OpenMRS Public License
  119. * Version 1.0 (the "License"); you may not use this file except in
  120. * compliance with the License. You may obtain a copy of the License at
  121. * http://license.openmrs.org
  122. *
  123. * Software distributed under the License is distributed on an "AS IS"
  124. * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
  125. * License for the specific language governing rights and limitations
  126. * under the License.
  127. *
  128. * Copyright (C) OpenMRS, LLC. All Rights Reserved.
  129. */
  130. package org.openmrs.notification.web.controller;
  131.  
  132. import java.text.NumberFormat;
  133. import java.util.HashMap;
  134. import java.util.List;
  135. import java.util.Locale;
  136. import java.util.Map;
  137. import java.util.Vector;
  138.  
  139. import javax.servlet.http.HttpServletRequest;
  140. import javax.servlet.http.HttpServletResponse;
  141. import javax.servlet.http.HttpSession;
  142.  
  143. import org.apache.commons.logging.Log;
  144. import org.apache.commons.logging.LogFactory;
  145. import org.openmrs.Role;
  146. import org.openmrs.User;
  147. import org.openmrs.api.APIAuthenticationException;
  148. import org.openmrs.api.UserService;
  149. import org.openmrs.api.context.Context;
  150. import org.openmrs.notification.Alert;
  151. import org.openmrs.notification.AlertRecipient;
  152. import org.openmrs.util.PrivilegeConstants;
  153. import org.openmrs.validator.AlertValidator;
  154. import org.openmrs.validator.RelationshipTypeValidator;
  155. import org.openmrs.web.WebConstants;
  156. import org.springframework.beans.propertyeditors.CustomDateEditor;
  157. import org.springframework.beans.propertyeditors.CustomNumberEditor;
  158. import org.springframework.validation.BindException;
  159. import org.springframework.validation.Errors;
  160. import org.springframework.web.bind.ServletRequestDataBinder;
  161. import org.springframework.web.servlet.ModelAndView;
  162. import org.springframework.web.servlet.mvc.SimpleFormController;
  163. import org.springframework.web.servlet.view.RedirectView;
  164.  
  165. public class AlertFormController extends SimpleFormController {
  166.  
  167. /** Logger for this class and subclasses */
  168. protected final Log log = LogFactory.getLog(getClass());
  169.  
  170. /**
  171. * Allows for Integers to be used as values in input tags. Normally, only strings and lists are
  172. * expected
  173. *
  174. * @see org.springframework.web.servlet.mvc.BaseCommandController#initBinder(javax.servlet.http.HttpServletRequest,
  175. * org.springframework.web.bind.ServletRequestDataBinder)
  176. */
  177. protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
  178. super.initBinder(request, binder);
  179.  
  180. Locale locale = Context.getLocale();
  181. NumberFormat nf = NumberFormat.getInstance(locale);
  182.  
  183. // NumberFormat nf = NumberFormat.getInstance(new Locale("en_US"));
  184. binder.registerCustomEditor(java.lang.Integer.class, new CustomNumberEditor(java.lang.Integer.class, nf, true));
  185. binder.registerCustomEditor(java.util.Date.class, new CustomDateEditor(Context.getDateFormat(), true, 10));
  186. }
  187.  
  188. /**
  189. * @see org.springframework.web.servlet.mvc.SimpleFormController#processFormSubmission(javax.servlet.http.HttpServletRequest,
  190. * javax.servlet.http.HttpServletResponse, java.lang.Object,
  191. * org.springframework.validation.BindException)
  192. */
  193. protected ModelAndView processFormSubmission(HttpServletRequest request, HttpServletResponse reponse, Object obj,
  194. BindException errors) throws Exception {
  195.  
  196. Alert alert = (Alert) obj;
  197. new AlertValidator().validate(obj, errors);
  198.  
  199. return super.processFormSubmission(request, reponse, alert, errors);
  200. }
  201.  
  202. /**
  203. * The onSubmit function receives the form/command object that was modified by the input form
  204. * and saves it to the db
  205. *
  206. * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest,
  207. * javax.servlet.http.HttpServletResponse, java.lang.Object,
  208. * org.springframework.validation.BindException)
  209. */
  210. protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object obj,
  211. BindException errors) throws Exception {
  212.  
  213. HttpSession httpSession = request.getSession();
  214.  
  215. String view = getFormView();
  216.  
  217. if (Context.isAuthenticated()) {
  218. Context.getAlertService().saveAlert((Alert) obj);
  219. view = getSuccessView();
  220. httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR, "Alert.saved");
  221. }
  222.  
  223. return new ModelAndView(new RedirectView(view));
  224. }
  225.  
  226. /**
  227. * This is called prior to displaying a form for the first time. It tells Spring the
  228. * form/command object to load into the request
  229. *
  230. * @see org.springframework.web.servlet.mvc.AbstractFormController#formBackingObject(javax.servlet.http.HttpServletRequest)
  231. */
  232. protected Object formBackingObject(HttpServletRequest request) throws Exception {
  233.  
  234. Alert alert = null;
  235.  
  236. if (Context.isAuthenticated()) {
  237. String a = request.getParameter("alertId");
  238. if (a != null)
  239. alert = Context.getAlertService().getAlert(Integer.valueOf(a));
  240. }
  241.  
  242. if (alert == null)
  243. alert = new Alert();
  244.  
  245. return alert;
  246. }
  247.  
  248. /**
  249. * @see org.springframework.web.servlet.mvc.SimpleFormController#referenceData(javax.servlet.http.HttpServletRequest,
  250. * java.lang.Object, org.springframework.validation.Errors)
  251. */
  252. protected Map<String, Object> referenceData(HttpServletRequest request, Object object, Errors errors) throws Exception {
  253. Map<String, Object> map = new HashMap<String, Object>();
  254.  
  255. if (Context.isAuthenticated()) {
  256. map.put("allRoles", Context.getUserService().getAllRoles());
  257. }
  258.  
  259. return map;
  260. }
  261.  
  262. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement