Skip to content
Snippets Groups Projects
Verified Commit 7648d2a6 authored by Timm Fitschen's avatar Timm Fitschen
Browse files

MAINT: refactor retrieve function in EntityTransactionService

parent 99c31b63
No related branches found
No related tags found
2 merge requests!80Release 0.9,!72F grpc select
Pipeline #30708 passed
caosdb-proto @ 96e7a1fb
Subproject commit fd60795f62335368255797db43eb0429ec1705fc
Subproject commit 96e7a1fb667ed1bb3b2602af6c69724519bf5118
......@@ -33,6 +33,7 @@ import org.caosdb.api.entity.v1.Entity;
import org.caosdb.api.entity.v1.EntityRequest;
import org.caosdb.api.entity.v1.EntityResponse;
import org.caosdb.api.entity.v1.EntityTransactionServiceGrpc.EntityTransactionServiceImplBase;
import org.caosdb.api.entity.v1.FindQueryResult;
import org.caosdb.api.entity.v1.IdResponse;
import org.caosdb.api.entity.v1.InsertRequest;
import org.caosdb.api.entity.v1.InsertResponse;
......@@ -79,6 +80,24 @@ public class EntityTransactionServiceImpl extends EntityTransactionServiceImplBa
this.fileTransmissionService = fileTransmissionService;
}
private void prepareDownload(
EntityResponse.Builder entityResponse, EntityInterface entity, FileDownload fileDownload)
throws Exception {
try {
entity.checkPermission(EntityPermission.RETRIEVE_FILE);
if (fileDownload == null) {
fileDownload = fileTransmissionService.registerFileDownload(null);
}
entity.getFileProperties().retrieveFromFileSystem();
entityResponse.setDownloadId(
fileTransmissionService.registerFileDownload(
fileDownload.getId(), entity.getFileProperties()));
} catch (AuthenticationException exc) {
entityResponse.addErrors(caosdbToGrpc.convert(ServerMessages.AUTHORIZATION_ERROR));
entityResponse.addInfos(caosdbToGrpc.convert(new Message(exc.getMessage())));
}
}
/**
* Handle read-only transactions. Of these only one may be a query at the moment, the others must
* be ID retrieves.
......@@ -144,25 +163,29 @@ public class EntityTransactionServiceImpl extends EntityTransactionServiceImplBa
builder
.addResponsesBuilder()
.setRetrieveResponse(RetrieveResponse.newBuilder().setSelectResult(selectResult));
} else if (container.getQuery() != null && container.getQuery().getType() == Query.Type.FIND) {
// this was a find query
final boolean download_files_container = container.getFlags().containsKey("download_files");
FindQueryResult.Builder findResult = FindQueryResult.newBuilder();
for (final EntityInterface entity : container) {
final EntityResponse.Builder entityResponse = caosdbToGrpc.convert(entity);
if ((download_files_container || entity.getFlags().containsKey("download_files"))
&& entity.hasFileProperties()) {
prepareDownload(entityResponse, entity, fileDownload);
}
findResult.addResultSet(entityResponse);
}
builder
.addResponsesBuilder()
.setRetrieveResponse(RetrieveResponse.newBuilder().setFindResult(findResult));
} else {
// normal retrieval via id
final boolean download_files_container = container.getFlags().containsKey("download_files");
for (final EntityInterface entity : container) {
final EntityResponse.Builder entityResponse = caosdbToGrpc.convert(entity);
if ((download_files_container || entity.getFlags().containsKey("download_files"))
&& entity.hasFileProperties()) {
try {
entity.checkPermission(EntityPermission.RETRIEVE_FILE);
if (fileDownload == null) {
fileDownload = fileTransmissionService.registerFileDownload(null);
}
entity.getFileProperties().retrieveFromFileSystem();
entityResponse.setDownloadId(
fileTransmissionService.registerFileDownload(
fileDownload.getId(), entity.getFileProperties()));
} catch (AuthenticationException exc) {
entityResponse.addErrors(caosdbToGrpc.convert(ServerMessages.AUTHORIZATION_ERROR));
entityResponse.addInfos(caosdbToGrpc.convert(new Message(exc.getMessage())));
}
prepareDownload(entityResponse, entity, fileDownload);
}
builder
.addResponsesBuilder()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment