use of cn.devezhao.persist4j.engine.StandardRecord in project rebuild by getrebuild.
the class FieldWriteback method buildTargetRecordData.
private Record buildTargetRecordData() {
final Record targetRecord = EntityHelper.forNew(targetEntity.getEntityCode(), UserService.SYSTEM_USER, false);
final JSONArray items = ((JSONObject) actionContext.getActionContent()).getJSONArray("items");
final Set<String> fieldVars = new HashSet<>();
for (Object o : items) {
JSONObject item = (JSONObject) o;
String sourceField = item.getString("sourceField");
String updateMode = item.getString("updateMode");
// fix: v2.2
if (updateMode == null) {
updateMode = sourceField.contains(DATE_EXPR) ? "FORMULA" : "FIELD";
}
if ("FIELD".equalsIgnoreCase(updateMode)) {
fieldVars.add(sourceField);
} else if ("FORMULA".equalsIgnoreCase(updateMode)) {
if (sourceField.contains(DATE_EXPR) && !sourceField.startsWith(CODE_PREFIX)) {
fieldVars.add(sourceField.split(DATE_EXPR)[0]);
} else {
Set<String> matchsVars = ContentWithFieldVars.matchsVars(sourceField);
for (String field : matchsVars) {
if (MetadataHelper.getLastJoinField(sourceEntity, field) == null) {
throw new MissingMetaExcetion(field, sourceEntity.getName());
}
fieldVars.add(field);
}
}
}
}
// 变量值
Record useSourceData = null;
if (!fieldVars.isEmpty()) {
String sql = String.format("select %s from %s where %s = '%s'", StringUtils.join(fieldVars, ","), sourceEntity.getName(), sourceEntity.getPrimaryField().getName(), actionContext.getSourceRecord());
useSourceData = Application.createQueryNoFilter(sql).record();
}
for (Object o : items) {
JSONObject item = (JSONObject) o;
String targetField = item.getString("targetField");
if (!MetadataHelper.checkAndWarnField(targetEntity, targetField)) {
continue;
}
EasyField targetFieldEasy = EasyMetaFactory.valueOf(targetEntity.getField(targetField));
String updateMode = item.getString("updateMode");
String sourceAny = item.getString("sourceField");
// 置空
if ("VNULL".equalsIgnoreCase(updateMode)) {
targetRecord.setNull(targetField);
} else // 固定值
if ("VFIXED".equalsIgnoreCase(updateMode)) {
RecordVisitor.setValueByLiteral(targetField, sourceAny, targetRecord);
} else // 字段
if ("FIELD".equalsIgnoreCase(updateMode)) {
Field sourceFieldMeta = MetadataHelper.getLastJoinField(sourceEntity, sourceAny);
if (sourceFieldMeta == null)
continue;
Object value = Objects.requireNonNull(useSourceData).getObjectValue(sourceAny);
if (value != null) {
if (targetFieldEasy.getDisplayType() == DisplayType.N2NREFERENCE) {
value = N2NReferenceSupport.items(sourceFieldMeta, actionContext.getSourceRecord());
}
Object newValue = EasyMetaFactory.valueOf(sourceFieldMeta).convertCompatibleValue(value, targetFieldEasy);
targetRecord.setObjectValue(targetField, newValue);
}
} else // 公式
if ("FORMULA".equalsIgnoreCase(updateMode)) {
if (useSourceData == null) {
log.warn("[useSourceData] is null, Set to empty");
useSourceData = new StandardRecord(sourceEntity, null);
}
// 高级公式代码
final boolean useCode = sourceAny.startsWith(CODE_PREFIX);
// 日期兼容 fix: v2.2
if (sourceAny.contains(DATE_EXPR) && !useCode) {
String fieldName = sourceAny.split(DATE_EXPR)[0];
Field sourceField2 = MetadataHelper.getLastJoinField(sourceEntity, fieldName);
if (sourceField2 == null)
continue;
Object value = useSourceData.getObjectValue(fieldName);
Object newValue = value == null ? null : ((EasyDateTime) EasyMetaFactory.valueOf(sourceField2)).convertCompatibleValue(value, targetFieldEasy, sourceAny);
if (newValue != null) {
targetRecord.setObjectValue(targetField, newValue);
}
} else // 高级公式(会涉及各种类型的运算)
// @see AggregationEvaluator#evalFormula
{
String clearFormual = useCode ? sourceAny.substring(4, sourceAny.length() - 4) : sourceAny.replace("×", "*").replace("÷", "/").replace("`", // compatible: v2.4
"\"");
Map<String, Object> envMap = new HashMap<>();
for (String fieldName : fieldVars) {
String replace = "{" + fieldName + "}";
String replaceWhitQuote = "\"" + replace + "\"";
String replaceWhitQuoteSingle = "'" + replace + "'";
boolean forceUseQuote = false;
if (clearFormual.contains(replaceWhitQuote)) {
clearFormual = clearFormual.replace(replaceWhitQuote, fieldName);
forceUseQuote = true;
} else if (clearFormual.contains(replaceWhitQuoteSingle)) {
clearFormual = clearFormual.replace(replaceWhitQuoteSingle, fieldName);
forceUseQuote = true;
} else if (clearFormual.contains(replace)) {
clearFormual = clearFormual.replace(replace, fieldName);
} else {
continue;
}
Object value = useSourceData.getObjectValue(fieldName);
if (value instanceof Date) {
value = CalendarUtils.getUTCDateTimeFormat().format(value);
} else if (value == null) {
// 数字字段置 `0`
Field isNumberField = MetadataHelper.getLastJoinField(sourceEntity, fieldName);
if (isNumberField != null && (isNumberField.getType() == FieldType.LONG || isNumberField.getType() == FieldType.DECIMAL)) {
value = 0;
} else {
value = StringUtils.EMPTY;
}
} else if (forceUseQuote) {
value = value.toString();
}
envMap.put(fieldName, value);
}
Object newValue = AviatorUtils.eval(clearFormual, envMap, false);
if (newValue != null) {
DisplayType dt = targetFieldEasy.getDisplayType();
if (dt == DisplayType.NUMBER) {
targetRecord.setLong(targetField, CommonsUtils.toLongHalfUp(newValue));
} else if (dt == DisplayType.DECIMAL) {
targetRecord.setDouble(targetField, ObjectUtils.toDouble(newValue));
} else if (dt == DisplayType.DATE || dt == DisplayType.DATETIME) {
targetRecord.setDate(targetField, (Date) newValue);
} else {
newValue = checkoutFieldValue(newValue, targetFieldEasy);
if (newValue != null) {
targetRecord.setObjectValue(targetField, newValue);
}
}
}
}
}
}
return targetRecord;
}
use of cn.devezhao.persist4j.engine.StandardRecord in project rebuild by getrebuild.
the class RestoreRecordCreator method create.
// ignoreNullValueWhenNew always true
@Override
public Record create(boolean ignoreNullValueWhenNew) {
Record record = new StandardRecord(entity, null);
for (Map.Entry<String, Object> e : source.entrySet()) {
String fileName = e.getKey();
if (!entity.containsField(fileName)) {
log.warn("Cannot found field [ " + entity.getName() + '#' + fileName + " ], will ignore");
continue;
}
Object value = e.getValue();
if (value == null || StringUtils.isEmpty(value.toString())) {
continue;
}
setFieldValue(entity.getField(fileName), value.toString(), record);
}
return record;
}
use of cn.devezhao.persist4j.engine.StandardRecord in project rebuild by getrebuild.
the class EntityHelper method forNew.
/**
* @param entity
* @param user
* @return
*/
private static Record forNew(Entity entity, ID user, boolean bindCommons) {
Assert.notNull(entity, "[entity] cannot be null");
Assert.notNull(user, "[user] cannot be null");
Record record = new StandardRecord(entity, user);
if (bindCommons) {
bindCommonsFieldsValue(record, true);
}
return record;
}
use of cn.devezhao.persist4j.engine.StandardRecord in project rebuild by getrebuild.
the class EntityHelper method forUpdate.
/**
* @param recordId
* @param user
* @param bindCommons 是否自动补充公共字段
* @return
*/
public static Record forUpdate(ID recordId, ID user, boolean bindCommons) {
Assert.notNull(recordId, "[recordId] cannot be null");
Assert.notNull(user, "[user] cannot be null");
Entity entity = MetadataHelper.getEntity(recordId.getEntityCode());
Record record = new StandardRecord(entity, user);
record.setID(entity.getPrimaryField().getName(), recordId);
if (bindCommons) {
bindCommonsFieldsValue(record, false);
}
return record;
}
Aggregations