Guest User

Untitled

a guest
Jul 8th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. package org.openmrs.module.locationbasedaccess.web.controller;
  2.  
  3. import java.util.Date;
  4. import java.util.Iterator;
  5. import java.util.List;
  6. import java.util.Map;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9.  
  10. import org.apache.commons.lang3.StringUtils;
  11. import org.openmrs.Patient;
  12. import org.openmrs.Person;
  13. import org.openmrs.PersonAttribute;
  14. import org.openmrs.PersonAttributeType;
  15. import org.openmrs.api.PatientService;
  16. import org.openmrs.api.PersonService;
  17. import org.openmrs.api.context.Context;
  18. import org.openmrs.module.locationbasedaccess.LocationBasedAccessConstants;
  19. import org.openmrs.module.webservices.rest.SimpleObject;
  20. import org.openmrs.module.webservices.rest.web.RestConstants;
  21. import org.openmrs.module.webservices.rest.web.RestUtil;
  22. import org.openmrs.module.webservices.rest.web.v1_0.controller.BaseRestController;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.beans.factory.annotation.Qualifier;
  25. import org.springframework.http.HttpStatus;
  26. import org.springframework.stereotype.Controller;
  27. import org.springframework.web.bind.annotation.ExceptionHandler;
  28. import org.springframework.web.bind.annotation.RequestBody;
  29. import org.springframework.web.bind.annotation.RequestMapping;
  30. import org.springframework.web.bind.annotation.RequestMethod;
  31. import org.springframework.web.bind.annotation.ResponseBody;
  32. import org.springframework.web.bind.annotation.ResponseStatus;
  33.  
  34. @Controller
  35. @RequestMapping(value = "/rest/" + RestConstants.VERSION_1 + "/patient-migration")
  36. public class PatientsMigrationController extends BaseRestController {
  37.  
  38. @Qualifier("patientService")
  39. @Autowired
  40. private PatientService patientService;
  41.  
  42. @RequestMapping(method = RequestMethod.POST)
  43. @ResponseBody
  44. @ResponseStatus(value = HttpStatus.OK)
  45. public void migratePatientLocation(@RequestBody Map<String, Object> body){
  46. List<String> patientList = (List<String>) body.get("patientList");
  47. String locationUuid = (String)body.get("locationUuid");
  48. String locationAttributeUuid = Context.getAdministrationService().getGlobalProperty(
  49. LocationBasedAccessConstants.LOCATION_ATTRIBUTE_GLOBAL_PROPERTY_NAME);
  50. if (StringUtils.isNotBlank(locationAttributeUuid)) {
  51. final PersonAttributeType personAttributeType = Context.getPersonService().getPersonAttributeTypeByUuid(locationAttributeUuid);
  52. PersonAttribute personAttribute = new PersonAttribute(personAttributeType, locationUuid);
  53. for(Iterator<String> iterator = patientList.iterator(); iterator.hasNext();){
  54. String patientUuid = iterator.next();
  55. Patient patient = patientService.getPatientByUuid(patientUuid);
  56. patient.addAttribute(personAttribute);
  57. patientService.savePatient(patient);
  58. }
  59. }
  60. }
  61.  
  62. @ExceptionHandler(NullPointerException.class)
  63. @ResponseBody
  64. public SimpleObject handleNotFound(NullPointerException exception, HttpServletRequest request,
  65. HttpServletResponse response) {
  66. response.setStatus(HttpServletResponse.SC_NOT_FOUND);
  67. return RestUtil.wrapErrorResponse(exception, "Patient not found");
  68. }
  69.  
  70. }
Add Comment
Please, Sign In to add comment