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