Search in sources :

Example 1 with BaseResource

use of org.hl7.fhir.r4.model.BaseResource in project kindling by HL7.

the class Publisher method processProfile.

private void processProfile(Profile ap, ConstraintStructure profile, String filename, ResourceDefn baseResource) throws Exception {
    // create StructureDefinition structures if needed (create differential definitions from spreadsheets)
    if (profile.getResource() == null) {
        StructureDefinition p = new ProfileGenerator(page.getDefinitions(), page.getWorkerContext(), page, page.getGenDate(), page.getVersion(), dataElements, fpUsages, page.getFolders().rootDir, page.getUml(), page.getRc()).generate(ap, profile, profile.getDefn(), profile.getId(), profile.getUsage(), page.getValidationErrors(), baseResource);
        p.setUserData("pack", ap);
        profile.setResource(p);
        if (profile.getResourceInfo() != null) {
            profile.getResourceInfo().setUserData(ToolResourceUtilities.RES_ACTUAL_RESOURCE, p);
        }
        if (page.getProfiles().has(p.getUrl()))
            throw new Exception("Duplicate Profile URL " + p.getUrl());
        page.getProfiles().see(p, page.packageInfo());
    } else {
        profile.getResource().setUserData("pack", ap);
        sortProfile(profile.getResource());
        for (ElementDefinition ed : profile.getResource().getDifferential().getElement()) if (!ed.hasId())
            throw new Exception("Missing ID");
        // special case: if the profile itself doesn't claim a date, it's date is the date of this publication
        if (!profile.getResource().hasDate())
            profile.getResource().setDate(page.getGenDate().getTime());
        if (profile.getResource().hasBaseDefinition() && !profile.getResource().hasSnapshot()) {
            // cause it probably doesn't, coming from the profile directly
            StructureDefinition base = getSnapShotForProfile(profile.getResource().getBaseDefinition());
            new ProfileUtilities(page.getWorkerContext(), page.getValidationErrors(), page).generateSnapshot(base, profile.getResource(), profile.getResource().getBaseDefinition().split("#")[0], "http://hl7.org/fhir", profile.getResource().getName());
        }
        if (page.getProfiles().has(profile.getResource().getUrl()))
            throw new Exception("Duplicate Profile URL " + profile.getResource().getUrl());
        System.out.println("register " + profile.getResource().getUrl());
        page.getProfiles().see(profile.getResource(), page.packageInfo());
    }
    if (!Utilities.noString(filename))
        profile.getResource().setUserData("filename", filename + ".html");
    if (Utilities.noString(profile.getResource().getUserString("path"))) {
        String path = "";
        ImplementationGuideDefn ig = page.getDefinitions().getUsageIG(ap.getCategory(), "processProfile");
        if (ig != null && !ig.isCore())
            path = ig.getCode() + File.separator;
        profile.getResource().setUserData("path", path + filename + ".html");
    }
}
Also used : StructureDefinition(org.hl7.fhir.r5.model.StructureDefinition) ProfileGenerator(org.hl7.fhir.definitions.generators.specification.ProfileGenerator) ProfileUtilities(org.hl7.fhir.r5.conformance.ProfileUtilities) ElementDefinition(org.hl7.fhir.r5.model.ElementDefinition) ImplementationGuideDefn(org.hl7.fhir.definitions.model.ImplementationGuideDefn) TransformerException(javax.xml.transform.TransformerException) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 2 with BaseResource

use of org.hl7.fhir.r4.model.BaseResource in project kindling by HL7.

the class FhirTurtleGenerator method processTypes.

private void processTypes(String baseResourceName, FHIRResource baseResource, ElementDefn td, String predicateBase, boolean innerIsBackbone) throws Exception {
    for (ElementDefn ed : td.getElements()) {
        String predicateName = predicateBase + "." + (ed.getName().endsWith("[x]") ? ed.getName().substring(0, ed.getName().length() - 3) : ed.getName());
        FHIRResource predicateResource;
        if (ed.getName().endsWith("[x]")) {
            predicateResource = fact.fhir_objectProperty(predicateName);
            // Choice entry
            if (ed.typeCode().equals("*")) {
                // Wild card -- any element works (probably should be more restrictive but...)
                Resource targetResource = RDFNamespace.FHIR.resourceRef("Element");
                baseResource.restriction(fact.fhir_cardinality_restriction(predicateResource.resource, targetResource, ed.getMinCardinality(), ed.getMaxCardinality()));
                predicateResource.domain(baseResource);
                predicateResource.range(targetResource);
            } else {
                // Create a restriction on the union of possible types
                List<Resource> typeOpts = new ArrayList<Resource>();
                for (TypeRef tr : ed.getTypes()) {
                    // TODO: Figure out how to get the type reference code
                    String trName = tr.getName();
                    if (trName.equals("SimpleQuantity"))
                        trName = "Quantity";
                    String qualifiedPredicateName = predicateName + Utilities.capitalize(trName);
                    Resource targetRes = fact.fhir_class(tr.getName()).resource;
                    FHIRResource qualifiedPredicate = fact.fhir_objectProperty(qualifiedPredicateName, predicateResource.resource).domain(baseResource).range(targetRes);
                    typeOpts.add(fact.fhir_cardinality_restriction(qualifiedPredicate.resource, targetRes, ed.getMinCardinality(), ed.getMaxCardinality()));
                }
                baseResource.restriction(fact.fhir_union(typeOpts));
            }
        } else {
            FHIRResource baseDef;
            if (ed.getTypes().isEmpty()) {
                predicateResource = fact.fhir_objectProperty(predicateName);
                String targetClassName = mapComponentName(baseResourceName, ed.getDeclaredTypeName());
                baseDef = fact.fhir_class(targetClassName, innerIsBackbone ? "BackboneElement" : "Element").addDefinition(ed.getDefinition());
                processTypes(targetClassName, baseDef, ed, predicateName, innerIsBackbone);
            } else {
                TypeRef targetType = ed.getTypes().get(0);
                String targetName = targetType.getName();
                if (targetName.startsWith("@")) {
                    // Link to earlier definition
                    ElementDefn targetRef = getElementForPath(targetName.substring(1));
                    String targetRefName = targetRef.getName();
                    String targetClassName = baseResourceName + Character.toUpperCase(targetRefName.charAt(0)) + targetRefName.substring(1);
                    baseDef = fact.fhir_class(targetClassName, innerIsBackbone ? "BackboneElement" : "Element").addDefinition(ed.getDefinition()).addTitle(ed.getShortDefn());
                    if (!processing.contains(targetRefName)) {
                        processing.add(targetRefName);
                        processTypes(targetClassName, baseDef, targetRef, predicateName, innerIsBackbone);
                        processing.remove(targetRefName);
                    }
                } else {
                    // A placeholder entry.  The rest of the information will be supplied elsewhere
                    baseDef = fact.fhir_class(targetName);
                }
                // XHTML the exception, in that the html doesn't derive from Primitive
                if (targetName.equals("xhtml"))
                    predicateResource = fact.fhir_dataProperty(predicateName);
                else
                    predicateResource = fact.fhir_objectProperty(predicateName);
            }
            predicateResource.addTitle(ed.getShortDefn()).addDefinition(ed.getDefinition()).domain(baseResource);
            baseResource.restriction(fact.fhir_cardinality_restriction(predicateResource.resource, baseDef.resource, ed.getMinCardinality(), ed.getMaxCardinality()));
            predicateResource.range(baseDef.resource);
            if (!Utilities.noString(ed.getW5()))
                predicateResource.addObjectProperty(RDFS.subPropertyOf, RDFNamespace.W5.resourceRef(ed.getW5()));
        }
    }
}
Also used : FHIRResource(org.hl7.fhir.rdf.FHIRResource) TypeRef(org.hl7.fhir.definitions.model.TypeRef) ElementDefn(org.hl7.fhir.definitions.model.ElementDefn) Resource(org.apache.jena.rdf.model.Resource) FHIRResource(org.hl7.fhir.rdf.FHIRResource) ArrayList(java.util.ArrayList)

Example 3 with BaseResource

use of org.hl7.fhir.r4.model.BaseResource in project quality-measure-and-cohort-service by Alvearie.

the class R4QualityMeasureResolverFactory method consumeFile.

private void consumeFile(String filename, String content, ResourceFieldHandler<Library, Identifier> libraryFieldHandler, MapFhirResourceResolver<Library, Identifier> libraryResolver, ResourceFieldHandler<Measure, Identifier> measureFieldHandler, MapFhirResourceResolver<Measure, Identifier> measureResolver) {
    String filenameId = FilenameUtils.getBaseName(filename);
    if (HapiUtils.canParseFile(filename, parser)) {
        IBaseResource baseResource = parser.parseResource(content);
        if (libraryFieldHandler.getSupportedClass().isInstance(baseResource)) {
            Library library = (Library) baseResource;
            setIdIfNotPresent(library, libraryFieldHandler, filenameId);
            libraryResolver.addResource(library);
        } else if (measureFieldHandler.getSupportedClass().isInstance(baseResource)) {
            Measure measure = (Measure) baseResource;
            setIdIfNotPresent(measure, measureFieldHandler, filenameId);
            measureResolver.addResource(measure);
        }
    }
}
Also used : Measure(org.hl7.fhir.r4.model.Measure) Library(org.hl7.fhir.r4.model.Library) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource)

Example 4 with BaseResource

use of org.hl7.fhir.r4.model.BaseResource in project elexis-server by elexis.

the class ResourceProviderUtil method createResource.

/**
 * @param transformer
 * @param fhirObject
 * @param log
 * @return TODO Support conditional create http://hl7.org/fhir/http.html#ccreate
 */
