Search in sources :

Example 1 with Field

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

the class DataListManager method formatFieldsLayout.

/**
 * @param entity
 * @param user
 * @param filter 过滤无读取权限的字段
 * @param config
 * @return
 */
public JSON formatFieldsLayout(String entity, ID user, boolean filter, ConfigBean config) {
    List<Map<String, Object>> columnList = new ArrayList<>();
    Entity entityMeta = MetadataHelper.getEntity(entity);
    Field namedField = entityMeta.getNameField();
    // 默认配置
    if (config == null) {
        columnList.add(formatField(namedField));
        if (!StringUtils.equalsIgnoreCase(namedField.getName(), EntityHelper.CreatedBy) && entityMeta.containsField(EntityHelper.CreatedBy)) {
            columnList.add(formatField(entityMeta.getField(EntityHelper.CreatedBy)));
        }
        if (!StringUtils.equalsIgnoreCase(namedField.getName(), EntityHelper.CreatedOn) && entityMeta.containsField(EntityHelper.CreatedOn)) {
            columnList.add(formatField(entityMeta.getField(EntityHelper.CreatedOn)));
        }
    } else {
        for (Object o : (JSONArray) config.getJSON("config")) {
            JSONObject item = (JSONObject) o;
            String field = item.getString("field");
            Field lastField = MetadataHelper.getLastJoinField(entityMeta, field);
            if (lastField == null) {
                log.warn("Unknown field '" + field + "' in '" + entity + "'");
                continue;
            }
            String[] fieldPath = field.split("\\.");
            Map<String, Object> formatted = null;
            if (fieldPath.length == 1) {
                formatted = formatField(lastField);
            } else {
                // 如果没有引用实体的读权限,则直接过滤掉字段
                Field parentField = entityMeta.getField(fieldPath[0]);
                if (!filter) {
                    formatted = formatField(lastField, parentField);
                } else if (Application.getPrivilegesManager().allowRead(user, lastField.getOwnEntity().getEntityCode())) {
                    formatted = formatField(lastField, parentField);
                }
            }
            if (formatted != null) {
                Integer width = item.getInteger("width");
                if (width != null) {
                    formatted.put("width", width);
                }
                columnList.add(formatted);
            }
        }
    }
    return JSONUtils.toJSONObject(new String[] { "entity", "nameField", "fields" }, new Object[] { entity, namedField.getName(), columnList });
}
Also used : Entity(cn.devezhao.persist4j.Entity) Field(cn.devezhao.persist4j.Field) EasyField(com.rebuild.core.metadata.easymeta.EasyField) JSONObject(com.alibaba.fastjson.JSONObject) ArrayList(java.util.ArrayList) JSONArray(com.alibaba.fastjson.JSONArray) JSONObject(com.alibaba.fastjson.JSONObject) Map(java.util.Map)

Example 2 with Field

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

the class FormsBuilder method setFormInitialValue.

/**
 * 表单初始值填充
 *
 * @param entity
 * @param formModel
 * @param initialVal 此值优先级大于字段默认值
 */
