From 8569eaa76f4d14b9af8d0c91d285b11a06bd5359 Mon Sep 17 00:00:00 2001 From: Greg Burd Date: Thu, 10 Aug 2017 13:09:34 -0400 Subject: [PATCH] Add 'Drafted' interface and extend the InsertOperation to recognize that so as to be able to persist the mutated set of keys only yet return a pojo with values matching the draft, not just the mutations. --- pom.xml | 2 +- .../java/net/helenus/core/HelenusSession.java | 16 ++++++++++++--- .../core/operation/InsertOperation.java | 13 +++--------- .../net/helenus/core/reflect/Drafted.java | 11 ++++++++++ .../helenus/core/reflect/MapExportable.java | 3 --- .../helenus/test/unit/core/dsl/Account.java | 20 ++++++++++++++++++- 6 files changed, 47 insertions(+), 18 deletions(-) create mode 100644 src/main/java/net/helenus/core/reflect/Drafted.java diff --git a/pom.xml b/pom.xml index e6708fd..f9e51ef 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ 4.0.0 net.helenus helenus-core - 2.0.14-SNAPSHOT + 2.0.15-SNAPSHOT jar helenus diff --git a/src/main/java/net/helenus/core/HelenusSession.java b/src/main/java/net/helenus/core/HelenusSession.java index 59b713f..dda58bd 100644 --- a/src/main/java/net/helenus/core/HelenusSession.java +++ b/src/main/java/net/helenus/core/HelenusSession.java @@ -19,6 +19,7 @@ import java.io.Closeable; import java.io.PrintStream; import java.util.Map; import java.util.Objects; +import java.util.Set; import java.util.concurrent.Executor; import java.util.concurrent.TimeUnit; import java.util.function.Function; @@ -31,6 +32,7 @@ import com.google.common.cache.CacheBuilder; import com.diffplug.common.base.Errors; import net.helenus.core.operation.*; +import net.helenus.core.reflect.Drafted; import net.helenus.core.reflect.HelenusPropertyNode; import net.helenus.mapping.HelenusEntity; import net.helenus.mapping.MappingUtil; @@ -346,13 +348,21 @@ public final class HelenusSession extends AbstractSessionOperations implements C return new InsertOperation(this, true); } - public InsertOperation insert(Object pojo) { + public InsertOperation insert(Object pojo) { + return insert(pojo, null); + } + + public InsertOperation insert(Drafted draft) { + return insert(draft.build(), draft.mutated()); + } + + public InsertOperation insert(Object pojo, Set mutations) { Objects.requireNonNull(pojo, "pojo is empty"); Class iface = MappingUtil.getMappingInterface(pojo); HelenusEntity entity = Helenus.entity(iface); - return new InsertOperation(this, entity, pojo, true); + return new InsertOperation(this, entity, pojo, mutations, true); } public InsertOperation upsert() { @@ -365,7 +375,7 @@ public final class HelenusSession extends AbstractSessionOperations implements C Class iface = MappingUtil.getMappingInterface(pojo); HelenusEntity entity = Helenus.entity(iface); - return new InsertOperation(this, entity, pojo, false); + return new InsertOperation(this, entity, pojo, null, false); } public DeleteOperation delete() { diff --git a/src/main/java/net/helenus/core/operation/InsertOperation.java b/src/main/java/net/helenus/core/operation/InsertOperation.java index c57b776..ddbcb57 100644 --- a/src/main/java/net/helenus/core/operation/InsertOperation.java +++ b/src/main/java/net/helenus/core/operation/InsertOperation.java @@ -48,21 +48,14 @@ public final class InsertOperation extends AbstractOperation mutations, boolean ifNotExists) { super(sessionOperations); this.entity = entity; this.ifNotExists = ifNotExists; Collection properties = entity.getOrderedProperties(); - Set keys = null; - - if (pojo instanceof MapExportable) { - keys = ((MapExportable) pojo).mutated(); - if (keys == null) { - keys = ((MapExportable) pojo).toMap().keySet(); - } - } + Set keys = (mutations == null) ? ((MapExportable) pojo).toMap().keySet() : mutations; for (HelenusProperty prop : properties) { diff --git a/src/main/java/net/helenus/core/reflect/Drafted.java b/src/main/java/net/helenus/core/reflect/Drafted.java new file mode 100644 index 0000000..aa5a54a --- /dev/null +++ b/src/main/java/net/helenus/core/reflect/Drafted.java @@ -0,0 +1,11 @@ +package net.helenus.core.reflect; + +import java.util.Set; + +public interface Drafted extends MapExportable { + + Set mutated(); + + T build(); + +} diff --git a/src/main/java/net/helenus/core/reflect/MapExportable.java b/src/main/java/net/helenus/core/reflect/MapExportable.java index fcb0e19..1e91946 100644 --- a/src/main/java/net/helenus/core/reflect/MapExportable.java +++ b/src/main/java/net/helenus/core/reflect/MapExportable.java @@ -16,7 +16,6 @@ package net.helenus.core.reflect; import java.util.Map; -import java.util.Set; public interface MapExportable { @@ -24,6 +23,4 @@ public interface MapExportable { Map toMap(); - default Set mutated() { return null; } - } diff --git a/src/test/java/net/helenus/test/unit/core/dsl/Account.java b/src/test/java/net/helenus/test/unit/core/dsl/Account.java index 9e585c0..f7d13b3 100644 --- a/src/test/java/net/helenus/test/unit/core/dsl/Account.java +++ b/src/test/java/net/helenus/test/unit/core/dsl/Account.java @@ -16,7 +16,10 @@ package net.helenus.test.unit.core.dsl; import java.util.Date; +import java.util.Map; +import java.util.Set; +import net.helenus.core.reflect.Drafted; import net.helenus.mapping.annotation.*; @Table @@ -35,6 +38,21 @@ public interface Account { @Transient default Draft draft() { return new Draft(); } - class Draft {} + class Draft implements Drafted { //TODO + @Override + public Set mutated() { + return null; + } + + @Override + public Object build() { + return null; + } + + @Override + public Map toMap() { + return null; + } + } }