Advertisement
Guest User

Untitled

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