Search in sources :

Example 66 with JsonParser

use of org.hl7.fhir.r4b.formats.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")), "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.r4b.formats.JsonParser)

Example 67 with JsonParser

use of org.hl7.fhir.r4b.formats.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()));
    }
    JsonParser json = new JsonParser();
    json.setOutputStyle(OutputStyle.PRETTY);
    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(hashNWS(ct.request));
    return ct;
}
Also used : Coding(org.hl7.fhir.r4b.model.Coding) IOException(java.io.IOException) ValueSet(org.hl7.fhir.r4b.model.ValueSet) JsonParser(org.hl7.fhir.r4b.formats.JsonParser)

Example 68 with JsonParser

use of org.hl7.fhir.r4b.formats.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());
    else
        ct.name = NAME_FOR_NO_SYSTEM;
    JsonParser json = new JsonParser();
    json.setOutputStyle(OutputStyle.PRETTY);
    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(hashNWS(ct.request));
    return ct;
}
Also used : IOException(java.io.IOException) ValueSet(org.hl7.fhir.r4b.model.ValueSet) JsonParser(org.hl7.fhir.r4b.formats.JsonParser)

Example 69 with JsonParser

use of org.hl7.fhir.r4b.formats.JsonParser in project org.hl7.fhir.core by hapifhir.

the class TerminologyCache method generateExpandToken.

public CacheToken generateExpandToken(ValueSet vs, boolean heirarchical) {
    CacheToken ct = new CacheToken();
    ValueSet vsc = getVSEssense(vs);
    for (ConceptSetComponent inc : vs.getCompose().getInclude()) if (inc.hasSystem())
        ct.setName(getNameForSystem(inc.getSystem()));
    for (ConceptSetComponent inc : vs.getCompose().getExclude()) if (inc.hasSystem())
        ct.setName(getNameForSystem(inc.getSystem()));
    for (ValueSetExpansionContainsComponent inc : vs.getExpansion().getContains()) if (inc.hasSystem())
        ct.setName(getNameForSystem(inc.getSystem()));
    JsonParser json = new JsonParser();
    json.setOutputStyle(OutputStyle.PRETTY);
    try {
        ct.request = "{\"hierarchical\" : " + (heirarchical ? "true" : "false") + ", \"valueSet\" :" + extracted(json, vsc) + "}\r\n";
    } catch (IOException e) {
        throw new Error(e);
    }
    ct.key = String.valueOf(hashNWS(ct.request));
    return ct;
}
Also used : ConceptSetComponent(org.hl7.fhir.r4b.model.ValueSet.ConceptSetComponent) ValueSetExpansionContainsComponent(org.hl7.fhir.r4b.model.ValueSet.ValueSetExpansionContainsComponent) IOException(java.io.IOException) ValueSet(org.hl7.fhir.r4b.model.ValueSet) JsonParser(org.hl7.fhir.r4b.formats.JsonParser)

Example 70 with JsonParser

use of org.hl7.fhir.r4b.formats.JsonParser in project org.hl7.fhir.core by hapifhir.

the class TerminologyCache method load.

private void load() throws FHIRException {
    for (String fn : new File(folder).list()) {
        if (fn.endsWith(".cache") && !fn.equals("validation.cache")) {
            int c = 0;
            try {
                String title = fn.substring(0, fn.lastIndexOf("."));
                NamedCache nc = new NamedCache();
                nc.name = title;
                caches.put(title, nc);
                String src = TextFile.fileToString(Utilities.path(folder, fn));
                if (src.startsWith("?"))
                    src = src.substring(1);
                int i = src.indexOf(ENTRY_MARKER);
                while (i > -1) {
                    c++;
                    String s = src.substring(0, i);
                    src = src.substring(i + ENTRY_MARKER.length() + 1);
                    i = src.indexOf(ENTRY_MARKER);
                    if (!Utilities.noString(s)) {
                        int j = s.indexOf(BREAK);
                        String q = s.substring(0, j);
                        String p = s.substring(j + BREAK.length() + 1).trim();
                        CacheEntry ce = new CacheEntry();
                        ce.persistent = true;
                        ce.request = q;
                        boolean e = p.charAt(0) == 'e';
                        p = p.substring(3);
                        JsonObject o = (JsonObject) new com.google.gson.JsonParser().parse(p);
                        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);
                        }
                        nc.map.put(String.valueOf(hashNWS(ce.request)), ce);
                        nc.list.add(ce);
                    }
                }
            } catch (Exception e) {
                throw new FHIRException("Error loading " + fn + ": " + e.getMessage() + " entry " + c, e);
            }
        }
    }
}
Also used : TerminologyServiceErrorClass(org.hl7.fhir.r4b.terminologies.ValueSetExpander.TerminologyServiceErrorClass) JsonObject(com.google.gson.JsonObject) ValidationResult(org.hl7.fhir.r4b.context.IWorkerContext.ValidationResult) FHIRException(org.hl7.fhir.exceptions.FHIRException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) FHIRException(org.hl7.fhir.exceptions.FHIRException) ConceptDefinitionComponent(org.hl7.fhir.r4b.model.CodeSystem.ConceptDefinitionComponent) IssueSeverity(org.hl7.fhir.utilities.validation.ValidationMessage.IssueSeverity) ValueSetExpansionOutcome(org.hl7.fhir.r4b.terminologies.ValueSetExpander.ValueSetExpansionOutcome) File(java.io.File) TextFile(org.hl7.fhir.utilities.TextFile) JsonParser(org.hl7.fhir.r4b.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