Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public PatientIdentifier save(PatientIdentifier delegate) {
}
}
}

boolean needToAdd = true;
for (PatientIdentifier pi : delegate.getPatient().getActiveIdentifiers()) {
if (pi.equals(delegate)) {
Expand All @@ -235,9 +235,10 @@ public PatientIdentifier save(PatientIdentifier delegate) {
if (needToAdd) {
delegate.getPatient().addIdentifier(delegate);
}
// make sure that the identifier has actually been added to the patient
Context.getPatientService().savePatient(delegate.getPatient());

return delegate;

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public DelegatingResourceDescription getRepresentationDescription(Representation
description.addProperty("middleName");
description.addProperty("familyName");
description.addProperty("familyName2");
description.addProperty("preferred");
description.addProperty("voided");
description.addSelfLink();
description.addLink("full", ".?v=" + RestConstants.REPRESENTATION_FULL);
Expand Down Expand Up @@ -120,7 +121,7 @@ public Model getGETModel(Representation rep) {
}
if (rep instanceof FullRepresentation) {
model
.property("preferred", new BooleanProperty())
.property("preferred", new BooleanProperty()._default(false))
.property("prefix", new StringProperty())
.property("familyNamePrefix", new StringProperty())
.property("familyNameSuffix", new StringProperty())
Expand Down Expand Up @@ -224,9 +225,21 @@ public PersonName save(PersonName newName) {
break;
}
}
if (needToAdd)
if (needToAdd) {
newName.getPerson().addName(newName);
}

// if this name is marked as preferred, then we need to clear any others that are marked as preferred
if (newName.isPreferred()) {
for (PersonName pn : newName.getPerson().getNames()) {
if (!pn.equals(newName)) {
pn.setPreferred(false);
}
}
}

Context.getPersonService().savePerson(newName.getPerson());

return newName;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/**
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.module.webservices.rest.web.v1_0.controller.openmrs1_8;

import static org.hamcrest.beans.HasPropertyWithValue.hasProperty;
import static org.hamcrest.core.AllOf.allOf;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsCollectionContaining.hasItem;
import static org.junit.Assert.assertThat;

import org.apache.commons.beanutils.PropertyUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.hamcrest.Matcher;
import org.junit.Before;
import org.junit.Test;
import org.openmrs.Person;
import org.openmrs.PersonName;
import org.openmrs.api.PersonService;
import org.openmrs.api.context.Context;
import org.openmrs.module.webservices.rest.SimpleObject;
import org.openmrs.module.webservices.rest.web.RestTestConstants1_8;
import org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceControllerTest;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.web.bind.annotation.RequestMethod;

public class PersonNameController1_8Test extends MainResourceControllerTest {

private PersonService service;

@Before
public void before() throws Exception {
executeDataSet("PersonControllerTest-otherPersonData.xml");
this.service = Context.getPersonService();
}

/**
* @see org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceControllerTest#getURI()
*/
@Override
public String getURI() {
return "person" + "/" + RestTestConstants1_8.PERSON_UUID + "/name";
}

/**
* @see org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceControllerTest#getAllCount()
*/
@Override
public long getAllCount() {
int count = 0;

Person person = service.getPersonByUuid(RestTestConstants1_8.PERSON_UUID);

for (PersonName name : person.getNames()) {
if (!name.isVoided()) {
count++;
}
}

return count;
}

/**
* @see org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceControllerTest#getUuid()
*/
@Override
public String getUuid() {
return RestTestConstants1_8.PERSON_NAME_UUID;
}

@Test
public void shouldSetPreferred() throws Exception {
PersonName nonPreferred = service.getPersonNameByUuid(getUuid());
Person person = nonPreferred.getPerson();
assertThat(nonPreferred.isPreferred(), is(true));

SimpleObject newName= new SimpleObject();
newName.add("givenName", "Moses");
newName.add("familyName", "Mujuzi");
newName.add("preferred", "true");

String json = new ObjectMapper().writeValueAsString(newName);

MockHttpServletRequest req = request(RequestMethod.POST, getURI());
req.setContent(json.getBytes());

SimpleObject result = deserialize(handle(req));
Object uuid = PropertyUtils.getProperty(result, "uuid");
PersonName preferred = service.getPersonNameByUuid(uuid.toString());

// sanity check
assertThat(nonPreferred.isPreferred(), is(false));
assertThat(preferred.isPreferred(), is(true));

// check before change
assertThat(person.getNames(), (Matcher) hasItem(allOf(
hasProperty("preferred", is(true)),
hasProperty("givenName", is("Moses"))
)));

// change non preferred to preferred
SimpleObject updateToTrue = new SimpleObject();
updateToTrue.add("preferred", "true");

String updateJson = new ObjectMapper().writeValueAsString(updateToTrue);

MockHttpServletRequest req1 = request(RequestMethod.POST, getURI() + "/" + nonPreferred.getUuid());
req1.setContent(updateJson.getBytes());
handle(req1);

// check after change
assertThat(person.getNames(), (Matcher) hasItem(allOf(
hasProperty("preferred", is(false)),
hasProperty("givenName", is("Moses"))
)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
*/
package org.openmrs.module.webservices.rest.web.v1_0.controller.openmrs1_9;

import static org.hamcrest.beans.HasPropertyWithValue.hasProperty;
import static org.hamcrest.core.AllOf.allOf;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsCollectionContaining.hasItem;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
Expand All @@ -19,8 +24,10 @@

import org.apache.commons.beanutils.PropertyUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.hamcrest.Matcher;
import org.junit.Before;
import org.junit.Test;
import org.openmrs.Patient;
import org.openmrs.PatientIdentifier;
import org.openmrs.api.APIException;
import org.openmrs.api.PatientService;
Expand Down Expand Up @@ -248,4 +255,52 @@ public void shouldUpdateAnExistingPatientIdentifier() throws Exception {
assertEquals(patientIdentifierNewValue, identifierValue);
assertEquals(patientIdentifierNewValue, patientIdentifier.getIdentifier());
}

@Test
public void shouldSetPreferred() throws Exception {
PatientIdentifier nonPreferred = service.getPatientIdentifierByUuid(getUuid());
Patient patient = nonPreferred.getPatient();
assertThat(nonPreferred.isPreferred(), is(true));

SimpleObject newIdentifier = new SimpleObject();
newIdentifier.add("identifier", "OMRS303");
newIdentifier.add("identifierType", "2f470aa8-1d73-43b7-81b5-01f0c0dfa53c");
newIdentifier.add("location", RestTestConstants1_8.LOCATION_UUID);
newIdentifier.add("preferred", "true");

String json = new ObjectMapper().writeValueAsString(newIdentifier);

MockHttpServletRequest req = request(RequestMethod.POST, "patient/" + patient.getUuid() + "/identifier");
req.setContent(json.getBytes());

SimpleObject result = deserialize(handle(req));
Object uuid = PropertyUtils.getProperty(result, "uuid");
PatientIdentifier preferred = service.getPatientIdentifierByUuid(uuid.toString());

// sanity check
assertThat(nonPreferred.isPreferred(), is(false));
assertThat(preferred.isPreferred(), is(true));

// check before change
assertThat(patient.getIdentifiers(), (Matcher) hasItem(allOf(
hasProperty("preferred", is(true)),
hasProperty("identifier", is("OMRS303"))
)));

// change non preferred to preferred
SimpleObject updateToTrue = new SimpleObject();
updateToTrue.add("preferred", "true");

String updateJson = new ObjectMapper().writeValueAsString(updateToTrue);

MockHttpServletRequest req1 = request(RequestMethod.POST, "patient/" + patient.getUuid() + "/identifier/" + nonPreferred.getUuid());
req1.setContent(updateJson.getBytes());
handle(req1);

// check after change
assertThat(patient.getIdentifiers(), (Matcher) hasItem(allOf(
hasProperty("preferred", is(false)),
hasProperty("identifier", is("OMRS303"))
)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void shouldCreatePatientIdentifier_WhenTypeIsSpecifiedByUuid() throws Exc
String personAttributeJson = "{" + " \"identifier\": \"101-6\"," + " \"identifierType\": {"
+ " \"uuid\" : \"1a339fe9-38bc-4ab3-b180-320988c0b968\"" + " },"
+ " \"location\" : {" + " \"uuid\" : \"8d6c993e-c2cc-11de-8d13-0010c6dffd0f\""
+ " }," + " \"preferred\": true" + " }";
+ " }}";

SimpleObject personAttributeSimpleObject = new SimpleObject();
personAttributeSimpleObject.putAll(new ObjectMapper().readValue(personAttributeJson, HashMap.class));
Expand All @@ -54,7 +54,7 @@ public void shouldCreatePatientIdentifier_WhenTypeIsSpecifiedByName() throws Exc
String personAttributeJson = "{" + " \"identifier\": \"101-6\"," + " \"identifierType\": {"
+ " \"name\" : \"OpenMRS Identification Number\"" + " },"
+ " \"location\" : {" + " \"uuid\" : \"8d6c993e-c2cc-11de-8d13-0010c6dffd0f\""
+ " }," + " \"preferred\": true" + " }";
+ " }}";

SimpleObject personAttributeSimpleObject = new SimpleObject();
personAttributeSimpleObject.putAll(new ObjectMapper().readValue(personAttributeJson, HashMap.class));
Expand Down
Loading