Skip to content

Fix 4.0.4 #902

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion @rbv
Submodule @rbv updated from 07ba14 to 0626bc
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</parent>
<groupId>com.rebuild</groupId>
<artifactId>rebuild</artifactId>
<version>4.0.3</version>
<version>4.0.4</version>
<name>rebuild</name>
<description>Building your business-systems freely!</description>
<url>https://getrebuild.com/</url>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/rebuild/core/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ public class Application implements ApplicationListener<ApplicationStartedEvent>
/**
* Rebuild Version
*/
public static final String VER = "4.0.3";
public static final String VER = "4.0.4";
/**
* Rebuild Build [MAJOR]{1}[MINOR]{2}[PATCH]{2}[BUILD]{2}
*/
public static final int BUILD = 4000309;
public static final int BUILD = 4000410;

static {
// Driver for DB
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
import cn.devezhao.persist4j.Field;
import cn.devezhao.persist4j.Record;
import com.alibaba.fastjson.JSONObject;
import com.rebuild.core.DefinedException;
import com.rebuild.core.metadata.easymeta.DisplayType;
import com.rebuild.core.metadata.easymeta.EasyField;
import com.rebuild.core.support.i18n.Language;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

Expand Down Expand Up @@ -67,15 +69,20 @@ public String generate(Record record) {
seriesFormat = DisplayType.SERIES.getDefaultFormat();
}

List<SeriesVar> vars = explainVars(seriesFormat, record);
for (SeriesVar var : vars) {
seriesFormat = seriesFormat.replace("{" + var.getSymbols() + "}", var.generate());
}
try {
List<SeriesVar> vars = explainVars(seriesFormat, record);
for (SeriesVar var : vars) {
seriesFormat = seriesFormat.replace("{" + var.getSymbols() + "}", var.generate());
}

if (seriesFormat.contains(CHECKSUM)) {
seriesFormat = seriesFormat.replace(CHECKSUM, String.valueOf(mod10(seriesFormat)));
}
return seriesFormat;

if (seriesFormat.contains(CHECKSUM)) {
seriesFormat = seriesFormat.replace(CHECKSUM, String.valueOf(mod10(seriesFormat)));
} catch (Exception ex) {
throw new DefinedException(Language.L("自动编号规则无效") + "(" + seriesFormat + ")");
}
return seriesFormat;
}

private static final Pattern VAR_PATTERN = Pattern.compile("\\{(@?[\\w.]+)}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import cn.devezhao.bizz.privileges.impl.BizzPermission;
import cn.devezhao.commons.CalendarUtils;
import cn.devezhao.commons.ObjectUtils;
import cn.devezhao.persist4j.Entity;
import cn.devezhao.persist4j.Field;
import cn.devezhao.persist4j.Record;
import cn.devezhao.persist4j.dialect.FieldType;
Expand Down Expand Up @@ -86,9 +87,11 @@ public class FieldWriteback extends FieldAggregation {

private static final String DATE_EXPR = "#";
private static final String CODE_PREFIX = "{{{{"; // ends with }}}}
private static final String SOURCE_FIELD_VAR_PREFIX = "^";

protected Set<ID> targetRecordIds;
protected Record targetRecordData;
private boolean targetRecordDataHasSourceFieldVars;

public FieldWriteback(ActionContext context) {
super(context, Boolean.TRUE);
Expand Down Expand Up @@ -168,7 +171,12 @@ private Object execute38(OperatingContext operatingContext) throws TriggerExcept
continue;
}

Record targetRecord = targetRecordData.clone();
Record targetRecord;
if (targetRecordDataHasSourceFieldVars) {
targetRecord = buildTargetRecordData(operatingContext, targetRecordId, false);
} else {
targetRecord = targetRecordData.clone();
}
targetRecord.setID(targetEntity.getPrimaryField().getName(), targetRecordId);
targetRecord.setDate(EntityHelper.ModifiedOn, CalendarUtils.now());
targetRecord.setID(EntityHelper.ModifiedBy, UserService.SYSTEM_USER);
Expand Down Expand Up @@ -306,18 +314,19 @@ else if (isOne2One) {
if (targetRecordIds.isEmpty()) {
log.debug("Target record(s) are empty.");
} else {
targetRecordData = buildTargetRecordData(operatingContext, false);
targetRecordData = buildTargetRecordData(operatingContext, null, false);
}
}

/**
* 构建目标记录
*
* @param operatingContext
* @param targetRecordId404
* @param fromRefresh
* @return
*/
protected Record buildTargetRecordData(OperatingContext operatingContext, Boolean fromRefresh) {
protected Record buildTargetRecordData(OperatingContext operatingContext, ID targetRecordId404, boolean fromRefresh) {
// v3.3 源字段为空时置空目标字段
final boolean clearFields = ((JSONObject) actionContext.getActionContent()).getBooleanValue("clearFields");
final boolean forceVNull = fromRefresh || (clearFields && operatingContext.getAction() == InternalPermission.DELETE_BEFORE);
Expand All @@ -329,6 +338,9 @@ protected Record buildTargetRecordData(OperatingContext operatingContext, Boolea
final Set<String> fieldVarsN2NPath = new HashSet<>();
// 变量值
Record useSourceData = null;
// v4.0.4 使用目标记录数据参与运算
Record useTargetData = null;
final Set<String> fieldVarsInTarget = new HashSet<>();

if (!forceVNull) {
for (Object o : items) {
Expand All @@ -348,7 +360,13 @@ protected Record buildTargetRecordData(OperatingContext operatingContext, Boolea
} else {
Set<String> matchsVars = ContentWithFieldVars.matchsVars(sourceField);
for (String field : matchsVars) {
if (N2NReferenceSupport.isN2NMixPath(field, sourceEntity)) {
if (field.startsWith(SOURCE_FIELD_VAR_PREFIX)) {
field = field.substring(1);
if (MetadataHelper.getLastJoinField(targetEntity, field) == null) {
throw new MissingMetaExcetion(field, targetEntity.getName());
}
fieldVarsInTarget.add(SOURCE_FIELD_VAR_PREFIX + field);
} else if (N2NReferenceSupport.isN2NMixPath(field, sourceEntity)) {
fieldVarsN2NPath.add(field);
} else {
if (MetadataHelper.getLastJoinField(sourceEntity, field) == null) {
Expand Down Expand Up @@ -377,6 +395,17 @@ protected Record buildTargetRecordData(OperatingContext operatingContext, Boolea
useSourceData.setObjectValue(field, n2nVal);
}
}
if (!fieldVarsInTarget.isEmpty()) {
this.targetRecordDataHasSourceFieldVars = true;
if (targetRecordId404 != null) {
String sql = MessageFormat.format("select {0},{1} from {2} where {1} = ?",
StringUtils.join(fieldVarsInTarget, ","),
targetEntity.getPrimaryField().getName(),
targetEntity.getName());
sql = sql.replace(SOURCE_FIELD_VAR_PREFIX, ""); // Remove `^`
useTargetData = Application.createQueryNoFilter(sql).setParameter(1, targetRecordId404).record();
}
}
}

for (Object o : items) {
Expand Down Expand Up @@ -452,7 +481,9 @@ else if ("FORMULA".equalsIgnoreCase(updateMode)) {

Map<String, Object> envMap = new HashMap<>();

for (String fieldName : fieldVars) {
Set<String> fieldVarsMix = new HashSet<>(fieldVars);
fieldVarsMix.addAll(fieldVarsInTarget);
for (String fieldName : fieldVarsMix) {
String replace = "{" + fieldName + "}";
String replaceWhitQuote = "\"" + replace + "\"";
String replaceWhitQuoteSingle = "'" + replace + "'";
Expand All @@ -470,54 +501,73 @@ else if ("FORMULA".equalsIgnoreCase(updateMode)) {
continue;
}

Object value = useSourceData.getObjectValue(fieldName);
Entity useEntity;
Field useVarField;
Object useValue = null;
if (fieldName.startsWith(SOURCE_FIELD_VAR_PREFIX)) {
String fieldName2 = fieldName.substring(1);
if (useTargetData == null) log.debug("No `useTargetData` for var : {}", fieldName);
else useValue = useTargetData.getObjectValue(fieldName2);
useEntity = this.targetEntity;
useVarField = MetadataHelper.getLastJoinField(useEntity, fieldName2);
} else {
useValue = useSourceData.getObjectValue(fieldName);
useEntity = this.sourceEntity;
useVarField = MetadataHelper.getLastJoinField(useEntity, fieldName);
}

// fix: 3.5.4
Field varField = MetadataHelper.getLastJoinField(sourceEntity, fieldName);
EasyField easyVarField = varField == null ? null : EasyMetaFactory.valueOf(varField);
boolean isMultiField = easyVarField != null && (easyVarField.getDisplayType() == DisplayType.MULTISELECT
|| easyVarField.getDisplayType() == DisplayType.TAG || easyVarField.getDisplayType() == DisplayType.N2NREFERENCE);
// fix: 3.8
boolean isStateField = easyVarField != null && easyVarField.getDisplayType() == DisplayType.STATE;
EasyField easyVarField = null;
boolean isMultiField = false;
boolean isStateField = false;
boolean isNumberField = false;
if (useVarField != null) {
easyVarField = EasyMetaFactory.valueOf(useVarField);
isMultiField = easyVarField.getDisplayType() == DisplayType.MULTISELECT
|| easyVarField.getDisplayType() == DisplayType.TAG
|| easyVarField.getDisplayType() == DisplayType.N2NREFERENCE;
isStateField = easyVarField.getDisplayType() == DisplayType.STATE;
isNumberField = useVarField.getType() == FieldType.LONG || useVarField.getType() == FieldType.DECIMAL;
}

if (isStateField) {
value = value == null ? "" : StateHelper.getLabel(varField, (Integer) value);
} else if (value instanceof Date) {
value = CalendarUtils.getUTCDateTimeFormat().format(value);
} else if (value == null) {
useValue = useValue == null ? "" : StateHelper.getLabel(useVarField, (Integer) useValue);
} else if (useValue instanceof Date) {
useValue = CalendarUtils.getUTCDateTimeFormat().format(useValue);
} else if (useValue == null) {
// N2N 保持 `NULL`
Field isN2NField = sourceEntity.containsField(fieldName) ? sourceEntity.getField(fieldName) : null;
Field isN2NField = useEntity.containsField(fieldName) ? useEntity.getField(fieldName) : null;
// 数字字段置 `0`
if (varField != null
&& (varField.getType() == FieldType.LONG || varField.getType() == FieldType.DECIMAL)) {
value = 0L;
if (isNumberField) {
useValue = 0L;
} else if (fieldVarsN2NPath.contains(fieldName)
|| (isN2NField != null && isN2NField.getType() == FieldType.REFERENCE_LIST)) {
// Keep NULL
log.debug("Keep NULL for N2N");
} else {
value = StringUtils.EMPTY;
useValue = StringUtils.EMPTY;
}
} else if (isMultiField) {
// v3.5.5: 目标值为多引用时保持 `ID[]`
if (easyVarField.getDisplayType() == DisplayType.N2NREFERENCE
&& targetFieldEasy.getDisplayType() == DisplayType.N2NREFERENCE) {
value = StringUtils.join((ID[]) value, MultiValue.MV_SPLIT);
useValue = StringUtils.join((ID[]) useValue, MultiValue.MV_SPLIT);
} else {
// force `TEXT`
EasyField fakeTextField = EasyMetaFactory.valueOf(MetadataHelper.getField("User", "fullName"));
value = easyVarField.convertCompatibleValue(value, fakeTextField);
useValue = easyVarField.convertCompatibleValue(useValue, fakeTextField);
}
} else if (value instanceof ID || forceUseQuote) {
value = value.toString();
} else if (useValue instanceof ID || forceUseQuote) {
useValue = useValue.toString();
}

// v3.6.3 整数/小数强制使用 BigDecimal 高精度
if (value instanceof Long) value = BigDecimal.valueOf((Long) value);
if (useValue instanceof Long) useValue = BigDecimal.valueOf((Long) useValue);

envMap.put(fieldName, value);
fieldName = fieldName.replace(SOURCE_FIELD_VAR_PREFIX, "_");
envMap.put(fieldName, useValue);
}

Object newValue = AviatorUtils.eval(clearFormula, envMap, Boolean.FALSE);
clearFormula = clearFormula.replace(SOURCE_FIELD_VAR_PREFIX, "_");
Object newValue = AviatorUtils.eval(clearFormula, envMap, false);

if (newValue != null) {
DisplayType targetType = targetFieldEasy.getDisplayType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void refresh() {
ID fakeSourceId = EntityHelper.newUnsavedId(fa.sourceEntity.getEntityCode());
Record fakeSourceRecord = EntityHelper.forUpdate(fakeSourceId, triggerUser, false);
OperatingContext oCtx = OperatingContext.create(triggerUser, BizzPermission.NONE, fakeSourceRecord, fakeSourceRecord);
fa.targetRecordData = fa.buildTargetRecordData(oCtx, true);
fa.targetRecordData = fa.buildTargetRecordData(oCtx, null, true);

try {
fa.execute(oCtx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class ContentWithFieldVars {
/**
* 通过 `{}` 包裹的变量或字段
*/
public static final Pattern PATT_VAR = Pattern.compile("\\{([0-9a-zA-Z._$]{3,})}");
public static final Pattern PATT_VAR = Pattern.compile("\\{(\\^?[0-9a-zA-Z._$]{3,})}");

/**
* 替换文本中的字段变量
Expand Down
Loading