use of com.b2international.snowowl.fhir.core.model.dt.Coding in project snow-owl by b2ihealthcare.
the class FhirValidateCodeRequest method collectCodingsToValidate.
private Set<Coding> collectCodingsToValidate(ValidateCodeRequest request) {
Set<Coding> codings = new HashSet<>(3);
if (request.getCode() != null) {
Coding coding = Coding.builder().code(request.getCode()).display(request.getDisplay()).build();
codings.add(coding);
}
if (request.getCoding() != null) {
codings.add(request.getCoding());
}
CodeableConcept codeableConcept = request.getCodeableConcept();
if (codeableConcept != null) {
if (codeableConcept.getCodings() != null) {
codeableConcept.getCodings().forEach(c -> codings.add(c));
}
}
return codings;
}
use of com.b2international.snowowl.fhir.core.model.dt.Coding in project snow-owl by b2ihealthcare.
the class FhirValidateCodeRequest method doExecute.
@Override
public ValidateCodeResult doExecute(ServiceProvider context, CodeSystem codeSystem) {
Set<Coding> codings = collectCodingsToValidate(request);
Map<String, Coding> codingsById = codings.stream().collect(Collectors.toMap(Coding::getCodeValue, c -> c));
// extract locales from the request
Map<String, Concept> conceptsById = CodeSystemRequests.prepareSearchConcepts().setLimit(codingsById.keySet().size()).filterByCodeSystemUri(codeSystem.getResourceURI()).filterByIds(codingsById.keySet()).setLocales(extractLocales(request.getDisplayLanguage())).buildAsync().execute(context).stream().collect(Collectors.toMap(Concept::getId, c -> c));
// check if both Maps have the same keys and report if not
Set<String> missingConceptIds = Sets.difference(codingsById.keySet(), conceptsById.keySet());
if (!missingConceptIds.isEmpty()) {
return ValidateCodeResult.builder().result(false).message(String.format("Could not find code%s '%s'.", missingConceptIds.size() == 1 ? "" : "s", ImmutableSortedSet.copyOf(missingConceptIds))).build();
}
// XXX it would be great to have support for multiple messages/validation results in a single request
for (String id : codingsById.keySet()) {
// check display if provided
Coding providedCoding = codingsById.get(id);
if (providedCoding.getDisplay() != null) {
Concept concept = conceptsById.get(id);
if (!providedCoding.getDisplay().equals(concept.getTerm())) {
return ValidateCodeResult.builder().result(false).display(concept.getTerm()).message(String.format("Incorrect display '%s' for code '%s'.", providedCoding.getDisplay(), providedCoding.getCodeValue())).build();
}
}
}
return ValidateCodeResult.builder().result(true).build();
}
use of com.b2international.snowowl.fhir.core.model.dt.Coding in project snow-owl by b2ihealthcare.
the class ValidationException method toOperationOutcome.
/**
* Creates an OperationOutcome representation from this exception. Useful when
* the exception must be propagated through protocols where Java serialization
* cannot be used (eg. HTTP), or the possible receiver cannot understand
* serialized Java class and object byte sequences.
*
* @return {@link OperationOutcome} representation of this
* {@link FhirException}, never <code>null</code>.
*/
@Override
public OperationOutcome toOperationOutcome() {
if (violations.isEmpty()) {
throw new IllegalArgumentException("There are no violations to report.");
}
Builder outcomeBuilder = OperationOutcome.builder();
for (ConstraintViolation<?> violation : violations) {
String issueDetails = String.format(getOperationOutcomeCode().getDisplayName(), violation.getPropertyPath());
StringBuilder builder = new StringBuilder(issueDetails);
builder.append(" [");
builder.append(violation.getInvalidValue());
builder.append("]. Violation: ");
builder.append(violation.getMessage());
builder.append(".");
Coding coding = Coding.builder().code(getOperationOutcomeCode().getCodeValue()).system(OperationOutcomeCode.CODE_SYSTEM_URI).display(builder.toString()).build();
CodeableConcept codeableConcept = CodeableConcept.builder().addCoding(coding).text(builder.toString()).build();
String location = violation.getRootBean().getClass().getSimpleName() + "." + violation.getPropertyPath().toString();
Issue issue = Issue.builder().severity(IssueSeverity.ERROR).code(IssueType.INVALID).details(codeableConcept).diagnostics(getMessage()).addLocation(location).build();
outcomeBuilder.addIssue(issue);
}
return outcomeBuilder.build();
}
use of com.b2international.snowowl.fhir.core.model.dt.Coding in project snow-owl by b2ihealthcare.
the class ExpandValueSetRequestDeserializationTest method testDeserialization.
// @Test
public void testDeserialization() {
Coding coding = Coding.builder().system("http://hl7.org/fhir/issue-severity").code("fatal").build();
LookupRequest request = LookupRequest.builder().coding(coding).build();
Fhir fhirParameters = new Parameters.Fhir(request);
fhirParameters.getParameters().forEach(p -> System.out.println(p));
Optional<Parameter> findFirst = fhirParameters.getParameters().stream().filter(p -> {
Coding pCoding = (Coding) p.getValue();
return pCoding.getSystemValue().equals("http://hl7.org/fhir/issue-severity");
}).findFirst();
assertTrue(findFirst.isPresent());
}
use of com.b2international.snowowl.fhir.core.model.dt.Coding in project snow-owl by b2ihealthcare.
the class ExpandValueSetRequestDeserializationTest method fullCircleTest.
// @Test
public void fullCircleTest() throws Exception {
Coding coding = Coding.builder().system("http://hl7.org/fhir/issue-severity").code("fatal").build();
System.out.println("Building the lookup request object.");
LookupRequest request = LookupRequest.builder().coding(coding).build();
Json json1 = new Parameters.Json(request);
System.out.println("JSON params:" + json1);
Fhir fhir = new Parameters.Fhir(json1.parameters());
String fhirJson = objectMapper.writeValueAsString(fhir);
System.out.println("This is the JSON request from the client: " + fhirJson);
System.out.println("This is happening in the server-side...");
Fhir parameters = objectMapper.readValue(fhirJson, Parameters.Fhir.class);
System.out.println("Deserialized into FHIR parameters..." + parameters.getParameters());
System.out.println("Back to Domain JSON...");
Json json = new Parameters.Json(parameters);
LookupRequest lookupRequest = objectMapper.convertValue(json, LookupRequest.class);
System.out.println("... and back to the object representation we started from:" + lookupRequest);
}
Aggregations