Search in sources :

Example 1 with ValidationException

use of org.ehrbase.api.exception.ValidationException in project ehrbase by ehrbase.

the class ValidationServiceImp method check.

@Override
public void check(EhrStatus ehrStatus) {
    // case of a system generated ehr
    if (ehrStatus == null) {
        return;
    }
    // first, check the built EhrStatus using the general Archie RM-Validator
    List<RMObjectValidationMessage> rmObjectValidationMessages = RM_OBJECT_VALIDATOR.validate(ehrStatus);
    if (!rmObjectValidationMessages.isEmpty()) {
        StringBuilder stringBuilder = new StringBuilder();
        for (RMObjectValidationMessage rmObjectValidationMessage : rmObjectValidationMessages) {
            stringBuilder.append(rmObjectValidationMessage.toString());
            stringBuilder.append("\n");
        }
        throw new ValidationException(stringBuilder.toString());
    }
    if (ehrStatus.getSubject() == null) {
        throw new ValidationException("subject is required");
    }
    if (ehrStatus.getSubject().getExternalRef() != null) {
        // but if it is there it has to have an ID
        if (ehrStatus.getSubject().getExternalRef().getId() == null || ehrStatus.getSubject().getExternalRef().getId().getValue().isEmpty()) {
            throw new ValidationException("ExternalRef ID is required");
        }
        // and a namespace
        if (ehrStatus.getSubject().getExternalRef().getNamespace() == null) {
            throw new ValidationException("ExternalRef namespace is required");
        // which needs to be valid
        } else if (!NAMESPACE_PATTERN.matcher(ehrStatus.getSubject().getExternalRef().getNamespace()).matches()) {
            throw new ValidationException("Subject's namespace format invalid");
        }
    }
}
Also used : ValidationException(org.ehrbase.api.exception.ValidationException) RMObjectValidationMessage(com.nedap.archie.rmobjectvalidator.RMObjectValidationMessage)

Example 2 with ValidationException

use of org.ehrbase.api.exception.ValidationException in project ehrbase by ehrbase.

the class CompositionServiceImp method internalCreate.

/**
 * Creation of a new composition. With optional custom contribution, or one will be created.
 *
 * @param ehrId          ID of EHR
 * @param composition    RMObject instance of the given Composition to be created
 * @param systemId       Audit system; or NULL if contribution is given
 * @param committerId    Audit committer; or NULL if contribution is given
 * @param description    (Optional) Audit description; or NULL if contribution is given
 * @param contributionId NULL if is not needed, or ID of given custom contribution
 * @return ID of created composition
 * @throws InternalServerException when creation failed
 */
private UUID internalCreate(UUID ehrId, Composition composition, UUID systemId, UUID committerId, String description, UUID contributionId) {
    // pre-step: validate
    try {
        validationService.check(composition);
    } catch (org.ehrbase.validation.ValidationException e) {
        throw new UnprocessableEntityException(e.getMessage());
    } catch (UnprocessableEntityException | ValidationException e) {
        throw e;
    } catch (IllegalArgumentException e) {
        throw new ValidationException(e);
    } catch (Exception e) {
        throw new InternalServerException(e);
    }
    // pre-step: check for valid ehrId
    if (!ehrService.hasEhr(ehrId)) {
        throw new ObjectNotFoundException("ehr", "No EHR found with given ID: " + ehrId.toString());
    }
    // actual creation
    final UUID compositionId;
    try {
        var compositionAccess = I_CompositionAccess.getNewInstance(getDataAccess(), composition, ehrId);
        var entryAccess = I_EntryAccess.getNewInstance(getDataAccess(), Objects.requireNonNull(composition.getArchetypeDetails().getTemplateId()).getValue(), 0, compositionAccess.getId(), composition);
        compositionAccess.addContent(entryAccess);
        if (contributionId != null) {
            // in case of custom contribution, set it and invoke commit that allows custom
            // contributions
            compositionAccess.setContributionId(contributionId);
            compositionId = compositionAccess.commit(LocalDateTime.now(), contributionId);
        } else {
            // else, invoke commit that ad hoc creates a new contribution for the composition
            if (committerId == null || systemId == null) {
                // mandatory fields
                throw new InternalServerException("Error on internal contribution handling for composition creation.");
            }
            compositionId = compositionAccess.commit(LocalDateTime.now(), committerId, systemId, description);
        }
    } catch (IllegalArgumentException e) {
        throw e;
    } catch (Exception e) {
        throw new InternalServerException(e);
    }
    logger.debug("Composition created: id={}", compositionId);
    return compositionId;
}
Also used : UnprocessableEntityException(org.ehrbase.api.exception.UnprocessableEntityException) ValidationException(org.ehrbase.api.exception.ValidationException) InternalServerException(org.ehrbase.api.exception.InternalServerException) ObjectNotFoundException(org.ehrbase.api.exception.ObjectNotFoundException) UUID(java.util.UUID) UnexpectedSwitchCaseException(org.ehrbase.api.exception.UnexpectedSwitchCaseException) ObjectNotFoundException(org.ehrbase.api.exception.ObjectNotFoundException) InternalServerException(org.ehrbase.api.exception.InternalServerException) ValidationException(org.ehrbase.api.exception.ValidationException) InvalidApiParameterException(org.ehrbase.api.exception.InvalidApiParameterException) UnprocessableEntityException(org.ehrbase.api.exception.UnprocessableEntityException)

Aggregations

ValidationException (org.ehrbase.api.exception.ValidationException)2 RMObjectValidationMessage (com.nedap.archie.rmobjectvalidator.RMObjectValidationMessage)1 UUID (java.util.UUID)1 InternalServerException (org.ehrbase.api.exception.InternalServerException)1 InvalidApiParameterException (org.ehrbase.api.exception.InvalidApiParameterException)1 ObjectNotFoundException (org.ehrbase.api.exception.ObjectNotFoundException)1 UnexpectedSwitchCaseException (org.ehrbase.api.exception.UnexpectedSwitchCaseException)1 UnprocessableEntityException (org.ehrbase.api.exception.UnprocessableEntityException)1