public void setFormInitialValue(Entity entity, JSON formModel, JSONObject initialVal) {
    if (initialVal == null || initialVal.isEmpty()) {
        return;
    }
    JSONArray elements = ((JSONObject) formModel).getJSONArray("elements");
    if (elements == null || elements.isEmpty()) {
        return;
    }
    // 已布局字段。字段是否布局会影响返回值
    Set<String> inFormFields = new HashSet<>();
    for (Object o : elements) {
        inFormFields.add(((JSONObject) o).getString("field"));
    }
    // 保持在初始值中(TODO 更多保持字段)
    Set<String> initialValKeeps = new HashSet<>();
    Map<String, Object> initialValReady = new HashMap<>();
    for (Map.Entry<String, Object> e : initialVal.entrySet()) {
        final String field = e.getKey();
        final String value = (String) e.getValue();
        if (StringUtils.isBlank(value))
            continue;
        // 引用字段值如 `&User`
        if (field.startsWith(DV_REFERENCE_PREFIX)) {
            Object mixValue = getReferenceMixValue(value);
            if (mixValue != null) {
                Entity source = MetadataHelper.getEntity(field.substring(1));
                Field[] reftoFields = MetadataHelper.getReferenceToFields(source, entity);
                // 如有多个则全部填充
                for (Field refto : reftoFields) {
                    initialValReady.put(refto.getName(), inFormFields.contains(refto.getName()) ? mixValue : value);
                }
            }
        } else // 主实体字段
        if (field.equals(DV_MAINID)) {
            Field dtmField = MetadataHelper.getDetailToMainField(entity);
            Object mixValue = inFormFields.contains(dtmField.getName()) ? getReferenceMixValue(value) : (DV_MAINID.equals(value) ? EntityHelper.UNSAVED_ID : value);
            if (mixValue != null) {
                initialValReady.put(dtmField.getName(), mixValue);
                initialValKeeps.add(dtmField.getName());
            }
        } else // 其他
        if (entity.containsField(field)) {
            if (EasyMetaFactory.getDisplayType(entity.getField(field)) == DisplayType.REFERENCE) {
                Object mixValue = inFormFields.contains(field) ? getReferenceMixValue(value) : value;
                if (mixValue != null) {
                    initialValReady.put(field, mixValue);
                }
            }
        } else {
            log.warn("Unknown value pair : " + field + " = " + value);
        }
    }
    if (initialValReady.isEmpty())
        return;
    // 已布局的移除
    for (Object o : elements) {
        JSONObject item = (JSONObject) o;
        String field = item.getString("field");
        if (initialValReady.containsKey(field)) {
            item.put("value", initialValKeeps.contains(field) ? initialValReady.get(field) : initialValReady.remove(field));
        }
    }
    // 如明细记录中的主实体字段值
    if (!initialValReady.isEmpty()) {
        ((JSONObject) formModel).put("initialValue", initialValReady);
    }
}
Also used : Entity(cn.devezhao.persist4j.Entity) JSONArray(com.alibaba.fastjson.JSONArray) EasyField(com.rebuild.core.metadata.easymeta.EasyField) Field(cn.devezhao.persist4j.Field) JSONObject(com.alibaba.fastjson.JSONObject) JSONObject(com.alibaba.fastjson.JSONObject)

Example 3 with Field

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

the class FormsBuilder method getHadApproval.

/**
 * @param entity
 * @param recordId
 * @return
 * @see RobotApprovalManager#hadApproval(Entity, ID)
 */
private ApprovalState getHadApproval(Entity entity, ID recordId) {
    // 新建时
    if (recordId == null) {
        return RobotApprovalManager.instance.hadApproval(entity, null);
    }
    // 普通实体
    if (entity.getMainEntity() == null) {
        return RobotApprovalManager.instance.hadApproval(entity, recordId);
    }
    // 明细实体
    ID mainid = FormBuilderContextHolder.getMainIdOfDetail();
    if (mainid == null) {
        Field dtmField = MetadataHelper.getDetailToMainField(entity);
        Object[] o = Application.getQueryFactory().uniqueNoFilter(recordId, dtmField.getName());
        if (o == null) {
            log.warn("No main-id found : {}", recordId);
            return null;
        }
        mainid = (ID) o[0];
    }
    return RobotApprovalManager.instance.hadApproval(entity.getMainEntity(), mainid);
}
Also used : EasyField(com.rebuild.core.metadata.easymeta.EasyField) Field(cn.devezhao.persist4j.Field) JSONObject(com.alibaba.fastjson.JSONObject) ID(cn.devezhao.persist4j.engine.ID)

Example 4 with Field

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

the class FormsBuilder method buildModelElements.

/**
 * 构建表单元素
 *
 * @param elements
 * @param entity
 * @param data
 * @param user
 * @param useAdvControl
 */
protected void buildModelElements(JSONArray elements, Entity entity, Record data, ID user, boolean useAdvControl) {
    final User formUser = Application.getUserStore().getUser(user);
    final Date now = CalendarUtils.now();
    // Check and clean
    for (Iterator<Object> iter = elements.iterator(); iter.hasNext(); ) {
        JSONObject el = (JSONObject) iter.next();
        String fieldName = el.getString("field");
        if (DIVIDER_LINE.equalsIgnoreCase(fieldName)) {
            continue;
        }
        // 已删除字段
        if (!MetadataHelper.checkAndWarnField(entity, fieldName)) {
            iter.remove();
            continue;
        }
        // v2.2 高级控制
        Object displayOnCreate = el.remove("displayOnCreate");
        Object displayOnUpdate = el.remove("displayOnUpdate");
        Object requiredOnCreate = el.remove("requiredOnCreate");
        Object requiredOnUpdate = el.remove("requiredOnUpdate");
        if (useAdvControl) {
            // 显示
            if (displayOnCreate != null && !(Boolean) displayOnCreate && data == null) {
                iter.remove();
                continue;
            }
            if (displayOnUpdate != null && !(Boolean) displayOnUpdate && data != null) {
                iter.remove();
                continue;
            }
            // 必填
            if (requiredOnCreate != null && (Boolean) requiredOnCreate && data == null) {
                el.put("nullable", false);
            }
            if (requiredOnUpdate != null && (Boolean) requiredOnUpdate && data != null) {
                el.put("nullable", false);
            }
        }
        // 自动只读的
        final boolean roViaAuto = el.getBooleanValue("readonly");
        final Field fieldMeta = entity.getField(fieldName);
        final EasyField easyField = EasyMetaFactory.valueOf(fieldMeta);
        final DisplayType dt = easyField.getDisplayType();
        el.put("label", easyField.getLabel());
        el.put("type", dt.name());
        el.put("readonly", (data != null && !fieldMeta.isUpdatable()) || roViaAuto);
        // 优先使用指定值
        final Boolean nullable = el.getBoolean("nullable");
        if (nullable != null) {
            el.put("nullable", nullable);
        } else {
            el.put("nullable", fieldMeta.isNullable());
        }
        // 字段扩展配置 FieldExtConfigProps
        JSONObject fieldExtAttrs = easyField.getExtraAttrs(true);
        el.putAll(fieldExtAttrs);
        if (dt == DisplayType.PICKLIST) {
            JSONArray options = PickListManager.instance.getPickList(fieldMeta);
            el.put("options", options);
        } else if (dt == DisplayType.STATE) {
            JSONArray options = StateManager.instance.getStateOptions(fieldMeta);
            el.put("options", options);
            el.remove(EasyFieldConfigProps.STATE_CLASS);
        } else if (dt == DisplayType.MULTISELECT) {
            JSONArray options = MultiSelectManager.instance.getSelectList(fieldMeta);
            el.put("options", options);
        } else if (dt == DisplayType.DATETIME) {
            String format = StringUtils.defaultIfBlank(easyField.getExtraAttr(EasyFieldConfigProps.DATETIME_FORMAT), easyField.getDisplayType().getDefaultFormat());
            el.put(EasyFieldConfigProps.DATETIME_FORMAT, format);
        } else if (dt == DisplayType.DATE) {
            String format = StringUtils.defaultIfBlank(easyField.getExtraAttr(EasyFieldConfigProps.DATE_FORMAT), easyField.getDisplayType().getDefaultFormat());
            el.put(EasyFieldConfigProps.DATE_FORMAT, format);
        } else if (dt == DisplayType.TIME) {
            String format = StringUtils.defaultIfBlank(easyField.getExtraAttr(EasyFieldConfigProps.TIME_FORMAT), easyField.getDisplayType().getDefaultFormat());
            el.put(EasyFieldConfigProps.TIME_FORMAT, format);
        } else if (dt == DisplayType.CLASSIFICATION) {
            el.put("openLevel", ClassificationManager.instance.getOpenLevel(fieldMeta));
        }
        // 编辑/视图
        if (data != null) {
            Object value = wrapFieldValue(data, easyField, user);
            if (value != null) {
                el.put("value", value);
            }
        } else // 新建记录
        {
            if (!fieldMeta.isCreatable()) {
                el.put("readonly", true);
                switch(fieldName) {
                    case EntityHelper.CreatedOn:
                    case EntityHelper.ModifiedOn:
                        el.put("value", CalendarUtils.getUTCDateTimeFormat().format(now));
                        break;
                    case EntityHelper.CreatedBy:
                    case EntityHelper.ModifiedBy:
                    case EntityHelper.OwningUser:
                        el.put("value", FieldValueHelper.wrapMixValue(formUser.getId(), formUser.getFullName()));
                        break;
                    case EntityHelper.OwningDept:
                        Department dept = formUser.getOwningDept();
                        Assert.notNull(dept, "Department of user is unset : " + formUser.getId());
                        el.put("value", FieldValueHelper.wrapMixValue((ID) dept.getIdentity(), dept.getName()));
                        break;
                    case EntityHelper.ApprovalId:
                        el.put("value", FieldValueHelper.wrapMixValue(null, Language.L("未提交")));
                        break;
                    case EntityHelper.ApprovalState:
                        el.put("value", ApprovalState.DRAFT.getState());
                        break;
                    default:
                        break;
                }
            }
            // 默认值
            if (el.get("value") == null) {
                if (dt == DisplayType.SERIES) {
                    el.put("value", Language.L("自动值"));
                } else {
                    Object defaultValue = easyField.exprDefaultValue();
                    if (defaultValue != null) {
                        el.put("value", easyField.wrapValue(defaultValue));
                    }
                }
            }
            // 触发器自动值
            if (roViaAuto && el.get("value") == null) {
                if (dt == DisplayType.EMAIL || dt == DisplayType.PHONE || dt == DisplayType.URL || dt == DisplayType.DATE || dt == DisplayType.DATETIME || dt == DisplayType.NUMBER || dt == DisplayType.DECIMAL || dt == DisplayType.SERIES || dt == DisplayType.TEXT || dt == DisplayType.NTEXT) {
                    el.put("value", Language.L("自动值"));
                }
            }
        }
    // end 新建记录
    }
// end for
}
Also used : User(com.rebuild.core.privileges.bizz.User) DisplayType(com.rebuild.core.metadata.easymeta.DisplayType) JSONArray(com.alibaba.fastjson.JSONArray) EasyField(com.rebuild.core.metadata.easymeta.EasyField) EasyField(com.rebuild.core.metadata.easymeta.EasyField) Field(cn.devezhao.persist4j.Field) Department(com.rebuild.core.privileges.bizz.Department) JSONObject(com.alibaba.fastjson.JSONObject) JSONObject(com.alibaba.fastjson.JSONObject) ID(cn.devezhao.persist4j.engine.ID)

