Search in sources :

Example 16 with Coding

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;
}
Also used : Coding(com.b2international.snowowl.fhir.core.model.dt.Coding) HashSet(java.util.HashSet) CodeableConcept(com.b2international.snowowl.fhir.core.model.dt.CodeableConcept)

Example 17 with Coding

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();
}
Also used : ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) ValidateCodeRequest(com.b2international.snowowl.fhir.core.model.codesystem.ValidateCodeRequest) Set(java.util.Set) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) HashSet(java.util.HashSet) Valid(javax.validation.Valid) CodeSystem(com.b2international.snowowl.fhir.core.model.codesystem.CodeSystem) Concept(com.b2international.snowowl.core.domain.Concept) Map(java.util.Map) ServiceProvider(com.b2international.snowowl.core.ServiceProvider) JsonUnwrapped(com.fasterxml.jackson.annotation.JsonUnwrapped) ValidateCodeResult(com.b2international.snowowl.fhir.core.model.ValidateCodeResult) CodeSystemRequests(com.b2international.snowowl.core.codesystem.CodeSystemRequests) Coding(com.b2international.snowowl.fhir.core.model.dt.Coding) CodeableConcept(com.b2international.snowowl.fhir.core.model.dt.CodeableConcept) Concept(com.b2international.snowowl.core.domain.Concept) CodeableConcept(com.b2international.snowowl.fhir.core.model.dt.CodeableConcept) Coding(com.b2international.snowowl.fhir.core.model.dt.Coding)

Example 18 with Coding

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();
}
Also used : Issue(com.b2international.snowowl.fhir.core.model.Issue) Coding(com.b2international.snowowl.fhir.core.model.dt.Coding) Builder(com.b2international.snowowl.fhir.core.model.OperationOutcome.Builder) CodeableConcept(com.b2international.snowowl.fhir.core.model.dt.CodeableConcept)

Example 19 with Coding

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());
}
Also used : OperationOutcomeCode(com.b2international.snowowl.fhir.core.codesystems.OperationOutcomeCode) ValidationException(com.b2international.snowowl.fhir.core.exceptions.ValidationException) IssueSeverity(com.b2international.snowowl.fhir.core.codesystems.IssueSeverity) Parameter(com.b2international.snowowl.fhir.core.model.dt.Parameter) FhirTest(com.b2international.snowowl.fhir.tests.FhirTest) Assert.assertTrue(org.junit.Assert.assertTrue) Fhir(com.b2international.snowowl.fhir.core.model.dt.Parameters.Fhir) Test(org.junit.Test) Uri(com.b2international.snowowl.fhir.core.model.dt.Uri) LookupRequest(com.b2international.snowowl.fhir.core.model.codesystem.LookupRequest) IssueType(com.b2international.snowowl.fhir.core.codesystems.IssueType) Json(com.b2international.snowowl.fhir.core.model.dt.Parameters.Json) ExpandValueSetRequest(com.b2international.snowowl.fhir.core.model.valueset.ExpandValueSetRequest) Issue(com.b2international.snowowl.fhir.core.model.Issue) Optional(java.util.Optional) Builder(com.b2international.snowowl.fhir.core.model.Issue.Builder) Parameters(com.b2international.snowowl.fhir.core.model.dt.Parameters) Coding(com.b2international.snowowl.fhir.core.model.dt.Coding) FhirExceptionIssueMatcher(com.b2international.snowowl.fhir.tests.FhirExceptionIssueMatcher) Coding(com.b2international.snowowl.fhir.core.model.dt.Coding) Parameter(com.b2international.snowowl.fhir.core.model.dt.Parameter) Fhir(com.b2international.snowowl.fhir.core.model.dt.Parameters.Fhir) LookupRequest(com.b2international.snowowl.fhir.core.model.codesystem.LookupRequest)

Example 20 with Coding

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);
}
Also used : Parameters(com.b2international.snowowl.fhir.core.model.dt.Parameters) Coding(com.b2international.snowowl.fhir.core.model.dt.Coding) Fhir(com.b2international.snowowl.fhir.core.model.dt.Parameters.Fhir) Json(com.b2international.snowowl.fhir.core.model.dt.Parameters.Json) LookupRequest(com.b2international.snowowl.fhir.core.model.codesystem.LookupRequest)

Aggregations

Coding (com.b2international.snowowl.fhir.core.model.dt.Coding)24 Test (org.junit.Test)16 FhirTest (com.b2international.snowowl.fhir.tests.FhirTest)13 CodeableConcept (com.b2international.snowowl.fhir.core.model.dt.CodeableConcept)11 Fhir (com.b2international.snowowl.fhir.core.model.dt.Parameters.Fhir)9 Issue (com.b2international.snowowl.fhir.core.model.Issue)6 LookupRequest (com.b2international.snowowl.fhir.core.model.codesystem.LookupRequest)6 Parameters (com.b2international.snowowl.fhir.core.model.dt.Parameters)5 Json (com.b2international.snowowl.fhir.core.model.dt.Parameters.Json)5 OperationOutcomeCode (com.b2international.snowowl.fhir.core.codesystems.OperationOutcomeCode)3 ValidateCodeRequest (com.b2international.snowowl.fhir.core.model.codesystem.ValidateCodeRequest)3 Parameter (com.b2international.snowowl.fhir.core.model.dt.Parameter)3 FhirRestTest (com.b2international.snowowl.fhir.tests.FhirRestTest)3 JsonPath (io.restassured.path.json.JsonPath)3 IssueSeverity (com.b2international.snowowl.fhir.core.codesystems.IssueSeverity)2 IssueType (com.b2international.snowowl.fhir.core.codesystems.IssueType)2 ValidationException (com.b2international.snowowl.fhir.core.exceptions.ValidationException)2 Designation (com.b2international.snowowl.fhir.core.model.Designation)2 Builder (com.b2international.snowowl.fhir.core.model.Issue.Builder)2 Uri (com.b2international.snowowl.fhir.core.model.dt.Uri)2