Search in sources :

Example 1 with Query

use of cn.devezhao.persist4j.Query in project rebuild by getrebuild.

the class BulkOperator method prepareRecords.

/**
 * 获取待操作记录
 *
 * @return
 */
protected ID[] prepareRecords() {
    if (this.records != null) {
        return this.records;
    }
    if (context.getRecords() != null) {
        this.records = context.getRecords();
        setTotal(this.records.length);
        return this.records;
    }
    JSONObject asFilterExp = (JSONObject) context.getExtraParams().get("customData");
    AdvFilterParser filterParser = new AdvFilterParser(asFilterExp);
    String sqlWhere = filterParser.toSqlWhere();
    // `(1=1)`.length < 10
    if (sqlWhere.length() < 10) {
        throw new FilterParseException("Must specify items of filter : " + sqlWhere);
    }
    Entity entity = MetadataHelper.getEntity(asFilterExp.getString("entity"));
    String sql = String.format("select %s from %s where (1=1) and %s", entity.getPrimaryField().getName(), entity.getName(), sqlWhere);
    // NOTE 注意没有分页
    Query query = Application.createQuery(sql, context.getOpUser());
    Object[][] array = QueryHelper.readArray(query);
    Set<ID> ids = new HashSet<>();
    for (Object[] o : array) {
        ids.add((ID) o[0]);
    }
    return ids.toArray(new ID[0]);
}
Also used : Entity(cn.devezhao.persist4j.Entity) AdvFilterParser(com.rebuild.core.service.query.AdvFilterParser) JSONObject(com.alibaba.fastjson.JSONObject) FilterParseException(com.rebuild.core.service.query.FilterParseException) Query(cn.devezhao.persist4j.Query) JSONObject(com.alibaba.fastjson.JSONObject) ID(cn.devezhao.persist4j.engine.ID) HashSet(java.util.HashSet)

Example 2 with Query

use of cn.devezhao.persist4j.Query in project rebuild by getrebuild.

the class DataImporter method findRepeatedRecordId.

/**
 * @param repeatFields
 * @param data
 * @return
 */
protected ID findRepeatedRecordId(Field[] repeatFields, Record data) {
    Map<String, Object> wheres = new HashMap<>();
    for (Field c : repeatFields) {
        String cName = c.getName();
        if (data.hasValue(cName)) {
            wheres.put(cName, data.getObjectValue(cName));
        }
    }
    log.info("Checking repeated : " + wheres);
    if (wheres.isEmpty())
        return null;
    Entity entity = data.getEntity();
    StringBuilder sql = new StringBuilder(String.format("select %s from %s where (1=1)", entity.getPrimaryField().getName(), entity.getName()));
    for (String c : wheres.keySet()) {
        sql.append(" and ").append(c).append(" = :").append(c);
    }
    Query query = Application.createQueryNoFilter(sql.toString());
    for (Map.Entry<String, Object> e : wheres.entrySet()) {
        query.setParameter(e.getKey(), e.getValue());
    }
    Object[] exists = query.unique();
    return exists == null ? null : (ID) exists[0];
}
Also used : Field(cn.devezhao.persist4j.Field) Entity(cn.devezhao.persist4j.Entity) Query(cn.devezhao.persist4j.Query)

Example 3 with Query

use of cn.devezhao.persist4j.Query in project rebuild by getrebuild.

the class RecordCheckout method checkoutReferenceValue.

protected ID checkoutReferenceValue(Field field, Cell cell) {
    final String val = cell.asString();
    final Entity refEntity = field.getReferenceEntity();
    // 支持ID
    if (ID.isId(val) && ID.valueOf(val).getEntityCode().intValue() == refEntity.getEntityCode()) {
        ID checkId = ID.valueOf(val);
        Object exists = Application.getQueryFactory().uniqueNoFilter(checkId, refEntity.getPrimaryField().getName());
        if (exists == null) {
            log.warn("Reference ID `{}` not exists", checkId);
            return null;
        } else {
            return checkId;
        }
    }
    Object val2Text = checkoutFieldValue(refEntity.getNameField(), cell, false);
    if (val2Text == null)
        return null;
    Query query;
    // 用户特殊处理
    if (refEntity.getEntityCode() == EntityHelper.User) {
        String sql = MessageFormat.format("select userId from User where loginName = ''{0}'' or email = ''{0}'' or fullName = ''{0}''", StringEscapeUtils.escapeSql(val2Text.toString()));
        query = Application.createQueryNoFilter(sql);
    } else {
        // 查找引用实体的名称字段和自动编号字段
        Set<String> queryFields = new HashSet<>();
        queryFields.add(refEntity.getNameField().getName());
        for (Field s : MetadataSorter.sortFields(refEntity, DisplayType.SERIES)) {
            queryFields.add(s.getName());
        }
        StringBuilder sql = new StringBuilder(String.format("select %s from %s where ", refEntity.getPrimaryField().getName(), refEntity.getName()));
        for (String qf : queryFields) {
            sql.append(String.format("%s = '%s' or ", qf, StringEscapeUtils.escapeSql((String) val2Text)));
        }
        sql = new StringBuilder(sql.substring(0, sql.length() - 4));
        query = Application.createQueryNoFilter(sql.toString());
    }
    Object[] found = query.unique();
    return found != null ? (ID) found[0] : null;
}
Also used : Entity(cn.devezhao.persist4j.Entity) Field(cn.devezhao.persist4j.Field) Query(cn.devezhao.persist4j.Query) ID(cn.devezhao.persist4j.engine.ID)

Example 4 with Query

use of cn.devezhao.persist4j.Query in project rebuild by getrebuild.

the class ProjectTaskController method queryCardDatas.

private JSONArray queryCardDatas(ConfigBean project, ID user, String queryWhere, int[] limits) {
    // 卡片显示字段
    JSON cardFields = project.getJSON("cardFields");
    final Set<String> fields2show = new HashSet<>();
    if (cardFields == null) {
        fields2show.add("createdOn");
        fields2show.add("endTime");
        fields2show.add("_tag");
    } else {
        for (Object o : (JSONArray) cardFields) {
            fields2show.add(o.toString());
        }
    }
    String queryFields = FMT_FIELDS11 + ",";
    if (fields2show.contains("createdBy"))
        queryFields += "createdBy,";
    else
        queryFields += "taskId,";
    if (fields2show.contains("modifiedOn"))
        queryFields += "modifiedOn,";
    else
        queryFields += "taskId,";
    if (fields2show.contains("description"))
        queryFields += "description,";
    else
        queryFields += "taskId,";
    if (fields2show.contains("attachments"))
        queryFields += "attachments,";
    else
        queryFields += "taskId,";
    queryFields = queryFields.substring(0, queryFields.length() - 1);
    String querySql = String.format("select %s from ProjectTask where %s", queryFields, queryWhere);
    Query query = Application.createQueryNoFilter(querySql);
    if (limits != null)
        query.setLimit(limits[0], limits[1]);
    Object[][] tasks = query.array();
    JSONArray alist = new JSONArray();
    for (Object[] o : tasks) {
        JSONObject item = formatTask(o, user, fields2show.contains("_tag"));
        if (fields2show.contains("createdBy")) {
            item.put("createdBy", new Object[] { o[12], UserHelper.getName((ID) o[12]) });
        }
        if (!fields2show.contains("createdOn")) {
            item.remove("createdOn");
        }
        if (fields2show.contains("modifiedOn")) {
            item.put("modifiedOn", I18nUtils.formatDate((Date) o[13]));
        }
        if (!fields2show.contains("endTime")) {
            item.remove("endTime");
        }
        if (fields2show.contains("description")) {
            item.put("description", StringUtils.isNotBlank((String) o[14]));
        }
        if (fields2show.contains("attachments")) {
            item.put("attachments", o[15] != null && ((String) o[15]).length() > 10);
        }
        item.remove("planName");
        alist.add(item);
    }
    return alist;
}
Also used : Query(cn.devezhao.persist4j.Query) 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) Date(java.util.Date) HashSet(java.util.HashSet)

Example 5 with Query

use of cn.devezhao.persist4j.Query in project rebuild by getrebuild.

the class SeriesReindexTask method exec.

@Override
public Integer exec() {
    if (EasyMetaFactory.getDisplayType(field) != DisplayType.SERIES) {
        throw new IllegalArgumentException("None SERIES field : " + field);
    }
    String sql = String.format("select %s from %s", field.getOwnEntity().getPrimaryField().getName(), field.getOwnEntity().getName());
    if (ONLY_REINDEX_BLANK) {
        sql += String.format(" where %s is null or %s = ''", field.getName(), field.getName());
    }
    Query query = Application.createQueryNoFilter(sql);
    Object[][] array = QueryHelper.readArray(query);
    setTotal(array.length);
    for (Object[] o : array) {
        if (this.isInterrupt()) {
            this.setInterrupted();
            break;
        }
        try {
            Record record = EntityHelper.forUpdate((ID) o[0], UserService.SYSTEM_USER, false);
            String series = SeriesGeneratorFactory.generate(field);
            record.setString(field.getName(), series);
            Application.getCommonsService().update(record, false);
            this.addSucceeded();
        } finally {
            this.addCompleted();
        }
    }
    return this.getSucceeded();
}
Also used : Query(cn.devezhao.persist4j.Query) Record(cn.devezhao.persist4j.Record)

Aggregations

Query (cn.devezhao.persist4j.Query)6 Entity (cn.devezhao.persist4j.Entity)3 Field (cn.devezhao.persist4j.Field)3 ID (cn.devezhao.persist4j.engine.ID)3 JSONObject (com.alibaba.fastjson.JSONObject)3 JSON (com.alibaba.fastjson.JSON)2 HashSet (java.util.HashSet)2 Record (cn.devezhao.persist4j.Record)1 JSONArray (com.alibaba.fastjson.JSONArray)1 AdvFilterParser (com.rebuild.core.service.query.AdvFilterParser)1 FilterParseException (com.rebuild.core.service.query.FilterParseException)1 Date (java.util.Date)1 Map (java.util.Map)1