Search in sources :

Example 66 with UriType

use of org.hl7.fhir.r4.model.UriType in project org.hl7.fhir.core by hapifhir.

the class BaseWorkerContext method validateOnServer.

private ValidationResult validateOnServer(ValueSet vs, Parameters pin, ValidationOptions options) throws FHIRException {
    boolean cache = false;
    if (vs != null) {
        for (ConceptSetComponent inc : vs.getCompose().getInclude()) {
            codeSystemsUsed.add(inc.getSystem());
        }
        for (ConceptSetComponent inc : vs.getCompose().getExclude()) {
            codeSystemsUsed.add(inc.getSystem());
        }
    }
    if (vs != null) {
        if (isTxCaching && cacheId != null && vs.getUrl() != null && cached.contains(vs.getUrl() + "|" + vs.getVersion())) {
            pin.addParameter().setName("url").setValue(new UriType(vs.getUrl() + (vs.hasVersion() ? "|" + vs.getVersion() : "")));
        } else if (options.getVsAsUrl()) {
            pin.addParameter().setName("url").setValue(new StringType(vs.getUrl()));
        } else {
            pin.addParameter().setName("valueSet").setResource(vs);
            if (vs.getUrl() != null) {
                cached.add(vs.getUrl() + "|" + vs.getVersion());
            }
        }
        cache = true;
        addDependentResources(pin, vs);
    }
    if (cache) {
        pin.addParameter().setName("cache-id").setValue(new StringType(cacheId));
    }
    for (ParametersParameterComponent pp : pin.getParameter()) {
        if (pp.getName().equals("profile")) {
            throw new Error(formatMessage(I18nConstants.CAN_ONLY_SPECIFY_PROFILE_IN_THE_CONTEXT));
        }
    }
    if (expParameters == null) {
        throw new Error(formatMessage(I18nConstants.NO_EXPANSIONPROFILE_PROVIDED));
    }
    pin.addParameter().setName("profile").setResource(expParameters);
    if (txLog != null) {
        txLog.clearLastId();
    }
    if (txClient == null) {
        throw new FHIRException(formatMessage(I18nConstants.ATTEMPT_TO_USE_TERMINOLOGY_SERVER_WHEN_NO_TERMINOLOGY_SERVER_IS_AVAILABLE));
    }
    Parameters pOut;
    if (vs == null) {
        pOut = txClient.validateCS(pin);
    } else {
        pOut = txClient.validateVS(pin);
    }
    return processValidationResult(pOut);
}
Also used : ConceptSetComponent(org.hl7.fhir.r4b.model.ValueSet.ConceptSetComponent) Parameters(org.hl7.fhir.r4b.model.Parameters) StringType(org.hl7.fhir.r4b.model.StringType) FHIRException(org.hl7.fhir.exceptions.FHIRException) UriType(org.hl7.fhir.r4b.model.UriType) ParametersParameterComponent(org.hl7.fhir.r4b.model.Parameters.ParametersParameterComponent)

Example 67 with UriType

use of org.hl7.fhir.r4.model.UriType in project org.hl7.fhir.core by hapifhir.

the class StructureMapUtilities method resolveGroupByTypes.

private ResolvedGroup resolveGroupByTypes(StructureMap map, String ruleid, StructureMapGroupComponent source, String srcType, String tgtType) throws FHIRException {
    String kn = "types^" + srcType + ":" + tgtType;
    if (source.hasUserData(kn))
        return (ResolvedGroup) source.getUserData(kn);
    ResolvedGroup res = new ResolvedGroup();
    res.targetMap = null;
    res.target = null;
    for (StructureMapGroupComponent grp : map.getGroup()) {
        if (matchesByType(map, grp, srcType, tgtType)) {
            if (res.targetMap == null) {
                res.targetMap = map;
                res.target = grp;
            } else
                throw new FHIRException("Multiple possible matches looking for rule for '" + srcType + "/" + tgtType + "', from rule '" + ruleid + "'");
        }
    }
    if (res.targetMap != null) {
        source.setUserData(kn, res);
        return res;
    }
    for (UriType imp : map.getImport()) {
        List<StructureMap> impMapList = findMatchingMaps(imp.getValue());
        if (impMapList.size() == 0)
            throw new FHIRException("Unable to find map(s) for " + imp.getValue());
        for (StructureMap impMap : impMapList) {
            if (!impMap.getUrl().equals(map.getUrl())) {
                for (StructureMapGroupComponent grp : impMap.getGroup()) {
                    if (matchesByType(impMap, grp, srcType, tgtType)) {
                        if (res.targetMap == null) {
                            res.targetMap = impMap;
                            res.target = grp;
                        } else
                            throw new FHIRException("Multiple possible matches for rule for '" + srcType + "/" + tgtType + "' in " + res.targetMap.getUrl() + " and " + impMap.getUrl() + ", from rule '" + ruleid + "'");
                    }
                }
            }
        }
    }
    if (res.target == null)
        throw new FHIRException("No matches found for rule for '" + srcType + " to " + tgtType + "' from " + map.getUrl() + ", from rule '" + ruleid + "'");
    source.setUserData(kn, res);
    return res;
}
Also used : StructureMap(org.hl7.fhir.r4b.model.StructureMap) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 68 with UriType

use of org.hl7.fhir.r4.model.UriType in project org.hl7.fhir.core by hapifhir.

the class StructureMapUtilities method translate.

public Base translate(TransformContext context, StructureMap map, Base source, String conceptMapUrl, String fieldToReturn) throws FHIRException {
    Coding src = new Coding();
    if (source.isPrimitive()) {
        src.setCode(source.primitiveValue());
    } else if ("Coding".equals(source.fhirType())) {
        Base[] b = source.getProperty("system".hashCode(), "system", true);
        if (b.length == 1)
            src.setSystem(b[0].primitiveValue());
        b = source.getProperty("code".hashCode(), "code", true);
        if (b.length == 1)
            src.setCode(b[0].primitiveValue());
    } else if ("CE".equals(source.fhirType())) {
        Base[] b = source.getProperty("codeSystem".hashCode(), "codeSystem", true);
        if (b.length == 1)
            src.setSystem(b[0].primitiveValue());
        b = source.getProperty("code".hashCode(), "code", true);
        if (b.length == 1)
            src.setCode(b[0].primitiveValue());
    } else
        throw new FHIRException("Unable to translate source " + source.fhirType());
    String su = conceptMapUrl;
    if (conceptMapUrl.equals("http://hl7.org/fhir/ConceptMap/special-oid2uri")) {
        String uri = worker.oid2Uri(src.getCode());
        if (uri == null)
            uri = "urn:oid:" + src.getCode();
        if ("uri".equals(fieldToReturn))
            return new UriType(uri);
        else
            throw new FHIRException("Error in return code");
    } else {
        ConceptMap cmap = null;
        if (conceptMapUrl.startsWith("#")) {
            for (Resource r : map.getContained()) {
                if (r instanceof ConceptMap && ((ConceptMap) r).getId().equals(conceptMapUrl.substring(1))) {
                    cmap = (ConceptMap) r;
                    su = map.getUrl() + "#" + conceptMapUrl;
                }
            }
            if (cmap == null)
                throw new FHIRException("Unable to translate - cannot find map " + conceptMapUrl);
        } else {
            if (conceptMapUrl.contains("#")) {
                String[] p = conceptMapUrl.split("\\#");
                StructureMap mapU = worker.fetchResource(StructureMap.class, p[0]);
                for (Resource r : mapU.getContained()) {
                    if (r instanceof ConceptMap && ((ConceptMap) r).getId().equals(p[1])) {
                        cmap = (ConceptMap) r;
                        su = conceptMapUrl;
                    }
                }
            }
            if (cmap == null)
                cmap = worker.fetchResource(ConceptMap.class, conceptMapUrl);
        }
        Coding outcome = null;
        boolean done = false;
        String message = null;
        if (cmap == null) {
            if (services == null)
                message = "No map found for " + conceptMapUrl;
            else {
                outcome = services.translate(context.getAppInfo(), src, conceptMapUrl);
                done = true;
            }
        } else {
            List<SourceElementComponentWrapper> list = new ArrayList<SourceElementComponentWrapper>();
            for (ConceptMapGroupComponent g : cmap.getGroup()) {
                for (SourceElementComponent e : g.getElement()) {
                    if (!src.hasSystem() && src.getCode().equals(e.getCode()))
                        list.add(new SourceElementComponentWrapper(g, e));
                    else if (src.hasSystem() && src.getSystem().equals(g.getSource()) && src.getCode().equals(e.getCode()))
                        list.add(new SourceElementComponentWrapper(g, e));
                }
            }
            if (list.size() == 0)
                done = true;
            else if (list.get(0).comp.getTarget().size() == 0)
                message = "Concept map " + su + " found no translation for " + src.getCode();
            else {
                for (TargetElementComponent tgt : list.get(0).comp.getTarget()) {
                    if (tgt.getEquivalence() == null || EnumSet.of(ConceptMapEquivalence.EQUAL, ConceptMapEquivalence.RELATEDTO, ConceptMapEquivalence.EQUIVALENT, ConceptMapEquivalence.WIDER).contains(tgt.getEquivalence())) {
                        if (done) {
                            message = "Concept map " + su + " found multiple matches for " + src.getCode();
                            done = false;
                        } else {
                            done = true;
                            outcome = new Coding().setCode(tgt.getCode()).setSystem(list.get(0).group.getTarget());
                        }
                    } else if (tgt.getEquivalence() == ConceptMapEquivalence.UNMATCHED) {
                        done = true;
                    }
                }
                if (!done)
                    message = "Concept map " + su + " found no usable translation for " + src.getCode();
            }
        }
        if (!done)
            throw new FHIRException(message);
        if (outcome == null)
            return null;
        if ("code".equals(fieldToReturn))
            return new CodeType(outcome.getCode());
        else
            return outcome;
    }
}
Also used : FHIRException(org.hl7.fhir.exceptions.FHIRException) StructureMap(org.hl7.fhir.r4b.model.StructureMap) TargetElementComponent(org.hl7.fhir.r4b.model.ConceptMap.TargetElementComponent) ConceptMapGroupComponent(org.hl7.fhir.r4b.model.ConceptMap.ConceptMapGroupComponent) SourceElementComponent(org.hl7.fhir.r4b.model.ConceptMap.SourceElementComponent)

Example 69 with UriType

use of org.hl7.fhir.r4.model.UriType in project org.hl7.fhir.core by hapifhir.

the class StructureMapUtilities method runTransform.

private Base runTransform(String ruleId, TransformContext context, StructureMap map, StructureMapGroupComponent group, StructureMapGroupRuleTargetComponent tgt, Variables vars, Base dest, String element, String srcVar, boolean root) throws FHIRException {
    try {
        switch(tgt.getTransform()) {
            case CREATE:
                String tn;
                if (tgt.getParameter().isEmpty()) {
                    // we have to work out the type. First, we see if there is a single type for the target. If there is, we use that
                    String[] types = dest.getTypesForProperty(element.hashCode(), element);
                    if (types.length == 1 && !"*".equals(types[0]) && !types[0].equals("Resource"))
                        tn = types[0];
                    else if (srcVar != null) {
                        tn = determineTypeFromSourceType(map, group, vars.get(VariableMode.INPUT, srcVar), types);
                    } else
                        throw new Error("Cannot determine type implicitly because there is no single input variable");
                } else {
                    tn = getParamStringNoNull(vars, tgt.getParameter().get(0), tgt.toString());
                    // ok, now we resolve the type name against the import statements
                    for (StructureMapStructureComponent uses : map.getStructure()) {
                        if (uses.getMode() == StructureMapModelMode.TARGET && uses.hasAlias() && tn.equals(uses.getAlias())) {
                            tn = uses.getUrl();
                            break;
                        }
                    }
                }
                Base res = services != null ? services.createType(context.getAppInfo(), tn) : ResourceFactory.createResourceOrType(tn);
                if (res.isResource() && !res.fhirType().equals("Parameters")) {
                    // res.setIdBase(tgt.getParameter().size() > 1 ? getParamString(vars, tgt.getParameter().get(0)) : UUID.randomUUID().toString().toLowerCase());
                    if (services != null)
                        res = services.createResource(context.getAppInfo(), res, root);
                }
                if (tgt.hasUserData("profile"))
                    res.setUserData("profile", tgt.getUserData("profile"));
                return res;
            case COPY:
                return getParam(vars, tgt.getParameter().get(0));
            case EVALUATE:
                ExpressionNode expr = (ExpressionNode) tgt.getUserData(MAP_EXPRESSION);
                if (expr == null) {
                    expr = fpe.parse(getParamStringNoNull(vars, tgt.getParameter().get(0), tgt.toString()));
                    tgt.setUserData(MAP_WHERE_EXPRESSION, expr);
                }
                List<Base> v = fpe.evaluate(vars, null, null, tgt.getParameter().size() == 2 ? getParam(vars, tgt.getParameter().get(0)) : new BooleanType(false), expr);
                if (v.size() == 0)
                    return null;
                else if (v.size() != 1)
                    throw new FHIRException("Rule \"" + ruleId + "\": Evaluation of " + expr.toString() + " returned " + v.size() + " objects");
                else
                    return v.get(0);
            case TRUNCATE:
                String src = getParamString(vars, tgt.getParameter().get(0));
                String len = getParamStringNoNull(vars, tgt.getParameter().get(1), tgt.toString());
                if (Utilities.isInteger(len)) {
                    int l = Integer.parseInt(len);
                    if (src.length() > l)
                        src = src.substring(0, l);
                }
                return new StringType(src);
            case ESCAPE:
                throw new Error("Rule \"" + ruleId + "\": Transform " + tgt.getTransform().toCode() + " not supported yet");
            case CAST:
                src = getParamString(vars, tgt.getParameter().get(0));
                if (tgt.getParameter().size() == 1)
                    throw new FHIRException("Implicit type parameters on cast not yet supported");
                String t = getParamString(vars, tgt.getParameter().get(1));
                if (t.equals("string"))
                    return new StringType(src);
                else
                    throw new FHIRException("cast to " + t + " not yet supported");
            case APPEND:
                StringBuilder sb = new StringBuilder(getParamString(vars, tgt.getParameter().get(0)));
                for (int i = 1; i < tgt.getParameter().size(); i++) sb.append(getParamString(vars, tgt.getParameter().get(i)));
                return new StringType(sb.toString());
            case TRANSLATE:
                return translate(context, map, vars, tgt.getParameter());
            case REFERENCE:
                Base b = getParam(vars, tgt.getParameter().get(0));
                if (b == null)
                    throw new FHIRException("Rule \"" + ruleId + "\": Unable to find parameter " + ((IdType) tgt.getParameter().get(0).getValue()).asStringValue());
                if (!b.isResource())
                    throw new FHIRException("Rule \"" + ruleId + "\": Transform engine cannot point at an element of type " + b.fhirType());
                else {
                    String id = b.getIdBase();
                    if (id == null) {
                        id = UUID.randomUUID().toString().toLowerCase();
                        b.setIdBase(id);
                    }
                    return new Reference().setReference(b.fhirType() + "/" + id);
                }
            case DATEOP:
                throw new Error("Rule \"" + ruleId + "\": Transform " + tgt.getTransform().toCode() + " not supported yet");
            case UUID:
                return new IdType(UUID.randomUUID().toString());
            case POINTER:
                b = getParam(vars, tgt.getParameter().get(0));
                if (b instanceof Resource)
                    return new UriType("urn:uuid:" + ((Resource) b).getId());
                else
                    throw new FHIRException("Rule \"" + ruleId + "\": Transform engine cannot point at an element of type " + b.fhirType());
            case CC:
                CodeableConcept cc = new CodeableConcept();
                cc.addCoding(buildCoding(getParamStringNoNull(vars, tgt.getParameter().get(0), tgt.toString()), getParamStringNoNull(vars, tgt.getParameter().get(1), tgt.toString())));
                return cc;
            case C:
                Coding c = buildCoding(getParamStringNoNull(vars, tgt.getParameter().get(0), tgt.toString()), getParamStringNoNull(vars, tgt.getParameter().get(1), tgt.toString()));
                return c;
            default:
                throw new Error("Rule \"" + ruleId + "\": Transform Unknown: " + tgt.getTransform().toCode());
        }
    } catch (Exception e) {
        throw new FHIRException("Exception executing transform " + tgt.toString() + " on Rule \"" + ruleId + "\": " + e.getMessage(), e);
    }
}
Also used : CommaSeparatedStringBuilder(org.hl7.fhir.utilities.CommaSeparatedStringBuilder) FHIRFormatError(org.hl7.fhir.exceptions.FHIRFormatError) FHIRException(org.hl7.fhir.exceptions.FHIRException) NotImplementedException(org.apache.commons.lang3.NotImplementedException) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) PathEngineException(org.hl7.fhir.exceptions.PathEngineException) FHIRLexerException(org.hl7.fhir.r4b.utils.FHIRLexer.FHIRLexerException) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 70 with UriType

use of org.hl7.fhir.r4.model.UriType in project org.hl7.fhir.core by hapifhir.

the class XVerExtensionManager method makeDefinition.

public StructureDefinition makeDefinition(String url) {
    String verSource = url.substring(20, 23);
    String verTarget = VersionUtilities.getMajMin(context.getVersion());
    String e = url.substring(54);
    JsonObject root = lists.get(verSource);
    JsonObject path = root.getAsJsonObject(e);
    if (path == null) {
        path = root.getAsJsonObject(e + "[x]");
    }
    StructureDefinition sd = new StructureDefinition();
    sd.setUserData(XVER_EXT_MARKER, "true");
    sd.setUserData("path", PackageHacker.fixPackageUrl("https://hl7.org/fhir/versions.html#extensions"));
    sd.setUrl(url);
    sd.setVersion(context.getVersion());
    sd.setFhirVersion(FHIRVersion.fromCode(context.getVersion()));
    sd.setKind(StructureDefinitionKind.COMPLEXTYPE);
    sd.setType("Extension");
    sd.setDerivation(TypeDerivationRule.CONSTRAINT);
    sd.setName("Extension-" + verSource + "-" + e);
    sd.setTitle("Extension Definition for " + e + " for Version " + verSource);
    sd.setStatus(PublicationStatus.ACTIVE);
    sd.setExperimental(false);
    sd.setDate(new Date());
    sd.setPublisher("FHIR Project");
    sd.setPurpose("Defined so the validator can validate cross version extensions (see http://hl7.org/fhir/versions.html#extensions)");
    sd.setAbstract(false);
    sd.addContext().setType(ExtensionContextType.ELEMENT).setExpression(head(e));
    sd.setBaseDefinition("http://hl7.org/fhir/StructureDefinition/Extension");
    if (path.has("types")) {
        sd.getDifferential().addElement().setPath("Extension.extension").setMax("0");
        sd.getDifferential().addElement().setPath("Extension.url").setFixed(new UriType(url));
        ElementDefinition val = sd.getDifferential().addElement().setPath("Extension.value[x]").setMin(1);
        populateTypes(path, val, verSource, verTarget);
    } else if (path.has("elements")) {
        for (JsonElement i : path.getAsJsonArray("elements")) {
            JsonObject elt = root.getAsJsonObject(e + "." + i.getAsString());
            if (elt != null) {
                String s = i.getAsString().replace("[x]", "");
                sd.getDifferential().addElement().setPath("Extension.extension").setSliceName(s);
                sd.getDifferential().addElement().setPath("Extension.extension.extension").setMax("0");
                sd.getDifferential().addElement().setPath("Extension.extension.url").setFixed(new UriType(s));
                ElementDefinition val = sd.getDifferential().addElement().setPath("Extension.extension.value[x]").setMin(1);
                if (!elt.has("types")) {
                    throw new FHIRException("Internal error - nested elements not supported yet");
                }
                populateTypes(elt, val, verSource, verTarget);
            }
        }
        sd.getDifferential().addElement().setPath("Extension.url").setFixed(new UriType(url));
        sd.getDifferential().addElement().setPath("Extension.value[x]").setMax("0");
    } else {
        throw new FHIRException("Internal error - attempt to define extension for " + url + " when it is invalid");
    }
    if (path.has("modifier") && path.get("modifier").getAsBoolean()) {
        ElementDefinition baseDef = new ElementDefinition("Extension");
        sd.getDifferential().getElement().add(0, baseDef);
        baseDef.setIsModifier(true);
    }
    return sd;
}
Also used : StructureDefinition(org.hl7.fhir.r4b.model.StructureDefinition) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) ElementDefinition(org.hl7.fhir.r4b.model.ElementDefinition) FHIRException(org.hl7.fhir.exceptions.FHIRException) Date(java.util.Date) UriType(org.hl7.fhir.r4b.model.UriType)

