Search in sources :

Example 41 with Argument

use of org.hl7.fhir.utilities.graphql.Argument 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)

Example 42 with Argument

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

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

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

the class GraphQLEngine method filterResources.

private List<Resource> filterResources(Argument fhirpath, Bundle bnd) throws EGraphQLException, FHIRException {
    List<Resource> result = new ArrayList<Resource>();
    if (bnd.getEntry().size() > 0) {
        if ((fhirpath == null))
            for (BundleEntryComponent be : bnd.getEntry()) result.add(be.getResource());
        else {
            FHIRPathEngine fpe = new FHIRPathEngine(context);
            ExpressionNode node = fpe.parse(getSingleValue(fhirpath));
            for (BundleEntryComponent be : bnd.getEntry()) if (fpe.evaluateToBoolean(null, be.getResource(), be.getResource(), node))
                result.add(be.getResource());
        }
    }
    return result;
}
Also used : BundleEntryComponent(org.hl7.fhir.r5.model.Bundle.BundleEntryComponent) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource)

Example 45 with Argument

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

the class GraphQLEngine method processSearchSingle.

private void processSearchSingle(ObjectValue target, Field field, boolean inheritedList, String suffix) throws EGraphQLException, FHIRException {
    if (services == null)
        throw new EGraphQLException("Resource Referencing services not provided");
    String id = "";
    for (Argument arg : field.getArguments()) if ((arg.getName().equals("id")))
        id = getSingleValue(arg);
    else
        throw new EGraphQLException("Unknown/invalid parameter " + arg.getName());
    if (Utilities.noString(id))
        throw new EGraphQLException("No id found");
    Resource res = (Resource) services.lookup(appInfo, field.getName(), id);
    if (res == null)
        throw new EGraphQLException("Resource " + field.getName() + "/" + id + " not found");
    Argument arg = target.addField(field.getAlias() + suffix, listStatus(field, false));
    ObjectValue obj = new ObjectValue();
    arg.addValue(obj);
    processObject(res, res, obj, field.getSelectionSet(), inheritedList, suffix);
}
Also used : ObjectValue(org.hl7.fhir.utilities.graphql.ObjectValue) Argument(org.hl7.fhir.utilities.graphql.Argument) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) EGraphQLException(org.hl7.fhir.utilities.graphql.EGraphQLException)

Aggregations

Argument (org.hl7.fhir.utilities.graphql.Argument)24 IBaseResource (org.hl7.fhir.instance.model.api.IBaseResource)22 ArrayList (java.util.ArrayList)21 EGraphQLException (org.hl7.fhir.utilities.graphql.EGraphQLException)20 ObjectValue (org.hl7.fhir.utilities.graphql.ObjectValue)20 StringValue (org.hl7.fhir.utilities.graphql.StringValue)16 FHIRException (org.hl7.fhir.exceptions.FHIRException)8 ReferenceResolution (org.hl7.fhir.utilities.graphql.IGraphQLStorageServices.ReferenceResolution)8 IOException (java.io.IOException)7 List (java.util.List)6 Resource (org.hl7.fhir.r4b.model.Resource)6 NameValue (org.hl7.fhir.utilities.graphql.NameValue)6 NumberValue (org.hl7.fhir.utilities.graphql.NumberValue)6 DomainResource (org.hl7.fhir.r4b.model.DomainResource)5 Nonnull (javax.annotation.Nonnull)4 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)4 Row (org.apache.spark.sql.Row)4 SAXException (org.xml.sax.SAXException)4 FhirPath (au.csiro.pathling.fhirpath.FhirPath)3 NonLiteralPath (au.csiro.pathling.fhirpath.NonLiteralPath)3