Example 5 with Field

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

the class ViewAddonsManager method getViewAddons.

/**
 * @param entity
 * @param user
 * @param applyType
 * @return
 */
private JSONObject getViewAddons(String entity, ID user, String applyType) {
    final ConfigBean config = getLayout(user, entity, applyType);
    final Permission useAction = TYPE_TAB.equals(applyType) ? BizzPermission.READ : BizzPermission.CREATE;
    final Entity entityMeta = MetadataHelper.getEntity(entity);
    final Set<Entity> mfRefs = hasMultiFieldsReferenceTo(entityMeta);
    // 未配置则使用全部
    if (config == null) {
        JSONArray useRefs = new JSONArray();
        for (Field field : entityMeta.getReferenceToFields(true)) {
            Entity e = field.getOwnEntity();
            if (e.getMainEntity() == null && Application.getPrivilegesManager().allow(user, e.getEntityCode(), useAction)) {
                useRefs.add(getEntityShow(field, mfRefs, applyType));
            }
        }
        // 跟进(动态)
        useRefs.add(getEntityShow(MetadataHelper.getField("Feeds", "relatedRecord"), mfRefs, applyType));
        // 任务(项目)
        useRefs.add(getEntityShow(MetadataHelper.getField("ProjectTask", "relatedRecord"), mfRefs, applyType));
        // 附件
        if (TYPE_TAB.equals(applyType)) {
            useRefs.add(getEntityShow(MetadataHelper.getField("Attachment", "relatedRecord"), mfRefs, applyType));
        }
        return JSONUtils.toJSONObject("items", useRefs);
    }
    // compatible: v2.2
    JSON configJson = config.getJSON("config");
    if (configJson instanceof JSONArray) {
        configJson = JSONUtils.toJSONObject("items", configJson);
    }
    JSONArray addons = new JSONArray();
    for (Object o : ((JSONObject) configJson).getJSONArray("items")) {
        String key;
        String label = null;
        // compatible: v2.8
        if (o instanceof JSONArray) {
            key = (String) ((JSONArray) o).get(0);
            label = (String) ((JSONArray) o).get(1);
        } else {
            key = o.toString();
        }
        // Entity.Field (v1.9)
        String[] ef = key.split("\\.");
        if (!MetadataHelper.containsEntity(ef[0])) {
            continue;
        }
        Entity addonEntity = MetadataHelper.getEntity(ef[0]);
        if (ef.length > 1 && !MetadataHelper.checkAndWarnField(addonEntity, ef[1])) {
            continue;
        }
        if (Application.getPrivilegesManager().allow(user, addonEntity.getEntityCode(), useAction)) {
            JSONObject show = ef.length > 1 ? getEntityShow(addonEntity.getField(ef[1]), mfRefs, applyType) : EasyMetaFactory.toJSON(addonEntity);
            if (StringUtils.isNotBlank(label))
                show.put("entityLabel", label);
            addons.add(show);
        }
    }
    return JSONUtils.toJSONObject(new String[] { "items", "autoExpand", "autoHide" }, new Object[] { addons, ((JSONObject) configJson).getBooleanValue("autoExpand"), ((JSONObject) configJson).getBooleanValue("autoHide") });
}
Also used : Entity(cn.devezhao.persist4j.Entity) Field(cn.devezhao.persist4j.Field) JSONObject(com.alibaba.fastjson.JSONObject) BizzPermission(cn.devezhao.bizz.privileges.impl.BizzPermission) Permission(cn.devezhao.bizz.privileges.Permission) JSONArray(com.alibaba.fastjson.JSONArray) JSON(com.alibaba.fastjson.JSON) JSONObject(com.alibaba.fastjson.JSONObject) ConfigBean(com.rebuild.core.configuration.ConfigBean)

Aggregations

Field (cn.devezhao.persist4j.Field)109 Entity (cn.devezhao.persist4j.Entity)52 JSONObject (com.alibaba.fastjson.JSONObject)42 EasyField (com.rebuild.core.metadata.easymeta.EasyField)35 ID (cn.devezhao.persist4j.engine.ID)34 ArrayList (java.util.ArrayList)25 JSONArray (com.alibaba.fastjson.JSONArray)23 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)15 Record (cn.devezhao.persist4j.Record)14 JSON (com.alibaba.fastjson.JSON)14 DisplayType (com.rebuild.core.metadata.easymeta.DisplayType)14 Map (java.util.Map)11 EasyEntity (com.rebuild.core.metadata.easymeta.EasyEntity)10 GetMapping (org.springframework.web.bind.annotation.GetMapping)10 HashMap (java.util.HashMap)9 Test (org.junit.jupiter.api.Test)9 ConfigBean (com.rebuild.core.configuration.ConfigBean)6 HashSet (java.util.HashSet)5 MetadataModificationException (com.rebuild.core.metadata.impl.MetadataModificationException)4 Department (com.rebuild.core.privileges.bizz.Department)4