Search in sources :

Example 1 with MetadataModificationException

use of com.rebuild.core.metadata.impl.MetadataModificationException in project rebuild by getrebuild.

the class MetaschemaImporter method performEntity.

/**
 * @param schema
 * @param mainEntity
 * @return
 * @throws MetadataModificationException
 */
private String performEntity(JSONObject schema, String mainEntity) throws MetadataModificationException {
    final String entityName = schema.getString("entity");
    final String entityLabel = schema.getString("entityLabel");
    Entity2Schema entity2Schema = new Entity2Schema(this.getUser());
    entity2Schema.createEntity(entityName, entityLabel, schema.getString("comments"), mainEntity, false);
    Entity newEntity = MetadataHelper.getEntity(entityName);
    this.setCompleted((int) (this.getCompleted() * 1.5));
    JSONArray fields = schema.getJSONArray("fields");
    try {
        List<Field> fieldsList = new ArrayList<>();
        for (Object field : fields) {
            Field unsafe = performField((JSONObject) field, newEntity);
            if (unsafe != null)
                fieldsList.add(unsafe);
        }
        // 同步字段到数据库
        new Field2Schema(UserService.ADMIN_USER).schema2Database(newEntity, fieldsList.toArray(new Field[0]));
    } catch (Exception ex) {
        entity2Schema.dropEntity(newEntity, true);
        if (ex instanceof MetadataModificationException) {
            throw ex;
        } else {
            throw new MetadataModificationException(ex);
        }
    }
    Record needUpdate = EntityHelper.forUpdate(EasyMetaFactory.valueOf(newEntity).getMetaId(), this.getUser(), false);
    String nameField = schema.getString("nameField");
    if (nameField != null) {
        needUpdate.setString("nameField", nameField);
    }
    String entityIcon = schema.getString("entityIcon");
    if (entityIcon != null) {
        needUpdate.setString("icon", entityIcon);
    }
    String quickFields = schema.getString("quickFields");
    if (quickFields != null) {
        needUpdate.setString("extConfig", JSONUtils.toJSONObject("quickFields", quickFields).toJSONString());
    }
    if (needUpdate.getAvailableFieldIterator().hasNext()) {
        Application.getCommonsService().update(needUpdate);
    }
    // 刷新元数据
    MetadataHelper.getMetadataFactory().refresh();
    // 表单回填
    JSONArray fillins = schema.getJSONArray(MetaSchemaGenerator.CFG_FILLINS);
    if (fillins != null) {
        for (Object o : fillins) {
            performFillin(entityName, (JSONObject) o);
        }
    }
    // 布局
    JSONObject layouts = schema.getJSONObject(MetaSchemaGenerator.CFG_LAYOUTS);
    if (layouts != null) {
        for (Map.Entry<String, Object> e : layouts.entrySet()) {
            performLayout(entityName, e.getKey(), (JSON) e.getValue());
        }
    }
    // 高级查询
    JSONArray filters = schema.getJSONArray(MetaSchemaGenerator.CFG_FILTERS);
    if (filters != null) {
        for (Object o : filters) {
            performFilter(entityName, (JSONObject) o);
        }
    }
    // 触发器
    JSONArray triggers = schema.getJSONArray(MetaSchemaGenerator.CFG_TRIGGERS);
    if (triggers != null) {
        for (Object o : triggers) {
            performTrigger(entityName, (JSONObject) o);
        }
    }
    // 审批流程
    JSONArray approvals = schema.getJSONArray(MetaSchemaGenerator.CFG_APPROVALS);
    if (approvals != null) {
        for (Object o : approvals) {
            performApproval(entityName, (JSONObject) o);
        }
    }
    // 记录转换
    JSONArray transforms = schema.getJSONArray(MetaSchemaGenerator.CFG_TRANSFORMS);
    if (transforms != null) {
        for (Object o : transforms) {
            performTransform(entityName, (JSONObject) o);
        }
    }
    return entityName;
}
Also used : Entity(cn.devezhao.persist4j.Entity) Entity2Schema(com.rebuild.core.metadata.impl.Entity2Schema) JSONArray(com.alibaba.fastjson.JSONArray) ArrayList(java.util.ArrayList) MetadataModificationException(com.rebuild.core.metadata.impl.MetadataModificationException) Field(cn.devezhao.persist4j.Field) JSONObject(com.alibaba.fastjson.JSONObject) Field2Schema(com.rebuild.core.metadata.impl.Field2Schema) JSONObject(com.alibaba.fastjson.JSONObject) Record(cn.devezhao.persist4j.Record) HashMap(java.util.HashMap) Map(java.util.Map) MetadataModificationException(com.rebuild.core.metadata.impl.MetadataModificationException)

Example 2 with MetadataModificationException

use of com.rebuild.core.metadata.impl.MetadataModificationException in project rebuild by getrebuild.

the class MetaschemaImporter method exec.

/**
 * 执行导入
 *
 * @return
 */
