Search in sources :

Example 1 with ServiceSpec

use of com.rebuild.core.service.ServiceSpec in project rebuild by getrebuild.

the class MetaEntityService method delete.

@Override
public int delete(ID recordId) {
    Object[] entityRecord = getPersistManagerFactory().createQuery("select entityName from MetaEntity where entityId = ?").setParameter(1, recordId).unique();
    final Entity delEntity = MetadataHelper.getEntity((String) entityRecord[0]);
    // 删除实体的相关配置
    // Field: belongEntity
    String[] confEntities = new String[] { "MetaField", "PickList", "LayoutConfig", "FilterConfig", "ShareAccess", "ChartConfig", "Attachment", "AutoFillinConfig", "RobotTriggerConfig", "RobotApprovalConfig", "DataReportConfig", "TransformConfig", "ExtformConfig", "NreferenceItem" };
    int del = 0;
    for (String conf : confEntities) {
        Entity confEntity = MetadataHelper.getEntity(conf);
        String ql = String.format("select %s from %s where belongEntity = ", confEntity.getPrimaryField().getName(), confEntity.getName());
        if (confEntity.getEntityCode() == EntityHelper.Attachment) {
            ql += delEntity.getEntityCode();
        } else {
            ql += String.format("'%s'", delEntity.getName());
            if (confEntity.getEntityCode() == EntityHelper.TransformConfig) {
                ql += String.format(" or targetEntity = '%s'", delEntity.getName());
            }
        }
        Object[][] usedArray = getPersistManagerFactory().createQuery(ql).array();
        ServiceSpec ss = Application.getService(confEntity.getEntityCode());
        for (Object[] used : usedArray) {
            ss.delete((ID) used[0]);
        }
        if (usedArray.length > 0) {
            log.warn("Deleted configuration of entity [ {} ] in [ {} ] : {}", delEntity.getName(), conf, usedArray.length);
        }
    }
    del += super.delete(recordId);
    return del;
}
Also used : Entity(cn.devezhao.persist4j.Entity) ServiceSpec(com.rebuild.core.service.ServiceSpec)

Example 2 with ServiceSpec

use of com.rebuild.core.service.ServiceSpec in project rebuild by getrebuild.

the class Application method init.

/**
 * 系统初始化
 *
 * @throws Exception
 */
public static boolean init() throws Exception {
    if (_READY)
        throw new IllegalStateException("REBUILD ALREADY STARTED");
    log.info("Initializing Rebuild context [ {} ] ...", _CONTEXT.getClass().getSimpleName());
    if (!(_READY = ServerStatus.checkAll())) {
        log.error(RebuildBanner.formatBanner("REBUILD STARTUP FILAED DURING THE STATUS CHECK.", "PLEASE VIEW LOGS FOR MORE DETAILS."));
        return false;
    }
    // 升级数据库
    new UpgradeDatabase().upgradeQuietly();
    // 版本升级会清除缓存
    int lastBuild = ObjectUtils.toInt(RebuildConfiguration.get(ConfigurationItem.AppBuild, true), 0);
    if (lastBuild < BUILD) {
        log.warn("Clean up the cache once when upgrading : {}", BUILD);
        Installer.clearAllCache();
        RebuildConfiguration.set(ConfigurationItem.AppBuild, BUILD);
    }
    // 刷新配置缓存
    for (ConfigurationItem item : ConfigurationItem.values()) {
        RebuildConfiguration.get(item, true);
    }
    // 加载自定义实体
    log.info("Loading customized/business entities ...");
    ((DynamicMetadataFactory) _CONTEXT.getBean(PersistManagerFactory.class).getMetadataFactory()).refresh();
    // 实体对应的服务类
    _ESS = new HashMap<>();
    for (Map.Entry<String, ServiceSpec> e : _CONTEXT.getBeansOfType(ServiceSpec.class).entrySet()) {
        ServiceSpec s = e.getValue();
        if (s.getEntityCode() > 0) {
            _ESS.put(s.getEntityCode(), s);
            if (devMode()) {
                log.info("Service specification : {} for [ {} ]", s.getClass().getName(), s.getEntityCode());
            }
        }
    }
    // 初始化业务组件
    List<Initialization> ordered = new ArrayList<>(_CONTEXT.getBeansOfType(Initialization.class).values());
    OrderComparator.sort(ordered);
    for (Initialization bean : ordered) {
        bean.init();
    }
    License.isRbvAttached();
    DataMigrator.dataMigrateIfNeed();
    return true;
}
Also used : ConfigurationItem(com.rebuild.core.support.ConfigurationItem) UpgradeDatabase(com.rebuild.core.support.setup.UpgradeDatabase) ServiceSpec(com.rebuild.core.service.ServiceSpec) DynamicMetadataFactory(com.rebuild.core.metadata.impl.DynamicMetadataFactory)