Aggregations

FHIRException (org.hl7.fhir.exceptions.FHIRException)60 UriType (org.hl7.fhir.r5.model.UriType)45 ArrayList (java.util.ArrayList)42 UriType (org.hl7.fhir.r4.model.UriType)41 UriType (org.hl7.fhir.r4b.model.UriType)29 UriType (org.hl7.fhir.dstu3.model.UriType)26 CommaSeparatedStringBuilder (org.hl7.fhir.utilities.CommaSeparatedStringBuilder)24 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)22 ElementDefinition (org.hl7.fhir.r5.model.ElementDefinition)19 List (java.util.List)17 XhtmlNode (org.hl7.fhir.utilities.xhtml.XhtmlNode)16 NotImplementedException (org.apache.commons.lang3.NotImplementedException)15 TypeRefComponent (org.hl7.fhir.r5.model.ElementDefinition.TypeRefComponent)14 UriType (org.hl7.fhir.dstu2016may.model.UriType)12 FHIRFormatError (org.hl7.fhir.exceptions.FHIRFormatError)12 Coding (org.hl7.fhir.r4.model.Coding)12 IOException (java.io.IOException)11 StringType (org.hl7.fhir.r4.model.StringType)10 org.hl7.fhir.r5.model (org.hl7.fhir.r5.model)10 StringType (org.hl7.fhir.r5.model.StringType)10