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
Branches
Tags
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; ...@@ -33,6 +33,7 @@ import org.caosdb.api.entity.v1.Entity;
import org.caosdb.api.entity.v1.EntityRequest; import org.caosdb.api.entity.v1.EntityRequest;
import org.caosdb.api.entity.v1.EntityResponse; import org.caosdb.api.entity.v1.EntityResponse;
import org.caosdb.api.entity.v1.EntityTransactionServiceGrpc.EntityTransactionServiceImplBase; 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.IdResponse;
import org.caosdb.api.entity.v1.InsertRequest; import org.caosdb.api.entity.v1.InsertRequest;
import org.caosdb.api.entity.v1.InsertResponse; import org.caosdb.api.entity.v1.InsertResponse;
...@@ -79,6 +80,24 @@ public class EntityTransactionServiceImpl extends EntityTransactionServiceImplBa ...@@ -79,6 +80,24 @@ public class EntityTransactionServiceImpl extends EntityTransactionServiceImplBa
this.fileTransmissionService = fileTransmissionService; 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 * Handle read-only transactions. Of these only one may be a query at the moment, the others must
* be ID retrieves. * be ID retrieves.
...@@ -144,25 +163,29 @@ public class EntityTransactionServiceImpl extends EntityTransactionServiceImplBa ...@@ -144,25 +163,29 @@ public class EntityTransactionServiceImpl extends EntityTransactionServiceImplBa
builder builder
.addResponsesBuilder() .addResponsesBuilder()
.setRetrieveResponse(RetrieveResponse.newBuilder().setSelectResult(selectResult)); .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 { } else {
// normal retrieval via id
final boolean download_files_container = container.getFlags().containsKey("download_files"); final boolean download_files_container = container.getFlags().containsKey("download_files");
for (final EntityInterface entity : container) { for (final EntityInterface entity : container) {
final EntityResponse.Builder entityResponse = caosdbToGrpc.convert(entity); final EntityResponse.Builder entityResponse = caosdbToGrpc.convert(entity);
if ((download_files_container || entity.getFlags().containsKey("download_files")) if ((download_files_container || entity.getFlags().containsKey("download_files"))
&& entity.hasFileProperties()) { && entity.hasFileProperties()) {
try { prepareDownload(entityResponse, entity, fileDownload);
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())));
}
} }
builder builder
.addResponsesBuilder() .addResponsesBuilder()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment