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.

This commit is contained in:
Greg Burd 2017-08-10 13:09:34 -04:00
parent a126607c09
commit 8569eaa76f
6 changed files with 47 additions and 18 deletions

View file

@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>net.helenus</groupId>
<artifactId>helenus-core</artifactId>
<version>2.0.14-SNAPSHOT</version>
<version>2.0.15-SNAPSHOT</version>
<packaging>jar</packaging>
<name>helenus</name>

View file

@ -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;
@ -347,12 +349,20 @@ public final class HelenusSession extends AbstractSessionOperations implements C
}
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<String> 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() {

View file

@ -48,21 +48,14 @@ public final class InsertOperation extends AbstractOperation<ResultSet, InsertOp
this.ifNotExists = ifNotExists;
}
public InsertOperation(AbstractSessionOperations sessionOperations, HelenusEntity entity, Object pojo,
boolean ifNotExists) {
public InsertOperation(AbstractSessionOperations sessionOperations, HelenusEntity entity,
Object pojo, Set<String> mutations, boolean ifNotExists) {
super(sessionOperations);
this.entity = entity;
this.ifNotExists = ifNotExists;
Collection<HelenusProperty> properties = entity.getOrderedProperties();
Set<String> keys = null;
if (pojo instanceof MapExportable) {
keys = ((MapExportable) pojo).mutated();
if (keys == null) {
keys = ((MapExportable) pojo).toMap().keySet();
}
}
Set<String> keys = (mutations == null) ? ((MapExportable) pojo).toMap().keySet() : mutations;
for (HelenusProperty prop : properties) {

View file

@ -0,0 +1,11 @@
package net.helenus.core.reflect;
import java.util.Set;
public interface Drafted<T> extends MapExportable {
Set<String> mutated();
T build();
}

View file

@ -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<String, Object> toMap();
default Set<String> mutated() { return null; }
}

View file

@ -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<String> mutated() {
return null;
}
@Override
public Object build() {
return null;
}
@Override
public Map<String, Object> toMap() {
return null;
}
}
}