Advertisement
yony258

Untitled

Dec 12th, 2012
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.62 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.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.RequestMapping;
  44. import org.springframework.web.bind.annotation.RequestMethod;
  45. import org.springframework.web.bind.annotation.RequestParam;
  46.  
  47. /**
  48.  * Controller for creating appointments.
  49.  */
  50. @Controller
  51. public class AppointmentFormController {
  52.    
  53.     /** Logger for this class and subclasses */
  54.     protected final Log log = LogFactory.getLog(getClass());
  55.    
  56.     @InitBinder
  57.     public void initBinder(WebDataBinder binder) {
  58.         binder.registerCustomEditor(TimeSlot.class, new TimeSlotEditor());
  59.     }
  60.    
  61.     @RequestMapping(value = "/module/appointment/appointmentForm", method = RequestMethod.GET)
  62.     public void showForm(ModelMap model, HttpServletRequest request) {
  63.         //request.getSession().setAttribute("availableTimes", null);
  64.     }
  65.    
  66.     @ModelAttribute("availableTimes")
  67.     public List<TimeSlot> getAvailableTimes(ModelMap model,
  68.             @RequestParam(value = "findAvailableTime", required = false) String findAvailableTime,
  69.             @RequestParam(value = "fromDate", required = false) Date fromDate,
  70.             @RequestParam(value = "toDate", required = false) Date toDate,
  71.             @RequestParam(value = "location", required = false) Location location) {
  72.        
  73.         if (findAvailableTime != null && findAvailableTime.length() > 0)
  74.             return Context.getService(AppointmentService.class).getAllTimeSlots();
  75.         else
  76.             return null;
  77.     }
  78.    
  79.     @ModelAttribute("appointment")
  80.     public Appointment getAppointment(@RequestParam(value = "appointmentId", required = false) Integer appointmentId) {
  81.         Appointment appointment = null;
  82.        
  83.         if (Context.isAuthenticated()) {
  84.             AppointmentService as = Context.getService(AppointmentService.class);
  85.             if (appointmentId != null)
  86.                 appointment = as.getAppointment(appointmentId);
  87.         }
  88.        
  89.         if (appointment == null)
  90.             appointment = new Appointment();
  91.        
  92.         return appointment;
  93.     }
  94.    
  95.     @ModelAttribute("providerList")
  96.     public List<Provider> getProviderList() {
  97.         return Context.getProviderService().getAllProviders();
  98.     }
  99.    
  100.     @ModelAttribute("appointmentTypeList")
  101.     public Set<AppointmentType> getAppointmentTypeList() {
  102.         return Context.getService(AppointmentService.class).getAllAppointmentTypes();
  103.     }
  104.    
  105.     @RequestMapping(method = RequestMethod.POST)
  106.     public String onSubmit(HttpServletRequest request, Appointment appointment, BindingResult result) throws Exception {
  107.         HttpSession httpSession = request.getSession();
  108.        
  109.         if (Context.isAuthenticated()) {
  110.             AppointmentService appointmentService = Context.getService(AppointmentService.class);
  111.            
  112.             if (request.getParameter("save") != null) {
  113.                 new AppointmentValidator().validate(appointment, result);
  114.                
  115.                 if (result.hasErrors())
  116.                     return null;
  117.                 else {
  118.                     //TODO: change to enum
  119.                     appointment.setStatus("SCHEDULED");
  120.                     appointmentService.saveAppointment(appointment);
  121.                     httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR, "appointment.Appointment.saved");
  122.                 }
  123.             }
  124.         }
  125.         return null;
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement