Search in sources :

Example 1 with StateConflictException

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;
}
Also used : InvalidApiParameterException(org.ehrbase.api.exception.InvalidApiParameterException) OPERATIONALTEMPLATE(org.openehr.schemas.v1.OPERATIONALTEMPLATE) TemplateDocument(org.openehr.schemas.v1.TemplateDocument) ByteArrayInputStream(java.io.ByteArrayInputStream) XmlException(org.apache.xmlbeans.XmlException) IOException(java.io.IOException) StateConflictException(org.ehrbase.api.exception.StateConflictException) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Example 2 with StateConflictException

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);
}
Also used : InvalidApiParameterException(org.ehrbase.api.exception.InvalidApiParameterException) InternalServerException(org.ehrbase.api.exception.InternalServerException) StateConflictException(org.ehrbase.api.exception.StateConflictException) UUID(java.util.UUID) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) PutMapping(org.springframework.web.bind.annotation.PutMapping)

Example 3 with StateConflictException

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);
}
Also used : InternalServerException(org.ehrbase.api.exception.InternalServerException) StateConflictException(org.ehrbase.api.exception.StateConflictException) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Aggregations

StateConflictException (org.ehrbase.api.exception.StateConflictException)3 InternalServerException (org.ehrbase.api.exception.InternalServerException)2 InvalidApiParameterException (org.ehrbase.api.exception.InvalidApiParameterException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 UUID (java.util.UUID)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1 XmlException (org.apache.xmlbeans.XmlException)1 OPERATIONALTEMPLATE (org.openehr.schemas.v1.OPERATIONALTEMPLATE)1 TemplateDocument (org.openehr.schemas.v1.TemplateDocument)1 PostMapping (org.springframework.web.bind.annotation.PostMapping)1 PutMapping (org.springframework.web.bind.annotation.PutMapping)1 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)1