Advertisement
Guest User

Untitled

a guest
Dec 11th, 2012
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 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.HashSet;
  18. import java.util.LinkedList;
  19. import java.util.List;
  20. import java.util.Set;
  21.  
  22. import javax.servlet.http.HttpServletRequest;
  23. import javax.servlet.http.HttpSession;
  24.  
  25. import org.apache.commons.logging.Log;
  26. import org.apache.commons.logging.LogFactory;
  27. import org.openmrs.Location;
  28. import org.openmrs.Provider;
  29. import org.openmrs.api.context.Context;
  30. import org.openmrs.module.appointment.Appointment;
  31. import org.openmrs.module.appointment.AppointmentType;
  32. import org.openmrs.module.appointment.TimeSlot;
  33. import org.openmrs.module.appointment.api.AppointmentService;
  34. import org.openmrs.module.appointment.validator.AppointmentValidator;
  35. import org.openmrs.module.appointment.web.TimeSlotEditor;
  36. import org.springframework.stereotype.Controller;
  37. import org.springframework.ui.ModelMap;
  38. import org.springframework.validation.BindingResult;
  39. import org.springframework.web.bind.WebDataBinder;
  40. import org.springframework.web.bind.annotation.InitBinder;
  41. import org.springframework.web.bind.annotation.ModelAttribute;
  42. import org.springframework.web.bind.annotation.RequestMapping;
  43. import org.springframework.web.bind.annotation.RequestMethod;
  44. import org.springframework.web.bind.annotation.RequestParam;
  45.  
  46. /**
  47. * Controller for creating appointments.
  48. */
  49. @Controller
  50. public class AppointmentFormController {
  51.  
  52. /** Logger for this class and subclasses */
  53. protected final Log log = LogFactory.getLog(getClass());
  54.  
  55. @InitBinder
  56. public void initBinder(WebDataBinder binder) {
  57. binder.registerCustomEditor(TimeSlot.class, new TimeSlotEditor());
  58. }
  59.  
  60. @RequestMapping(value = "/module/appointment/appointmentForm", method = RequestMethod.GET)
  61. public void showForm(ModelMap model) {
  62. //default empty Object
  63. Set<AppointmentType> appointmentTypeList = new HashSet<AppointmentType>();
  64. List<Provider> providerList = new LinkedList<Provider>();
  65. List<TimeSlot> availableTimes = new LinkedList<TimeSlot>();
  66.  
  67. //only fill the Object is the user has authenticated properly
  68. if (Context.isAuthenticated()) {
  69. AppointmentService appointmentService = Context.getService(AppointmentService.class);
  70. appointmentTypeList = appointmentService.getAllAppointmentTypes();
  71. providerList = Context.getProviderService().getAllProviders();
  72. availableTimes = appointmentService.getAllTimeSlots();
  73. }
  74.  
  75. model.addAttribute("appointmentTypeList", appointmentTypeList);
  76. model.addAttribute("providerList", providerList);
  77. model.addAttribute("availableTimes", availableTimes);
  78. }
  79.  
  80. @ModelAttribute("appointment")
  81. public Appointment getAppointment(@RequestParam(value = "appointmentId", required = false) Integer appointmentId) {
  82. Appointment appointment = null;
  83.  
  84. if (Context.isAuthenticated()) {
  85. AppointmentService as = Context.getService(AppointmentService.class);
  86. if (appointmentId != null)
  87. appointment = as.getAppointment(appointmentId);
  88. }
  89.  
  90. if (appointment == null)
  91. appointment = new Appointment();
  92.  
  93. return appointment;
  94. }
  95.  
  96. @RequestMapping(method = RequestMethod.POST)
  97. public String onSubmit(HttpServletRequest request, Appointment appointment, BindingResult result) throws Exception {
  98. HttpSession httpSession = request.getSession();
  99.  
  100. if (Context.isAuthenticated()) {
  101. AppointmentService appointmentService = Context.getService(AppointmentService.class);
  102.  
  103. if (request.getParameter("save") != null) {
  104. new AppointmentValidator().validate(appointment, result);
  105. }
  106. if (result.hasErrors())
  107. return null;
  108. }
  109. return "";
  110. }
  111.  
  112. @RequestMapping(params = "findAvailableTime", method = RequestMethod.POST)
  113. public void onFindTimesClick(ModelMap model, @RequestParam(value = "fromDate", required = true) Date fromDate,
  114. @RequestParam(value = "toDate", required = true) Date toDate,
  115. @RequestParam(value = "location", required = false) Location location) throws Exception {
  116.  
  117. if (Context.isAuthenticated()) {
  118. List<TimeSlot> availableTimes = Context.getService(AppointmentService.class).getAllTimeSlots();
  119. model.addAttribute("availableTimes", availableTimes);
  120.  
  121. showForm(model);
  122. }
  123. }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement