Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FM2-276: Add create, update and delete methods for RelatedPerson #317

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import ca.uhn.fhir.model.api.TemporalPrecisionEnum;
import lombok.AccessLevel;
import lombok.Setter;
import org.hl7.fhir.r4.model.Address;
import org.hl7.fhir.r4.model.DateType;
import org.hl7.fhir.r4.model.HumanName;
import org.hl7.fhir.r4.model.Identifier;
import org.hl7.fhir.r4.model.Period;
import org.hl7.fhir.r4.model.RelatedPerson;
Expand Down Expand Up @@ -129,6 +131,42 @@ public RelatedPerson toFhirResource(@Nonnull Relationship relationship) {

@Override
public Relationship toOpenmrsType(@Nonnull RelatedPerson resource) {
throw new UnsupportedOperationException();
Relationship relationship = new Relationship();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't always generate a new relationship... there needs to be some allowance for updating an existing relationship, right? So I think we need to change RelatedPersonTranslator to extend UpdatableOpenmrsTranslator. Likewise personA should be retrieved from the database, if at all possible... Honestly, I think that's the really hard part about this....

relationship.setUuid(resource.getId());

Person personA = new Person();

if (resource.getBirthDateElement().getPrecision() == TemporalPrecisionEnum.DAY) {
personA.setBirthdate(resource.getBirthDate());
}

if (resource.getBirthDateElement().getPrecision() == TemporalPrecisionEnum.MONTH
|| resource.getBirthDateElement().getPrecision() == TemporalPrecisionEnum.YEAR) {
personA.setBirthdate(resource.getBirthDate());
personA.setBirthdateEstimated(true);
}

if (resource.hasGender()) {
personA.setGender(genderTranslator.toOpenmrsType(resource.getGender()));
}

for (HumanName name : resource.getName()) {
personA.addName(nameTranslator.toOpenmrsType(name));
}

for (Address address : resource.getAddress()) {
personA.addAddress(addressTranslator.toOpenmrsType(address));
}

relationship.setStartDate(resource.getPeriod().getStart());
relationship.setEndDate(resource.getPeriod().getEnd());

if (resource.getPatient() != null) {
relationship.setPersonB(patientReferenceTranslator.toOpenmrsType(resource.getPatient()));
}

relationship.setPersonA(personA);

return relationship;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,36 @@
import java.util.HashSet;

import ca.uhn.fhir.model.api.Include;
import ca.uhn.fhir.rest.annotation.Create;
import ca.uhn.fhir.rest.annotation.Delete;
import ca.uhn.fhir.rest.annotation.IdParam;
import ca.uhn.fhir.rest.annotation.IncludeParam;
import ca.uhn.fhir.rest.annotation.OptionalParam;
import ca.uhn.fhir.rest.annotation.Read;
import ca.uhn.fhir.rest.annotation.ResourceParam;
import ca.uhn.fhir.rest.annotation.Search;
import ca.uhn.fhir.rest.annotation.Sort;
import ca.uhn.fhir.rest.annotation.Update;
import ca.uhn.fhir.rest.api.MethodOutcome;
import ca.uhn.fhir.rest.api.SortSpec;
import ca.uhn.fhir.rest.api.server.IBundleProvider;
import ca.uhn.fhir.rest.param.DateRangeParam;
import ca.uhn.fhir.rest.param.StringAndListParam;
import ca.uhn.fhir.rest.param.TokenAndListParam;
import ca.uhn.fhir.rest.server.IResourceProvider;
import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
import lombok.AccessLevel;
import lombok.Setter;
import org.apache.commons.collections.CollectionUtils;
import org.hl7.fhir.convertors.conv30_40.RelatedPerson30_40;
import org.hl7.fhir.dstu3.model.IdType;
import org.hl7.fhir.dstu3.model.OperationOutcome;
import org.hl7.fhir.dstu3.model.RelatedPerson;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.openmrs.module.fhir2.api.FhirRelatedPersonService;
import org.openmrs.module.fhir2.api.search.SearchQueryBundleProviderR3Wrapper;
import org.openmrs.module.fhir2.providers.util.FhirProviderUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -63,6 +71,34 @@ public RelatedPerson getRelatedPersonById(@IdParam @Nonnull IdType id) {
return RelatedPerson30_40.convertRelatedPerson(relatedPerson);
}

@Create
public MethodOutcome createRelatedPerson(@ResourceParam RelatedPerson relatedPerson) {
return FhirProviderUtils.buildCreate(RelatedPerson30_40
.convertRelatedPerson(relatedPersonService.create(RelatedPerson30_40.convertRelatedPerson(relatedPerson))));
}

@Update
public MethodOutcome updateRelatedPerson(@IdParam IdType id, @ResourceParam RelatedPerson relatedPerson) {
if (id == null || id.getIdPart() == null) {
throw new InvalidRequestException("id must be specified to update");
ibacher marked this conversation as resolved.
Show resolved Hide resolved
}

relatedPerson.setId(id.getIdPart());

return FhirProviderUtils.buildUpdate(RelatedPerson30_40.convertRelatedPerson(
relatedPersonService.update(id.getIdPart(), RelatedPerson30_40.convertRelatedPerson(relatedPerson))));
}

@Delete
public OperationOutcome deleteRelatedPerson(@IdParam @Nonnull IdType id) {
org.hl7.fhir.r4.model.RelatedPerson relatedPerson = relatedPersonService.delete(id.getIdPart());
if (relatedPerson == null) {
throw new ResourceNotFoundException("Could not find person to delete with id " + id.getIdPart());
}

return FhirProviderUtils.buildDelete(RelatedPerson30_40.convertRelatedPerson(relatedPerson));
}

@Search
@SuppressWarnings("unused")
public IBundleProvider searchRelatedPerson(@OptionalParam(name = RelatedPerson.SP_NAME) StringAndListParam name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,34 @@
import java.util.HashSet;

import ca.uhn.fhir.model.api.Include;
import ca.uhn.fhir.rest.annotation.Create;
import ca.uhn.fhir.rest.annotation.Delete;
import ca.uhn.fhir.rest.annotation.IdParam;
import ca.uhn.fhir.rest.annotation.IncludeParam;
import ca.uhn.fhir.rest.annotation.OptionalParam;
import ca.uhn.fhir.rest.annotation.Read;
import ca.uhn.fhir.rest.annotation.ResourceParam;
import ca.uhn.fhir.rest.annotation.Search;
import ca.uhn.fhir.rest.annotation.Sort;
import ca.uhn.fhir.rest.annotation.Update;
import ca.uhn.fhir.rest.api.MethodOutcome;
import ca.uhn.fhir.rest.api.SortSpec;
import ca.uhn.fhir.rest.api.server.IBundleProvider;
import ca.uhn.fhir.rest.param.DateRangeParam;
import ca.uhn.fhir.rest.param.StringAndListParam;
import ca.uhn.fhir.rest.param.TokenAndListParam;
import ca.uhn.fhir.rest.server.IResourceProvider;
import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
import lombok.AccessLevel;
import lombok.Setter;
import org.apache.commons.collections.CollectionUtils;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.r4.model.IdType;
import org.hl7.fhir.r4.model.OperationOutcome;
import org.hl7.fhir.r4.model.RelatedPerson;
import org.openmrs.module.fhir2.api.FhirRelatedPersonService;
import org.openmrs.module.fhir2.providers.util.FhirProviderUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
Expand All @@ -60,6 +68,32 @@ public RelatedPerson getRelatedPersonById(@IdParam @Nonnull IdType id) {
return relatedPerson;
}

@Create
public MethodOutcome createRelatedPerson(@ResourceParam RelatedPerson relatedPerson) {
return FhirProviderUtils.buildCreate(relatedPersonService.create(relatedPerson));
}

@Update
public MethodOutcome updateRelatedPerson(@IdParam IdType id, @ResourceParam RelatedPerson relatedPerson) {
if (id == null || id.getIdPart() == null) {
throw new InvalidRequestException("id must be specified to update");
}

relatedPerson.setId(id.getIdPart());

return FhirProviderUtils.buildUpdate(relatedPersonService.update(id.getIdPart(), relatedPerson));
}

@Delete
public OperationOutcome deleteRelatedPerson(@IdParam @Nonnull IdType id) {
RelatedPerson relatedPerson = relatedPersonService.delete(id.getIdPart());
if (relatedPerson == null) {
throw new ResourceNotFoundException("Could not find person to delete with id " + id.getIdPart());
}

return FhirProviderUtils.buildDelete(relatedPerson);
}

@Search
@SuppressWarnings("unused")
public IBundleProvider searchRelatedPerson(@OptionalParam(name = RelatedPerson.SP_NAME) StringAndListParam name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,4 +321,150 @@ public void shouldTranslateToFHIRBirthdate() {
assertThat(result.getBirthDateElement().getYear(), equalTo(dateType.getYear()));
assertThat(result.getBirthDateElement().getMonth(), equalTo(dateType.getMonth()));
}

@Test
public void shouldTranslateRelatedPersonToRelationship() {
RelatedPerson relatedPerson = new RelatedPerson();

Relationship result = relatedPersonTranslator.toOpenmrsType(relatedPerson);

assertThat(result, notNullValue());
}

@Test
public void shouldTranslateRelatedPersonIdToRelationshipUuid() {
RelatedPerson relatedPerson = new RelatedPerson();
relatedPerson.setId(RELATIONSHIP_UUID);

Relationship result = relatedPersonTranslator.toOpenmrsType(relatedPerson);

assertThat(result, notNullValue());
assertThat(result.getUuid(), equalTo(RELATIONSHIP_UUID));
}

@Test
public void shouldTranslateFhirNameToOpenmrsName() {
PersonName personName = new PersonName();
personName.setGivenName(PERSON_GIVEN_NAME);
personName.setFamilyName(PERSON_FAMILY_NAME);

RelatedPerson relatedPerson = new RelatedPerson();
HumanName name = relatedPerson.addName();
name.addGiven(PERSON_GIVEN_NAME);
name.setFamily(PERSON_FAMILY_NAME);
when(nameTranslator.toOpenmrsType(name)).thenReturn(personName);

Relationship result = relatedPersonTranslator.toOpenmrsType(relatedPerson);

assertThat(result, notNullValue());
assertThat(result.getPersonA().getGivenName(), equalTo(PERSON_GIVEN_NAME));
assertThat(result.getPersonA().getFamilyName(), equalTo(PERSON_FAMILY_NAME));
}

@Test
public void shouldTranslateFhirAddressToOpenmrsAddress() {
PersonAddress personAddress = new PersonAddress();
personAddress.setUuid(ADDRESS_UUID);
personAddress.setCityVillage(ADDRESS_CITY);

RelatedPerson relatedPerson = new RelatedPerson();
Address address = relatedPerson.addAddress();
address.setId(ADDRESS_UUID);
address.setCity(ADDRESS_CITY);
when(addressTranslator.toOpenmrsType(address)).thenReturn(personAddress);

Relationship result = relatedPersonTranslator.toOpenmrsType(relatedPerson);

assertThat(result, notNullValue());
assertThat(result.getPersonA().getPersonAddress(), notNullValue());
assertThat(result.getPersonA().getPersonAddress().getUuid(), equalTo(ADDRESS_UUID));
assertThat(result.getPersonA().getPersonAddress().getCityVillage(), equalTo(ADDRESS_CITY));

}

@Test
public void shouldTranslateFhirGenderToOpenmrsGender() {
RelatedPerson relatedPerson = new RelatedPerson();
relatedPerson.setGender(Enumerations.AdministrativeGender.MALE);
when(genderTranslator.toOpenmrsType(Enumerations.AdministrativeGender.MALE)).thenReturn("M");

Relationship result = relatedPersonTranslator.toOpenmrsType(relatedPerson);

assertThat(result, notNullValue());
assertThat(result.getPersonA().getGender(), equalTo("M"));
}

@Test
public void shouldTranslateToOpenmrsBirthdate() {
RelatedPerson relatedPerson = new RelatedPerson();
Date date = new Date();
DateType dateType = new DateType();

// for TemporalPrecisionEnum.DAY
dateType.setValue(date, TemporalPrecisionEnum.DAY);
relatedPerson.setBirthDateElement(dateType);

Relationship result = relatedPersonTranslator.toOpenmrsType(relatedPerson);

assertThat(result, notNullValue());
assertThat(result.getPersonA().getBirthdateEstimated(), equalTo(false));
assertThat(result.getPersonA().getBirthdate(), equalTo(date));

// for TemporalPrecisionEnum.Month
dateType.setValue(date, TemporalPrecisionEnum.MONTH);
relatedPerson.setBirthDateElement(dateType);

result = relatedPersonTranslator.toOpenmrsType(relatedPerson);

assertThat(result, notNullValue());
assertThat(result.getPersonA().getBirthdateEstimated(), equalTo(true));
assertThat(result.getPersonA().getBirthdate(), equalTo(date));

// for TemporalPrecisionEnum.YEAR
dateType.setValue(date, TemporalPrecisionEnum.YEAR);
relatedPerson.setBirthDateElement(dateType);

result = relatedPersonTranslator.toOpenmrsType(relatedPerson);

assertThat(result, notNullValue());
assertThat(result.getPersonA().getBirthdateEstimated(), equalTo(true));
assertThat(result.getPersonA().getBirthdate(), equalTo(date));
}

@Test
public void shouldTranslateFhirStartDateAndEndDateToOpenmrsStartDateAndEndDate() {
RelatedPerson relatedPerson = new RelatedPerson();
Calendar calendar = Calendar.getInstance();
calendar.set(2000, Calendar.APRIL, 16);
relatedPerson.getPeriod().setStart(calendar.getTime());

Relationship result = relatedPersonTranslator.toOpenmrsType(relatedPerson);

assertThat(result, notNullValue());
assertThat(result.getStartDate().getTime(), equalTo(calendar.getTime().getTime()));

calendar.set(2000, Calendar.SEPTEMBER, 29);
relatedPerson.getPeriod().setEnd(calendar.getTime());

result = relatedPersonTranslator.toOpenmrsType(relatedPerson);

assertThat(result, notNullValue());
assertThat(result.getEndDate().getTime(), equalTo(calendar.getTime().getTime()));
}

@Test
public void shouldTranslateFhirPatientToOpenmrsPersonB() {
Patient patient = new Patient();
patient.setUuid(PATIENT_UUID);
RelatedPerson relatedPerson = new RelatedPerson();
Reference reference = new Reference();
relatedPerson.setPatient(reference);

when(patientReferenceTranslator.toOpenmrsType(relatedPerson.getPatient())).thenReturn(patient);

Relationship result = relatedPersonTranslator.toOpenmrsType(relatedPerson);

assertThat(result, notNullValue());
assertThat(result.getPersonB().getUuid(), equalTo(PATIENT_UUID));
}
}
Loading