Skip to content

Commit

Permalink
javers#528 Handling of @ShallowReference on collections and maps
Browse files Browse the repository at this point in the history
  • Loading branch information
FmeuMan committed Mar 11, 2019
1 parent b71ebc3 commit 6ae9576
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 13 deletions.
30 changes: 17 additions & 13 deletions javers-core/src/main/java/org/javers/core/graph/EdgeBuilder.java
Expand Up @@ -8,9 +8,6 @@
import org.javers.core.metamodel.object.PropertyOwnerContext;
import org.javers.core.metamodel.type.*;

import java.util.ArrayList;
import java.util.List;

/**
* @author bartosz walacik
*/
Expand Down Expand Up @@ -48,17 +45,24 @@ MultiEdge createMultiEdge(JaversProperty containerProperty, EnumerableType enume

Object container = node.getPropertyValue(containerProperty);

EnumerableFunction edgeBuilder;
EnumerableFunction edgeBuilder = (input, context) -> {
final LiveCdo cdo = cdoFactory.create(input, context);
if (!containerProperty.isShallowReference()) {
return buildNodeStubOrReuse(cdo);
} else {
return cdo.getGlobalId();
}
};

if (enumerableType instanceof KeyValueType){
KeyValueType mapType = (KeyValueType)enumerableType;
edgeBuilder = new MultiEdgeMapBuilder(
typeMapper.getJaversType(mapType.getKeyType()) instanceof ManagedType,
typeMapper.getJaversType(mapType.getValueType()) instanceof ManagedType
typeMapper.getJaversType(mapType.getValueType()) instanceof ManagedType,
edgeBuilder
);

} else if (enumerableType instanceof ContainerType) {
edgeBuilder = (input, context) -> buildNodeStubOrReuse(cdoFactory.create(input, context));
} else {
} else if (!(enumerableType instanceof ContainerType)) {
throw new JaversException(JaversExceptionCode.NOT_IMPLEMENTED);
}

Expand All @@ -69,24 +73,24 @@ MultiEdge createMultiEdge(JaversProperty containerProperty, EnumerableType enume
private class MultiEdgeMapBuilder implements EnumerableFunction {
private final boolean managedKeys;
private final boolean managedValues;
private final EnumerableFunction edgeBuilder;

public MultiEdgeMapBuilder(boolean managedKeys, boolean managedValues) {
public MultiEdgeMapBuilder(boolean managedKeys, boolean managedValues, EnumerableFunction edgeBuilder) {
this.managedKeys = managedKeys;
this.managedValues = managedValues;
this.edgeBuilder = edgeBuilder;
}

@Override
public Object apply(Object keyOrValue, EnumerationAwareOwnerContext context) {
MapEnumerationOwnerContext mapContext = (MapEnumerationOwnerContext)context;

if (managedKeys && mapContext.isKey()) {
LiveNode objectNode = buildNodeStubOrReuse(cdoFactory.create(keyOrValue, context));
return objectNode;
return edgeBuilder.apply(keyOrValue, context);
}

if (managedValues && !mapContext.isKey()) {
LiveNode objectNode = buildNodeStubOrReuse(cdoFactory.create(keyOrValue, context));
return objectNode;
return edgeBuilder.apply(keyOrValue, context);
}

return keyOrValue;
Expand Down
Expand Up @@ -53,6 +53,38 @@ class JaversCommitE2ETest extends Specification {
commit.snapshots.size() == 1
}

def "should not commit snapshot of ShallowReference collection property"() {
given:
def javers = javers().build()
def entity = new PhoneWithShallowCategory(id:1, shallowCategories:[new CategoryC(1, "old shallow")])

when:
def commit = javers.commit("", entity)

then:
commit.snapshots.each {
println it.toString()
println ".. props:"+ it.state.propertyNames
}
commit.snapshots.size() == 1
}

def "should not commit snapshot of ShallowReference map property"() {
given:
def javers = javers().build()
def entity = new PhoneWithShallowCategory(id:1, shallowCategoryMap:["foo":new CategoryC(1, "old shallow")])

when:
def commit = javers.commit("", entity)

then:
commit.snapshots.each {
println it.toString()
println ".. props:"+ it.state.propertyNames
}
commit.snapshots.size() == 1
}

def "should mark changed properties"() {
given:
def javers = javers().build()
Expand Down
Expand Up @@ -12,6 +12,10 @@ class PhoneWithShallowCategory {
String number = "123"
@ShallowReference
CategoryC shallowCategory
@ShallowReference
Set<CategoryC> shallowCategories
@ShallowReference
Map<String, CategoryC> shallowCategoryMap
CategoryC deepCategory

@Id
Expand Down

0 comments on commit 6ae9576

Please sign in to comment.