mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
80 lines
2.3 KiB
Java
80 lines
2.3 KiB
Java
package io.ebeaninternal.server.deploy;
|
|
|
|
import io.ebean.PersistenceIOException;
|
|
import io.ebean.SqlUpdate;
|
|
import io.ebean.bean.EntityBean;
|
|
import io.ebeaninternal.api.json.SpiJsonReader;
|
|
import io.ebeaninternal.api.json.SpiJsonWriter;
|
|
import io.ebeaninternal.server.deploy.meta.DeployBeanDescriptor;
|
|
|
|
import java.io.IOException;
|
|
|
|
/**
|
|
* Bean descriptor used with element collection of list/set of embeddable.
|
|
*/
|
|
class BeanDescriptorElementEmbedded<T> extends BeanDescriptorElement<T> {
|
|
|
|
private final BeanPropertyAssocOne embeddedProperty;
|
|
|
|
private final EntityBean prototype;
|
|
|
|
private BeanDescriptor targetDescriptor;
|
|
|
|
BeanDescriptorElementEmbedded(BeanDescriptorMap owner, DeployBeanDescriptor<T> deploy, ElementHelp elementHelp) {
|
|
super(owner, deploy, elementHelp);
|
|
try {
|
|
this.prototype = (EntityBean) beanType.newInstance();
|
|
} catch (Exception e) {
|
|
throw new IllegalStateException("Unable to create entity bean prototype for "+beanType);
|
|
}
|
|
BeanPropertyAssocOne<?>[] embedded = propertiesEmbedded();
|
|
if (embedded.length == 1) {
|
|
embeddedProperty = embedded[0];
|
|
} else {
|
|
embeddedProperty = null;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void initialiseOther(BeanDescriptorInitContext initContext) {
|
|
super.initialiseOther(initContext);
|
|
this.targetDescriptor = embeddedProperty.getTargetDescriptor();
|
|
}
|
|
|
|
@Override
|
|
public EntityBean createEntityBeanForJson() {
|
|
return (EntityBean)prototype._ebean_newInstance();
|
|
}
|
|
|
|
public void bindElementValue(SqlUpdate insert, Object value) {
|
|
targetDescriptor.bindElementValue(insert, value);
|
|
}
|
|
|
|
@Override
|
|
public void jsonWriteElement(SpiJsonWriter ctx, Object element) {
|
|
writeJsonElement(ctx, element);
|
|
}
|
|
|
|
@Override
|
|
public T jsonRead(SpiJsonReader jsonRead, String path) throws IOException {
|
|
return readJsonElement(jsonRead, path);
|
|
}
|
|
|
|
@SuppressWarnings("unchecked")
|
|
T readJsonElement(SpiJsonReader jsonRead, String path) throws IOException {
|
|
return (T)targetDescriptor.jsonRead(jsonRead, path);
|
|
}
|
|
|
|
void writeJsonElement(SpiJsonWriter ctx, Object element) {
|
|
try {
|
|
if (element == null) {
|
|
ctx.writeNull();
|
|
} else {
|
|
targetDescriptor.jsonWrite(ctx, (EntityBean)element);
|
|
}
|
|
} catch (IOException e) {
|
|
throw new PersistenceIOException(e);
|
|
}
|
|
}
|
|
}
|