use of org.hl7.fhir.r4.model.UrlType 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.r4.model.UrlType in project pathling by aehrc.
the class ImportExecutor method readStringsFromUrl.
@Nonnull
private Dataset<String> readStringsFromUrl(@Nonnull final ParametersParameterComponent urlParam) {
final String url = ((UrlType) urlParam.getValue()).getValueAsString();
final String decodedUrl = URLDecoder.decode(url, StandardCharsets.UTF_8);
final String convertedUrl = PersistenceScheme.convertS3ToS3aUrl(decodedUrl);
final Dataset<String> jsonStrings;
try {
// Check that the user is authorized to execute the operation.
accessRules.ifPresent(ar -> ar.checkCanImportFrom(convertedUrl));
final FilterFunction<String> nonBlanks = s -> !s.isBlank();
jsonStrings = spark.read().textFile(convertedUrl).filter(nonBlanks);
} catch (final SecurityError e) {
throw new InvalidUserInputError("Not allowed to import from URL: " + convertedUrl, e);
} catch (final Exception e) {
throw new InvalidUserInputError("Error reading from URL: " + convertedUrl, e);
}
return jsonStrings;
}
use of org.hl7.fhir.r4.model.UrlType in project pathling by aehrc.
the class ImportTest method buildImportParameters.
@SuppressWarnings("SameParameterValue")
@Nonnull
Parameters buildImportParameters(@Nonnull final URL jsonURL, @Nonnull final ResourceType resourceType) {
final Parameters parameters = new Parameters();
final ParametersParameterComponent sourceParam = parameters.addParameter().setName("source");
sourceParam.addPart().setName("resourceType").setValue(new CodeType(resourceType.toCode()));
sourceParam.addPart().setName("url").setValue(new UrlType(jsonURL.toExternalForm()));
return parameters;
}
use of org.hl7.fhir.r4.model.UrlType in project pathling by aehrc.
the class ExtractTest method extract.
@Test
void extract() throws URISyntaxException, MalformedURLException {
TestHelpers.mockResource(database, spark, ResourceType.DIAGNOSTICREPORT);
final String uri = "http://localhost:" + port + "/fhir/DiagnosticReport/$extract?column=id";
final ResponseEntity<String> response = restTemplate.exchange(uri, HttpMethod.GET, RequestEntity.get(new URI(uri)).build(), String.class);
assertTrue(response.getStatusCode().is2xxSuccessful());
final Parameters result = (Parameters) jsonParser.parseResource(response.getBody());
final URL url = new URL(((UrlType) result.getParameter("url")).getValueAsString());
final String actual;
try {
final InputStream expectedStream = url.openStream();
final StringWriter writer = new StringWriter();
IOUtils.copy(expectedStream, writer, UTF_8);
actual = writer.toString();
} catch (final IOException e) {
throw new RuntimeException("Problem retrieving extract result", e);
}
final String expected = getResourceAsString("responses/ExtractTest/extract.csv");
assertEquals(expected, actual);
}
use of org.hl7.fhir.r4.model.UrlType in project kindling by HL7.
the class OldSpreadsheetParser method processValue.
private DataType processValue(Sheet sheet, int row, String column, String source, ElementDefn e) throws Exception {
if (Utilities.noString(source))
return null;
if (e.getTypes().size() != 1)
throw new Exception("Unable to process " + column + " unless a single type is specified (types = " + e.typeCode() + ") " + getLocation(row) + ", column = " + column);
String type = e.typeCode();
if (definitions != null) {
if (definitions.getConstraints().containsKey(type))
type = definitions.getConstraints().get(type).getBaseType();
} else {
StructureDefinition sd = context.fetchTypeDefinition(type);
if (// not loaded yet?
sd != null)
type = sd.getType();
if (type.equals("SimpleQuantity"))
type = "Quantity";
}
if (source.startsWith("{")) {
JsonParser json = new JsonParser();
return json.parseType(source, type);
} else if (source.startsWith("<")) {
XmlParser xml = new XmlParser();
return xml.parseType(source, type);
} else {
if (source.startsWith("\"") && source.endsWith("\""))
source = source.substring(1, source.length() - 1);
if (type.equals("string"))
return new StringType(source);
if (type.equals("boolean"))
return new BooleanType(Boolean.valueOf(source));
if (type.equals("integer"))
return new IntegerType(Integer.valueOf(source));
if (type.equals("integer64"))
return new Integer64Type(Long.valueOf(source));
if (type.equals("unsignedInt"))
return new UnsignedIntType(Integer.valueOf(source));
if (type.equals("positiveInt"))
return new PositiveIntType(Integer.valueOf(source));
if (type.equals("decimal"))
return new DecimalType(new BigDecimal(source));
if (type.equals("base64Binary"))
return new Base64BinaryType(Base64.decode(source.toCharArray()));
if (type.equals("instant"))
return new InstantType(source);
if (type.equals("uri"))
return new UriType(source);
if (type.equals("url"))
return new UrlType(source);
if (type.equals("canonical"))
return new CanonicalType(source);
if (type.equals("date"))
return new DateType(source);
if (type.equals("dateTime"))
return new DateTimeType(source);
if (type.equals("time"))
return new TimeType(source);
if (type.equals("code"))
return new CodeType(source);
if (type.equals("oid"))
return new OidType(source);
if (type.equals("uuid"))
return new UuidType(source);
if (type.equals("id"))
return new IdType(source);
if (type.startsWith("Reference(")) {
Reference r = new Reference();
r.setReference(source);
return r;
}
if (type.equals("Period")) {
if (source.contains("->")) {
String[] parts = source.split("\\-\\>");
Period p = new Period();
p.setStartElement(new DateTimeType(parts[0].trim()));
if (parts.length > 1)
p.setEndElement(new DateTimeType(parts[1].trim()));
return p;
} else
throw new Exception("format not understood parsing " + source + " into a period");
}
if (type.equals("CodeableConcept")) {
CodeableConcept cc = new CodeableConcept();
if (source.contains(":")) {
String[] parts = source.split("\\:");
String system = "";
if (parts[0].equalsIgnoreCase("SCT"))
system = "http://snomed.info/sct";
else if (parts[0].equalsIgnoreCase("LOINC"))
system = "http://loinc.org";
else if (parts[0].equalsIgnoreCase("AMTv2"))
system = "http://nehta.gov.au/amtv2";
else
system = "http://hl7.org/fhir/" + parts[0];
String code = parts[1];
String display = parts.length > 2 ? parts[2] : null;
cc.addCoding().setSystem(system).setCode(code).setDisplay(display);
} else
throw new Exception("format not understood parsing " + source + " into a codeable concept");
return cc;
}
if (type.equals("Identifier")) {
Identifier id = new Identifier();
id.setSystem("urn:ietf:rfc:3986");
id.setValue(source);
return id;
}
if (type.equals("Quantity")) {
int s = 0;
if (source.startsWith("<=") || source.startsWith("=>"))
s = 2;
else if (source.startsWith("<") || source.startsWith(">"))
s = 1;
int i = s;
while (i < source.length() && Character.isDigit(source.charAt(i))) i++;
Quantity q = new Quantity();
if (s > 0)
q.setComparator(QuantityComparator.fromCode(source.substring(0, s)));
if (i > s)
q.setValue(new BigDecimal(source.substring(s, i)));
if (i < source.length())
q.setUnit(source.substring(i).trim());
return q;
}
throw new Exception("Unable to process primitive value '" + source + "' provided for " + column + " - unhandled type " + type + " @ " + getLocation(row));
}
}
Aggregations