Search in sources :

Example 11 with StringValue

use of org.hl7.fhir.utilities.graphql.StringValue 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);
    }
}
Also used : CodeInfo(au.csiro.redmatch.terminology.CodeInfo) Matcher(java.util.regex.Matcher) Field(java.lang.reflect.Field) org.hl7.fhir.r4.model(org.hl7.fhir.r4.model) Pattern(java.util.regex.Pattern) JsonElement(com.google.gson.JsonElement)

Example 12 with StringValue

use of org.hl7.fhir.utilities.graphql.StringValue in project org.hl7.fhir.core by hapifhir.

the class GraphQLEngine method processValues.

private void processValues(Resource context, Selection sel, Property prop, ObjectValue target, List<Base> values, boolean extensionMode, boolean inheritedList, String suffix) throws EGraphQLException, FHIRException {
    boolean il = false;
    Argument arg = null;
    ExpressionNode expression = null;
    if (sel.getField().hasDirective("slice")) {
        Directive dir = sel.getField().directive("slice");
        String s = ((StringValue) dir.getArguments().get(0).getValues().get(0)).getValue();
        if (s.equals("$index"))
            expression = magicExpression;
        else
            expression = fpe.parse(s);
    }
    if (// special: instruction to drop this node...
    sel.getField().hasDirective("flatten"))
        il = prop.isList() && !sel.getField().hasDirective("first");
    else if (sel.getField().hasDirective("first")) {
        if (expression != null)
            throw new FHIRException("You cannot mix @slice and @first");
        arg = target.addField(sel.getField().getAlias() + suffix, listStatus(sel.getField(), inheritedList));
    } else if (expression == null)
        arg = target.addField(sel.getField().getAlias() + suffix, listStatus(sel.getField(), prop.isList() || inheritedList));
    int index = 0;
    for (Base value : values) {
        String ss = "";
        if (expression != null) {
            if (expression == magicExpression)
                ss = suffix + '.' + Integer.toString(index);
            else
                ss = suffix + '.' + fpe.evaluateToString(null, null, null, value, expression);
            if (!sel.getField().hasDirective("flatten"))
                arg = target.addField(sel.getField().getAlias() + suffix, listStatus(sel.getField(), prop.isList() || inheritedList));
        }
        if (value.isPrimitive() && !extensionMode) {
            if (!sel.getField().getSelectionSet().isEmpty())
                throw new EGraphQLException("Encountered a selection set on a scalar field type");
            processPrimitive(arg, value);
        } else {
            if (sel.getField().getSelectionSet().isEmpty())
                throw new EGraphQLException("No Fields selected on a complex object");
            if (arg == null)
                processObject(context, value, target, sel.getField().getSelectionSet(), il, ss);
            else {
                ObjectValue n = new ObjectValue();
                arg.addValue(n);
                processObject(context, value, n, sel.getField().getSelectionSet(), il, ss);
            }
        }
        if (sel.getField().hasDirective("first"))
            return;
        index++;
    }
}
Also used : ObjectValue(org.hl7.fhir.utilities.graphql.ObjectValue) Argument(org.hl7.fhir.utilities.graphql.Argument) ExpressionNode(org.hl7.fhir.r4b.model.ExpressionNode) StringValue(org.hl7.fhir.utilities.graphql.StringValue) Directive(org.hl7.fhir.utilities.graphql.Directive) FHIRException(org.hl7.fhir.exceptions.FHIRException) EGraphQLException(org.hl7.fhir.utilities.graphql.EGraphQLException) Base(org.hl7.fhir.r4b.model.Base)

Example 13 with StringValue

use of org.hl7.fhir.utilities.graphql.StringValue in project org.hl7.fhir.core by hapifhir.

the class GraphQLEngine method processSearchFull.

private void processSearchFull(ObjectValue target, Field field, boolean inheritedList, String suffix) throws EGraphQLException, FHIRException {
    if (services == null)
        throw new EGraphQLException("Resource Referencing services not provided");
    List<Argument> params = new ArrayList<Argument>();
    Argument carg = null;
    for (Argument arg : field.getArguments()) if (arg.getName().equals("cursor"))
        carg = arg;
    else
        params.add(arg);
    if ((carg != null)) {
        params.clear();
        ;
        String[] parts = getSingleValue(carg).split(":");
        params.add(new Argument("search-id", new StringValue(parts[0])));
        params.add(new Argument("search-offset", new StringValue(parts[1])));
    }
    Bundle bnd = (Bundle) services.search(appInfo, field.getName().substring(0, field.getName().length() - 10), params);
    SearchWrapper bndWrapper = new SearchWrapper(field.getName(), bnd);
    Argument arg = target.addField(field.getAlias() + suffix, listStatus(field, false));
    ObjectValue obj = new ObjectValue();
    arg.addValue(obj);
    processObject(null, bndWrapper, obj, field.getSelectionSet(), inheritedList, suffix);
}
Also used : ObjectValue(org.hl7.fhir.utilities.graphql.ObjectValue) Argument(org.hl7.fhir.utilities.graphql.Argument) StringValue(org.hl7.fhir.utilities.graphql.StringValue) EGraphQLException(org.hl7.fhir.utilities.graphql.EGraphQLException)

Example 14 with StringValue

use of org.hl7.fhir.utilities.graphql.StringValue in project org.hl7.fhir.core by hapifhir.

the class GraphQLEngine method processReverseReferenceSearch.

private void processReverseReferenceSearch(Resource source, Field field, ObjectValue target, boolean inheritedList, String suffix) throws EGraphQLException, FHIRException {
    if (services == null)
        throw new EGraphQLException("Resource Referencing services not provided");
    List<Argument> params = new ArrayList<Argument>();
    Argument parg = null;
    for (Argument a : field.getArguments()) if (!(a.getName().equals("_reference")))
        params.add(a);
    else if ((parg == null))
        parg = a;
    else
        throw new EGraphQLException("Duplicate parameter _reference");
    if (parg == null)
        throw new EGraphQLException("Missing parameter _reference");
    Argument arg = new Argument();
    params.add(arg);
    arg.setName(getSingleValue(parg));
    arg.addValue(new StringValue(source.fhirType() + "/" + source.getId()));
    Bundle bnd = (Bundle) services.search(appInfo, field.getName().substring(0, field.getName().length() - 10), params);
    Base bndWrapper = new SearchWrapper(field.getName(), bnd);
    arg = target.addField(field.getAlias() + suffix, listStatus(field, false));
    ObjectValue obj = new ObjectValue();
    arg.addValue(obj);
    processObject(null, bndWrapper, obj, field.getSelectionSet(), inheritedList, suffix);
}
Also used : ObjectValue(org.hl7.fhir.utilities.graphql.ObjectValue) Argument(org.hl7.fhir.utilities.graphql.Argument) StringValue(org.hl7.fhir.utilities.graphql.StringValue) EGraphQLException(org.hl7.fhir.utilities.graphql.EGraphQLException)

Example 15 with StringValue

use of org.hl7.fhir.utilities.graphql.StringValue in project org.hl7.fhir.core by hapifhir.

the class GraphQLEngine method processReverseReferenceList.

private void processReverseReferenceList(Resource source, Field field, ObjectValue target, boolean inheritedList, String suffix) throws EGraphQLException, FHIRException {
    if (services == null)
        throw new EGraphQLException("Resource Referencing services not provided");
    List<IBaseResource> list = new ArrayList<>();
    List<Argument> params = new ArrayList<>();
    Argument parg = null;
    for (Argument a : field.getArguments()) if (!(a.getName().equals("_reference")))
        params.add(a);
    else if ((parg == null))
        parg = a;
    else
        throw new EGraphQLException("Duplicate parameter _reference");
    if (parg == null)
        throw new EGraphQLException("Missing parameter _reference");
    Argument arg = new Argument();
    params.add(arg);
    arg.setName(getSingleValue(parg));
    arg.addValue(new StringValue(source.fhirType() + "/" + source.getIdPart()));
    services.listResources(appInfo, field.getName().substring(0, field.getName().length() - 4), params, list);
    arg = null;
    ObjectValue obj = null;
    List<Resource> vl = filterResources(field.argument("fhirpath"), list);
    if (!vl.isEmpty()) {
        arg = target.addField(field.getAlias() + suffix, listStatus(field, true));
        for (Resource v : vl) {
            obj = new ObjectValue();
            arg.addValue(obj);
            processObject(v, v, obj, field.getSelectionSet(), inheritedList, suffix);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource)

Aggregations

StringValue (org.hl7.fhir.utilities.graphql.StringValue)12 Argument (org.hl7.fhir.utilities.graphql.Argument)10 ArrayList (java.util.ArrayList)8 EGraphQLException (org.hl7.fhir.utilities.graphql.EGraphQLException)8 ObjectValue (org.hl7.fhir.utilities.graphql.ObjectValue)8 IBaseResource (org.hl7.fhir.instance.model.api.IBaseResource)4 List (java.util.List)3 LinkedList (java.util.LinkedList)2 FHIRException (org.hl7.fhir.exceptions.FHIRException)2 Base (org.hl7.fhir.r4b.model.Base)2 Bundle (org.hl7.fhir.r4b.model.Bundle)2 Directive (org.hl7.fhir.utilities.graphql.Directive)2 NameValue (org.hl7.fhir.utilities.graphql.NameValue)2 NumberValue (org.hl7.fhir.utilities.graphql.NumberValue)2 CodeInfo (au.csiro.redmatch.terminology.CodeInfo)1 IObservation (ch.elexis.core.findings.IObservation)1 TransientCoding (ch.elexis.core.findings.util.model.TransientCoding)1 JsonElement (com.google.gson.JsonElement)1 Field (java.lang.reflect.Field)1 Matcher (java.util.regex.Matcher)1