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