Search in sources :

Example 31 with EGraphQLException

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

the class GraphDefinitionEngine method execute.

public void execute() throws EGraphEngine, EGraphQLException, FHIRException {
    assert services != null;
    assert start != null;
    assert bundle != null;
    assert baseURL != null;
    assert graphDefinition != null;
    graphDefinition.checkNoModifiers("definition", "Building graph from GraphDefinition");
    check(!start.fhirType().equals(graphDefinition.getStart()), "The Graph definition requires that the start (focus reosource) is " + graphDefinition.getStart() + ", but instead found " + start.fhirType());
    if (!isInBundle(start)) {
        addToBundle(start);
    }
    for (GraphDefinitionLinkComponent l : graphDefinition.getLink()) {
        processLink(start.fhirType(), start, l, 1);
    }
}
Also used : GraphDefinitionLinkComponent(org.hl7.fhir.r4b.model.GraphDefinition.GraphDefinitionLinkComponent)

Example 32 with EGraphQLException

use of org.hl7.fhir.utilities.graphql.EGraphQLException 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 33 with EGraphQLException

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

the class GraphQLEngine method processReference.

private void processReference(Resource context, Base source, Field field, ObjectValue target, boolean inheritedList, String suffix) throws EGraphQLException, FHIRException {
    if (!(source instanceof Reference))
        throw new EGraphQLException("Not done yet");
    if (services == null)
        throw new EGraphQLException("Resource Referencing services not provided");
    Reference ref = (Reference) source;
    ReferenceResolution res = services.lookup(appInfo, context, ref);
    if (res != null) {
        if (targetTypeOk(field.getArguments(), res.getTarget())) {
            Argument arg = target.addField(field.getAlias() + suffix, listStatus(field, inheritedList));
            ObjectValue obj = new ObjectValue();
            arg.addValue(obj);
            processObject((Resource) res.getTargetContext(), (Base) res.getTarget(), obj, field.getSelectionSet(), inheritedList, suffix);
        }
    } else if (!hasArgument(field.getArguments(), "optional", "true"))
        throw new EGraphQLException("Unable to resolve reference to " + ref.getReference());
}
Also used : ObjectValue(org.hl7.fhir.utilities.graphql.ObjectValue) Argument(org.hl7.fhir.utilities.graphql.Argument) Reference(org.hl7.fhir.r4b.model.Reference) ReferenceResolution(org.hl7.fhir.utilities.graphql.IGraphQLStorageServices.ReferenceResolution) EGraphQLException(org.hl7.fhir.utilities.graphql.EGraphQLException)

Example 34 with EGraphQLException

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

the class GraphQLEngine method filter.

private List<Base> filter(Resource context, Property prop, String fieldName, List<Argument> arguments, List<Base> values, boolean extensionMode) throws FHIRException, EGraphQLException {
    List<Base> result = new ArrayList<Base>();
    if (values.size() > 0) {
        int count = Integer.MAX_VALUE;
        int offset = 0;
        StringBuilder fp = new StringBuilder();
        for (Argument arg : arguments) {
            List<Value> vl = resolveValues(arg);
            if ((vl.size() != 1))
                throw new EGraphQLException("Incorrect number of arguments");
            if (values.get(0).isPrimitive())
                throw new EGraphQLException("Attempt to use a filter (" + arg.getName() + ") on a primtive type (" + prop.getTypeCode() + ")");
            if ((arg.getName().equals("fhirpath")))
                fp.append(" and " + vl.get(0).toString());
            else if ((arg.getName().equals("_count")))
                count = Integer.valueOf(vl.get(0).toString());
            else if ((arg.getName().equals("_offset")))
                offset = Integer.valueOf(vl.get(0).toString());
            else {
                Property p = values.get(0).getNamedProperty(arg.getName());
                if (p == null)
                    throw new EGraphQLException("Attempt to use an unknown filter (" + arg.getName() + ") on a type (" + prop.getTypeCode() + ")");
                fp.append(" and " + arg.getName() + " = '" + vl.get(0).toString() + "'");
            }
        }
        // Account for situations where the GraphQL expression selected e.g.
        // effectiveDateTime but the field contains effectivePeriod
        String propName = prop.getName();
        List<Base> newValues = new ArrayList<>(values.size());
        for (Base value : values) {
            if (propName.endsWith("[x]")) {
                String propNameShortened = propName.substring(0, propName.length() - 3);
                if (fieldName.startsWith(propNameShortened) && fieldName.length() > propNameShortened.length()) {
                    if (!value.fhirType().equalsIgnoreCase(fieldName.substring(propNameShortened.length()))) {
                        continue;
                    }
                }
            }
            newValues.add(value);
        }
        int i = 0;
        int t = 0;
        if (fp.length() == 0)
            for (Base v : newValues) {
                if ((i >= offset) && passesExtensionMode(v, extensionMode)) {
                    result.add(v);
                    t++;
                    if (t >= count)
                        break;
                }
                i++;
            }
        else {
            ExpressionNode node = fpe.parse(fp.substring(5));
            for (Base v : newValues) {
                if ((i >= offset) && passesExtensionMode(v, extensionMode) && fpe.evaluateToBoolean(null, context, v, node)) {
                    result.add(v);
                    t++;
                    if (t >= count)
                        break;
                }
                i++;
            }
        }
    }
    return result;
}
Also used : Argument(org.hl7.fhir.utilities.graphql.Argument) ExpressionNode(org.hl7.fhir.r4b.model.ExpressionNode) ArrayList(java.util.ArrayList) StringValue(org.hl7.fhir.utilities.graphql.StringValue) ObjectValue(org.hl7.fhir.utilities.graphql.ObjectValue) VariableValue(org.hl7.fhir.utilities.graphql.VariableValue) Value(org.hl7.fhir.utilities.graphql.Value) NameValue(org.hl7.fhir.utilities.graphql.NameValue) NumberValue(org.hl7.fhir.utilities.graphql.NumberValue) EGraphQLException(org.hl7.fhir.utilities.graphql.EGraphQLException) Property(org.hl7.fhir.r4b.model.Property) Base(org.hl7.fhir.r4b.model.Base)

Example 35 with EGraphQLException

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

the class GraphQLEngine method filter.

private List<Base> filter(Resource context, Property prop, String fieldName, List<Argument> arguments, List<Base> values, boolean extensionMode) throws FHIRException, EGraphQLException {
    List<Base> result = new ArrayList<Base>();
    if (values.size() > 0) {
        int count = Integer.MAX_VALUE;
        int offset = 0;
        StringBuilder fp = new StringBuilder();
        for (Argument arg : arguments) {
            List<Value> vl = resolveValues(arg);
            if ((vl.size() != 1))
                throw new EGraphQLException("Incorrect number of arguments");
            if (values.get(0).isPrimitive())
                throw new EGraphQLException("Attempt to use a filter (" + arg.getName() + ") on a primtive type (" + prop.getTypeCode() + ")");
            if ((arg.getName().equals("fhirpath")))
                fp.append(" and " + vl.get(0).toString());
            else if ((arg.getName().equals("_count")))
                count = Integer.valueOf(vl.get(0).toString());
            else if ((arg.getName().equals("_offset")))
                offset = Integer.valueOf(vl.get(0).toString());
            else {
                Property p = values.get(0).getNamedProperty(arg.getName());
                if (p == null)
                    throw new EGraphQLException("Attempt to use an unknown filter (" + arg.getName() + ") on a type (" + prop.getTypeCode() + ")");
                fp.append(" and " + arg.getName() + " = '" + vl.get(0).toString() + "'");
            }
        }
        // Account for situations where the GraphQL expression selected e.g.
        // effectiveDateTime but the field contains effectivePeriod
        String propName = prop.getName();
        List<Base> newValues = new ArrayList<>(values.size());
        for (Base value : values) {
            if (propName.endsWith("[x]")) {
                String propNameShortened = propName.substring(0, propName.length() - 3);
                if (fieldName.startsWith(propNameShortened) && fieldName.length() > propNameShortened.length()) {
                    if (!value.fhirType().equalsIgnoreCase(fieldName.substring(propNameShortened.length()))) {
                        continue;
                    }
                }
            }
            newValues.add(value);
        }
        int i = 0;
        int t = 0;
        if (fp.length() == 0)
            for (Base v : newValues) {
                if ((i >= offset) && passesExtensionMode(v, extensionMode)) {
                    result.add(v);
                    t++;
                    if (t >= count)
                        break;
                }
                i++;
            }
        else {
            ExpressionNode node = fpe.parse(fp.substring(5));
            for (Base v : newValues) {
                if ((i >= offset) && passesExtensionMode(v, extensionMode) && fpe.evaluateToBoolean(null, context, v, node)) {
                    result.add(v);
                    t++;
                    if (t >= count)
                        break;
                }
                i++;
            }
        }
    }
    return result;
}
Also used : Argument(org.hl7.fhir.utilities.graphql.Argument) StringValue(org.hl7.fhir.utilities.graphql.StringValue) ObjectValue(org.hl7.fhir.utilities.graphql.ObjectValue) VariableValue(org.hl7.fhir.utilities.graphql.VariableValue) Value(org.hl7.fhir.utilities.graphql.Value) NameValue(org.hl7.fhir.utilities.graphql.NameValue) NumberValue(org.hl7.fhir.utilities.graphql.NumberValue) EGraphQLException(org.hl7.fhir.utilities.graphql.EGraphQLException)

Aggregations

EGraphQLException (org.hl7.fhir.utilities.graphql.EGraphQLException)24 IBaseResource (org.hl7.fhir.instance.model.api.IBaseResource)21 Argument (org.hl7.fhir.utilities.graphql.Argument)20 ObjectValue (org.hl7.fhir.utilities.graphql.ObjectValue)20 ArrayList (java.util.ArrayList)16 StringValue (org.hl7.fhir.utilities.graphql.StringValue)12 ReferenceResolution (org.hl7.fhir.utilities.graphql.IGraphQLStorageServices.ReferenceResolution)8 FHIRException (org.hl7.fhir.exceptions.FHIRException)5 DomainResource (org.hl7.fhir.r4b.model.DomainResource)5 ExpressionNode (org.hl7.fhir.r4b.model.ExpressionNode)5 Resource (org.hl7.fhir.r4b.model.Resource)5 Base (org.hl7.fhir.r4b.model.Base)4 Directive (org.hl7.fhir.utilities.graphql.Directive)4 NameValue (org.hl7.fhir.utilities.graphql.NameValue)4 NumberValue (org.hl7.fhir.utilities.graphql.NumberValue)4 Value (org.hl7.fhir.utilities.graphql.Value)4 VariableValue (org.hl7.fhir.utilities.graphql.VariableValue)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 IOException (java.io.IOException)3 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)3