From 249dce565c2b4f9348e1db57b9fd558648b7ffc3 Mon Sep 17 00:00:00 2001
From: florian <f.spreckelsen@inidscale.com>
Date: Tue, 3 Aug 2021 13:43:03 +0200
Subject: [PATCH] ENH: Add a CopoyTo method to Entity

---
 include/caosdb/entity.h    | 5 +++++
 src/caosdb/entity.cpp      | 7 +++++++
 src/caosdb/transaction.cpp | 4 ++--
 3 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/include/caosdb/entity.h b/include/caosdb/entity.h
index edd2018..6d4b451 100644
--- a/include/caosdb/entity.h
+++ b/include/caosdb/entity.h
@@ -2,6 +2,7 @@
  * This file is a part of the CaosDB Project.
  *
  * Copyright (C) 2021 Timm Fitschen <t.fitschen@indiscale.com>
+ * Copyright (C) 2021 Florian Spreckelsen <f.spreckelsen@indiscale.com>
  * Copyright (C) 2021 IndiScale GmbH <info@indiscale.com>
  *
  * This program is free software: you can redistribute it and/or modify
@@ -450,6 +451,10 @@ public:
 
   auto AppendParent(const Parent &parent) -> void;
   auto Switch(ProtoEntity *entity) -> void;
+  /**
+   * Copy all of this entity's features to the target ProtoEntity.
+   */
+  auto CopyTo(ProtoEntity *target) -> void;
 
 private:
   static auto CreateProtoEntity() -> ProtoEntity *;
diff --git a/src/caosdb/entity.cpp b/src/caosdb/entity.cpp
index 22f0eff..999ba37 100644
--- a/src/caosdb/entity.cpp
+++ b/src/caosdb/entity.cpp
@@ -1,6 +1,8 @@
 /*
  * This file is a part of the CaosDB Project.
+ *
  * Copyright (C) 2021 Timm Fitschen <t.fitschen@indiscale.com>
+ * Copyright (C) 2021 Florian Spreckelsen <f.spreckelsen@indiscale.com>
  * Copyright (C) 2021 IndiScale GmbH <info@indiscale.com>
  *
  * This program is free software: you can redistribute it and/or modify
@@ -175,12 +177,17 @@ auto Entity::SetVersionId(const std::string &id) -> void {
   this->wrapped->mutable_version()->set_id(id);
 }
 
+// TODO(tf) Re-implement s.th. properties and parents are kept.
 auto Entity::Switch(ProtoEntity *entity) -> void {
   this->wrapped->Swap(entity);
   this->wrapped->Clear();
   this->wrapped = entity;
 }
 
+auto Entity::CopyTo(ProtoEntity *target) -> void {
+  target->CopyFrom(*(this->wrapped));
+}
+
 auto Entity::SetRole(const std::string &role) -> void {
   this->wrapped->set_role(role);
 }
diff --git a/src/caosdb/transaction.cpp b/src/caosdb/transaction.cpp
index 380362a..7dd4a71 100644
--- a/src/caosdb/transaction.cpp
+++ b/src/caosdb/transaction.cpp
@@ -148,8 +148,8 @@ auto Transaction::InsertEntity(Entity *entity) -> void {
   auto *sub_request = this->request->add_requests();
   auto *proto_entity = sub_request->mutable_insert_request();
 
-  // swap and switch
-  entity->Switch(proto_entity);
+  // copy the original entity for the transaction
+  entity->CopyTo(proto_entity);
 }
 
 auto Transaction::Execute() -> TransactionStatus {
-- 
GitLab