From 74e6cc01b0a2d34a1adb7f505b866e8694f31b60 Mon Sep 17 00:00:00 2001
From: Timm Fitschen <t.fitschen@indiscale.com>
Date: Tue, 31 Aug 2021 11:31:56 +0200
Subject: [PATCH] BUG: Fixes for caosdb-server#169 and caosdb-cpplib#10

---
 .../caosdb/server/datatype/BooleanValue.java  |  2 +-
 .../grpc/EntityTransactionServiceImpl.java    | 48 +++++++++++++++----
 2 files changed, 39 insertions(+), 11 deletions(-)

diff --git a/src/main/java/org/caosdb/server/datatype/BooleanValue.java b/src/main/java/org/caosdb/server/datatype/BooleanValue.java
index 8fbe6bcb..2bcbb48b 100644
--- a/src/main/java/org/caosdb/server/datatype/BooleanValue.java
+++ b/src/main/java/org/caosdb/server/datatype/BooleanValue.java
@@ -39,6 +39,6 @@ public class BooleanValue extends AbstractEnumValue {
   }
 
   public boolean getValue() {
-    return toString().equals("TRUE");
+    return toDatabaseString().equals("TRUE");
   }
 }
diff --git a/src/main/java/org/caosdb/server/grpc/EntityTransactionServiceImpl.java b/src/main/java/org/caosdb/server/grpc/EntityTransactionServiceImpl.java
index 32cb1e27..ca8f6364 100644
--- a/src/main/java/org/caosdb/server/grpc/EntityTransactionServiceImpl.java
+++ b/src/main/java/org/caosdb/server/grpc/EntityTransactionServiceImpl.java
@@ -86,9 +86,11 @@ public class EntityTransactionServiceImpl extends EntityTransactionServiceImplBa
   private final FileTransmissionServiceImpl fileTransmissionService;
 
   public String getStringUnit(final EntityInterface entity) {
-    for (final Property p : entity.getProperties()) {
+    final Iterator<Property> iterator = entity.getProperties().iterator();
+    while (iterator.hasNext()) {
+      final Property p = iterator.next();
       if (MagicTypes.UNIT.getId() == p.getId()) {
-        p.setEntityStatus(EntityStatus.IGNORE);
+        iterator.remove();
         return p.getValue().toString();
       }
     }
@@ -115,10 +117,18 @@ public class EntityTransactionServiceImpl extends EntityTransactionServiceImplBa
       entityBuilder.setDataType(convert(from.getDatatype()));
     }
     if (from.hasValue()) {
+      try {
+        from.parseValue();
+      } catch (final Message e) {
+        // ignore. This problem should be handled elsewhere because this is
+        // only for the serialization of the data and not for the validation.
+        // In any case, the string representation can be used.
+      }
       entityBuilder.setValue(convert(from.getValue()));
     }
-    if (from.hasUnit()) {
-      entityBuilder.setUnit(getStringUnit(from));
+    final String unit = getStringUnit(from);
+    if (unit != null) {
+      entityBuilder.setUnit(unit);
     }
     if (from.hasProperties()) {
       entityBuilder.addAllProperties(convert(from.getProperties()));
@@ -218,10 +228,18 @@ public class EntityTransactionServiceImpl extends EntityTransactionServiceImplBa
     if (from.hasDatatype()) {
       builder.setDataType(convert(from.getDatatype()));
     }
-    if (from.hasUnit()) {
-      builder.setUnit(getStringUnit(from));
+    final String unit = getStringUnit(from);
+    if (unit != null) {
+      builder.setUnit(unit);
     }
     if (from.hasValue()) {
+      try {
+        from.parseValue();
+      } catch (final Message e) {
+        // ignore. This problem should be handled elsewhere because this is
+        // only for the serialization of the data and not for the validation.
+        // In any case, the string representation can be used.
+      }
       builder.setValue(convert(from.getValue()));
     }
     builder.setImportance(convert(from.getStatementStatus()));
@@ -763,7 +781,9 @@ public class EntityTransactionServiceImpl extends EntityTransactionServiceImplBa
     }
 
     if (from.getPropertiesCount() > 0) {
-      entity.getProperties().addAll(convertProperties(from.getPropertiesList()));
+      final StatementStatus defaultImportance =
+          entity.getRole() == Role.RecordType ? StatementStatus.RECOMMENDED : StatementStatus.FIX;
+      entity.getProperties().addAll(convertProperties(from.getPropertiesList(), defaultImportance));
     }
     if (from.getParentsCount() > 0) {
       entity.getParents().addAll(convertParents(from.getParentsList()));
@@ -882,16 +902,18 @@ public class EntityTransactionServiceImpl extends EntityTransactionServiceImplBa
   }
 
   private Collection<Property> convertProperties(
-      final List<org.caosdb.api.entity.v1alpha1.Property> propertiesList) {
+      final List<org.caosdb.api.entity.v1alpha1.Property> propertiesList,
+      final StatementStatus defaultImportance) {
     final Collection<Property> result = new LinkedList<>();
     propertiesList.forEach(
         e -> {
-          result.add(convert(e));
+          result.add(convert(e, defaultImportance));
         });
     return result;
   }
 
-  private Property convert(final org.caosdb.api.entity.v1alpha1.Property e) {
+  private Property convert(
+      final org.caosdb.api.entity.v1alpha1.Property e, final StatementStatus defaultImportance) {
     final Property result = new Property();
 
     try {
@@ -912,6 +934,12 @@ public class EntityTransactionServiceImpl extends EntityTransactionServiceImplBa
     }
     if (e.getImportance() != Importance.IMPORTANCE_UNSPECIFIED) {
       result.setStatementStatus(convert(e.getImportance()));
+    } else {
+      result.setStatementStatus(defaultImportance);
+    }
+    // TODO remove this hard-coded setting when the API supports flags
+    if (result.getFlag("inheritance") == null) {
+      result.setFlag("inheritance", "fix");
     }
 
     return result;
-- 
GitLab