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