Skip to content

Commit

Permalink
明确defaultValue的处理规则
Browse files Browse the repository at this point in the history
  • Loading branch information
entropy-cloud committed Jul 28, 2024
1 parent 1df56c7 commit 69cec15
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 28 deletions.
21 changes: 20 additions & 1 deletion docs/dev-guide/recipe/crud.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# 1. 传递一些实体上没有的字段到后台
# 增删改查相关

## 1. 传递一些实体上没有的字段到后台

在meta中增加prop,然后设置它的virtual属性为true,表示是虚拟字段,就不会自动拷贝到实体上。

Expand Down Expand Up @@ -57,3 +59,20 @@ CrudBizModel已经注入了transactionTemplate,可以通过this.txn()来使用

## 4. 查询时要求参数必填
propMeta上可以配置ui:queryMandatory或者query form的cell上配置mandatory

## 5. 自动设置实体上的属性

### ORM save时自动设置缺省值
1. 在Excel数据模型中设置defaultValue,则新建实体的时候如果没有设置该字段,则会自动设置为缺省值。
2. 保存时,如果字段要求非空,但是当前值为null,则也会设置为缺省值。
3. 保存时,如果字段当前值为null,但是具有seq标签,则自动生成一个序列号设置到实体上。

### OrmEntityCopier执行XMeta中设置的autoExpr
在XMeta层面,如果为prop配置了autoExpr,则当前台没有提交该属性时会自动执行autoExpr来设置。
```xml
<prop name="orderNo">
<autoExpr when="save">
<app:GenOrderNo/>
</autoExpr>
</prop>
```
18 changes: 15 additions & 3 deletions nop-biz/src/main/java/io/nop/biz/crud/AutoExprRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,25 @@ public static void runAutoExpr(String action,
scope.setLocalValue(null, BizConstants.VAR_OBJ_META, objMeta);

for (IObjPropMeta propMeta : objMeta.getProps()) {
ObjConditionExpr autoExpr = propMeta.getAutoExpr();
if (autoExpr == null)
if (propMeta.isVirtual())
continue;

String name = propMeta.getName();
if (propMeta.getMapToProp() != null)
name = propMeta.getMapToProp();

if (ignoreFields != null && ignoreFields.contains(propMeta.getName()))
continue;

ObjConditionExpr autoExpr = propMeta.getAutoExpr();
if (autoExpr == null) {
// 只有save的时候才考虑设置缺省值。
if (propMeta.getDefaultValue() != null && BizConstants.METHOD_SAVE.equals(action)) {
BeanTool.setProperty(entity, name, propMeta.getDefaultValue());
}
continue;
}

if (autoExpr.getWhen() != null && !autoExpr.getWhen().contains(action))
continue;

Expand All @@ -45,7 +57,7 @@ public static void runAutoExpr(String action,
}

if (value != Undefined.undefined)
BeanTool.setProperty(entity, propMeta.getName(), value);
BeanTool.setProperty(entity, name, value);
}
}
}
14 changes: 7 additions & 7 deletions nop-biz/src/main/java/io/nop/biz/crud/OrmEntityCopier.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public void copyToEntity(Object src, IOrmEntity target, FieldSelectionBean selec
IObjPropMeta propMeta = getProp(objMeta, name);
if (propMeta != null) {
// 如果明确从前台提交参数,那么以提交的值为准。如果禁止前台提交,应该设置字段的insertable=false,updatable=false
if (propMeta.getAutoExpr() != null) {
if (propMeta.getAutoExpr() != null || propMeta.getDefaultValue() != null) {
ignoreAutoExprProps.add(propMeta.getName());
}
}
Expand All @@ -146,7 +146,7 @@ public void copyToEntity(Object src, IOrmEntity target, FieldSelectionBean selec
return;

IObjPropMeta propMeta = getProp(objMeta, name);
if (propMeta != null && propMeta.getAutoExpr() != null) {
if (propMeta != null && (propMeta.getAutoExpr() != null || propMeta.getDefaultValue() != null)) {
ignoreAutoExprProps.add(propMeta.getName());
}
copyField(beanModel, null, src, target, name, name, null, propMeta, baseBizObjName, scope);
Expand All @@ -170,7 +170,7 @@ public void copyToEntity(Object src, IOrmEntity target, FieldSelectionBean selec

IObjPropMeta propMeta = getProp(objMeta, from);
if (propMeta != null) {
if (propMeta.getAutoExpr() != null)
if (propMeta.getAutoExpr() != null || propMeta.getDefaultValue() != null)
ignoreAutoExprProps.add(propMeta.getName());
}
copyField(beanModel, map, src, target, from, name, field, propMeta, baseBizObjName, scope);
Expand All @@ -192,6 +192,10 @@ private void copyField(IBeanModel beanModel, Map<String, Object> map,
FieldSelectionBean field, IObjPropMeta propMeta, String baseBizObjName, IEvalScope scope) {
Object fromValue = beanModel.getProperty(src, from);
if (propMeta != null) {
// 虚拟字段不会设置到实体上
if (propMeta.isVirtual())
return;

IEvalAction setter = propMeta.getSetter();
if (setter != null) {
scope = scope.newChildScope();
Expand All @@ -202,10 +206,6 @@ private void copyField(IBeanModel beanModel, Map<String, Object> map,
return;
}

// 虚拟字段不会设置到实体上
if (propMeta.isVirtual())
return;

if (propMeta.getMapToProp() != null) {
BeanTool.setComplexProperty(target, propMeta.getMapToProp(), fromValue);
return;
Expand Down
36 changes: 19 additions & 17 deletions nop-orm/src/main/java/io/nop/orm/persister/EntityPersisterImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -368,24 +368,28 @@ void incOptimisticLockVersion(IOrmEntity entity) {
}
}

void checkMandatoryWhenSave(IOrmEntity entity) {
void checkColumnValueWhenSave(IOrmEntity entity) {
for (IColumnModel propModel : entityModel.getColumns()) {
if (!propModel.isMandatory())
continue;

Object value = entity.orm_propValue(propModel.getPropId());
if (value == null) {
if (propModel.containsTag(OrmConstants.TAG_SEQ)) {
// 如果指定了seq标签,且字段非空,则自动根据sequence配置生成
String propKey = OrmModelHelper.buildEntityPropKey(propModel);
Object seqValue = propModel.getStdDataType().isNumericType() ?
env.getSequenceGenerator().generateLong(propKey, false)
: env.getSequenceGenerator().generateString(propKey, false);
entity.orm_propValue(propModel.getPropId(), seqValue);
} else if (propModel.getDefaultValue() != null) {
entity.orm_propValue(propModel.getPropId(), propModel.getDefaultValue());
if (propModel.isMandatory()) {
if (propModel.getDefaultValue() != null) {
entity.orm_propValue(propModel.getPropId(), propModel.getDefaultValue());
} else if (propModel.containsTag(OrmConstants.TAG_SEQ)) {
// 如果指定了seq标签,且字段非空,则自动根据sequence配置生成
String propKey = OrmModelHelper.buildEntityPropKey(propModel);
Object seqValue = propModel.getStdDataType().isNumericType() ?
env.getSequenceGenerator().generateLong(propKey, false)
: env.getSequenceGenerator().generateString(propKey, false);
entity.orm_propValue(propModel.getPropId(), seqValue);
} else {
if (CFG_ORM_CHECK_MANDATORY_WHEN_SAVE.get())
throw newError(ERR_ORM_MANDATORY_PROP_IS_NULL, entity).param(ARG_PROP_NAME, propModel.getName());
}
} else {
throw newError(ERR_ORM_MANDATORY_PROP_IS_NULL, entity).param(ARG_PROP_NAME, propModel.getName());
// 即使不是mandatory的字段,只要设置了defaultValue,则新建的时候都会自动设置
if (!entity.orm_propInited(propModel.getPropId()) && propModel.getDefaultValue() != null)
entity.orm_propValue(propModel.getPropId(), propModel.getDefaultValue());
}
}
}
Expand Down Expand Up @@ -442,9 +446,7 @@ void processTenantId(IOrmEntity entity) {
protected void queueSave(IOrmEntity entity, IOrmSessionImplementor session) {
OrmTimestampHelper.onCreate(entityModel, entity);

if (CFG_ORM_CHECK_MANDATORY_WHEN_SAVE.get()) {
this.checkMandatoryWhenSave(entity);
}
this.checkColumnValueWhenSave(entity);

ShardSelection shard = getShardSelection(entity);

Expand Down

0 comments on commit 69cec15

Please sign in to comment.