Search in sources :

Example 1 with ObjectNotFoundException

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

the class TemplateServiceImp method findOperationalTemplate.

@Override
public String findOperationalTemplate(String templateId, OperationalTemplateFormat format) throws ObjectNotFoundException, InvalidApiParameterException, InternalServerException {
    Optional<OPERATIONALTEMPLATE> operationaltemplate;
    if (format.equals(OperationalTemplateFormat.XML)) {
        try {
            operationaltemplate = this.knowledgeCacheService.retrieveOperationalTemplate(templateId);
            if (!operationaltemplate.isPresent()) {
                throw new ObjectNotFoundException("template", "Template with the specified id does not exist");
            }
        } catch (NullPointerException e) {
            // TODO: is this NPE really thrown in case of not found template anymore?
            throw new ObjectNotFoundException("template", "Template with the specified id does not exist", e);
        }
    } else {
        // TODO only XML at the moment
        throw new InvalidApiParameterException("Requested operational template type not supported");
    }
    XmlOptions opts = new XmlOptions();
    opts.setSaveSyntheticDocumentElement(new QName("http://schemas.openehr.org/v1", "template"));
    return operationaltemplate.map(o -> o.xmlText(opts)).orElseThrow(() -> new InternalServerException("Failure while retrieving operational template"));
}
Also used : InvalidApiParameterException(org.ehrbase.api.exception.InvalidApiParameterException) TemplateService(org.ehrbase.api.service.TemplateService) TemplateMetaDataDto(org.ehrbase.response.ehrscape.TemplateMetaDataDto) OPERATIONALTEMPLATE(org.openehr.schemas.v1.OPERATIONALTEMPLATE) TemplateMetaData(org.ehrbase.ehr.knowledge.TemplateMetaData) ObjectNotFoundException(org.ehrbase.api.exception.ObjectNotFoundException) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) CARCHETYPEROOT(org.openehr.schemas.v1.CARCHETYPEROOT) Service(org.springframework.stereotype.Service) DSLContext(org.jooq.DSLContext) OperationalTemplateFormat(org.ehrbase.api.definitions.OperationalTemplateFormat) Logger(org.slf4j.Logger) StructuredStringFormat(org.ehrbase.response.ehrscape.StructuredStringFormat) Collectors(java.util.stream.Collectors) StructuredString(org.ehrbase.response.ehrscape.StructuredString) StandardCharsets(java.nio.charset.StandardCharsets) WebTemplate(org.ehrbase.webtemplate.model.WebTemplate) Objects(java.util.Objects) List(java.util.List) CompositionService(org.ehrbase.api.service.CompositionService) XmlOptions(org.apache.xmlbeans.XmlOptions) InternalServerException(org.ehrbase.api.exception.InternalServerException) InvalidApiParameterException(org.ehrbase.api.exception.InvalidApiParameterException) ServerConfig(org.ehrbase.api.definitions.ServerConfig) Optional(java.util.Optional) QName(javax.xml.namespace.QName) OBJECTID(org.openehr.schemas.v1.OBJECTID) CompositionFormat(org.ehrbase.response.ehrscape.CompositionFormat) Transactional(org.springframework.transaction.annotation.Transactional) OPERATIONALTEMPLATE(org.openehr.schemas.v1.OPERATIONALTEMPLATE) QName(javax.xml.namespace.QName) InternalServerException(org.ehrbase.api.exception.InternalServerException) ObjectNotFoundException(org.ehrbase.api.exception.ObjectNotFoundException) XmlOptions(org.apache.xmlbeans.XmlOptions)

Example 2 with ObjectNotFoundException

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

the class TemplateFileStorageService method deleteTemplate.

/**
 * {@inheritDoc}
 */
@Override
public boolean deleteTemplate(String templateId) {
    boolean deleted;
    try {
        File file = optFileMap.get(templateId);
        if (!file.exists()) {
            throw new ObjectNotFoundException("ADMIN TEMPLATE", String.format("File with name %s does not exist.", templateId));
        }
        deleted = Files.deleteIfExists(file.toPath());
        if (deleted) {
            optFileMap.remove(templateId);
        }
        return deleted;
    } catch (IOException e) {
        throw new InternalServerException(e.getMessage());
    }
}
Also used : InternalServerException(org.ehrbase.api.exception.InternalServerException) ObjectNotFoundException(org.ehrbase.api.exception.ObjectNotFoundException)

