Co-authored-by: Christian Ohr <christian.ohr@icw.de>
This commit is contained in:
parent
05735900fd
commit
1c27726055
|
@ -1067,9 +1067,19 @@ class ParserState<T> {
|
||||||
* At some point it would be good to write code which can present a view
|
* At some point it would be good to write code which can present a view
|
||||||
* of one type backed by another type and use that.
|
* of one type backed by another type and use that.
|
||||||
*/
|
*/
|
||||||
|
FhirTerser t = myContext.newTerser();
|
||||||
|
|
||||||
|
// Clean up the cached resources
|
||||||
|
myGlobalResources.remove(myInstance);
|
||||||
|
myGlobalReferences.removeAll(t.getAllPopulatedChildElementsOfType(myInstance, IBaseReference.class));
|
||||||
|
|
||||||
IParser parser = myContext.newJsonParser();
|
IParser parser = myContext.newJsonParser();
|
||||||
String asString = parser.encodeResourceToString(myInstance);
|
String asString = parser.encodeResourceToString(myInstance);
|
||||||
myInstance = parser.parseResource(wantedProfileType, asString);
|
myInstance = parser.parseResource(wantedProfileType, asString);
|
||||||
|
|
||||||
|
// Add newly created instance
|
||||||
|
myGlobalResources.add(myInstance);
|
||||||
|
myGlobalReferences.addAll(t.getAllPopulatedChildElementsOfType(myInstance, IBaseReference.class));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,31 @@
|
||||||
|
package ca.uhn.fhir.parser;
|
||||||
|
|
||||||
|
import ca.uhn.fhir.model.api.annotation.Child;
|
||||||
|
import ca.uhn.fhir.model.api.annotation.Extension;
|
||||||
|
import ca.uhn.fhir.model.api.annotation.ResourceDef;
|
||||||
|
import org.hl7.fhir.dstu3.model.CodeType;
|
||||||
|
import org.hl7.fhir.dstu3.model.Patient;
|
||||||
|
|
||||||
|
|
||||||
|
@ResourceDef(
|
||||||
|
name = "Patient",
|
||||||
|
profile = "http://acme.org//StructureDefinition/patient-with-eyes"
|
||||||
|
)
|
||||||
|
public class ExtendedPatient extends Patient {
|
||||||
|
|
||||||
|
@Child(name = "eyeColour")
|
||||||
|
@Extension(url = "http://acme.org/#extpt", definedLocally = false, isModifier = false)
|
||||||
|
private CodeType myEyeColour;
|
||||||
|
|
||||||
|
public CodeType getEyeColour() {
|
||||||
|
if (myEyeColour == null) {
|
||||||
|
myEyeColour = new CodeType();
|
||||||
|
}
|
||||||
|
return myEyeColour;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEyeColour(CodeType theEyeColour) {
|
||||||
|
myEyeColour = theEyeColour;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,56 @@
|
||||||
|
package ca.uhn.fhir.parser;
|
||||||
|
|
||||||
|
import ca.uhn.fhir.context.FhirContext;
|
||||||
|
import org.hl7.fhir.dstu3.model.Bundle;
|
||||||
|
import org.hl7.fhir.dstu3.model.CodeType;
|
||||||
|
import org.hl7.fhir.dstu3.model.Patient;
|
||||||
|
import org.hl7.fhir.dstu3.model.Reference;
|
||||||
|
import org.hl7.fhir.instance.model.api.IBaseResource;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertSame;
|
||||||
|
|
||||||
|
public class ExtendedPatientTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBundleReferences() {
|
||||||
|
|
||||||
|
FhirContext fhirContext = FhirContext.forDstu3();
|
||||||
|
fhirContext.setDefaultTypeForProfile("http://acme.org//StructureDefinition/patient-with-eyes", ExtendedPatient.class);
|
||||||
|
|
||||||
|
ExtendedPatient homer = new ExtendedPatient();
|
||||||
|
homer.setId("homer");
|
||||||
|
homer.addName().setFamily("Simpson").addGiven("Homer");
|
||||||
|
|
||||||
|
ExtendedPatient marge = new ExtendedPatient();
|
||||||
|
marge.setId("marge");
|
||||||
|
marge.addName().setFamily("Simpson").addGiven("Marge");
|
||||||
|
marge.setEyeColour(new CodeType("blue"));
|
||||||
|
marge.getLink().add(new Patient.PatientLinkComponent()
|
||||||
|
.setType(Patient.LinkType.REFER)
|
||||||
|
.setOther(new Reference("Patient/homer")));
|
||||||
|
|
||||||
|
Bundle bundle = new Bundle()
|
||||||
|
.addEntry(new Bundle.BundleEntryComponent()
|
||||||
|
.setFullUrl("http://acme.org/Patient/homer").setResource(homer)
|
||||||
|
.setSearch(new Bundle.BundleEntrySearchComponent()
|
||||||
|
.setMode(Bundle.SearchEntryMode.INCLUDE)))
|
||||||
|
.addEntry(new Bundle.BundleEntryComponent()
|
||||||
|
.setFullUrl("http://acme.org/Patient/marge").setResource(marge)
|
||||||
|
.setSearch(new Bundle.BundleEntrySearchComponent()));
|
||||||
|
|
||||||
|
IParser p = fhirContext.newXmlParser().setPrettyPrint(true);
|
||||||
|
String encoded = p.encodeResourceToString(bundle);
|
||||||
|
|
||||||
|
Bundle parsedBundle = p.parseResource(Bundle.class, encoded);
|
||||||
|
|
||||||
|
ExtendedPatient parsedHomer = (ExtendedPatient)parsedBundle.getEntry().get(0).getResource();
|
||||||
|
ExtendedPatient parsedMarge = (ExtendedPatient)parsedBundle.getEntry().get(1).getResource();
|
||||||
|
|
||||||
|
IBaseResource referencedHomer = parsedMarge.getLinkFirstRep().getOther().getResource();
|
||||||
|
assertNotNull(referencedHomer);
|
||||||
|
assertSame(parsedHomer, referencedHomer);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,31 @@
|
||||||
|
package ca.uhn.fhir.parser;
|
||||||
|
|
||||||
|
import ca.uhn.fhir.model.api.annotation.Child;
|
||||||
|
import ca.uhn.fhir.model.api.annotation.Extension;
|
||||||
|
import ca.uhn.fhir.model.api.annotation.ResourceDef;
|
||||||
|
import org.hl7.fhir.r4.model.CodeType;
|
||||||
|
import org.hl7.fhir.r4.model.Patient;
|
||||||
|
|
||||||
|
|
||||||
|
@ResourceDef(
|
||||||
|
name = "Patient",
|
||||||
|
profile = "http://acme.org//StructureDefinition/patient-with-eyes"
|
||||||
|
)
|
||||||
|
public class ExtendedPatient extends Patient {
|
||||||
|
|
||||||
|
@Child(name = "eyeColour")
|
||||||
|
@Extension(url = "http://acme.org/#extpt", definedLocally = false, isModifier = false)
|
||||||
|
private CodeType myEyeColour;
|
||||||
|
|
||||||
|
public CodeType getEyeColour() {
|
||||||
|
if (myEyeColour == null) {
|
||||||
|
myEyeColour = new CodeType();
|
||||||
|
}
|
||||||
|
return myEyeColour;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEyeColour(CodeType theEyeColour) {
|
||||||
|
myEyeColour = theEyeColour;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,56 @@
|
||||||
|
package ca.uhn.fhir.parser;
|
||||||
|
|
||||||
|
import ca.uhn.fhir.context.FhirContext;
|
||||||
|
import org.hl7.fhir.r4.model.Bundle;
|
||||||
|
import org.hl7.fhir.r4.model.CodeType;
|
||||||
|
import org.hl7.fhir.r4.model.Patient;
|
||||||
|
import org.hl7.fhir.r4.model.Reference;
|
||||||
|
import org.hl7.fhir.instance.model.api.IBaseResource;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertSame;
|
||||||
|
|
||||||
|
public class ExtendedPatientTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBundleReferences() {
|
||||||
|
|
||||||
|
FhirContext fhirContext = FhirContext.forR4();
|
||||||
|
fhirContext.setDefaultTypeForProfile("http://acme.org//StructureDefinition/patient-with-eyes", ExtendedPatient.class);
|
||||||
|
|
||||||
|
ExtendedPatient homer = new ExtendedPatient();
|
||||||
|
homer.setId("homer");
|
||||||
|
homer.addName().setFamily("Simpson").addGiven("Homer");
|
||||||
|
|
||||||
|
ExtendedPatient marge = new ExtendedPatient();
|
||||||
|
marge.setId("marge");
|
||||||
|
marge.addName().setFamily("Simpson").addGiven("Marge");
|
||||||
|
marge.setEyeColour(new CodeType("blue"));
|
||||||
|
marge.getLink().add(new Patient.PatientLinkComponent()
|
||||||
|
.setType(Patient.LinkType.REFER)
|
||||||
|
.setOther(new Reference("Patient/homer")));
|
||||||
|
|
||||||
|
Bundle bundle = new Bundle()
|
||||||
|
.addEntry(new Bundle.BundleEntryComponent()
|
||||||
|
.setFullUrl("http://acme.org/Patient/homer").setResource(homer)
|
||||||
|
.setSearch(new Bundle.BundleEntrySearchComponent()
|
||||||
|
.setMode(Bundle.SearchEntryMode.INCLUDE)))
|
||||||
|
.addEntry(new Bundle.BundleEntryComponent()
|
||||||
|
.setFullUrl("http://acme.org/Patient/marge").setResource(marge)
|
||||||
|
.setSearch(new Bundle.BundleEntrySearchComponent()));
|
||||||
|
|
||||||
|
IParser p = fhirContext.newXmlParser().setPrettyPrint(true);
|
||||||
|
String encoded = p.encodeResourceToString(bundle);
|
||||||
|
|
||||||
|
Bundle parsedBundle = p.parseResource(Bundle.class, encoded);
|
||||||
|
|
||||||
|
ExtendedPatient parsedHomer = (ExtendedPatient)parsedBundle.getEntry().get(0).getResource();
|
||||||
|
ExtendedPatient parsedMarge = (ExtendedPatient)parsedBundle.getEntry().get(1).getResource();
|
||||||
|
|
||||||
|
IBaseResource referencedHomer = parsedMarge.getLinkFirstRep().getOther().getResource();
|
||||||
|
assertNotNull(referencedHomer);
|
||||||
|
assertSame(parsedHomer, referencedHomer);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue