Search in sources :

Example 1 with UnprocessableEntityException

use of org.ehrbase.api.exception.UnprocessableEntityException 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)

Example 2 with UnprocessableEntityException

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

the class TemplateStoreAccess method adminUpdateTemplate.

/**
 * Replaces the old content of a template with the new provided content in the database storage.
 * The target template id must be provided within the new template. This is a destructive
 * operation thus the old template will be checked against Compositions if there are any usages of
 * the template to avoid data inconsistencies.
 *
 * @param domainAccess - Database connection context
 * @param template     - New template data to store
 * @return - Updated template XML content
 */
public static String adminUpdateTemplate(I_DomainAccess domainAccess, OPERATIONALTEMPLATE template) {
    // Check if template is used anymore
    Result<AdminGetTemplateUsageRecord> usingCompositions = Routines.adminGetTemplateUsage(domainAccess.getContext().configuration(), template.getTemplateId().getValue());
    if (usingCompositions.isNotEmpty()) {
        // There are compositions using this template -> Return list of uuids
        throw new UnprocessableEntityException(String.format("Cannot delete template %s since the following compositions are still using it %s", template.getTemplateId().getValue(), usingCompositions.toString()));
    } else {
        XmlOptions opts = new XmlOptions();
        opts.setSaveSyntheticDocumentElement(new QName("http://schemas.openehr.org/v1", "template"));
        TemplateStoreRecord tsr = new TemplateStoreRecord();
        tsr.setContent(template.xmlText(opts));
        // Replace template with db function
        return Routines.adminUpdateTemplate(domainAccess.getContext().configuration(), template.getTemplateId().getValue(), template.xmlText(opts));
    }
}
Also used : UnprocessableEntityException(org.ehrbase.api.exception.UnprocessableEntityException) TemplateStoreRecord(org.ehrbase.jooq.pg.tables.records.TemplateStoreRecord) AdminGetTemplateUsageRecord(org.ehrbase.jooq.pg.tables.records.AdminGetTemplateUsageRecord) QName(javax.xml.namespace.QName) XmlOptions(org.apache.xmlbeans.XmlOptions)

Example 3 with UnprocessableEntityException

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

the class ValidationServiceImp method check.

@Override
public void check(String templateID, Composition composition) throws Exception {
    WebTemplate webTemplate;
    try {
        webTemplate = knowledgeCacheService.getQueryOptMetaData(templateID);
    } catch (IllegalArgumentException e) {
        throw new UnprocessableEntityException(e.getMessage());
    }
    // Validate the composition based on WebTemplate
    var constraintViolations = compositionValidator.validate(composition, webTemplate);
    if (!constraintViolations.isEmpty()) {
        throw new ConstraintViolationException(constraintViolations);
    }
    // check codephrases against terminologies
    ItemStructureVisitor itemStructureVisitor = new ItemStructureVisitor(terminologyService);
    itemStructureVisitor.validate(composition);
}
Also used : UnprocessableEntityException(org.ehrbase.api.exception.UnprocessableEntityException) WebTemplate(org.ehrbase.webtemplate.model.WebTemplate) ItemStructureVisitor(org.ehrbase.validation.terminology.ItemStructureVisitor) ConstraintViolationException(org.ehrbase.validation.ConstraintViolationException)

Aggregations

UnprocessableEntityException (org.ehrbase.api.exception.UnprocessableEntityException)3 UUID (java.util.UUID)1 QName (javax.xml.namespace.QName)1 XmlOptions (org.apache.xmlbeans.XmlOptions)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 ValidationException (org.ehrbase.api.exception.ValidationException)1 AdminGetTemplateUsageRecord (org.ehrbase.jooq.pg.tables.records.AdminGetTemplateUsageRecord)1 TemplateStoreRecord (org.ehrbase.jooq.pg.tables.records.TemplateStoreRecord)1 ConstraintViolationException (org.ehrbase.validation.ConstraintViolationException)1 ItemStructureVisitor (org.ehrbase.validation.terminology.ItemStructureVisitor)1 WebTemplate (org.ehrbase.webtemplate.model.WebTemplate)1