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