Advertisement
yony258

Untitled

Dec 11th, 2012
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 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.  
  63. }
  64.  
  65. @ModelAttribute("availableTimes")
  66. public List<TimeSlot> getAvailableTimes(@RequestParam(value = "fromDate", required = false) Date fromDate,
  67. @RequestParam(value = "toDate", required = false) Date toDate,
  68. @RequestParam(value = "location", required = false) Location location) {
  69. if (fromDate != null)
  70. return Context.getService(AppointmentService.class).getAllTimeSlots();
  71. return null;
  72. }
  73.  
  74. @ModelAttribute("appointment")
  75. public Appointment getAppointment(@RequestParam(value = "appointmentId", required = false) Integer appointmentId) {
  76. Appointment appointment = null;
  77.  
  78. if (Context.isAuthenticated()) {
  79. AppointmentService as = Context.getService(AppointmentService.class);
  80. if (appointmentId != null)
  81. appointment = as.getAppointment(appointmentId);
  82. }
  83.  
  84. if (appointment == null)
  85. appointment = new Appointment();
  86.  
  87. return appointment;
  88. }
  89.  
  90. @ModelAttribute("providerList")
  91. public List<Provider> getProviderList() {
  92. return Context.getProviderService().getAllProviders();
  93. }
  94.  
  95. @ModelAttribute("appointmentTypeList")
  96. public Set<AppointmentType> getAppointmentTypeList() {
  97. return Context.getService(AppointmentService.class).getAllAppointmentTypes();
  98. }
  99.  
  100. @RequestMapping(method = RequestMethod.POST)
  101. public String onSubmit(HttpServletRequest request, Appointment appointment, BindingResult result) throws Exception {
  102. HttpSession httpSession = request.getSession();
  103.  
  104. if (Context.isAuthenticated()) {
  105. AppointmentService appointmentService = Context.getService(AppointmentService.class);
  106.  
  107. if (request.getParameter("save") != null) {
  108. new AppointmentValidator().validate(appointment, result);
  109. }
  110. if (result.hasErrors())
  111. return null;
  112. }
  113. return "";
  114. }
  115.  
  116. @RequestMapping(params = "findAvailableTime", method = RequestMethod.POST)
  117. public void onFindTimesClick(ModelMap model, Appointment appointment,
  118. @RequestParam(value = "fromDate", required = false) Date fromDate,
  119. @RequestParam(value = "toDate", required = false) Date toDate,
  120. @RequestParam(value = "location", required = false) Location location) throws Exception {
  121.  
  122. if (Context.isAuthenticated()) {
  123. if (fromDate == null)
  124. getAvailableTimes(new Date(), toDate, location);
  125. else
  126. getAvailableTimes(fromDate, toDate, location);
  127. }
  128. }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement