use of org.ehrbase.api.exception.StateConflictException in project ehrbase by ehrbase.
the class KnowledgeCacheService method addOperationalTemplateIntern.
public String addOperationalTemplateIntern(byte[] content, boolean overwrite) {
TemplateDocument document;
try {
document = TemplateDocument.Factory.parse(new ByteArrayInputStream(content));
} catch (XmlException | IOException e) {
throw new InvalidApiParameterException(e.getMessage());
}
OPERATIONALTEMPLATE template = document.getTemplate();
if (template == null) {
throw new InvalidApiParameterException("Could not parse input template");
}
if (template.getConcept() == null || template.getConcept().isEmpty()) {
throw new IllegalArgumentException("Supplied template has nil or empty concept");
}
if (template.getDefinition() == null || template.getDefinition().isNil()) {
throw new IllegalArgumentException("Supplied template has nil or empty definition");
}
if (template.getDescription() == null || !template.getDescription().validate()) {
throw new IllegalArgumentException("Supplied template has nil or empty description");
}
if (!TemplateUtils.isSupported(template)) {
throw new IllegalArgumentException(MessageFormat.format("The supplied template is not supported (unsupported types: {0})", String.join(",", TemplateUtils.UNSUPPORTED_RM_TYPES)));
}
String templateId;
try {
templateId = TemplateUtils.getTemplateId(template);
} catch (IllegalArgumentException a) {
throw new InvalidApiParameterException("Invalid template input content");
}
// pre-check: if already existing throw proper exception
if (!allowTemplateOverwrite && !overwrite && retrieveOperationalTemplate(templateId).isPresent()) {
throw new StateConflictException("Operational template with this template ID already exists: " + templateId);
} else {
invalidateCache(template);
}
templateStorage.storeTemplate(template);
putIntoCache(template);
if (cacheOptions.isPreBuildQueries()) {
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(1);
executor.submit(() -> {
try {
precalculateQueries(templateId);
} catch (RuntimeException e) {
log.error("An error occurred while processing template: {}", templateId);
}
});
}
// retrieve the template Id for this new entry
return templateId;
}
use of org.ehrbase.api.exception.StateConflictException in project ehrbase by ehrbase.
the class OpenehrEhrController method createEhrWithId.
@PutMapping(path = "/{ehr_id}")
@ResponseStatus(value = HttpStatus.CREATED)
@Override
public ResponseEntity<EhrResponseData> createEhrWithId(@RequestHeader(value = "openEHR-VERSION", required = false) String openehrVersion, @RequestHeader(value = "openEHR-AUDIT_DETAILS", required = false) String openehrAuditDetails, @RequestHeader(value = HttpHeaders.ACCEPT, required = false) String accept, @RequestHeader(value = PREFER, required = false) String prefer, @PathVariable(value = "ehr_id") String ehrIdString, @RequestBody(required = false) EhrStatus ehrStatus, HttpServletRequest request) {
// can't use getEhrUuid(..) because here another exception needs to be thrown (-> 400, not 404 in response)
UUID ehrId;
try {
ehrId = UUID.fromString(ehrIdString);
} catch (IllegalArgumentException e) {
throw new InvalidApiParameterException("EHR ID format not a UUID");
}
if (ehrService.hasEhr(ehrId)) {
throw new StateConflictException("EHR with this ID already exists");
}
final UUID resultEhrId;
if (ehrStatus != null) {
resultEhrId = ehrService.create(ehrStatus, ehrId);
} else {
resultEhrId = ehrService.create(null, ehrId);
}
if (!ehrId.equals(resultEhrId)) {
throw new InternalServerException("Error creating EHR with custom ID and/or status");
}
return internalPostEhrProcessing(accept, prefer, resultEhrId, request);
}
use of org.ehrbase.api.exception.StateConflictException in project ehrbase by ehrbase.
the class OpenehrDirectoryController method createDirectory.
/**
* {@inheritDoc}
*/
@Override
@PostMapping(path = "/{ehr_id}/directory")
public ResponseEntity<DirectoryResponseData> createDirectory(@PathVariable(name = "ehr_id") UUID ehrId, @RequestHeader(name = OPENEHR_VERSION, required = false) String openEhrVersion, @RequestHeader(name = OPENEHR_AUDIT_DETAILS, required = false) String openEhrAuditDetails, @RequestHeader(name = HttpHeaders.CONTENT_TYPE) String contentType, @RequestHeader(name = HttpHeaders.ACCEPT, defaultValue = MediaType.APPLICATION_JSON_VALUE) String accept, @RequestHeader(name = PREFER, defaultValue = RETURN_MINIMAL) String prefer, @RequestBody Folder folder) {
// Check for existence of EHR record
checkEhrExists(ehrId);
// Check for duplicate directories
if (ehrService.getDirectoryId(ehrId) != null) {
throw new StateConflictException(String.format("EHR with id %s already contains a directory.", ehrId.toString()));
}
var createdFolder = folderService.create(ehrId, folder).orElseThrow(() -> new InternalServerException("An error occurred while creating folder"));
return createDirectoryResponse(HttpMethod.POST, prefer, accept, createdFolder, ehrId);
}
Aggregations