Search in sources :

Example 1 with EasyDateTime

use of com.rebuild.core.metadata.easymeta.EasyDateTime 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;
}
Also used : DisplayType(com.rebuild.core.metadata.easymeta.DisplayType) JSONArray(com.alibaba.fastjson.JSONArray) EasyDateTime(com.rebuild.core.metadata.easymeta.EasyDateTime) MissingMetaExcetion(cn.devezhao.persist4j.metadata.MissingMetaExcetion) EasyField(com.rebuild.core.metadata.easymeta.EasyField) EasyField(com.rebuild.core.metadata.easymeta.EasyField) Field(cn.devezhao.persist4j.Field) JSONObject(com.alibaba.fastjson.JSONObject) StandardRecord(cn.devezhao.persist4j.engine.StandardRecord) StandardRecord(cn.devezhao.persist4j.engine.StandardRecord) Record(cn.devezhao.persist4j.Record) JSONObject(com.alibaba.fastjson.JSONObject)

Aggregations

Field (cn.devezhao.persist4j.Field)1 Record (cn.devezhao.persist4j.Record)1 StandardRecord (cn.devezhao.persist4j.engine.StandardRecord)1 MissingMetaExcetion (cn.devezhao.persist4j.metadata.MissingMetaExcetion)1 JSONArray (com.alibaba.fastjson.JSONArray)1 JSONObject (com.alibaba.fastjson.JSONObject)1 DisplayType (com.rebuild.core.metadata.easymeta.DisplayType)1 EasyDateTime (com.rebuild.core.metadata.easymeta.EasyDateTime)1 EasyField (com.rebuild.core.metadata.easymeta.EasyField)1