Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.openmrs.module.locationbasedaccess.web.controller;
- import java.util.Date;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Map;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.commons.lang3.StringUtils;
- import org.openmrs.Patient;
- import org.openmrs.Person;
- import org.openmrs.PersonAttribute;
- import org.openmrs.PersonAttributeType;
- import org.openmrs.api.PatientService;
- import org.openmrs.api.PersonService;
- import org.openmrs.api.context.Context;
- import org.openmrs.module.locationbasedaccess.LocationBasedAccessConstants;
- import org.openmrs.module.webservices.rest.SimpleObject;
- import org.openmrs.module.webservices.rest.web.RestConstants;
- import org.openmrs.module.webservices.rest.web.RestUtil;
- import org.openmrs.module.webservices.rest.web.v1_0.controller.BaseRestController;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Qualifier;
- import org.springframework.http.HttpStatus;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.ExceptionHandler;
- import org.springframework.web.bind.annotation.RequestBody;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.ResponseBody;
- import org.springframework.web.bind.annotation.ResponseStatus;
- @Controller
- @RequestMapping(value = "/rest/" + RestConstants.VERSION_1 + "/patient-migration")
- public class PatientsMigrationController extends BaseRestController {
- @Qualifier("patientService")
- @Autowired
- private PatientService patientService;
- @RequestMapping(method = RequestMethod.POST)
- @ResponseBody
- @ResponseStatus(value = HttpStatus.OK)
- public void migratePatientLocation(@RequestBody Map<String, Object> body){
- List<String> patientList = (List<String>) body.get("patientList");
- String locationUuid = (String)body.get("locationUuid");
- String locationAttributeUuid = Context.getAdministrationService().getGlobalProperty(
- LocationBasedAccessConstants.LOCATION_ATTRIBUTE_GLOBAL_PROPERTY_NAME);
- if (StringUtils.isNotBlank(locationAttributeUuid)) {
- final PersonAttributeType personAttributeType = Context.getPersonService().getPersonAttributeTypeByUuid(locationAttributeUuid);
- PersonAttribute personAttribute = new PersonAttribute(personAttributeType, locationUuid);
- for(Iterator<String> iterator = patientList.iterator(); iterator.hasNext();){
- String patientUuid = iterator.next();
- Patient patient = patientService.getPatientByUuid(patientUuid);
- patient.addAttribute(personAttribute);
- patientService.savePatient(patient);
- }
- }
- }
- @ExceptionHandler(NullPointerException.class)
- @ResponseBody
- public SimpleObject handleNotFound(NullPointerException exception, HttpServletRequest request,
- HttpServletResponse response) {
- response.setStatus(HttpServletResponse.SC_NOT_FOUND);
- return RestUtil.wrapErrorResponse(exception, "Patient not found");
- }
- }
Add Comment
Please, Sign In to add comment