protected <T extends BaseResource, U extends Identifiable> MethodOutcome createResource(IFhirTransformer<T, U> transformer, T fhirObject, Logger log) {
    Optional<U> created;
    try {
        created = transformer.createLocalObject(fhirObject);
        if (created.isPresent()) {
            Optional<T> updated = transformer.getFhirObject(created.get());
            if (updated.isPresent()) {
                MethodOutcome outcome = new MethodOutcome();
                outcome.setCreated(true);
                outcome.setId(updated.get().getIdElement());
                outcome.setResource(updated.get());
                return outcome;
            }
        }
    } catch (IFhirTransformerException e) {
        OperationOutcome opOutcome = generateOperationOutcome(e);
        throw new PreconditionFailedException(e.getMessage(), opOutcome);
    }
    log.warn("Object creation failed [{}]", fhirObject);
    throw new InternalErrorException("Creation failed");
}
Also used : IFhirTransformerException(ch.elexis.core.findings.util.fhir.IFhirTransformerException) OperationOutcome(org.hl7.fhir.r4.model.OperationOutcome) PreconditionFailedException(ca.uhn.fhir.rest.server.exceptions.PreconditionFailedException) InternalErrorException(ca.uhn.fhir.rest.server.exceptions.InternalErrorException) MethodOutcome(ca.uhn.fhir.rest.api.MethodOutcome)

Example 5 with BaseResource

use of org.hl7.fhir.r4.model.BaseResource in project elexis-server by elexis.

the class ResourceProviderUtil method updateResource.

protected <T extends BaseResource, U extends Identifiable> MethodOutcome updateResource(IdType theId, IFhirTransformer<T, U> transformer, T fhirObject, Logger log) {
    String versionId = theId.getVersionIdPart();
    Optional<U> localObject = transformer.getLocalObject(fhirObject);
    MethodOutcome outcome = new MethodOutcome();
    if (localObject.isPresent()) {
        if (versionId == null) {
            log.warn("Version agnostic update on {}", localObject.get());
        }
        if (versionId != null && !versionId.equals(localObject.get().getLastupdate().toString())) {
            throw new ResourceVersionConflictException("Expected version " + localObject.get().getLastupdate().toString());
        }
        try {
            transformer.updateLocalObject(fhirObject, localObject.get());
        } catch (IFhirTransformerException e) {
            OperationOutcome opOutcome = generateOperationOutcome(e);
            throw new PreconditionFailedException(e.getMessage(), opOutcome);
        }
        Optional<T> updatedObject = transformer.getFhirObject(localObject.get());
        if (updatedObject.isPresent()) {
            outcome.setId(updatedObject.get().getIdElement());
            outcome.setResource(updatedObject.get());
            return outcome;
        }
        log.warn("Object update failed [{}]", fhirObject);
        throw new InternalErrorException("Object update failed");
    } else {
        OperationOutcome issueOutcome = new OperationOutcome();
        issueOutcome.addIssue().setDiagnostics("No local object found");
        outcome.setOperationOutcome(issueOutcome);
    }
    return outcome;
}
Also used : IFhirTransformerException(ch.elexis.core.findings.util.fhir.IFhirTransformerException) OperationOutcome(org.hl7.fhir.r4.model.OperationOutcome) PreconditionFailedException(ca.uhn.fhir.rest.server.exceptions.PreconditionFailedException) InternalErrorException(ca.uhn.fhir.rest.server.exceptions.InternalErrorException) ResourceVersionConflictException(ca.uhn.fhir.rest.server.exceptions.ResourceVersionConflictException) MethodOutcome(ca.uhn.fhir.rest.api.MethodOutcome)

Aggregations

IBaseResource (org.hl7.fhir.instance.model.api.IBaseResource)4 FhirContext (ca.uhn.fhir.context.FhirContext)3 IParser (ca.uhn.fhir.parser.IParser)3 SuppressParserErrorHandler (org.hl7.davinci.SuppressParserErrorHandler)3 MethodOutcome (ca.uhn.fhir.rest.api.MethodOutcome)2 InternalErrorException (ca.uhn.fhir.rest.server.exceptions.InternalErrorException)2 PreconditionFailedException (ca.uhn.fhir.rest.server.exceptions.PreconditionFailedException)2 IFhirTransformerException (ch.elexis.core.findings.util.fhir.IFhirTransformerException)2 ArrayList (java.util.ArrayList)2 FHIRException (org.hl7.fhir.exceptions.FHIRException)2 OperationOutcome (org.hl7.fhir.r4.model.OperationOutcome)2 ProfileUtilities (org.hl7.fhir.r5.conformance.ProfileUtilities)2 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)2 ResourceVersionConflictException (ca.uhn.fhir.rest.server.exceptions.ResourceVersionConflictException)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URISyntaxException (java.net.URISyntaxException)1