Search in sources :

Example 1 with Entity2Schema

use of com.rebuild.core.metadata.impl.Entity2Schema 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 Entity2Schema

use of com.rebuild.core.metadata.impl.Entity2Schema 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 Entity2Schema

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

the class MetaEntityController method entityDrop.

@RequestMapping("entity/entity-drop")
public RespBody entityDrop(HttpServletRequest request) {
    final Entity entity = getEntityById(getIdParameterNotNull(request, "id"));
    final boolean force = getBoolParameter(request, "force", false);
    try {
        boolean drop = new Entity2Schema().dropEntity(entity, force);
        return drop ? RespBody.ok() : RespBody.error();
    } catch (Exception ex) {
        log.error("entity-drop", ex);
        return RespBody.error(ex.getLocalizedMessage());
    }
}
Also used : EasyEntity(com.rebuild.core.metadata.easymeta.EasyEntity) CopyEntity(com.rebuild.core.metadata.impl.CopyEntity) Entity(cn.devezhao.persist4j.Entity) Entity2Schema(com.rebuild.core.metadata.impl.Entity2Schema) IOException(java.io.IOException)

Example 4 with Entity2Schema

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

the class MetaEntityController method entityNew.

@PostMapping("entity/entity-new")
public RespBody entityNew(HttpServletRequest request) {
    final JSONObject reqJson = (JSONObject) ServletUtils.getRequestJson(request);
    String label = reqJson.getString("label");
    String comments = reqJson.getString("comments");
    String mainEntity = reqJson.getString("mainEntity");
    if (StringUtils.isNotBlank(mainEntity)) {
        if (!MetadataHelper.containsEntity(mainEntity)) {
            return RespBody.errorl("无效主实体 : %s", mainEntity);
        }
        Entity useMain = MetadataHelper.getEntity(mainEntity);
        if (useMain.getMainEntity() != null) {
            return RespBody.errorl("明细实体不能作为主实体");
        } else if (useMain.getDetailEntity() != null) {
            return RespBody.errorl("选择的主实体已被 [%s] 使用", useMain.getDetailEntity());
        }
    }
    try {
        String entityName = new Entity2Schema().createEntity(label, comments, mainEntity, getBoolParameter(request, "nameField"));
        return RespBody.ok(entityName);
    } catch (Exception ex) {
        log.error("entity-new", ex);
        return RespBody.error(ex.getLocalizedMessage());
    }
}
Also used : EasyEntity(com.rebuild.core.metadata.easymeta.EasyEntity) CopyEntity(com.rebuild.core.metadata.impl.CopyEntity) Entity(cn.devezhao.persist4j.Entity) JSONObject(com.alibaba.fastjson.JSONObject) Entity2Schema(com.rebuild.core.metadata.impl.Entity2Schema) IOException(java.io.IOException)

Example 5 with Entity2Schema

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

the class TestSupport method addTestEntities.

/**
 * 添加测试用实体
 *
 * @param dropExists
 * @return
 * @throws Exception
 */
@SuppressWarnings("SameParameterValue")
protected static boolean addTestEntities(boolean dropExists) throws Exception {
    boolean changed = false;
    if (dropExists) {
        if (MetadataHelper.containsEntity(TestAllFields)) {
            LOG.warn("Dropping test entity : " + TestAllFields);
            new Entity2Schema(UserService.ADMIN_USER).dropEntity(MetadataHelper.getEntity(TestAllFields), true);
        }
        if (MetadataHelper.containsEntity(SalesOrder)) {
            LOG.warn("Dropping test entity : " + SalesOrder);
            new Entity2Schema(UserService.ADMIN_USER).dropEntity(MetadataHelper.getEntity(SalesOrder), true);
        }
        if (MetadataHelper.containsEntity(Account)) {
            LOG.warn("Dropping test entity : " + Account);
            new Entity2Schema(UserService.ADMIN_USER).dropEntity(MetadataHelper.getEntity(Account), true);
        }
    }
    if (!MetadataHelper.containsEntity(TestAllFields)) {
        Entity2Schema entity2Schema = new Entity2Schema(UserService.ADMIN_USER);
        String entityName = entity2Schema.createEntity(TestAllFields.toUpperCase(), null, null, true);
        Entity testEntity = MetadataHelper.getEntity(entityName);
        for (DisplayType dt : DisplayType.values()) {
            if (dt == DisplayType.ID || dt == DisplayType.ANYREFERENCE) {
                continue;
            }
            String fieldName = dt.name().toUpperCase();
            if (BlockList.isBlock(fieldName))
                fieldName += "1";
            if (dt == DisplayType.REFERENCE || dt == DisplayType.N2NREFERENCE) {
                new Field2Schema(UserService.ADMIN_USER).createField(testEntity, fieldName, dt, null, entityName, null);
            } else if (dt == DisplayType.CLASSIFICATION) {
                JSON extra = JSON.parseObject("{classification:'018-0000000000000001'}");
                new Field2Schema(UserService.ADMIN_USER).createField(testEntity, fieldName, dt, null, entityName, extra);
            } else if (dt == DisplayType.STATE) {
                JSON extra = JSON.parseObject("{stateClass:'com.rebuild.core.support.state.HowtoState'}");
                new Field2Schema(UserService.ADMIN_USER).createField(testEntity, fieldName, dt, null, entityName, extra);
            } else {
                new Field2Schema(UserService.ADMIN_USER).createField(testEntity, fieldName, dt, null, null, null);
            }
        }
        changed = true;
    }
    if (!MetadataHelper.containsEntity(Account)) {
        String metaschema = FileUtils.readFileToString(ResourceUtils.getFile("classpath:schema-Account999.json"));
        MetaschemaImporter importer = new MetaschemaImporter(JSON.parseObject(metaschema));
        TaskExecutors.run((HeavyTask<?>) importer.setUser(UserService.ADMIN_USER));
        changed = true;
    }
    if (!MetadataHelper.containsEntity(SalesOrder)) {
        String metaschema = FileUtils.readFileToString(ResourceUtils.getFile("classpath:schema-SalesOrder999.json"));
        MetaschemaImporter importer = new MetaschemaImporter(JSON.parseObject(metaschema));
        TaskExecutors.run((HeavyTask<?>) importer.setUser(UserService.ADMIN_USER));
        changed = true;
    }
    return changed;
}
Also used : Entity(cn.devezhao.persist4j.Entity) Entity2Schema(com.rebuild.core.metadata.impl.Entity2Schema) DisplayType(com.rebuild.core.metadata.easymeta.DisplayType) Field2Schema(com.rebuild.core.metadata.impl.Field2Schema) JSON(com.alibaba.fastjson.JSON) MetaschemaImporter(com.rebuild.core.rbstore.MetaschemaImporter)

Aggregations

Entity2Schema (com.rebuild.core.metadata.impl.Entity2Schema)6 Entity (cn.devezhao.persist4j.Entity)5 JSONObject (com.alibaba.fastjson.JSONObject)4 Field (cn.devezhao.persist4j.Field)2 EasyEntity (com.rebuild.core.metadata.easymeta.EasyEntity)2 CopyEntity (com.rebuild.core.metadata.impl.CopyEntity)2 Field2Schema (com.rebuild.core.metadata.impl.Field2Schema)2 MetadataModificationException (com.rebuild.core.metadata.impl.MetadataModificationException)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Record (cn.devezhao.persist4j.Record)1 ID (cn.devezhao.persist4j.engine.ID)1 JSON (com.alibaba.fastjson.JSON)1 JSONArray (com.alibaba.fastjson.JSONArray)1 DisplayType (com.rebuild.core.metadata.easymeta.DisplayType)1 MetaschemaImporter (com.rebuild.core.rbstore.MetaschemaImporter)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 Test (org.junit.jupiter.api.Test)1