WhoCreated, WhoModified on OneToMany connections. (#942)

* WhoCreated, WhoModified on OneToMany connections.

* PostgreSQL table name fix in tests.
This commit is contained in:
Nagy Vilmos
2017-01-11 20:54:20 +13:00
committed by Rob Bygrave
parent e49c42302a
commit 8fd04807c7
7 changed files with 272 additions and 14 deletions
@@ -140,6 +140,21 @@ public class AnnotationFields extends AnnotationParser {
if (formula != null) {
prop.setSqlFormula(formula.select(), formula.join());
}
initWhoCreated(prop);
initWhoModified(prop);
}
private void initWhoModified(DeployBeanProperty prop) {
if (get(prop, WhoModified.class) != null) {
generatedPropFactory.setWhoModified(prop);
}
}
private void initWhoCreated(DeployBeanProperty prop) {
if (get(prop, WhoCreated.class) != null) {
generatedPropFactory.setWhoCreated(prop);
}
}
private void readField(DeployBeanProperty prop) {
@@ -282,12 +297,8 @@ public class AnnotationFields extends AnnotationParser {
generatedPropFactory.setUpdateTimestamp(prop);
}
if (get(prop, WhoCreated.class) != null) {
generatedPropFactory.setWhoCreated(prop);
}
if (get(prop, WhoModified.class) != null) {
generatedPropFactory.setWhoModified(prop);
}
initWhoCreated(prop);
initWhoModified(prop);
if (get(prop, HistoryExclude.class) != null) {
prop.setExcludedFromHistory();
@@ -3,8 +3,11 @@ package io.ebeaninternal.server.persist.dmlbind;
import io.ebean.bean.EntityBean;
import io.ebeaninternal.server.core.PersistRequestBean;
import io.ebeaninternal.server.deploy.BeanPropertyAssocOne;
import io.ebeaninternal.server.deploy.generatedproperty.GeneratedProperty;
import io.ebeaninternal.server.deploy.id.ImportedId;
import io.ebeaninternal.server.persist.dml.DmlMode;
import io.ebeaninternal.server.persist.dml.GenerateDmlRequest;
import org.jetbrains.annotations.Nullable;
import java.sql.SQLException;
import java.util.List;
@@ -17,10 +20,14 @@ public class BindableAssocOne implements Bindable {
private final BeanPropertyAssocOne<?> assocOne;
private final ImportedId importedId;
private final DmlMode dmlMode;
private final GeneratedProperty generatedProperty;
public BindableAssocOne(BeanPropertyAssocOne<?> assocOne) {
public BindableAssocOne(BeanPropertyAssocOne<?> assocOne, DmlMode mode) {
this.assocOne = assocOne;
this.importedId = assocOne.getImportedId();
this.dmlMode = mode;
this.generatedProperty = assocOne.getGeneratedProperty();
}
public String toString() {
@@ -33,7 +40,9 @@ public class BindableAssocOne implements Bindable {
}
public void addToUpdate(PersistRequestBean<?> request, List<Bindable> list) {
if (request.isAddToUpdate(assocOne)) {
if (generatedProperty != null && generatedProperty.includeInAllUpdates()) {
list.add(this);
} else if (request.isAddToUpdate(assocOne)) {
list.add(this);
}
}
@@ -43,8 +52,41 @@ public class BindableAssocOne implements Bindable {
}
public void dmlBind(BindableRequest request, EntityBean bean) throws SQLException {
if (generatedProperty != null) {
bindGeneratedProperty(request, bean);
} else {
bindNotGeneratedProperty(request, bean);
}
}
private void bindGeneratedProperty(BindableRequest request, EntityBean bean) throws SQLException {
if (DmlMode.INSERT.equals(dmlMode) && generatedProperty.includeInInsert()) {
bindGeneratedInsert(request, bean);
} else if (DmlMode.UPDATE.equals(dmlMode) && generatedProperty.includeInAllUpdates()) {
bindGeneratedUpdate(request, bean);
}
}
private void bindGeneratedUpdate(BindableRequest request, EntityBean bean) throws SQLException {
Object objectValue = generatedProperty.getUpdateValue(assocOne, bean, request.now());
EntityBean generatedValue = castToEntityBean(objectValue);
assocOne.setValueChanged(bean, generatedValue);
registerDeferred(request, bean, generatedValue);
}
private void bindGeneratedInsert(BindableRequest request, EntityBean bean) throws SQLException {
Object objectValue = generatedProperty.getInsertValue(assocOne, bean, request.now());
EntityBean generatedValue = castToEntityBean(objectValue);
assocOne.setValue(bean, generatedValue);
registerDeferred(request, bean, generatedValue);
}
private void bindNotGeneratedProperty(BindableRequest request, EntityBean bean) throws SQLException {
EntityBean assocBean = (EntityBean) assocOne.getValue(bean);
registerDeferred(request, bean, assocBean);
}
private void registerDeferred(BindableRequest request, EntityBean bean, EntityBean assocBean) throws SQLException {
Object boundValue = importedId.bind(request, assocBean);
if (boundValue == null && assocBean != null) {
// this is the scenario for a derived foreign key
@@ -55,4 +97,15 @@ public class BindableAssocOne implements Bindable {
}
}
@Nullable
private EntityBean castToEntityBean(Object objectValue) {
EntityBean generatedValue;
if (objectValue instanceof EntityBean || objectValue == null) {
generatedValue = (EntityBean) objectValue;
} else {
throw new IllegalStateException("Bean " + objectValue.getClass() + " is not enhanced?");
}
return generatedValue;
}
}
@@ -35,7 +35,7 @@ public class FactoryAssocOnes {
}
break;
}
list.add(new BindableAssocOne(one));
list.add(new BindableAssocOne(one, mode));
}
}
}