use of org.hl7.fhir.utilities.graphql.Package in project redmatch by aehrc.
the class FhirExporter method getValue.
/**
* Resolves a value.
*
* @param value The value specified in the transformation rules.
* @param fhirType The type of the FHIR attribute where this value will be set.
* @param vertex A vertex with patient data.
* @param recordId The id of this record. Used to create the references to FHIR ids.
* @param enumFactory If the type is an enumeration, this is the factory to create an instance.
* @param fhirPackage The target FHIR package.
* @return The value or null if the value cannot be determined. This can also be a list.
*/
private Base getValue(Value value, Class<?> fhirType, JsonObject vertex, String recordId, Class<?> enumFactory, VersionedFhirPackage fhirPackage) throws IOException {
// If this is a field-based value then make sure that there is a value and if not return null
if (value instanceof FieldBasedValue) {
FieldBasedValue fbv = (FieldBasedValue) value;
// Account for field ids of the form xx___y
String fieldId = fbv.getFieldId();
String shortFieldId = null;
String regex = "(?<fieldId>.*)___\\d+$";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(fieldId);
if (matcher.find()) {
shortFieldId = matcher.group("fieldId");
log.debug("Transformed fieldId into '" + fieldId + "'");
}
boolean hasValue = false;
JsonElement jsonElement = vertex.get(fieldId);
if (jsonElement != null) {
String rawValue = jsonElement.getAsString();
if (!rawValue.isEmpty()) {
hasValue = true;
}
}
if (!hasValue && shortFieldId != null) {
jsonElement = vertex.get(shortFieldId);
if (jsonElement != null) {
String rawValue = jsonElement.getAsString();
if (!rawValue.isEmpty()) {
hasValue = true;
}
}
}
if (!hasValue) {
return null;
}
}
if (value instanceof BooleanValue) {
return new BooleanType(((BooleanValue) value).getValue());
} else if (value instanceof CodeLiteralValue) {
String code = ((CodeLiteralValue) value).getCode();
return getCode(code, enumFactory);
} else if (value instanceof ConceptLiteralValue) {
ConceptLiteralValue clv = (ConceptLiteralValue) value;
String system = clv.getSystem();
String code = clv.getCode();
String display = clv.getDisplay() != null ? clv.getDisplay() : "";
return getConcept(system, code, display, fhirType);
} else if (value instanceof DoubleValue) {
return new DecimalType(((DoubleValue) value).getValue());
} else if (value instanceof IntegerValue) {
return new IntegerType(((IntegerValue) value).getValue());
} else if (value instanceof ReferenceValue) {
ReferenceValue rv = (ReferenceValue) value;
Reference ref = new Reference();
String resourceType = rv.getResourceType();
String resourceId = rv.getResourceId();
boolean unique = uniqueIds.contains(resourceType + "<" + resourceId + ">");
CodeInfo codeInfo = terminologyService.lookup(fhirPackage, resourceType);
if (codeInfo.isProfile()) {
resourceType = StringUtils.getLastPath(codeInfo.getBaseResource());
}
if (unique) {
// This is a reference to a unique resource - no need to append row id
if (fhirResourceMap.containsKey(resourceId)) {
ref.setReference("/" + resourceType + "/" + resourceId);
} else {
log.debug("Did not find resource " + resourceType + "/" + resourceId);
}
} else {
if (fhirResourceMap.containsKey(resourceId + "-" + recordId)) {
ref.setReference("/" + resourceType + "/" + resourceId + "-" + recordId);
} else {
log.debug("Did not find resource " + resourceType + "/" + resourceId + "-" + recordId);
}
}
return ref;
} else if (value instanceof StringValue) {
if (fhirType.equals(StringType.class)) {
return new StringType(((StringValue) value).getStringValue());
} else if (fhirType.equals(MarkdownType.class)) {
return new MarkdownType(((StringValue) value).getStringValue());
} else if (fhirType.equals(IdType.class)) {
return new IdType(((StringValue) value).getStringValue());
} else if (fhirType.equals(UriType.class)) {
return new UriType(((StringValue) value).getStringValue());
} else if (fhirType.equals(OidType.class)) {
return new OidType(((StringValue) value).getStringValue());
} else if (fhirType.equals(UuidType.class)) {
return new UuidType(((StringValue) value).getStringValue());
} else if (fhirType.equals(CanonicalType.class)) {
return new CanonicalType(((StringValue) value).getStringValue());
} else if (fhirType.equals(UrlType.class)) {
return new UrlType(((StringValue) value).getStringValue());
} else {
throw new TransformationException("Got StringValue for FHIR type " + fhirType.getName() + ". This should not happen!");
}
} else if (value instanceof CodeSelectedValue) {
CodeSelectedValue csv = (CodeSelectedValue) value;
String fieldId = csv.getFieldId();
Mapping m = getSelectedMapping(fieldId, vertex);
if (m == null) {
throw new TransformationException("Mapping for field " + fieldId + " is required but was not found.");
}
return getTarget(m).getCodeElement();
} else if (value instanceof ConceptSelectedValue) {
ConceptSelectedValue csv = (ConceptSelectedValue) value;
String fieldId = csv.getFieldId();
Mapping m = getSelectedMapping(fieldId, vertex);
if (m == null) {
throw new TransformationException("Mapping for field " + fieldId + " is required but was not found.");
}
if (fhirType.isAssignableFrom(Coding.class)) {
return getTarget(m);
} else if (fhirType.isAssignableFrom(CodeableConcept.class)) {
return new CodeableConcept().addCoding(getTarget(m));
} else {
throw new TransformationException("FHIR type of field " + fieldId + " (" + fhirType + ") is incompatible with CONCEPT_SELECTED.");
}
} else if (value instanceof ConceptValue) {
// Ontoserver REDCap plugin format: 74400008|Appendicitis|http://snomed.info/sct
ConceptValue cv = (ConceptValue) value;
String fieldId = cv.getFieldId();
Mapping m = getMapping(fieldId);
if (m != null) {
if (fhirType.isAssignableFrom(Coding.class)) {
return getTarget(m);
} else if (fhirType.isAssignableFrom(CodeableConcept.class)) {
return new CodeableConcept().addCoding(getTarget(m));
} else {
throw new TransformationException("FHIR type of field " + fieldId + " (" + fhirType + ") is incompatible with CONCEPT.");
}
} else {
au.csiro.redmatch.model.Field field = doc.getSchema().getField(fieldId);
Coding c = field.getCoding(vertex);
if (c != null) {
if (fhirType.isAssignableFrom(Coding.class)) {
return c;
} else if (fhirType.isAssignableFrom(CodeableConcept.class)) {
return new CodeableConcept().addCoding(c);
} else {
throw new TransformationException("FHIR type of field " + fieldId + " (" + fhirType + ") is incompatible with CONCEPT.");
}
}
}
throw new TransformationException("Could not get concept for field " + fieldId + ".");
} else if (value instanceof FieldValue) {
FieldValue fv = (FieldValue) value;
String fieldId = fv.getFieldId();
FieldValue.DatePrecision pr = fv.getDatePrecision();
au.csiro.redmatch.model.Field field = doc.getSchema().getField(fieldId);
return field.getValue(vertex, fhirType, pr);
} else {
throw new TransformationException("Unable to get VALUE for " + value);
}
}
use of org.hl7.fhir.utilities.graphql.Package in project bunsen by cerner.
the class AvroConverterTest method testCompile.
@Test
public void testCompile() throws IOException {
List<Schema> schemas = AvroConverter.generateSchemas(FhirContexts.forStu3(), ImmutableMap.of(TestData.US_CORE_PATIENT, Collections.emptyList(), TestData.VALUE_SET, Collections.emptyList(), TestData.US_CORE_MEDICATION_REQUEST, ImmutableList.of(TestData.US_CORE_MEDICATION)));
// Wrap the schemas in a protocol to simplify the invocation of the compiler.
Protocol protocol = new Protocol("fhir-test", "FHIR Resources for Testing", null);
protocol.setTypes(schemas);
SpecificCompiler compiler = new SpecificCompiler(protocol);
Path generatedCodePath = Files.createTempDirectory("generated_code");
generatedCodePath.toFile().deleteOnExit();
compiler.compileToDestination(null, generatedCodePath.toFile());
// Check that java files were created as expected.
Set<String> javaFiles = Files.find(generatedCodePath, 10, (path, basicFileAttributes) -> true).map(path -> generatedCodePath.relativize(path)).map(Object::toString).collect(Collectors.toSet());
// Ensure common types were generated
Assert.assertTrue(javaFiles.contains("com/cerner/bunsen/stu3/avro/Period.java"));
Assert.assertTrue(javaFiles.contains("com/cerner/bunsen/stu3/avro/Coding.java"));
Assert.assertTrue(javaFiles.contains("com/cerner/bunsen/stu3/avro/ValueSet.java"));
// The specific profile should be created in the expected sub-package.
Assert.assertTrue(javaFiles.contains("com/cerner/bunsen/stu3/avro/us/core/Patient.java"));
// Check extension types.
Assert.assertTrue(javaFiles.contains("com/cerner/bunsen/stu3/avro/us/core/UsCoreRace.java"));
// Choice types include each choice that could be used.
Assert.assertTrue(javaFiles.contains("com/cerner/bunsen/stu3/avro/ChoiceBooleanInteger.java"));
// Contained types created.
Assert.assertTrue(javaFiles.contains("com/cerner/bunsen/stu3/avro/us/core/MedicationRequestContained.java"));
}
use of org.hl7.fhir.utilities.graphql.Package in project beneficiary-fhir-data by CMSgov.
the class SamhsaMatcherR4FromClaimTransformerV2Test method verifySamhsaMatcherForDiagnosisPackage.
/**
* Verify SAMHSA matcher for package with the given system, code and if the expectation is that
* there should be a match for this combination.
*
* @param system the system value
* @param code the code
* @param shouldMatch if the matcher should match on this combination
*/
private void verifySamhsaMatcherForDiagnosisPackage(String system, String code, boolean shouldMatch, ExplanationOfBenefit explanationOfBenefit) {
ExplanationOfBenefit modifiedEob = explanationOfBenefit.copy();
// Set diagnosis DRG
for (ExplanationOfBenefit.DiagnosisComponent diagnosisComponent : modifiedEob.getDiagnosis()) {
diagnosisComponent.getDiagnosisCodeableConcept().setCoding(new ArrayList<>());
CodeableConcept codeableConcept = new CodeableConcept();
Coding coding = new Coding(system, code, null);
codeableConcept.setCoding(Collections.singletonList(coding));
diagnosisComponent.setPackageCode(codeableConcept);
}
// Set procedure to empty so we dont check it for matches
for (ExplanationOfBenefit.ProcedureComponent diagnosisComponent : modifiedEob.getProcedure()) {
CodeableConcept codeableConcept = diagnosisComponent.getProcedureCodeableConcept();
ArrayList<Coding> codingList = new ArrayList<>();
codeableConcept.setCoding(codingList);
}
// Set item coding to non-SAMHSA so we dont check it for matches
List<Coding> codings = new ArrayList<>();
Coding coding = new Coding();
coding.setSystem(TransformerConstants.CODING_SYSTEM_HCPCS);
coding.setCode(NON_SAMHSA_HCPCS_CODE);
codings.add(coding);
modifiedEob.getItem().get(0).getProductOrService().setCoding(codings);
assertEquals(shouldMatch, samhsaMatcherV2.test(modifiedEob));
}
use of org.hl7.fhir.utilities.graphql.Package in project beneficiary-fhir-data by CMSgov.
the class SamhsaMatcherR4FromClaimTransformerV2Test method verifySamhsaMatcherForItemWithSingleCoding.
/**
* Verify SAMHSA matcher for item with the given system, code and if the expectation is that there
* should be a match for this combination.
*
* @param system the system value
* @param code the code
* @param shouldMatch if the matcher should match on this combination
* @param explanationOfBenefit the explanation of benefit
*/
private void verifySamhsaMatcherForItemWithSingleCoding(String system, String code, boolean shouldMatch, ExplanationOfBenefit explanationOfBenefit) {
ExplanationOfBenefit modifiedEob = explanationOfBenefit.copy();
// Set Top level diagnosis and package code to null so we can test item logic
for (ExplanationOfBenefit.DiagnosisComponent diagnosisComponent : modifiedEob.getDiagnosis()) {
CodeableConcept codeableConcept = diagnosisComponent.getDiagnosisCodeableConcept();
codeableConcept.setCoding(new ArrayList<>());
diagnosisComponent.setPackageCode(null);
}
List<Coding> codings = new ArrayList<>();
Coding coding = new Coding();
coding.setSystem(system);
coding.setCode(code);
codings.add(coding);
modifiedEob.getItem().get(0).getProductOrService().setCoding(codings);
assertEquals(shouldMatch, samhsaMatcherV2.test(modifiedEob));
}
use of org.hl7.fhir.utilities.graphql.Package in project beneficiary-fhir-data by CMSgov.
the class SamhsaMatcherFromClaimTransformerTest method verifySamhsaMatcherForItemWithMultiCoding.
/**
* Verify SAMHSA matcher for item with the given system, code and if the expectation is that there
* should be a match for this combination.
*
* @param system the system value of the first coding
* @param code the code of the first coding
* @param system2 the system value of the second coding
* @param code2 the code of the second coding
* @param shouldMatch if the matcher should match on this combination
* @param explanationOfBenefit the explanation of benefit
*/
private void verifySamhsaMatcherForItemWithMultiCoding(String system, String code, String system2, String code2, boolean shouldMatch, ExplanationOfBenefit explanationOfBenefit) {
ExplanationOfBenefit modifiedEob = explanationOfBenefit.copy();
// Set Top level diagnosis and package code to null so we can test item logic
for (ExplanationOfBenefit.DiagnosisComponent diagnosisComponent : modifiedEob.getDiagnosis()) {
CodeableConcept codeableConcept = diagnosisComponent.getDiagnosisCodeableConcept();
codeableConcept.setCoding(new ArrayList<>());
diagnosisComponent.setPackageCode(null);
}
List<Coding> codings = new ArrayList<>();
Coding coding = new Coding();
coding.setSystem(system);
coding.setCode(code);
Coding coding2 = new Coding();
coding2.setSystem(system2);
coding2.setCode(code2);
codings.add(coding);
codings.add(coding2);
modifiedEob.getItem().get(0).getService().setCoding(codings);
assertEquals(shouldMatch, samhsaMatcher.test(modifiedEob));
}
Aggregations