@Override
protected String exec() {
    setTotal(100);
    if (!DynamicMetadataContextHolder.isSkipLanguageRefresh(false)) {
        DynamicMetadataContextHolder.setSkipLanguageRefresh();
        needClearContextHolder = true;
    }
    String entityName = performEntity(data, null);
    Entity createdEntity = MetadataHelper.getEntity(entityName);
    setCompleted(45);
    JSONObject detailData = data.getJSONObject("detail");
    if (detailData == null)
        detailData = data.getJSONObject("slave");
    if (detailData != null) {
        try {
            performEntity(detailData, createdEntity.getName());
            setCompleted(90);
        } catch (MetadataModificationException ex) {
            // 出现异常,删除主实体
            new Entity2Schema(this.getUser()).dropEntity(createdEntity, true);
            throw ex;
        }
    }
    final ID sessionUser = UserContextHolder.getUser(true);
    if (sessionUser == null)
        UserContextHolder.setUser(getUser());
    // 字段选项
    try {
        for (Map.Entry<Field, JSONObject> e : picklistHolders.entrySet()) {
            Field field = e.getKey();
            Application.getBean(PickListService.class).updateBatch(MetadataHelper.getField(field.getOwnEntity().getName(), field.getName()), e.getValue());
        }
    } finally {
        if (sessionUser == null)
            UserContextHolder.clear();
    }
    setCompleted(100);
    return entityName;
}
Also used : Entity(cn.devezhao.persist4j.Entity) Field(cn.devezhao.persist4j.Field) JSONObject(com.alibaba.fastjson.JSONObject) Entity2Schema(com.rebuild.core.metadata.impl.Entity2Schema) ID(cn.devezhao.persist4j.engine.ID) HashMap(java.util.HashMap) Map(java.util.Map) MetadataModificationException(com.rebuild.core.metadata.impl.MetadataModificationException)

Example 3 with MetadataModificationException

use of com.rebuild.core.metadata.impl.MetadataModificationException in project rebuild by getrebuild.

the class ApprovalFields2Schema method createFields.

/**
 * @param approvalEntity
 * @return Returns true if successful
 * @throws MetadataModificationException
 */
public boolean createFields(Entity approvalEntity) throws MetadataModificationException {
    if (MetadataHelper.hasApprovalField(approvalEntity)) {
        if (!approvalEntity.containsField(EntityHelper.ApprovalLastUser)) {
            return createApporvalLastUser(approvalEntity);
        }
        return false;
    }
    if (!(MetadataHelper.hasPrivilegesField(approvalEntity) || EasyMetaFactory.valueOf(approvalEntity).isPlainEntity())) {
        throw new RebuildException("UNSUPPORTED ENTITY : " + approvalEntity.getName());
    }
    Field apporvalId = createUnsafeField(approvalEntity, EntityHelper.ApprovalId, Language.L("审批流程"), DisplayType.REFERENCE, true, false, false, true, true, null, "RobotApprovalConfig", CascadeModel.Ignore, null, null);
    Field apporvalState = createUnsafeField(approvalEntity, EntityHelper.ApprovalState, Language.L("审批状态"), DisplayType.STATE, true, false, false, true, true, null, null, null, null, ApprovalState.DRAFT.getState());
    Field apporvalStepId = createUnsafeField(approvalEntity, EntityHelper.ApprovalStepNode, Language.L("审批步骤"), DisplayType.TEXT, true, false, false, true, false, null, null, null, null, null);
    Field apporvalLastUser = buildApporvalLastUser(approvalEntity);
    boolean schemaReady = schema2Database(approvalEntity, new Field[] { apporvalId, apporvalState, apporvalStepId, apporvalLastUser });
    if (!schemaReady) {
        Application.getCommonsService().delete(recordedMetaId.toArray(new ID[0]));
        throw new MetadataModificationException(Language.L("无法同步元数据到数据库"));
    }
    MetadataHelper.getMetadataFactory().refresh();
    return true;
}
Also used : Field(cn.devezhao.persist4j.Field) RebuildException(com.rebuild.core.RebuildException) ID(cn.devezhao.persist4j.engine.ID) MetadataModificationException(com.rebuild.core.metadata.impl.MetadataModificationException)

Example 4 with MetadataModificationException

use of com.rebuild.core.metadata.impl.MetadataModificationException in project rebuild by getrebuild.

the class ApprovalFields2Schema method createApporvalLastUser.

// v2.7 最后审批人
private boolean createApporvalLastUser(Entity approvalEntity) {
    Field apporvalLastUser = buildApporvalLastUser(approvalEntity);
    boolean schemaReady = schema2Database(approvalEntity, new Field[] { apporvalLastUser });
    if (!schemaReady) {
        Application.getCommonsService().delete(recordedMetaId.toArray(new ID[0]));
        throw new MetadataModificationException(Language.L("无法同步元数据到数据库"));
    }
    MetadataHelper.getMetadataFactory().refresh();
    return true;
}
Also used : Field(cn.devezhao.persist4j.Field) ID(cn.devezhao.persist4j.engine.ID) MetadataModificationException(com.rebuild.core.metadata.impl.MetadataModificationException)

Aggregations

Field (cn.devezhao.persist4j.Field)4 MetadataModificationException (com.rebuild.core.metadata.impl.MetadataModificationException)4 ID (cn.devezhao.persist4j.engine.ID)3 Entity (cn.devezhao.persist4j.Entity)2 JSONObject (com.alibaba.fastjson.JSONObject)2 Entity2Schema (com.rebuild.core.metadata.impl.Entity2Schema)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Record (cn.devezhao.persist4j.Record)1 JSONArray (com.alibaba.fastjson.JSONArray)1 RebuildException (com.rebuild.core.RebuildException)1 Field2Schema (com.rebuild.core.metadata.impl.Field2Schema)1 ArrayList (java.util.ArrayList)1