Example 3 with ObjectNotFoundException

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

the class TemplateFileStorageService method adminUpdateTemplate.

/**
 * {@inheritDoc}
 */
@Override
public String adminUpdateTemplate(OPERATIONALTEMPLATE template) {
    try {
        File file = optFileMap.get(template.getTemplateId().getValue());
        if (!file.exists()) {
            throw new ObjectNotFoundException("ADMIN TEMPLATE STORE FILESYSTEM", String.format("File with name %s does not exist", template.getTemplateId()));
        }
        // Remove old content
        Files.delete(file.toPath());
        optFileMap.remove(template.getTemplateId().getValue());
        // Save new content
        XmlOptions opts = new XmlOptions();
        opts.setSaveSyntheticDocumentElement(new QName("http://schemas.openehr.org/v1", "template"));
        saveTemplateFile(template.getTemplateId().getValue(), template.xmlText(opts).getBytes(StandardCharsets.UTF_8));
        return template.xmlText(opts);
    } catch (IOException e) {
        throw new InternalServerException(e.getMessage());
    }
}
Also used : QName(javax.xml.namespace.QName) InternalServerException(org.ehrbase.api.exception.InternalServerException) ObjectNotFoundException(org.ehrbase.api.exception.ObjectNotFoundException) XmlOptions(org.apache.xmlbeans.XmlOptions)

Example 4 with ObjectNotFoundException

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

the class CompositionServiceImp method getOriginalVersionComposition.

@Override
public Optional<OriginalVersion<Composition>> getOriginalVersionComposition(UUID versionedObjectUid, int version) {
    // check for valid version parameter
    if ((version == 0) || I_CompositionAccess.getLastVersionNumber(getDataAccess(), versionedObjectUid) < version) {
        throw new ObjectNotFoundException("versioned_composition", "No VERSIONED_COMPOSITION with given version: " + version);
    }
    // retrieve requested object
    I_CompositionAccess compositionAccess = I_CompositionAccess.retrieveCompositionVersion(getDataAccess(), versionedObjectUid, version);
    if (compositionAccess == null) {
        return Optional.empty();
    }
    // create data for output, i.e. fields of the OriginalVersion<Composition>
    ObjectVersionId versionId = new ObjectVersionId(versionedObjectUid + "::" + getServerConfig().getNodename() + "::" + version);
    DvCodedText lifecycleState = new DvCodedText("complete", new CodePhrase(// TODO: once lifecycle state is supported, get it here dynamically
    "532"));
    AuditDetails commitAudit = compositionAccess.getAuditDetailsAccess().getAsAuditDetails();
    ObjectRef<HierObjectId> contribution = new ObjectRef<>(new HierObjectId(compositionAccess.getContributionId().toString()), "openehr", "contribution");
    List<UUID> attestationIdList = I_AttestationAccess.retrieveListOfAttestationsByRef(getDataAccess(), compositionAccess.getAttestationRef());
    List<Attestation> attestations = // as default, gets content if available in the following lines
    null;
    if (!attestationIdList.isEmpty()) {
        attestations = new ArrayList<>();
        for (UUID id : attestationIdList) {
            I_AttestationAccess a = new AttestationAccess(getDataAccess()).retrieveInstance(id);
            attestations.add(a.getAsAttestation());
        }
    }
    ObjectVersionId precedingVersionId = null;
    // check if there is a preceding version and set it, if available
    if (version > 1) {
        // in the current scope version is an int and therefore: preceding = current - 1
        precedingVersionId = new ObjectVersionId(versionedObjectUid + "::" + getServerConfig().getNodename() + "::" + (version - 1));
    }
    Optional<CompositionDto> compositionDto = retrieve(versionedObjectUid, version);
    Composition composition = null;
    if (compositionDto.isPresent()) {
        composition = compositionDto.get().getComposition();
    }
    OriginalVersion<Composition> versionComposition = new OriginalVersion<>(versionId, precedingVersionId, composition, lifecycleState, commitAudit, contribution, null, null, attestations);
    return Optional.of(versionComposition);
}
Also used : OriginalVersion(com.nedap.archie.rm.changecontrol.OriginalVersion) Composition(com.nedap.archie.rm.composition.Composition) VersionedComposition(com.nedap.archie.rm.ehr.VersionedComposition) DvCodedText(com.nedap.archie.rm.datavalues.DvCodedText) CodePhrase(com.nedap.archie.rm.datatypes.CodePhrase) I_AttestationAccess(org.ehrbase.dao.access.interfaces.I_AttestationAccess) I_AttestationAccess(org.ehrbase.dao.access.interfaces.I_AttestationAccess) AttestationAccess(org.ehrbase.dao.access.jooq.AttestationAccess) ObjectVersionId(com.nedap.archie.rm.support.identification.ObjectVersionId) Attestation(com.nedap.archie.rm.generic.Attestation) ObjectNotFoundException(org.ehrbase.api.exception.ObjectNotFoundException) CompositionDto(org.ehrbase.response.ehrscape.CompositionDto) I_CompositionAccess(org.ehrbase.dao.access.interfaces.I_CompositionAccess) AuditDetails(com.nedap.archie.rm.generic.AuditDetails) ObjectRef(com.nedap.archie.rm.support.identification.ObjectRef) UUID(java.util.UUID) HierObjectId(com.nedap.archie.rm.support.identification.HierObjectId)

