Search in sources :

Example 76 with JsonParser

use of org.hl7.fhir.r5.elementmodel.JsonParser in project org.hl7.fhir.core by hapifhir.

the class TerminologyCache method save.

private void save(NamedCache nc) {
    if (folder == null)
        return;
    try {
        OutputStreamWriter sw = new OutputStreamWriter(new FileOutputStream(Utilities.path(folder, nc.name + CACHE_FILE_EXTENSION)), "UTF-8");
        sw.write(ENTRY_MARKER + "\r\n");
        JsonParser json = new JsonParser();
        json.setOutputStyle(OutputStyle.PRETTY);
        for (CacheEntry ce : nc.list) {
            sw.write(ce.request.trim());
            sw.write(BREAK + "\r\n");
            if (ce.e != null) {
                sw.write("e: {\r\n");
                if (ce.e.getValueset() != null)
                    sw.write("  \"valueSet\" : " + json.composeString(ce.e.getValueset()).trim() + ",\r\n");
                sw.write("  \"error\" : \"" + Utilities.escapeJson(ce.e.getError()).trim() + "\"\r\n}\r\n");
            } else {
                sw.write("v: {\r\n");
                boolean first = true;
                if (ce.v.getDisplay() != null) {
                    if (first)
                        first = false;
                    else
                        sw.write(",\r\n");
                    sw.write("  \"display\" : \"" + Utilities.escapeJson(ce.v.getDisplay()).trim() + "\"");
                }
                if (ce.v.getCode() != null) {
                    if (first)
                        first = false;
                    else
                        sw.write(",\r\n");
                    sw.write("  \"code\" : \"" + Utilities.escapeJson(ce.v.getCode()).trim() + "\"");
                }
                if (ce.v.getSystem() != null) {
                    if (first)
                        first = false;
                    else
                        sw.write(",\r\n");
                    sw.write("  \"system\" : \"" + Utilities.escapeJson(ce.v.getSystem()).trim() + "\"");
                }
                if (ce.v.getSeverity() != null) {
                    if (first)
                        first = false;
                    else
                        sw.write(",\r\n");
                    sw.write("  \"severity\" : " + "\"" + ce.v.getSeverity().toCode().trim() + "\"" + "");
                }
                if (ce.v.getMessage() != null) {
                    if (first)
                        first = false;
                    else
                        sw.write(",\r\n");
                    sw.write("  \"error\" : \"" + Utilities.escapeJson(ce.v.getMessage()).trim() + "\"");
                }
                if (ce.v.getErrorClass() != null) {
                    if (first)
                        first = false;
                    else
                        sw.write(",\r\n");
                    sw.write("  \"class\" : \"" + Utilities.escapeJson(ce.v.getErrorClass().toString()) + "\"");
                }
                if (ce.v.getDefinition() != null) {
                    if (first)
                        first = false;
                    else
                        sw.write(",\r\n");
                    sw.write("  \"definition\" : \"" + Utilities.escapeJson(ce.v.getDefinition()).trim() + "\"");
                }
                sw.write("\r\n}\r\n");
            }
            sw.write(ENTRY_MARKER + "\r\n");
        }
        sw.close();
    } catch (Exception e) {
        System.out.println("error saving " + nc.name + ": " + e.getMessage());
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) FHIRException(org.hl7.fhir.exceptions.FHIRException) JsonParser(org.hl7.fhir.r5.formats.JsonParser)

Example 77 with JsonParser

use of org.hl7.fhir.r5.elementmodel.JsonParser in project org.hl7.fhir.core by hapifhir.

the class TerminologyCache method getCacheEntry.

private CacheEntry getCacheEntry(String request, String resultString) throws IOException {
    CacheEntry ce = new CacheEntry();
    ce.persistent = true;
    ce.request = request;
    boolean e = resultString.charAt(0) == 'e';
    resultString = resultString.substring(3);
    JsonObject o = (JsonObject) new com.google.gson.JsonParser().parse(resultString);
    String error = loadJS(o.get("error"));
    if (e) {
        if (o.has("valueSet"))
            ce.e = new ValueSetExpansionOutcome((ValueSet) new JsonParser().parse(o.getAsJsonObject("valueSet")), error, TerminologyServiceErrorClass.UNKNOWN);
        else
            ce.e = new ValueSetExpansionOutcome(error, TerminologyServiceErrorClass.UNKNOWN);
    } else {
        String t = loadJS(o.get("severity"));
        IssueSeverity severity = t == null ? null : IssueSeverity.fromCode(t);
        String display = loadJS(o.get("display"));
        String code = loadJS(o.get("code"));
        String system = loadJS(o.get("system"));
        String definition = loadJS(o.get("definition"));
        t = loadJS(o.get("class"));
        TerminologyServiceErrorClass errorClass = t == null ? null : TerminologyServiceErrorClass.valueOf(t);
        ce.v = new ValidationResult(severity, error, system, new ConceptDefinitionComponent().setDisplay(display).setDefinition(definition).setCode(code)).setErrorClass(errorClass);
    }
    return ce;
}
Also used : TerminologyServiceErrorClass(org.hl7.fhir.r5.terminologies.ValueSetExpander.TerminologyServiceErrorClass) ConceptDefinitionComponent(org.hl7.fhir.r5.model.CodeSystem.ConceptDefinitionComponent) IssueSeverity(org.hl7.fhir.utilities.validation.ValidationMessage.IssueSeverity) JsonObject(com.google.gson.JsonObject) ValueSetExpansionOutcome(org.hl7.fhir.r5.terminologies.ValueSetExpander.ValueSetExpansionOutcome) ValidationResult(org.hl7.fhir.r5.context.IWorkerContext.ValidationResult) JsonParser(org.hl7.fhir.r5.formats.JsonParser)

Example 78 with JsonParser

use of org.hl7.fhir.r5.elementmodel.JsonParser in project org.hl7.fhir.core by hapifhir.

the class TerminologyCache method save.

private <K extends Resource> void save(K resource, String title) {
    if (folder == null)
        return;
    try {
        OutputStreamWriter sw = new OutputStreamWriter(new FileOutputStream(Utilities.path(folder, title + CACHE_FILE_EXTENSION)), "UTF-8");
        JsonParser json = new JsonParser();
        json.setOutputStyle(OutputStyle.PRETTY);
        sw.write(json.composeString(resource).trim());
        sw.close();
    } catch (Exception e) {
        System.out.println("error saving capability statement " + e.getMessage());
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) FHIRException(org.hl7.fhir.exceptions.FHIRException) JsonParser(org.hl7.fhir.r5.formats.JsonParser)

Example 79 with JsonParser

use of org.hl7.fhir.r5.elementmodel.JsonParser in project org.hl7.fhir.core by hapifhir.

the class TerminologyCache method generateValidationToken.

public CacheToken generateValidationToken(ValidationOptions options, Coding code, ValueSet vs) {
    CacheToken ct = new CacheToken();
    if (code.hasSystem()) {
        ct.name = getNameForSystem(code.getSystem());
        ct.hasVersion = code.hasVersion();
    } else
        ct.name = NAME_FOR_NO_SYSTEM;
    nameCacheToken(vs, ct);
    JsonParser json = new JsonParser();
    json.setOutputStyle(OutputStyle.PRETTY);
    if (vs != null && vs.hasUrl() && vs.hasVersion()) {
        try {
            ct.request = "{\"code\" : " + json.composeString(code, "codeableConcept") + ", \"url\": \"" + Utilities.escapeJson(vs.getUrl()) + "\", \"version\": \"" + Utilities.escapeJson(vs.getVersion()) + "\"" + (options == null ? "" : ", " + options.toJson()) + "}\r\n";
        } catch (IOException e) {
            throw new Error(e);
        }
    } else {
        ValueSet vsc = getVSEssense(vs);
        try {
            ct.request = "{\"code\" : " + json.composeString(code, "code") + ", \"valueSet\" :" + (vsc == null ? "null" : extracted(json, vsc)) + (options == null ? "" : ", " + options.toJson()) + "}";
        } catch (IOException e) {
            throw new Error(e);
        }
    }
    ct.key = String.valueOf(hashJson(ct.request));
    return ct;
}
Also used : IOException(java.io.IOException) JsonParser(org.hl7.fhir.r5.formats.JsonParser)

Example 80 with JsonParser

use of org.hl7.fhir.r5.elementmodel.JsonParser in project org.hl7.fhir.core by hapifhir.

the class TerminologyCache method generateValidationToken.

public CacheToken generateValidationToken(ValidationOptions options, CodeableConcept code, ValueSet vs) {
    CacheToken ct = new CacheToken();
    for (Coding c : code.getCoding()) {
        if (c.hasSystem()) {
            ct.setName(getNameForSystem(c.getSystem()));
            ct.hasVersion = c.hasVersion();
        }
    }
    nameCacheToken(vs, ct);
    JsonParser json = new JsonParser();
    json.setOutputStyle(OutputStyle.PRETTY);
    if (vs != null && vs.hasUrl() && vs.hasVersion()) {
        try {
            ct.request = "{\"code\" : " + json.composeString(code, "codeableConcept") + ", \"url\": \"" + Utilities.escapeJson(vs.getUrl()) + "\", \"version\": \"" + Utilities.escapeJson(vs.getVersion()) + "\"" + (options == null ? "" : ", " + options.toJson()) + "+}\r\n";
        } catch (IOException e) {
            throw new Error(e);
        }
    } else {
        ValueSet vsc = getVSEssense(vs);
        try {
            ct.request = "{\"code\" : " + json.composeString(code, "codeableConcept") + ", \"valueSet\" :" + extracted(json, vsc) + (options == null ? "" : ", " + options.toJson()) + "}";
        } catch (IOException e) {
            throw new Error(e);
        }
    }
    ct.key = String.valueOf(hashJson(ct.request));
    return ct;
}
Also used : IOException(java.io.IOException) JsonParser(org.hl7.fhir.r5.formats.JsonParser)

Aggregations

FileOutputStream (java.io.FileOutputStream)82 JsonParser (org.hl7.fhir.r5.formats.JsonParser)66 FHIRException (org.hl7.fhir.exceptions.FHIRException)58 FileInputStream (java.io.FileInputStream)53 IOException (java.io.IOException)49 XmlParser (org.hl7.fhir.r5.formats.XmlParser)44 ByteArrayOutputStream (java.io.ByteArrayOutputStream)41 File (java.io.File)36 JsonParser (org.hl7.fhir.r4.formats.JsonParser)35 CSFileInputStream (org.hl7.fhir.utilities.CSFileInputStream)29 JsonParser (org.hl7.fhir.dstu3.formats.JsonParser)26 JsonParser (org.hl7.fhir.r4b.formats.JsonParser)26 CSFile (org.hl7.fhir.utilities.CSFile)26 ArrayList (java.util.ArrayList)23 TextFile (org.hl7.fhir.utilities.TextFile)20 InputStream (java.io.InputStream)18 IParser (org.hl7.fhir.r5.formats.IParser)18 Resource (org.hl7.fhir.dstu3.model.Resource)17 FileNotFoundException (java.io.FileNotFoundException)16 Resource (org.hl7.fhir.r4.model.Resource)16