#115 - Mapping - Add support for @ElementCollection enhancement

Initial support - add simple Set support and Json support for simple List and Set.
This commit is contained in:
Rob Bygrave committed 2018-03-20 17:24:35 +13:00
1 parent 74a9019535
commit 85c4385498
19 files changed
+422 -48

No files matched your search

@@ -18,18 +18,17 @@ public class BeanCollectionHelpFactory {
/**
* Create the helper based on the many property.
*/
public static <T> BeanCollectionHelp<T> create(BeanPropertyAssocMany<T> manyProperty) {
public static <T> BeanCollectionHelp<T> create(BeanPropertyAssocMany<T> many) {
boolean elementCollection = manyProperty.isElementCollection();
ManyType manyType = manyProperty.getManyType();
boolean elementCollection = many.isElementCollection();
ManyType manyType = many.getManyType();
switch (manyType) {
case LIST:
return elementCollection ? new BeanListHelpElement<>(manyProperty) : new BeanListHelp<>(manyProperty);
return elementCollection ? new BeanListHelpElement<>(many) : new BeanListHelp<>(many);
case SET:
return new BeanSetHelp<>(manyProperty);
return elementCollection ? new BeanSetHelpElement<>(many) : new BeanSetHelp<>(many);
case MAP:
return new BeanMapHelp<>(manyProperty);
return new BeanMapHelp<>(many);
default:
throw new RuntimeException("Invalid type " + manyType);
}
@@ -406,7 +406,7 @@ public class BeanDescriptor<T> implements BeanType<T>, STreeType {
private final BeanDescriptorDraftHelp<T> draftHelp;
private final BeanDescriptorCacheHelp<T> cacheHelp;
private final BeanDescriptorJsonHelp<T> jsonHelp;
final BeanDescriptorJsonHelp<T> jsonHelp;
private DocStoreBeanAdapter<T> docStoreAdapter;
private DocumentMapping docMapping;
private boolean docStoreEmbeddedInvalidation;
@@ -3330,6 +3330,14 @@ public class BeanDescriptor<T> implements BeanType<T>, STreeType {
jsonHelp.jsonWriteDirtyProperties(writeJson, bean, dirtyProps);
}
public void jsonWriteElement(SpiJsonWriter ctx, Object element) {
throw new IllegalStateException("Unexpected - expect Element override");
}
public Object jsonReadCollection(ReadJson readJson, EntityBean parentBean) throws IOException {
throw new IllegalStateException("Unexpected - expect Element override");
}
public void jsonWrite(SpiJsonWriter writeJson, EntityBean bean) throws IOException {
jsonHelp.jsonWrite(writeJson, bean, null);
}
@@ -1,15 +1,34 @@
package io.ebeaninternal.server.deploy;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import io.ebean.PersistenceIOException;
import io.ebean.bean.EntityBean;
import io.ebeaninternal.server.deploy.meta.DeployBeanDescriptor;
import io.ebeaninternal.server.text.json.ReadJson;
import io.ebeaninternal.server.text.json.SpiJsonWriter;
import io.ebeaninternal.server.type.ScalarType;
import java.io.IOException;
/**
* Bean descriptor used with ElementCollection (where we don't have a mapped type/class).
*/
public class BeanDescriptorElement<T> extends BeanDescriptor<T> {
class BeanDescriptorElement<T> extends BeanDescriptor<T> {
public BeanDescriptorElement(BeanDescriptorMap owner, DeployBeanDescriptor<T> deploy) {
private final ScalarType<Object> scalarType;
private final ElementHelp elementHelp;
BeanDescriptorElement(BeanDescriptorMap owner, DeployBeanDescriptor<T> deploy, ElementHelp elementHelp) {
super(owner, deploy);
this.elementHelp = elementHelp;
BeanProperty[] props = propertiesNonTransient();
if (props.length == 1) {
this.scalarType = props[0].getScalarType();
} else {
this.scalarType = null;
}
}
public boolean isElementType() {
@@ -21,4 +40,28 @@ public class BeanDescriptorElement<T> extends BeanDescriptor<T> {
return new ElementEntityBean(properties);
}
public void jsonWriteElement(SpiJsonWriter ctx, Object element) {
try {
scalarType.jsonWrite(ctx.gen(), element);
} catch (IOException e) {
throw new PersistenceIOException(e);
}
}
public Object jsonReadCollection(ReadJson readJson, EntityBean parentBean) throws IOException {
JsonParser parser = readJson.getParser();
ElementCollector add = elementHelp.createCollector();
do {
JsonToken token = parser.nextToken();
if (JsonToken.VALUE_NULL == token || JsonToken.END_ARRAY == token) {
break;
}
Object element = scalarType.jsonRead(parser);
add.addElement(element);
} while (true);
return add.collection();
}
}
@@ -1658,8 +1658,19 @@ public class BeanDescriptorManager implements BeanDescriptorMap {
/**
* Create a BeanDescriptor for an ElementCollection target.
*/
public <A> BeanDescriptor<A> createElementDescriptor(DeployBeanDescriptor<A> elementDescriptor) {
return new BeanDescriptorElement<>(this, elementDescriptor);
public <A> BeanDescriptor<A> createElementDescriptor(DeployBeanDescriptor<A> elementDescriptor, ManyType manyType) {
return new BeanDescriptorElement<>(this, elementDescriptor, elementHelper(manyType));
}
private ElementHelp elementHelper(ManyType manyType) {
switch (manyType) {
case LIST: return new ElementHelpList();
case SET: return new ElementHelpSet();
case MAP: return new ElementHelpMap();
default:
throw new IllegalStateException("manyType unexpected "+manyType);
}
}
public void visitMetrics(MetricVisitor visitor) {
@@ -1,7 +1,6 @@
package io.ebeaninternal.server.deploy;
import io.ebean.bean.BeanCollection;
import io.ebean.bean.BeanCollectionAdd;
import io.ebean.bean.EntityBean;
import io.ebeaninternal.server.text.json.SpiJsonWriter;
@@ -24,14 +23,8 @@ public class BeanListHelpElement<T> extends BeanListHelp<T> {
}
}
@Override
public BeanCollectionAdd getBeanCollectionAdd(Object bc, String mapKey) {
return super.getBeanCollectionAdd(bc, mapKey);
}
@Override
void jsonWriteElement(SpiJsonWriter ctx, Object element) {
many.jsonWriteValue(ctx, element);
//targetDescriptor.jsonWrite(ctx, (EntityBean) element);
many.jsonWriteElementValue(ctx, element);
}
}
@@ -881,4 +881,31 @@ public class BeanPropertyAssocMany<T> extends BeanPropertyAssoc<T> implements ST
public BeanCollectionHelp<T> getHelp() {
return help;
}
public void jsonWriteElementValue(SpiJsonWriter ctx, Object element) {
throw new IllegalStateException("Never Expected");
}
/**
* Read the collection (JSON Array) containing entity beans.
*/
public Object jsonReadCollection(ReadJson readJson, EntityBean parentBean) throws IOException {
BeanCollection<?> collection = createEmpty(parentBean);
BeanCollectionAdd add = getBeanCollectionAdd(collection, null);
do {
EntityBean detailBean = (EntityBean) targetDescriptor.jsonRead(readJson, name);
if (detailBean == null) {
// read the entire array
break;
}
add.addEntityBean(detailBean);
if (parentBean != null && childMasterProperty != null) {
// bind detail bean back to master via mappedBy property
childMasterProperty.setValue(detailBean, parentBean);
}
} while (true);
return collection;
}
}
@@ -3,8 +3,6 @@ package io.ebeaninternal.server.deploy;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import io.ebean.bean.BeanCollection;
import io.ebean.bean.BeanCollectionAdd;
import io.ebean.bean.EntityBean;
import io.ebeaninternal.server.text.json.ReadJson;
@@ -57,23 +55,7 @@ class BeanPropertyAssocManyJsonHelp {
return;
}
BeanCollection<?> collection = many.createEmpty(parentBean);
BeanCollectionAdd add = many.getBeanCollectionAdd(collection, null);
do {
EntityBean detailBean = (EntityBean) many.targetDescriptor.jsonRead(readJson, many.name);
if (detailBean == null) {
// read the entire array
break;
}
add.addEntityBean(detailBean);
if (parentBean != null && many.childMasterProperty != null) {
// bind detail bean back to master via mappedBy property
many.childMasterProperty.setValue(detailBean, parentBean);
}
} while (true);
many.setValue(parentBean, collection);
many.setValue(parentBean, many.jsonReadCollection(readJson, parentBean));
}
/**
@@ -1,6 +1,12 @@
package io.ebeaninternal.server.deploy;
import io.ebean.PersistenceIOException;
import io.ebean.bean.EntityBean;
import io.ebeaninternal.server.deploy.meta.DeployBeanPropertySimpleCollection;
import io.ebeaninternal.server.text.json.ReadJson;
import io.ebeaninternal.server.text.json.SpiJsonWriter;
import java.io.IOException;
public class BeanPropertySimpleCollection<T> extends BeanPropertyAssocMany<T> {
@@ -27,4 +33,25 @@ public class BeanPropertySimpleCollection<T> extends BeanPropertyAssocMany<T> {
targetDescriptor = descriptor.getBeanDescriptor(targetType);
}
}
/**
* Json write scalar value.
*/
@Override
public void jsonWriteValue(SpiJsonWriter writeJson, Object value) {
try {
scalarType.jsonWrite(writeJson.gen(), value);
} catch (IOException e) {
throw new PersistenceIOException(e);
}
}
public void jsonWriteElementValue(SpiJsonWriter ctx, Object element) {
elementDescriptor.jsonWriteElement(ctx, element);
}
@Override
public Object jsonReadCollection(ReadJson readJson, EntityBean parentBean) throws IOException {
return elementDescriptor.jsonReadCollection(readJson, parentBean);
}
}
@@ -16,7 +16,7 @@ import java.util.Set;
/**
* Helper specifically for dealing with Sets.
*/
public final class BeanSetHelp<T> extends BaseCollectionHelp<T> {
public class BeanSetHelp<T> extends BaseCollectionHelp<T> {
/**
* When attached to a specific many property.
@@ -0,0 +1,30 @@
package io.ebeaninternal.server.deploy;
import io.ebean.bean.BeanCollection;
import io.ebean.bean.EntityBean;
import io.ebeaninternal.server.text.json.SpiJsonWriter;
/**
* Helper for element collection List.
*/
public class BeanSetHelpElement<T> extends BeanSetHelp<T> {
BeanSetHelpElement(BeanPropertyAssocMany<T> many) {
super(many);
}
@Override
public void add(BeanCollection<?> collection, EntityBean bean, boolean withCheck) {
Object elementValue = bean._ebean_getField(0);
if (withCheck) {
collection.internalAddWithCheck(elementValue);
} else {
collection.internalAdd(elementValue);
}
}
@Override
void jsonWriteElement(SpiJsonWriter ctx, Object element) {
many.jsonWriteElementValue(ctx, element);
}
}
@@ -0,0 +1,17 @@
package io.ebeaninternal.server.deploy;
/**
* Collects (List/Set/Map) of elements.
*/
public interface ElementCollector {
/**
* Add an element.
*/
void addElement(Object element);
/**
* Return the populated collection/map.
*/
Object collection();
}
@@ -0,0 +1,13 @@
package io.ebeaninternal.server.deploy;
/**
* Helper for handling ElementCollection.
*/
interface ElementHelp {
/**
* Return a new collector.
*/
ElementCollector createCollector();
}
@@ -0,0 +1,27 @@
package io.ebeaninternal.server.deploy;
import java.util.ArrayList;
import java.util.List;
class ElementHelpList implements ElementHelp {
@Override
public ElementCollector createCollector() {
return new Collector();
}
private static class Collector implements ElementCollector {
private List<Object> list = new ArrayList<>();
@Override
public void addElement(Object element) {
list.add(element);
}
@Override
public Object collection() {
return list;
}
}
}
@@ -0,0 +1,28 @@
package io.ebeaninternal.server.deploy;
import java.util.LinkedHashMap;
import java.util.Map;
class ElementHelpMap implements ElementHelp {
@Override
public ElementCollector createCollector() {
return new Collector();
}
private static class Collector implements ElementCollector {
private Map<Object, Object> map = new LinkedHashMap<>();
@Override
public void addElement(Object element) {
throw new RuntimeException("asd");
//map.put()
}
@Override
public Object collection() {
return map;
}
}
}
@@ -0,0 +1,27 @@
package io.ebeaninternal.server.deploy;
import java.util.LinkedHashSet;
import java.util.Set;
class ElementHelpSet implements ElementHelp {
@Override
public ElementCollector createCollector() {
return new Collector();
}
private static class Collector implements ElementCollector {
private Set<Object> set = new LinkedHashSet<>();
@Override
public void addElement(Object element) {
set.add(element);
}
@Override
public Object collection() {
return set;
}
}
}
@@ -189,14 +189,23 @@ class AnnotationAssocManys extends AnnotationParser {
if (fullTableName == null) {
fullTableName = descriptor.getBaseTable()+"_"+ CamelCaseHelper.toUnderscoreFromCamel(prop.getName());
}
//namingConvention.
BeanTable localTable = factory.getBeanTable(descriptor.getBeanType());
if (collectionTable != null) {
prop.getTableJoin().addJoinColumn(true, collectionTable.joinColumns(), localTable);
}
if (!prop.getTableJoin().hasJoinColumns()) {
BeanProperty localId = localTable.getIdProperty();
if (localId != null) {
// add foreign key based on convention
String fkColName = namingConvention.getForeignKey(descriptor.getBaseTable(), localId.getName());
prop.getTableJoin().addJoinColumn(new DeployTableJoinColumn(localId.getDbColumn(), fkColName));
}
}
BeanTable beanTable = factory.getCollectionBeanTable(fullTableName, prop.getTargetType());
prop.setBeanTable(beanTable);
if (collectionTable != null) {
prop.getTableJoin().addJoinColumn(true, collectionTable.joinColumns(), beanTable);
}
Class<?> elementType = prop.getTargetType();
DeployBeanDescriptor<?> elementDescriptor = factory.createDeployDescriptor(elementType);
@@ -220,7 +229,7 @@ class AnnotationAssocManys extends AnnotationParser {
Class<?> owningType = prop.getOwningType();
factory.createUnidirectional(elementDescriptor, owningType, beanTable, prop.getTableJoin());
prop.setElementDescriptor(factory.createElementDescriptor(elementDescriptor));
prop.setElementDescriptor(factory.createElementDescriptor(elementDescriptor, prop.getManyType()));
}
/**