Example 5 with ObjectNotFoundException

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

the class CompositionServiceImp method internalDelete.

/**
 * Deletion of an existing composition. With optional custom contribution, or existing one will be
 * updated.
 *
 * @param compositionId  ID of existing composition
 * @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 Time of deletion, if successful
 */
private boolean internalDelete(UUID compositionId, UUID systemId, UUID committerId, String description, UUID contributionId) {
    I_CompositionAccess compositionAccess;
    try {
        compositionAccess = I_CompositionAccess.retrieveInstance(getDataAccess(), compositionId);
    } catch (Exception e) {
        throw new ObjectNotFoundException(I_CompositionAccess.class.getName(), "Error while retrieving composition", e);
    }
    if (compositionAccess == null) {
        throw new ObjectNotFoundException(I_CompositionAccess.class.getName(), "Could not find composition:" + compositionId);
    }
    int result;
    if (contributionId != null) {
        // if custom contribution should be set
        compositionAccess.setContributionId(contributionId);
        try {
            result = compositionAccess.delete(LocalDateTime.now(), contributionId);
        } catch (Exception e) {
            throw new InternalServerException(e);
        }
    } else {
        // if not continue with standard delete
        try {
            if (committerId == null || systemId == null) {
                throw new InternalServerException("Failed to update composition, missing mandatory audit meta data.");
            }
            result = compositionAccess.delete(LocalDateTime.now(), committerId, systemId, description);
        } catch (Exception e) {
            throw new InternalServerException(e);
        }
    }
    if (result <= 0) {
        throw new InternalServerException("Delete failed on composition:" + compositionAccess.getId());
    } else {
        return true;
    }
}
Also used : InternalServerException(org.ehrbase.api.exception.InternalServerException) ObjectNotFoundException(org.ehrbase.api.exception.ObjectNotFoundException) I_CompositionAccess(org.ehrbase.dao.access.interfaces.I_CompositionAccess) 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)

Aggregations

ObjectNotFoundException (org.ehrbase.api.exception.ObjectNotFoundException)32 UUID (java.util.UUID)18 InternalServerException (org.ehrbase.api.exception.InternalServerException)17 GetMapping (org.springframework.web.bind.annotation.GetMapping)9 HttpHeaders (org.springframework.http.HttpHeaders)8 ObjectVersionId (com.nedap.archie.rm.support.identification.ObjectVersionId)7 InvalidApiParameterException (org.ehrbase.api.exception.InvalidApiParameterException)7 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)7 StructuredString (org.ehrbase.response.ehrscape.StructuredString)6 Timestamp (java.sql.Timestamp)5 Objects (java.util.Objects)5 Optional (java.util.Optional)5 OriginalVersion (com.nedap.archie.rm.changecontrol.OriginalVersion)4 ApiResponses (io.swagger.v3.oas.annotations.responses.ApiResponses)4 LocalDateTime (java.time.LocalDateTime)4 I_CompositionAccess (org.ehrbase.dao.access.interfaces.I_CompositionAccess)4 Composition (com.nedap.archie.rm.composition.Composition)3 EhrStatus (com.nedap.archie.rm.ehr.EhrStatus)3 VersionedEhrStatus (com.nedap.archie.rm.ehr.VersionedEhrStatus)3 RevisionHistory (com.nedap.archie.rm.generic.RevisionHistory)3