use of com.rebuild.core.metadata.easymeta.DisplayType in project rebuild by getrebuild.
the class GroupAggregationController method buildIfGroupField.
private String[] buildIfGroupField(EasyField field) {
DisplayType dt = field.getDisplayType();
// 分组字段类型的支持
boolean allow = dt == DisplayType.TEXT || dt == DisplayType.DATE || dt == DisplayType.DATETIME || dt == DisplayType.CLASSIFICATION || dt == DisplayType.REFERENCE;
if (!allow)
return null;
String[] build = buildField(field.getRawMeta());
if (dt == DisplayType.CLASSIFICATION) {
build[2] += ":" + field.getExtraAttr(EasyFieldConfigProps.CLASSIFICATION_USE);
} else if (dt == DisplayType.REFERENCE) {
build[2] += ":" + field.getRawMeta().getReferenceEntity().getName();
}
return build;
}
use of com.rebuild.core.metadata.easymeta.DisplayType in project rebuild by getrebuild.
the class FieldAggregation method execute.
@Override
public void execute(OperatingContext operatingContext) throws TriggerException {
final String chainName = actionContext.getConfigId() + ":" + operatingContext.getAction().getName();
final List<String> tschain = checkTriggerChain(chainName);
if (tschain == null)
return;
this.prepare(operatingContext);
if (targetRecordId == null) {
log.warn("No target record found");
return;
}
// 聚合数据过滤
JSONObject dataFilter = ((JSONObject) actionContext.getActionContent()).getJSONObject("dataFilter");
String dataFilterSql = null;
if (dataFilter != null && !dataFilter.isEmpty()) {
dataFilterSql = new AdvFilterParser(dataFilter).toSqlWhere();
}
// 构建目标记录数据
Record targetRecord = EntityHelper.forUpdate(targetRecordId, UserService.SYSTEM_USER, false);
JSONArray items = ((JSONObject) actionContext.getActionContent()).getJSONArray("items");
for (Object o : items) {
JSONObject item = (JSONObject) o;
String targetField = item.getString("targetField");
if (!MetadataHelper.checkAndWarnField(targetEntity, targetField)) {
continue;
}
String filterSql = followSourceWhere;
if (dataFilterSql != null) {
filterSql = String.format("( %s ) and ( %s )", followSourceWhere, dataFilterSql);
}
Object evalValue = new AggregationEvaluator(item, sourceEntity, filterSql).eval();
if (evalValue == null)
continue;
DisplayType dt = EasyMetaFactory.getDisplayType(targetEntity.getField(targetField));
if (dt == DisplayType.NUMBER) {
targetRecord.setLong(targetField, CommonsUtils.toLongHalfUp(evalValue));
} else if (dt == DisplayType.DECIMAL) {
targetRecord.setDouble(targetField, ObjectUtils.toDouble(evalValue));
} else if (dt == DisplayType.DATE || dt == DisplayType.DATETIME) {
if (evalValue instanceof Date)
targetRecord.setDate(targetField, (Date) evalValue);
else
targetRecord.setNull(targetField);
} else {
log.warn("Unsupported file-type {} with {}", dt, targetRecordId);
}
}
// 有需要才执行
if (!targetRecord.isEmpty()) {
final boolean forceUpdate = ((JSONObject) actionContext.getActionContent()).getBooleanValue("forceUpdate");
// 跳过权限
PrivilegesGuardContextHolder.setSkipGuard(targetRecordId);
// 强制更新 (v2.9)
if (forceUpdate) {
GeneralEntityServiceContextHolder.setAllowForceUpdate(targetRecordId);
}
// 会关联触发下一触发器(如有)
tschain.add(chainName);
TRIGGERS_CHAIN.set(tschain);
ServiceSpec useService = MetadataHelper.isBusinessEntity(targetEntity) ? Application.getEntityService(targetEntity.getEntityCode()) : Application.getService(targetEntity.getEntityCode());
targetRecord.setDate(EntityHelper.ModifiedOn, CalendarUtils.now());
try {
useService.update(targetRecord);
} finally {
PrivilegesGuardContextHolder.getSkipGuardOnce();
GeneralEntityServiceContextHolder.isAllowForceUpdateOnce();
}
}
}
use of com.rebuild.core.metadata.easymeta.DisplayType in project rebuild by getrebuild.
the class FieldWriteback method checkoutFieldValue.
/**
* @see DisplayType
* @see com.rebuild.core.metadata.EntityRecordCreator
*/
private Object checkoutFieldValue(Object value, EasyField field) {
DisplayType dt = field.getDisplayType();
Object newValue = null;
if (dt == DisplayType.PICKLIST || dt == DisplayType.CLASSIFICATION || dt == DisplayType.REFERENCE || dt == DisplayType.ANYREFERENCE) {
ID id = ID.isId(value) ? ID.valueOf(value.toString()) : null;
if (id != null) {
int idCode = id.getEntityCode();
if (dt == DisplayType.PICKLIST) {
if (idCode == EntityHelper.PickList)
newValue = id;
} else if (dt == DisplayType.CLASSIFICATION) {
if (idCode == EntityHelper.ClassificationData)
newValue = id;
} else if (dt == DisplayType.REFERENCE) {
if (field.getRawMeta().getReferenceEntity().getEntityCode() == idCode)
newValue = id;
} else {
newValue = id;
}
}
} else if (dt == DisplayType.N2NREFERENCE) {
String[] ids = value.toString().split(",");
List<String> idsList = new ArrayList<>();
for (String id : ids) {
if (ID.isId(id))
idsList.add(id);
}
if (ids.length == idsList.size())
newValue = value.toString();
} else if (dt == DisplayType.BOOL) {
if (value instanceof Boolean) {
newValue = value;
} else {
newValue = BooleanUtils.toBooleanObject(value.toString());
}
} else if (dt == DisplayType.MULTISELECT || dt == DisplayType.STATE) {
if (value instanceof Integer || value instanceof Long) {
newValue = value;
}
} else {
// TODO 验证字段格式
newValue = value.toString();
}
if (newValue == null) {
log.warn("Value `{}` cannot be convert to field (value) : {}", value, field.getRawMeta());
}
return newValue;
}
use of com.rebuild.core.metadata.easymeta.DisplayType 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 com.rebuild.core.metadata.easymeta.DisplayType in project rebuild by getrebuild.
the class AdvFilterParser method parseItem.
/**
* 解析查询项为 SQL
*
* @param item
* @param values
* @return
*/
private String parseItem(JSONObject item, JSONObject values) {
String field = item.getString("field");
// fix: _$unthy
if (field.startsWith("&"))
field = field.replace("&", "&");
boolean hasNameFlag = field.startsWith("&");
if (hasNameFlag) {
field = field.substring(1);
}
Field fieldMeta = MetadataHelper.getLastJoinField(rootEntity, field);
if (fieldMeta == null) {
log.warn("Unknown field '{}' in '{}'", field, rootEntity.getName());
return null;
}
DisplayType dt = EasyMetaFactory.getDisplayType(fieldMeta);
if (dt == DisplayType.CLASSIFICATION || (dt == DisplayType.PICKLIST && hasNameFlag)) /* 快速查询 */
{
field = "&" + field;
} else if (hasNameFlag) {
if (!(dt == DisplayType.REFERENCE || dt == DisplayType.N2NREFERENCE)) {
log.warn("Non reference-field '{}' in '{}'", field, rootEntity.getName());
return null;
}
// 转化为名称字段
if (dt == DisplayType.REFERENCE) {
fieldMeta = fieldMeta.getReferenceEntity().getNameField();
dt = EasyMetaFactory.getDisplayType(fieldMeta);
field += "." + fieldMeta.getName();
}
}
String op = item.getString("op");
String value = item.getString("value");
String valueEnd = null;
// exists ( in (...) )
if (hasNameFlag && dt == DisplayType.N2NREFERENCE) {
Entity refEntity = fieldMeta.getReferenceEntity();
String inWhere = String.format("select %s from %s where %s %s %s", refEntity.getPrimaryField().getName(), refEntity.getName(), refEntity.getNameField().getName(), ParseHelper.convetOperation(op), quoteValue('%' + value + '%', FieldType.STRING));
return String.format("exists (select recordId from NreferenceItem where ^%s = recordId and belongField = '%s' and referenceId in (%s))", rootEntity.getPrimaryField().getName(), fieldMeta.getName(), inWhere);
}
// 根据字段类型转换 `op`
final boolean isDatetime = dt == DisplayType.DATETIME;
// 日期时间
if (isDatetime || dt == DisplayType.DATE) {
final boolean isREX = ParseHelper.RED.equalsIgnoreCase(op) || ParseHelper.REM.equalsIgnoreCase(op) || ParseHelper.REY.equalsIgnoreCase(op);
if (ParseHelper.TDA.equalsIgnoreCase(op) || ParseHelper.YTA.equalsIgnoreCase(op) || ParseHelper.TTA.equalsIgnoreCase(op)) {
if (ParseHelper.YTA.equalsIgnoreCase(op)) {
value = formatDate(addDay(-1), 0);
} else if (ParseHelper.TTA.equalsIgnoreCase(op)) {
value = formatDate(addDay(1), 0);
} else {
value = formatDate(CalendarUtils.now(), 0);
}
if (isDatetime) {
op = ParseHelper.BW;
valueEnd = parseValue(value, op, fieldMeta, true);
}
} else if (ParseHelper.CUW.equalsIgnoreCase(op) || ParseHelper.CUM.equalsIgnoreCase(op) || ParseHelper.CUQ.equalsIgnoreCase(op) || ParseHelper.CUY.equalsIgnoreCase(op)) {
Date begin = Moment.moment().startOf(op.substring(2)).date();
value = formatDate(begin, 0);
Date end = Moment.moment(begin).endOf(op.substring(2)).date();
valueEnd = formatDate(end, 0);
if (isDatetime) {
value += ParseHelper.ZERO_TIME;
valueEnd += ParseHelper.FULL_TIME;
}
op = ParseHelper.BW;
} else if (ParseHelper.EQ.equalsIgnoreCase(op) && dt == DisplayType.DATETIME && StringUtils.length(value) == 10) {
op = ParseHelper.BW;
valueEnd = parseValue(value, op, fieldMeta, true);
} else if (isREX || ParseHelper.FUD.equalsIgnoreCase(op) || ParseHelper.FUM.equalsIgnoreCase(op) || ParseHelper.FUY.equalsIgnoreCase(op)) {
int xValue = NumberUtils.toInt(value) * (isREX ? -1 : 1);
Date date;
if (ParseHelper.REM.equalsIgnoreCase(op) || ParseHelper.FUM.equalsIgnoreCase(op)) {
date = CalendarUtils.addMonth(xValue);
} else if (ParseHelper.REY.equalsIgnoreCase(op) || ParseHelper.FUY.equalsIgnoreCase(op)) {
date = CalendarUtils.addMonth(xValue * 12);
} else {
date = CalendarUtils.addDay(xValue);
}
if (isREX) {
value = formatDate(date, 0);
valueEnd = formatDate(CalendarUtils.now(), 0);
} else {
value = formatDate(CalendarUtils.now(), 0);
valueEnd = formatDate(date, 0);
}
if (isDatetime) {
value += ParseHelper.ZERO_TIME;
valueEnd += ParseHelper.FULL_TIME;
}
op = ParseHelper.BW;
}
} else if (dt == DisplayType.TIME) {
// 前端输入 HH:mm
if (value != null && value.length() == 5) {
if (ParseHelper.EQ.equalsIgnoreCase(op)) {
op = ParseHelper.BW;
valueEnd = value + ":59";
}
value += ":00";
}
} else if (dt == DisplayType.MULTISELECT) {
// 多选的包含/不包含要按位计算
if (ParseHelper.IN.equalsIgnoreCase(op) || ParseHelper.NIN.equalsIgnoreCase(op)) {
op = ParseHelper.IN.equalsIgnoreCase(op) ? ParseHelper.BAND : ParseHelper.NBAND;
long maskValue = 0;
for (String s : value.split("\\|")) {
maskValue += ObjectUtils.toLong(s);
}
value = maskValue + "";
}
}
StringBuilder sb = new StringBuilder(field).append(' ').append(ParseHelper.convetOperation(op));
// 无需值
if (ParseHelper.NL.equalsIgnoreCase(op) || ParseHelper.NT.equalsIgnoreCase(op)) {
return sb.toString();
}
sb.append(' ');
if (ParseHelper.BFD.equalsIgnoreCase(op)) {
value = formatDate(addDay(-NumberUtils.toInt(value)), isDatetime ? 1 : 0);
} else if (ParseHelper.BFM.equalsIgnoreCase(op)) {
value = formatDate(addMonth(-NumberUtils.toInt(value)), isDatetime ? 1 : 0);
} else if (ParseHelper.BFY.equalsIgnoreCase(op)) {
value = formatDate(addMonth(-NumberUtils.toInt(value) * 12), isDatetime ? 1 : 0);
} else if (ParseHelper.AFD.equalsIgnoreCase(op)) {
value = formatDate(addDay(NumberUtils.toInt(value)), isDatetime ? 2 : 0);
} else if (ParseHelper.AFM.equalsIgnoreCase(op)) {
value = formatDate(addMonth(NumberUtils.toInt(value)), isDatetime ? 2 : 0);
} else if (ParseHelper.AFY.equalsIgnoreCase(op)) {
value = formatDate(addMonth(NumberUtils.toInt(value) * 12), isDatetime ? 2 : 0);
} else // 部门/用户
if (ParseHelper.SFU.equalsIgnoreCase(op)) {
value = getUser().toLiteral();
} else if (ParseHelper.SFB.equalsIgnoreCase(op)) {
Department dept = UserHelper.getDepartment(getUser());
if (dept != null) {
value = dept.getIdentity().toString();
int ref = fieldMeta.getReferenceEntity().getEntityCode();
if (ref == EntityHelper.User) {
sb.insert(sb.indexOf(" "), ".deptId");
} else if (ref == EntityHelper.Department) {
// NOOP
} else {
value = null;
}
}
} else if (ParseHelper.SFD.equalsIgnoreCase(op)) {
Department dept = UserHelper.getDepartment(getUser());
if (dept != null) {
int refe = fieldMeta.getReferenceEntity().getEntityCode();
if (refe == EntityHelper.Department) {
value = StringUtils.join(UserHelper.getAllChildren(dept), "|");
}
}
} else if (ParseHelper.SFT.equalsIgnoreCase(op)) {
// No any
if (value == null)
value = "0";
// In Sql
value = String.format("( select userId from TeamMember where teamId in ('%s') )", StringUtils.join(value.split("\\|"), "', '"));
}
if (StringUtils.isBlank(value)) {
log.warn("No search value defined : " + item.toJSONString());
return null;
}
// 快速搜索的占位符 `{1}`
if (value.matches("\\{\\d+}")) {
if (values == null || values.isEmpty()) {
return null;
}
String valHold = value.replaceAll("[{}]", "");
value = parseValue(values.get(valHold), op, fieldMeta, false);
} else {
value = parseValue(value, op, fieldMeta, false);
}
// No value for search
if (value == null) {
return null;
}
// 区间
final boolean isBetween = op.equalsIgnoreCase(ParseHelper.BW);
if (isBetween && valueEnd == null) {
valueEnd = parseValue(item.getString("value2"), op, fieldMeta, true);
if (valueEnd == null) {
valueEnd = value;
}
}
// IN
if (op.equalsIgnoreCase(ParseHelper.IN) || op.equalsIgnoreCase(ParseHelper.NIN) || op.equalsIgnoreCase(ParseHelper.SFD) || op.equalsIgnoreCase(ParseHelper.SFT)) {
sb.append(value);
} else {
// LIKE
if (op.equalsIgnoreCase(ParseHelper.LK) || op.equalsIgnoreCase(ParseHelper.NLK)) {
value = '%' + value + '%';
}
sb.append(quoteValue(value, fieldMeta.getType()));
}
if (isBetween) {
sb.insert(0, "( ").append(" and ").append(quoteValue(valueEnd, fieldMeta.getType())).append(" )");
}
return sb.toString();
}
Aggregations