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());
}
}
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;
}
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;
}
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;
}
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);
}
}
}
}
Aggregations