Example 3 with ServiceSpec

use of com.rebuild.core.service.ServiceSpec in project rebuild by getrebuild.

the class GeneralEntityServiceTest method getServiceSpec.

@Test
public void getServiceSpec() {
    ServiceSpec ss = Application.getService(EntityHelper.User);
    Assertions.assertTrue(ss instanceof UserService);
    EntityService es = Application.getEntityService(MetadataHelper.getEntity(TestAllFields).getEntityCode());
    Assertions.assertTrue(es instanceof GeneralEntityService);
    boolean exThrows = false;
    try {
        Application.getEntityService(EntityHelper.User);
    } catch (RebuildException ok) {
        exThrows = true;
    }
    Assertions.assertTrue(exThrows);
}
Also used : UserService(com.rebuild.core.privileges.UserService) ServiceSpec(com.rebuild.core.service.ServiceSpec) RebuildException(com.rebuild.core.RebuildException) Test(org.junit.jupiter.api.Test)

Example 4 with ServiceSpec

use of com.rebuild.core.service.ServiceSpec 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 5 with ServiceSpec

use of com.rebuild.core.service.ServiceSpec in project rebuild by getrebuild.

the class FieldWriteback 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 (targetRecordIds.isEmpty())
        return;
    if (targetRecordData.isEmpty()) {
        log.info("No data of target record available : {}", targetRecordId);
        return;
    }
    final ServiceSpec useService = MetadataHelper.isBusinessEntity(targetEntity) ? Application.getEntityService(targetEntity.getEntityCode()) : Application.getService(targetEntity.getEntityCode());
    final boolean forceUpdate = ((JSONObject) actionContext.getActionContent()).getBooleanValue("forceUpdate");
    boolean tschainAdded = false;
    for (ID targetRecordId : targetRecordIds) {
        if (operatingContext.getAction() == BizzPermission.DELETE && targetRecordId.equals(operatingContext.getAnyRecord().getPrimary())) {
            // 删除时无需更新自己
            continue;
        }
        // 跳过权限
        PrivilegesGuardContextHolder.setSkipGuard(targetRecordId);
        // 强制更新 (v2.9)
        if (forceUpdate) {
            GeneralEntityServiceContextHolder.setAllowForceUpdate(targetRecordId);
        }
        // 会关联触发下一触发器
        if (!tschainAdded) {
            tschainAdded = true;
            tschain.add(chainName);
            TRIGGERS_CHAIN.set(tschain);
        }
        Record targetRecord = targetRecordData.clone();
        targetRecord.setID(targetEntity.getPrimaryField().getName(), targetRecordId);
        targetRecord.setDate(EntityHelper.ModifiedOn, CalendarUtils.now());
        GeneralEntityServiceContextHolder.setRepeatedCheckMode(GeneralEntityServiceContextHolder.RCM_CHECK_MAIN);
        try {
            useService.createOrUpdate(targetRecord);
        } finally {
            PrivilegesGuardContextHolder.getSkipGuardOnce();
            GeneralEntityServiceContextHolder.isAllowForceUpdateOnce();
            GeneralEntityServiceContextHolder.getRepeatedCheckModeOnce();
        }
    }
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) ServiceSpec(com.rebuild.core.service.ServiceSpec) StandardRecord(cn.devezhao.persist4j.engine.StandardRecord) Record(cn.devezhao.persist4j.Record) ID(cn.devezhao.persist4j.engine.ID)

Aggregations

ServiceSpec (com.rebuild.core.service.ServiceSpec)5 Record (cn.devezhao.persist4j.Record)2 JSONObject (com.alibaba.fastjson.JSONObject)2 Entity (cn.devezhao.persist4j.Entity)1 ID (cn.devezhao.persist4j.engine.ID)1 StandardRecord (cn.devezhao.persist4j.engine.StandardRecord)1 JSONArray (com.alibaba.fastjson.JSONArray)1 RebuildException (com.rebuild.core.RebuildException)1 DisplayType (com.rebuild.core.metadata.easymeta.DisplayType)1 DynamicMetadataFactory (com.rebuild.core.metadata.impl.DynamicMetadataFactory)1 UserService (com.rebuild.core.privileges.UserService)1 AdvFilterParser (com.rebuild.core.service.query.AdvFilterParser)1 ConfigurationItem (com.rebuild.core.support.ConfigurationItem)1 UpgradeDatabase (com.rebuild.core.support.setup.UpgradeDatabase)1 Date (java.util.Date)1 Test (org.junit.jupiter.api.Test)1