Search in sources :

Example 1 with ReferenceResolution

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

the class GraphDefinitionEngine method processLinkPath.

private void processLinkPath(String focusPath, Resource focus, GraphDefinitionLinkComponent link, int depth) {
    String path = focusPath + " -> " + link.getPath();
    check(link.hasPath(), "Path is needed at " + path);
    check(!link.hasSliceName(), "SliceName is not yet supported at " + path);
    ExpressionNode node;
    if (link.getPathElement().hasUserData(TAG_NAME)) {
        node = (ExpressionNode) link.getPathElement().getUserData(TAG_NAME);
    } else {
        node = engine.parse(link.getPath());
        link.getPathElement().setUserData(TAG_NAME, node);
    }
    List<Base> matches = engine.evaluate(null, focus, focus, focus, node);
    check(!validating || matches.size() >= (link.hasMin() ? link.getMin() : 0), "Link at path " + path + " requires at least " + link.getMin() + " matches, but only found " + matches.size());
    check(!validating || matches.size() <= (link.hasMax() ? Integer.parseInt(link.getMax()) : Integer.MAX_VALUE), "Link at path " + path + " requires at most " + link.getMax() + " matches, but found " + matches.size());
    for (Base sel : matches) {
        // todo: should a URL be ok?
        check(sel.fhirType().equals("Reference"), "Selected node from an expression must be a Reference");
        ReferenceResolution res = services.lookup(appInfo, focus, (Reference) sel);
        if (res != null) {
            // todo
            check(res.getTargetContext() != focus, "how to handle contained resources is not yet resolved");
            for (GraphDefinitionLinkTargetComponent tl : link.getTarget()) {
                if (tl.getType().equals(res.getTarget().fhirType())) {
                    Resource r = (Resource) res.getTarget();
                    if (!isInBundle(r)) {
                        addToBundle(r);
                        for (GraphDefinitionLinkComponent l : graphDefinition.getLink()) {
                            processLink(focus.fhirType(), r, l, depth + 1);
                        }
                    }
                }
            }
        }
    }
}
Also used : GraphDefinitionLinkComponent(org.hl7.fhir.r4b.model.GraphDefinition.GraphDefinitionLinkComponent) ExpressionNode(org.hl7.fhir.r4b.model.ExpressionNode) ReferenceResolution(org.hl7.fhir.utilities.graphql.IGraphQLStorageServices.ReferenceResolution) Resource(org.hl7.fhir.r4b.model.Resource) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) GraphDefinitionLinkTargetComponent(org.hl7.fhir.r4b.model.GraphDefinition.GraphDefinitionLinkTargetComponent) Base(org.hl7.fhir.r4b.model.Base)

Example 2 with ReferenceResolution

use of org.hl7.fhir.utilities.graphql.IGraphQLStorageServices.ReferenceResolution 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 : ReferenceResolution(org.hl7.fhir.utilities.graphql.IGraphQLStorageServices.ReferenceResolution)

Example 3 with ReferenceResolution

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

the class GraphQLEngineTests method lookup.

@Override
public ReferenceResolution lookup(Object appInfo, IBaseResource context, IBaseReference reference) throws FHIRException {
    try {
        if (reference.getReferenceElement().isLocal()) {
            if (!(context instanceof DomainResource))
                return null;
            for (Resource r : ((DomainResource) context).getContained()) {
                if (('#' + r.getId()).equals(reference.getReferenceElement().getValue())) {
                    return new ReferenceResolution(context, r);
                }
            }
        } else {
            String[] parts = reference.getReferenceElement().getValue().split("/");
            String filename = TestingUtilities.resourceNameToFile(parts[0].toLowerCase() + '-' + parts[1].toLowerCase() + ".xml");
            if (new File(filename).exists())
                return new ReferenceResolution(null, new XmlParser().parse(new FileInputStream(filename)));
        }
        return null;
    } catch (Exception e) {
        throw new FHIRException(e);
    }
}
Also used : XmlParser(org.hl7.fhir.r4.formats.XmlParser) DomainResource(org.hl7.fhir.r4.model.DomainResource) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) Resource(org.hl7.fhir.r4.model.Resource) DomainResource(org.hl7.fhir.r4.model.DomainResource) File(java.io.File) TextFile(org.hl7.fhir.utilities.TextFile) FHIRException(org.hl7.fhir.exceptions.FHIRException) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 4 with ReferenceResolution

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

the class GraphQLEngine method processCanonicalReference.

private void processCanonicalReference(Resource context, Base source, Field field, ObjectValue target, boolean inheritedList, String suffix) throws EGraphQLException, FHIRException {
    if (!(source instanceof CanonicalType))
        throw new EGraphQLException("Not done yet");
    if (services == null)
        throw new EGraphQLException("Resource Referencing services not provided");
    Reference ref = new Reference(source.primitiveValue());
    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) ReferenceResolution(org.hl7.fhir.utilities.graphql.IGraphQLStorageServices.ReferenceResolution) EGraphQLException(org.hl7.fhir.utilities.graphql.EGraphQLException)

Example 5 with ReferenceResolution

use of org.hl7.fhir.utilities.graphql.IGraphQLStorageServices.ReferenceResolution 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) ReferenceResolution(org.hl7.fhir.utilities.graphql.IGraphQLStorageServices.ReferenceResolution) EGraphQLException(org.hl7.fhir.utilities.graphql.EGraphQLException)

Aggregations

ReferenceResolution (org.hl7.fhir.utilities.graphql.IGraphQLStorageServices.ReferenceResolution)10 Argument (org.hl7.fhir.utilities.graphql.Argument)4 EGraphQLException (org.hl7.fhir.utilities.graphql.EGraphQLException)4 ObjectValue (org.hl7.fhir.utilities.graphql.ObjectValue)4 IBaseResource (org.hl7.fhir.instance.model.api.IBaseResource)3 Reference (org.hl7.fhir.r4b.model.Reference)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 FHIRException (org.hl7.fhir.exceptions.FHIRException)1 XmlParser (org.hl7.fhir.r4.formats.XmlParser)1 DomainResource (org.hl7.fhir.r4.model.DomainResource)1 Resource (org.hl7.fhir.r4.model.Resource)1 Base (org.hl7.fhir.r4b.model.Base)1 CanonicalType (org.hl7.fhir.r4b.model.CanonicalType)1 ExpressionNode (org.hl7.fhir.r4b.model.ExpressionNode)1 GraphDefinitionLinkComponent (org.hl7.fhir.r4b.model.GraphDefinition.GraphDefinitionLinkComponent)1 GraphDefinitionLinkTargetComponent (org.hl7.fhir.r4b.model.GraphDefinition.GraphDefinitionLinkTargetComponent)1