Advertisement
Guest User

Untitled

a guest
Dec 31st, 2012
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.14 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.module.appointment.web.controller;
  15.  
  16. import java.util.Date;
  17. import java.util.List;
  18. import java.util.Set;
  19.  
  20. import javax.servlet.http.HttpServletRequest;
  21. import javax.servlet.http.HttpSession;
  22.  
  23. import org.apache.commons.logging.Log;
  24. import org.apache.commons.logging.LogFactory;
  25. import org.openmrs.Location;
  26. import org.openmrs.Provider;
  27. import org.openmrs.api.context.Context;
  28. import org.openmrs.module.appointment.Appointment;
  29. import org.openmrs.module.appointment.AppointmentType;
  30. import org.openmrs.module.appointment.TimeSlot;
  31. import org.openmrs.module.appointment.api.AppointmentService;
  32. import org.openmrs.module.appointment.validator.AppointmentValidator;
  33. import org.openmrs.module.appointment.web.AppointmentTypeEditor;
  34. import org.openmrs.module.appointment.web.ProviderEditor;
  35. import org.openmrs.module.appointment.web.TimeSlotEditor;
  36. import org.openmrs.web.WebConstants;
  37. import org.springframework.stereotype.Controller;
  38. import org.springframework.ui.ModelMap;
  39. import org.springframework.validation.BindingResult;
  40. import org.springframework.web.bind.WebDataBinder;
  41. import org.springframework.web.bind.annotation.InitBinder;
  42. import org.springframework.web.bind.annotation.ModelAttribute;
  43. import org.springframework.web.bind.annotation.PathVariable;
  44. import org.springframework.web.bind.annotation.RequestMapping;
  45. import org.springframework.web.bind.annotation.RequestMethod;
  46. import org.springframework.web.bind.annotation.RequestParam;
  47.  
  48. /**
  49. * Controller for creating appointments.
  50. */
  51. @Controller
  52. public class AppointmentFormController {
  53.  
  54. /** Logger for this class and subclasses */
  55. protected final Log log = LogFactory.getLog(getClass());
  56.  
  57. @InitBinder
  58. public void initBinder(WebDataBinder binder) {
  59. binder.registerCustomEditor(TimeSlot.class, new TimeSlotEditor());
  60. binder.registerCustomEditor(AppointmentType.class, new AppointmentTypeEditor());
  61. binder.registerCustomEditor(Provider.class, new ProviderEditor());
  62. }
  63.  
  64. @RequestMapping(value = "/module/appointment/appointmentForm", method = RequestMethod.GET)
  65. public void showForm(ModelMap model, HttpServletRequest request) {
  66. if (Context.isAuthenticated())
  67. model.put("selectedLocation", Context.getUserContext().getLocation());
  68. }
  69.  
  70. @ModelAttribute("selectedLocation")
  71. public Location getLocation(@RequestParam(value = "locationId", required = false) Location location) {
  72. if (location != null)
  73. return location;
  74. else
  75. return null;
  76. }
  77.  
  78. @ModelAttribute("appointment")
  79. public Appointment getAppointment(@RequestParam(value = "appointmentId", required = false) Integer appointmentId) {
  80. Appointment appointment = null;
  81.  
  82. if (Context.isAuthenticated()) {
  83. AppointmentService as = Context.getService(AppointmentService.class);
  84. if (appointmentId != null)
  85. appointment = as.getAppointment(appointmentId);
  86. }
  87.  
  88. if (appointment == null)
  89. appointment = new Appointment();
  90.  
  91. return appointment;
  92. }
  93.  
  94. @ModelAttribute("availableTimes")
  95. public List<TimeSlot> getAvailableTimes(HttpServletRequest request, Appointment appointment,
  96. @RequestParam(value = "fromDate", required = false) Date fromDate,
  97. @RequestParam(value = "toDate", required = false) Date toDate,
  98. @RequestParam(value = "providerSelect", required = false) Provider provider,
  99. @RequestParam(value = "locationId", required = false) Location location) {
  100. AppointmentType appointmentType = appointment.getAppointmentType();
  101. if (appointmentType == null || (fromDate != null && toDate != null && !fromDate.before(toDate)))
  102. return null;
  103.  
  104. try {
  105. List<TimeSlot> availableTimeSlots = Context.getService(AppointmentService.class).getTimeSlotsByConstraints(
  106. appointmentType, fromDate, toDate, provider, location);
  107. TimeSlot currentSelectedSlot = appointment.getTimeSlot();
  108. //The appointment time slot should be selected from the latest list
  109. if (currentSelectedSlot != null && !availableTimeSlots.contains(currentSelectedSlot))
  110. appointment.setTimeSlot(null);
  111. return availableTimeSlots;
  112. }
  113. catch (Exception ex) {
  114. return null;
  115. }
  116. }
  117.  
  118. @ModelAttribute("providerList")
  119. public List<Provider> getProviderList() {
  120. return Context.getProviderService().getAllProviders();
  121. }
  122.  
  123. @ModelAttribute("appointmentTypeList")
  124. public Set<AppointmentType> getAppointmentTypeList() {
  125. return Context.getService(AppointmentService.class).getAllAppointmentTypes();
  126. }
  127.  
  128. @RequestMapping(method = RequestMethod.POST)
  129. public String onSubmit(HttpServletRequest request, Appointment appointment, BindingResult result,
  130. @RequestParam(value = "fromDate", required = false) Date fromDate,
  131. @RequestParam(value = "toDate", required = false) Date toDate) throws Exception {
  132. HttpSession httpSession = request.getSession();
  133.  
  134. if (Context.isAuthenticated()) {
  135. AppointmentService appointmentService = Context.getService(AppointmentService.class);
  136.  
  137. if (request.getParameter("save") != null) {
  138. new AppointmentValidator().validate(appointment, result);
  139.  
  140. if (result.hasErrors())
  141. return null;
  142. else {
  143. //TODO: change to enum
  144. appointment.setStatus("SCHEDULED");
  145. appointmentService.saveAppointment(appointment);
  146. httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR, "appointment.Appointment.saved");
  147. return "redirect:appointmentList.list";
  148. }
  149. }
  150. if (request.getParameter("findAvailableTime") != null) {
  151. if (fromDate != null && toDate != null && !fromDate.before(toDate))
  152. result.rejectValue("timeSlot", "appointment.Appointment.error.InvalidDateInterval");
  153. }
  154. }
  155. return null;
  156. }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement