Search in sources :

Example 6 with AdvFilterParser

use of com.rebuild.core.service.query.AdvFilterParser in project rebuild by getrebuild.

the class ChartData method getFilterSql.

/**
 * 获取过滤 SQL
 *
 * @return
 */
protected String getFilterSql() {
    String previewFilter = StringUtils.EMPTY;
    // 限制预览数据量
    if (isFromPreview() && getSourceEntity().containsField(EntityHelper.AutoId)) {
        String maxAidSql = String.format("select max(%s) from %s", EntityHelper.AutoId, getSourceEntity().getName());
        Object[] o = Application.createQueryNoFilter(maxAidSql).unique();
        long maxAid = ObjectUtils.toLong(o[0]);
        if (maxAid > 5000) {
            previewFilter = String.format("(%s >= %d) and ", EntityHelper.AutoId, Math.max(maxAid - 2000, 0));
        }
    }
    JSONObject filterExp = config.getJSONObject("filter");
    if (filterExp == null || filterExp.isEmpty()) {
        return previewFilter + "(1=1)";
    }
    AdvFilterParser filterParser = new AdvFilterParser(filterExp);
    String sqlWhere = filterParser.toSqlWhere();
    if (sqlWhere != null) {
        sqlWhere = previewFilter + sqlWhere;
    }
    return StringUtils.defaultIfBlank(sqlWhere, "(1=1)");
}
Also used : AdvFilterParser(com.rebuild.core.service.query.AdvFilterParser) JSONObject(com.alibaba.fastjson.JSONObject) JSONObject(com.alibaba.fastjson.JSONObject)

Example 7 with AdvFilterParser

use of com.rebuild.core.service.query.AdvFilterParser in project rebuild by getrebuild.

the class ProjectTaskController method taskList.

@RequestMapping("tasks/list")
public JSON taskList(HttpServletRequest request) {
    final ID user = getRequestUser(request);
    final ID projectId = getIdParameterNotNull(request, "project");
    final String planKey = getParameterNotNull(request, "plan");
    String queryWhere = "(" + buildCustomPlanSql(planKey, projectId) + ")";
    // 关键词搜索
    String search = getParameter(request, "search");
    if (StringUtils.isNotBlank(search)) {
        queryWhere += " and taskName like '%" + StringEscapeUtils.escapeSql(search) + "%'";
    }
    // 高级查询
    JSON advFilter = ServletUtils.getRequestJson(request);
    if (advFilter != null) {
        String filterSql = new AdvFilterParser((JSONObject) advFilter).toSqlWhere();
        if (filterSql != null) {
            queryWhere += " and (" + filterSql + ")";
        }
    }
    int pageNo = getIntParameter(request, "pageNo", 1);
    int pageSize = getIntParameter(request, "pageSize", 40);
    int count = -1;
    if (pageNo == 1) {
        String countSql = "select count(taskId) from ProjectTask where " + queryWhere;
        Object[] count2 = Application.createQueryNoFilter(countSql).unique();
        count = ObjectUtils.toInt(count2[0]);
        if (count == 0) {
            return NO_TASKS;
        }
    }
    queryWhere += " order by " + buildQuerySort(request);
    ConfigBean project = ProjectManager.instance.getProject(projectId, user);
    JSONArray alist = queryCardDatas(project, user, queryWhere, new int[] { pageSize, pageNo * pageSize - pageSize });
    return JSONUtils.toJSONObject(new String[] { "count", "tasks" }, new Object[] { count, alist });
}
Also used : AdvFilterParser(com.rebuild.core.service.query.AdvFilterParser) JSONObject(com.alibaba.fastjson.JSONObject) JSONArray(com.alibaba.fastjson.JSONArray) JSON(com.alibaba.fastjson.JSON) JSONObject(com.alibaba.fastjson.JSONObject) ID(cn.devezhao.persist4j.engine.ID) ConfigBean(com.rebuild.core.configuration.ConfigBean) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 8 with AdvFilterParser

use of com.rebuild.core.service.query.AdvFilterParser 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();
        }
    }
}
Also used : AdvFilterParser(com.rebuild.core.service.query.AdvFilterParser) JSONObject(com.alibaba.fastjson.JSONObject) DisplayType(com.rebuild.core.metadata.easymeta.DisplayType) JSONArray(com.alibaba.fastjson.JSONArray) ServiceSpec(com.rebuild.core.service.ServiceSpec) Record(cn.devezhao.persist4j.Record) JSONObject(com.alibaba.fastjson.JSONObject) Date(java.util.Date)

Example 9 with AdvFilterParser

use of com.rebuild.core.service.query.AdvFilterParser in project rebuild by getrebuild.

the class ProtocolFilterParser method parseRef.

/**
 * @param content
 * @param cascadingValue
 * @return
 */
public String parseRef(String content, String cascadingValue) {
    String[] fieldAndEntity = content.split("\\.");
    if (fieldAndEntity.length != 2 || !MetadataHelper.checkAndWarnField(fieldAndEntity[1], fieldAndEntity[0])) {
        return null;
    }
    final Entity entity = MetadataHelper.getEntity(fieldAndEntity[1]);
    final Field field = entity.getField(fieldAndEntity[0]);
    List<String> sqls = new ArrayList<>();
    JSONObject advFilter = getFieldDataFilter(field);
    if (advFilter != null)
        sqls.add(new AdvFilterParser(advFilter).toSqlWhere());
    if (hasFieldCascadingField(field) && ID.isId(cascadingValue)) {
        String cascadingFieldParent = field.getExtraAttrs().getString("_cascadingFieldParent");
        String cascadingFieldChild = field.getExtraAttrs().getString("_cascadingFieldChild");
        if (StringUtils.isNotBlank(cascadingFieldParent)) {
            String[] fs = cascadingFieldParent.split(MetadataHelper.SPLITER_RE);
            sqls.add(String.format("%s = '%s'", fs[1], cascadingValue));
        }
        if (StringUtils.isNotBlank(cascadingFieldChild)) {
            String[] fs = cascadingFieldChild.split(MetadataHelper.SPLITER_RE);
            Entity refEntity = entity.getField(fs[0]).getReferenceEntity();
            String sql = String.format("exists (select %s from %s where ^%s = %s and %s = '%s')", fs[1], refEntity.getName(), field.getReferenceEntity().getPrimaryField().getName(), fs[1], refEntity.getPrimaryField().getName(), cascadingValue);
            sqls.add(sql);
        }
    }
    return sqls.isEmpty() ? null : "( " + StringUtils.join(sqls, " and ") + " )";
}
Also used : Entity(cn.devezhao.persist4j.Entity) Field(cn.devezhao.persist4j.Field) AdvFilterParser(com.rebuild.core.service.query.AdvFilterParser) JSONObject(com.alibaba.fastjson.JSONObject) ArrayList(java.util.ArrayList)

Example 10 with AdvFilterParser

use of com.rebuild.core.service.query.AdvFilterParser in project rebuild by getrebuild.

the class RoleBaseQueryFilter method buildCustomFilter.

/**
 * 自定义权限
 *
 * @param ep
 * @return
 * @see PrivilegesManager#andPassCustomFilter(ID, ID, Permission, Privileges)
 */
private String buildCustomFilter(Privileges ep) {
    if (user == null || useAction == null || !ep.getClass().isAssignableFrom(CustomEntityPrivileges.class))
        return null;
    JSONObject hasFilter = ((CustomEntityPrivileges) ep).getCustomFilter(useAction);
    if (hasFilter == null)
        return null;
    AdvFilterParser advFilterParser = new AdvFilterParser(hasFilter);
    advFilterParser.setUser(user.getId());
    return advFilterParser.toSqlWhere();
}
Also used : CustomEntityPrivileges(com.rebuild.core.privileges.bizz.CustomEntityPrivileges) AdvFilterParser(com.rebuild.core.service.query.AdvFilterParser) JSONObject(com.alibaba.fastjson.JSONObject)

Aggregations

JSONObject (com.alibaba.fastjson.JSONObject)10 AdvFilterParser (com.rebuild.core.service.query.AdvFilterParser)10 ID (cn.devezhao.persist4j.engine.ID)4 JSONArray (com.alibaba.fastjson.JSONArray)4 JSON (com.alibaba.fastjson.JSON)3 Entity (cn.devezhao.persist4j.Entity)2 ConfigBean (com.rebuild.core.configuration.ConfigBean)2 CustomEntityPrivileges (com.rebuild.core.privileges.bizz.CustomEntityPrivileges)2 Team (cn.devezhao.bizz.security.member.Team)1 Field (cn.devezhao.persist4j.Field)1 Query (cn.devezhao.persist4j.Query)1 Record (cn.devezhao.persist4j.Record)1 DisplayType (com.rebuild.core.metadata.easymeta.DisplayType)1 ServiceSpec (com.rebuild.core.service.ServiceSpec)1 FilterParseException (com.rebuild.core.service.query.FilterParseException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 PostMapping (org.springframework.web.bind.annotation.PostMapping)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1