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

WIP: fix #220

parent 216d6d1a
No related branches found
No related tags found
2 merge requests!58REL: prepare release 0.7.2,!57F 220
...@@ -540,7 +540,8 @@ public class Entity extends AbstractObservable implements EntityInterface { ...@@ -540,7 +540,8 @@ public class Entity extends AbstractObservable implements EntityInterface {
return getToElementStrategy().toElement(this, getSerializeFieldStrategy()); return getToElementStrategy().toElement(this, getSerializeFieldStrategy());
} }
private SetFieldStrategy getSerializeFieldStrategy() { @Override
public SetFieldStrategy getSerializeFieldStrategy() {
if (this.serializeFieldStrategy == null) { if (this.serializeFieldStrategy == null) {
this.serializeFieldStrategy = new SetFieldStrategy(getSelections()); this.serializeFieldStrategy = new SetFieldStrategy(getSelections());
} }
......
...@@ -199,4 +199,6 @@ public interface EntityInterface ...@@ -199,4 +199,6 @@ public interface EntityInterface
* AbstractCollectionDatatype's elements' data type is an instance of ReferenceDatatype. * AbstractCollectionDatatype's elements' data type is an instance of ReferenceDatatype.
*/ */
public abstract boolean isReferenceList(); public abstract boolean isReferenceList();
public abstract SetFieldStrategy getSerializeFieldStrategy();
} }
...@@ -580,4 +580,9 @@ public class EntityWrapper implements EntityInterface { ...@@ -580,4 +580,9 @@ public class EntityWrapper implements EntityInterface {
public void setSerializeFieldStrategy(SetFieldStrategy s) { public void setSerializeFieldStrategy(SetFieldStrategy s) {
this.entity.setSerializeFieldStrategy(s); this.entity.setSerializeFieldStrategy(s);
} }
@Override
public SetFieldStrategy getSerializeFieldStrategy() {
return this.entity.getSerializeFieldStrategy();
}
} }
...@@ -135,10 +135,11 @@ public class SetFieldStrategy { ...@@ -135,10 +135,11 @@ public class SetFieldStrategy {
if (this.cache == null) { if (this.cache == null) {
this.cache = new HashMap<String, Boolean>(); this.cache = new HashMap<String, Boolean>();
// always include the id, version and the name // always include the id, version, role and the name
this.cache.put("id", true); this.cache.put("id", true);
this.cache.put("version", true); this.cache.put("version", true);
this.cache.put("name", true); this.cache.put("name", true);
this.cache.put("role", true);
// ... and the referenced entity. // ... and the referenced entity.
this.cache.put("_referenced", true); this.cache.put("_referenced", true);
......
...@@ -24,6 +24,7 @@ package org.caosdb.server.grpc; ...@@ -24,6 +24,7 @@ package org.caosdb.server.grpc;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.NoSuchElementException;
import java.util.TimeZone; import java.util.TimeZone;
import org.caosdb.api.entity.v1.AtomicDataType; import org.caosdb.api.entity.v1.AtomicDataType;
import org.caosdb.api.entity.v1.CollectionValues; import org.caosdb.api.entity.v1.CollectionValues;
...@@ -69,8 +70,8 @@ import org.caosdb.server.entity.Message; ...@@ -69,8 +70,8 @@ import org.caosdb.server.entity.Message;
import org.caosdb.server.entity.Role; import org.caosdb.server.entity.Role;
import org.caosdb.server.entity.StatementStatus; import org.caosdb.server.entity.StatementStatus;
import org.caosdb.server.entity.container.ParentContainer; import org.caosdb.server.entity.container.ParentContainer;
import org.caosdb.server.entity.container.PropertyContainer;
import org.caosdb.server.entity.wrapper.Property; import org.caosdb.server.entity.wrapper.Property;
import org.caosdb.server.entity.xml.SetFieldStrategy;
import org.caosdb.server.permissions.EntityACI; import org.caosdb.server.permissions.EntityACI;
import org.caosdb.server.permissions.EntityPermission; import org.caosdb.server.permissions.EntityPermission;
...@@ -97,24 +98,25 @@ public class CaosDBToGrpcConverters { ...@@ -97,24 +98,25 @@ public class CaosDBToGrpcConverters {
public EntityResponse.Builder convert(final EntityInterface from) { public EntityResponse.Builder convert(final EntityInterface from) {
SetFieldStrategy s = from.getSerializeFieldStrategy();
final Builder entityBuilder = Entity.newBuilder(); final Builder entityBuilder = Entity.newBuilder();
if (from.hasId()) { if (from.hasId() && s.isToBeSet("id")) {
entityBuilder.setId(Integer.toString(from.getId())); entityBuilder.setId(Integer.toString(from.getId()));
} }
if (from.getRole() != null) { if (from.getRole() != null && s.isToBeSet("role")) {
entityBuilder.setRole(convert(from.getRole())); entityBuilder.setRole(convert(from.getRole()));
} }
if (from.hasName()) { if (from.hasName() && s.isToBeSet("name")) {
entityBuilder.setName(from.getName()); entityBuilder.setName(from.getName());
} }
if (from.hasDescription()) { if (from.hasDescription() && s.isToBeSet("description")) {
entityBuilder.setDescription(from.getDescription()); entityBuilder.setDescription(from.getDescription());
} }
if (from.hasDatatype()) { if (from.hasDatatype() && s.isToBeSet("datatype")) {
entityBuilder.setDataType(convert(from.getDatatype())); entityBuilder.setDataType(convert(from.getDatatype()));
} }
if (from.hasValue()) { if (from.hasValue() && s.isToBeSet("value")) {
try { try {
from.parseValue(); from.parseValue();
} catch (final Message e) { } catch (final Message e) {
...@@ -125,17 +127,20 @@ public class CaosDBToGrpcConverters { ...@@ -125,17 +127,20 @@ public class CaosDBToGrpcConverters {
entityBuilder.setValue(convert(from.getValue())); entityBuilder.setValue(convert(from.getValue()));
} }
final String unit = getStringUnit(from); final String unit = getStringUnit(from);
if (unit != null) { if (unit != null && s.isToBeSet("unit")) {
entityBuilder.setUnit(unit); entityBuilder.setUnit(unit);
} }
if (from.hasProperties()) { if (from.hasProperties()) {
entityBuilder.addAllProperties(convert(from.getProperties())); entityBuilder.addAllProperties(convertProperties(from));
} }
if (from.hasParents()) { if (from.hasParents() && s.isToBeSet("parent")) {
entityBuilder.addAllParents(convert(from.getParents())); entityBuilder.addAllParents(convert(from.getParents()));
} }
if (from.hasFileProperties()) { if (from.hasFileProperties()) {
entityBuilder.setFileDescriptor(convert(from.getFileProperties())); FileDescriptor.Builder fileDescriptor = convert(s, from.getFileProperties());
if (fileDescriptor != null) {
entityBuilder.setFileDescriptor(fileDescriptor);
}
} }
final EntityResponse.Builder responseBuilder = EntityResponse.newBuilder(); final EntityResponse.Builder responseBuilder = EntityResponse.newBuilder();
...@@ -146,10 +151,18 @@ public class CaosDBToGrpcConverters { ...@@ -146,10 +151,18 @@ public class CaosDBToGrpcConverters {
return responseBuilder; return responseBuilder;
} }
private FileDescriptor.Builder convert(FileProperties fileProperties) { private FileDescriptor.Builder convert(SetFieldStrategy s, FileProperties fileProperties) {
FileDescriptor.Builder result = FileDescriptor.newBuilder(); FileDescriptor.Builder result = null;
if (s.isToBeSet("path")) {
result = FileDescriptor.newBuilder();
result.setPath(fileProperties.getPath()); result.setPath(fileProperties.getPath());
}
if (s.isToBeSet("size")) {
if (result == null) {
result = FileDescriptor.newBuilder();
}
result.setSize(fileProperties.getSize()); result.setSize(fileProperties.getSize());
}
return result; return result;
} }
...@@ -216,23 +229,24 @@ public class CaosDBToGrpcConverters { ...@@ -216,23 +229,24 @@ public class CaosDBToGrpcConverters {
final org.caosdb.api.entity.v1.Property.Builder builder = final org.caosdb.api.entity.v1.Property.Builder builder =
org.caosdb.api.entity.v1.Property.newBuilder(); org.caosdb.api.entity.v1.Property.newBuilder();
if (from.hasId()) { SetFieldStrategy s = from.getSerializeFieldStrategy();
if (from.hasId() && s.isToBeSet("id")) {
builder.setId(from.getId().toString()); builder.setId(from.getId().toString());
} }
if (from.hasName()) { if (from.hasName() && s.isToBeSet("name")) {
builder.setName(from.getName()); builder.setName(from.getName());
} }
if (from.hasDescription()) { if (from.hasDescription() && s.isToBeSet("description")) {
builder.setDescription(from.getDescription()); builder.setDescription(from.getDescription());
} }
if (from.hasDatatype()) { if (from.hasDatatype() && s.isToBeSet("datatype")) {
builder.setDataType(convert(from.getDatatype())); builder.setDataType(convert(from.getDatatype()));
} }
final String unit = getStringUnit(from); final String unit = getStringUnit(from);
if (unit != null) { if (unit != null && s.isToBeSet("unit")) {
builder.setUnit(unit); builder.setUnit(unit);
} }
if (from.hasValue()) { if (from.hasValue() && s.isToBeSet("value")) {
try { try {
from.parseValue(); from.parseValue();
} catch (final Message e) { } catch (final Message e) {
...@@ -242,7 +256,9 @@ public class CaosDBToGrpcConverters { ...@@ -242,7 +256,9 @@ public class CaosDBToGrpcConverters {
} }
builder.setValue(convert(from.getValue())); builder.setValue(convert(from.getValue()));
} }
if (s.isToBeSet("importance")) {
builder.setImportance(convert(from.getStatementStatus())); builder.setImportance(convert(from.getStatementStatus()));
}
return builder.build(); return builder.build();
} }
...@@ -429,20 +445,42 @@ public class CaosDBToGrpcConverters { ...@@ -429,20 +445,42 @@ public class CaosDBToGrpcConverters {
return ReferenceDataType.newBuilder().setName(datatype.getName()); return ReferenceDataType.newBuilder().setName(datatype.getName());
} }
public Iterable<? extends org.caosdb.api.entity.v1.Property> convert( public Iterable<? extends org.caosdb.api.entity.v1.Property> convertProperties(
final PropertyContainer from) { final EntityInterface from) {
final Iterator<org.caosdb.server.entity.wrapper.Property> iterator = from.iterator();
final Iterator<org.caosdb.server.entity.wrapper.Property> iterator =
from.getProperties().iterator();
return () -> return () ->
new Iterator<>() { new Iterator<>() {
private Property property;
@Override @Override
public boolean hasNext() { public boolean hasNext() {
return iterator.hasNext(); while (iterator.hasNext()) {
this.property = iterator.next();
if (from.getSerializeFieldStrategy().isToBeSet(this.property.getName())) {
this.property.setSerializeFieldStrategy(
from.getSerializeFieldStrategy().forProperty(this.property));
return true;
}
}
return false;
} }
@Override @Override
public org.caosdb.api.entity.v1.Property next() { public org.caosdb.api.entity.v1.Property next() {
return convert(iterator.next()); if (this.property == null) {
// trigger this.property to be non-null
if (!hasNext()) {
throw new NoSuchElementException("The iterator has no more elements.");
}
}
Property next_property = this.property;
this.property = null;
return convert(next_property);
} }
}; };
} }
...@@ -466,26 +504,28 @@ public class CaosDBToGrpcConverters { ...@@ -466,26 +504,28 @@ public class CaosDBToGrpcConverters {
public void appendMessages( public void appendMessages(
final EntityInterface from, final org.caosdb.api.entity.v1.EntityResponse.Builder builder) { final EntityInterface from, final org.caosdb.api.entity.v1.EntityResponse.Builder builder) {
if (from.hasMessage(Message.MessageType.Error.toString())) { SetFieldStrategy s = from.getSerializeFieldStrategy();
if (from.hasMessage(Message.MessageType.Error.toString()) && s.isToBeSet("error")) {
builder.addAllErrors(convert(from.getMessages(Message.MessageType.Error.toString()))); builder.addAllErrors(convert(from.getMessages(Message.MessageType.Error.toString())));
} }
if (from.hasMessage(Message.MessageType.Warning.toString())) { if (from.hasMessage(Message.MessageType.Warning.toString()) && s.isToBeSet("warning")) {
builder.addAllWarnings(convert(from.getMessages(Message.MessageType.Warning.toString()))); builder.addAllWarnings(convert(from.getMessages(Message.MessageType.Warning.toString())));
} }
if (from.hasMessage(Message.MessageType.Info.toString())) { if (from.hasMessage(Message.MessageType.Info.toString()) && s.isToBeSet("info")) {
builder.addAllInfos(convert(from.getMessages(Message.MessageType.Info.toString()))); builder.addAllInfos(convert(from.getMessages(Message.MessageType.Info.toString())));
} }
} }
public void appendMessages( public void appendMessages(
final EntityInterface from, final org.caosdb.api.entity.v1.IdResponse.Builder builder) { final EntityInterface from, final org.caosdb.api.entity.v1.IdResponse.Builder builder) {
if (from.hasMessage(Message.MessageType.Error.toString())) { SetFieldStrategy s = from.getSerializeFieldStrategy();
if (from.hasMessage(Message.MessageType.Error.toString()) && s.isToBeSet("error")) {
builder.addAllErrors(convert(from.getMessages(Message.MessageType.Error.toString()))); builder.addAllErrors(convert(from.getMessages(Message.MessageType.Error.toString())));
} }
if (from.hasMessage(Message.MessageType.Warning.toString())) { if (from.hasMessage(Message.MessageType.Warning.toString()) && s.isToBeSet("warning")) {
builder.addAllWarnings(convert(from.getMessages(Message.MessageType.Warning.toString()))); builder.addAllWarnings(convert(from.getMessages(Message.MessageType.Warning.toString())));
} }
if (from.hasMessage(Message.MessageType.Info.toString())) { if (from.hasMessage(Message.MessageType.Info.toString()) && s.isToBeSet("info")) {
builder.addAllInfos(convert(from.getMessages(Message.MessageType.Info.toString()))); builder.addAllInfos(convert(from.getMessages(Message.MessageType.Info.toString())));
} }
} }
......
...@@ -26,6 +26,7 @@ import java.util.HashMap; ...@@ -26,6 +26,7 @@ import java.util.HashMap;
import java.util.TimeZone; import java.util.TimeZone;
import java.util.UUID; import java.util.UUID;
import org.apache.shiro.SecurityUtils; import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.caosdb.api.entity.v1.DeleteRequest; import org.caosdb.api.entity.v1.DeleteRequest;
import org.caosdb.api.entity.v1.DeleteResponse; import org.caosdb.api.entity.v1.DeleteResponse;
import org.caosdb.api.entity.v1.Entity; import org.caosdb.api.entity.v1.Entity;
...@@ -52,10 +53,12 @@ import org.caosdb.server.entity.DeleteEntity; ...@@ -52,10 +53,12 @@ import org.caosdb.server.entity.DeleteEntity;
import org.caosdb.server.entity.EntityInterface; import org.caosdb.server.entity.EntityInterface;
import org.caosdb.server.entity.FileProperties; import org.caosdb.server.entity.FileProperties;
import org.caosdb.server.entity.InsertEntity; import org.caosdb.server.entity.InsertEntity;
import org.caosdb.server.entity.Message;
import org.caosdb.server.entity.RetrieveEntity; import org.caosdb.server.entity.RetrieveEntity;
import org.caosdb.server.entity.UpdateEntity; import org.caosdb.server.entity.UpdateEntity;
import org.caosdb.server.entity.container.RetrieveContainer; import org.caosdb.server.entity.container.RetrieveContainer;
import org.caosdb.server.entity.container.WritableContainer; import org.caosdb.server.entity.container.WritableContainer;
import org.caosdb.server.permissions.EntityPermission;
import org.caosdb.server.transaction.Retrieve; import org.caosdb.server.transaction.Retrieve;
import org.caosdb.server.transaction.RetrieveACL; import org.caosdb.server.transaction.RetrieveACL;
import org.caosdb.server.transaction.UpdateACL; import org.caosdb.server.transaction.UpdateACL;
...@@ -136,6 +139,8 @@ public class EntityTransactionServiceImpl extends EntityTransactionServiceImplBa ...@@ -136,6 +139,8 @@ public class EntityTransactionServiceImpl extends EntityTransactionServiceImplBa
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 {
entity.checkPermission(EntityPermission.RETRIEVE_FILE);
if (fileDownload == null) { if (fileDownload == null) {
fileDownload = fileTransmissionService.registerFileDownload(null); fileDownload = fileTransmissionService.registerFileDownload(null);
} }
...@@ -143,6 +148,10 @@ public class EntityTransactionServiceImpl extends EntityTransactionServiceImplBa ...@@ -143,6 +148,10 @@ public class EntityTransactionServiceImpl extends EntityTransactionServiceImplBa
entityResponse.setDownloadId( entityResponse.setDownloadId(
fileTransmissionService.registerFileDownload( fileTransmissionService.registerFileDownload(
fileDownload.getId(), entity.getFileProperties())); 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