diff --git a/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PatientIdentifierResource1_8.java b/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PatientIdentifierResource1_8.java index 3d41ab92b..1b62538c5 100644 --- a/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PatientIdentifierResource1_8.java +++ b/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PatientIdentifierResource1_8.java @@ -214,7 +214,14 @@ public PatientIdentifier getByUniqueId(String uniqueId) { */ @Override public PatientIdentifier save(PatientIdentifier delegate) { - // make sure it has already been added to the patient + if (delegate.isPreferred()) { + for (PatientIdentifier pI : delegate.getPatient().getActiveIdentifiers()) { + if (!pI.equals(delegate)) { + pI.setPreferred(false); + } + } + } + boolean needToAdd = true; for (PatientIdentifier pi : delegate.getPatient().getActiveIdentifiers()) { if (pi.equals(delegate)) { @@ -222,9 +229,14 @@ public PatientIdentifier save(PatientIdentifier delegate) { break; } } - if (needToAdd) + + if (needToAdd) { delegate.getPatient().addIdentifier(delegate); - return service().savePatientIdentifier(delegate); + } + + service().savePatientIdentifier(delegate); + return delegate; + } @Override diff --git a/omod-1.9/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/PatientIdentifierController1_9Test.java b/omod-1.9/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/PatientIdentifierController1_9Test.java index 439d3ccdd..5eff7dec9 100644 --- a/omod-1.9/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/PatientIdentifierController1_9Test.java +++ b/omod-1.9/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/PatientIdentifierController1_9Test.java @@ -10,6 +10,7 @@ package org.openmrs.module.webservices.rest.web.v1_0.controller.openmrs1_9; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; @@ -113,6 +114,33 @@ public void shouldEditAPatientIdentifier() throws Exception { assertEquals(newLocationUuid, patientIdentifierType.getLocation().getUuid()); } + @Test + public void shouldUnsetOtherPreferredIdentifiers() throws Exception { + PatientIdentifier exisitingIdentifier = service.getPatientIdentifierByUuid(getUuid()); + long originalCount = getAllCount(); + assertTrue(exisitingIdentifier.isPreferred()); + SimpleObject patientIdentifier = new SimpleObject(); + patientIdentifier.add("identifier", "abc123ez"); + patientIdentifier.add("identifierType", "2f470aa8-1d73-43b7-81b5-01f0c0dfa53c"); + patientIdentifier.add("location", RestTestConstants1_8.LOCATION_UUID); + patientIdentifier.add("preferred", true); + + String json = new ObjectMapper().writeValueAsString(patientIdentifier); + + MockHttpServletRequest req = request(RequestMethod.POST, getURI()); + req.setContent(json.getBytes()); + + SimpleObject newPatientIdentifier = deserialize(handle(req)); + Object uuid = PropertyUtils.getProperty(newPatientIdentifier, "uuid"); + assertNotNull(uuid); + assertEquals(originalCount + 1, getAllCount()); + + PatientIdentifier newIdentifer = service.getPatientIdentifierByUuid(uuid.toString()); + assertFalse(exisitingIdentifier.isPreferred()); + assertTrue(newIdentifer.isPreferred()); + assertEquals(newIdentifer.getIdentifier(), "abc123ez"); + } + @Test public void shouldVoidAPatientIdentifier() throws Exception { assertEquals(false, service.getPatientIdentifierByUuid(getUuid()).isVoided());