Search in sources :

Example 1 with DataSpecificationException

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

the class EntityRecordCreator method verify.

@Override
public void verify(Record record) {
    // 自动只读字段忽略非空检查
    final Set<String> autoReadonlyFields = EasyMetaFactory.getAutoReadonlyFields(entity.getName());
    // 非空
    List<String> notNulls = new ArrayList<>();
    // 格式
    List<String> notWells = new ArrayList<>();
    // 新建
    if (record.getPrimary() == null) {
        for (Field field : entity.getFields()) {
            if (MetadataHelper.isCommonsField(field))
                continue;
            EasyField easyField = EasyMetaFactory.valueOf(field);
            if (easyField.getDisplayType() == DisplayType.SERIES || easyField.getDisplayType() == DisplayType.BARCODE) {
                continue;
            }
            Object hasVal = record.getObjectValue(field.getName());
            boolean canNull = field.isNullable() || autoReadonlyFields.contains(field.getName());
            if (NullValue.isNull(hasVal)) {
                if (!canNull) {
                    notNulls.add(easyField.getLabel());
                }
            } else {
                if (field.isCreatable()) {
                    if (!matchsPattern(easyField, hasVal)) {
                        notWells.add(easyField.getLabel());
                    }
                } else {
                    if (!isForceCreateable(field)) {
                        log.warn("Remove non-creatable field : " + field);
                        record.removeValue(field.getName());
                    }
                }
            }
        }
    } else // 更新
    {
        for (String fieldName : record.getAvailableFields()) {
            Field field = entity.getField(fieldName);
            if (MetadataHelper.isCommonsField(field))
                continue;
            Object hasVal = record.getObjectValue(field.getName());
            boolean canNull = field.isNullable() || autoReadonlyFields.contains(field.getName());
            EasyField easyField = EasyMetaFactory.valueOf(field);
            if (NullValue.isNull(hasVal)) {
                if (!canNull) {
                    notNulls.add(easyField.getLabel());
                }
            } else {
                if (field.isUpdatable()) {
                    if (!matchsPattern(easyField, hasVal)) {
                        notWells.add(easyField.getLabel());
                    }
                } else {
                    log.warn("Remove non-updatable field : " + field);
                    record.removeValue(fieldName);
                }
            }
        }
    }
    if (!notNulls.isEmpty()) {
        throw new DataSpecificationException(Language.L("%s 不允许为空", StringUtils.join(notNulls, " / ")));
    }
    if (!notWells.isEmpty()) {
        throw new DataSpecificationException(Language.L("%s 格式不正确", StringUtils.join(notWells, " / ")));
    }
}
Also used : Field(cn.devezhao.persist4j.Field) EasyField(com.rebuild.core.metadata.easymeta.EasyField) ArrayList(java.util.ArrayList) JSONObject(com.alibaba.fastjson.JSONObject) DataSpecificationException(com.rebuild.core.service.DataSpecificationException) EasyField(com.rebuild.core.metadata.easymeta.EasyField)

Example 2 with DataSpecificationException

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

the class GeneralEntityService method delete.

@Override
public int delete(ID record, String[] cascades) {
    final ID currentUser = UserContextHolder.getUser();
    final RecycleStore recycleBin = useRecycleStore(record);
    this.deleteInternal(record);
    int affected = 1;
    Map<String, Set<ID>> recordsOfCascaded = getCascadedRecords(record, cascades, BizzPermission.DELETE);
    for (Map.Entry<String, Set<ID>> e : recordsOfCascaded.entrySet()) {
        if (log.isDebugEnabled()) {
            log.debug("Cascade delete - " + e.getKey() + " > " + e.getValue());
        }
        for (ID id : e.getValue()) {
            if (Application.getPrivilegesManager().allowDelete(currentUser, id)) {
                if (recycleBin != null)
                    recycleBin.add(id, record);
                int deleted = 0;
                try {
                    deleted = this.deleteInternal(id);
                } catch (DataSpecificationException ex) {
                    log.warn("Cannot delete : " + id + " Ex : " + ex);
                } finally {
                    if (deleted > 0) {
                        affected++;
                    } else if (recycleBin != null) {
                        // If not delete
                        recycleBin.removeLast();
                    }
                }
            } else {
                log.warn("No have privileges to DELETE : " + currentUser + " > " + id);
            }
        }
    }
    if (recycleBin != null)
        recycleBin.store();
    return affected;
}
Also used : RecycleStore(com.rebuild.core.service.general.recyclebin.RecycleStore) DataSpecificationException(com.rebuild.core.service.DataSpecificationException) ID(cn.devezhao.persist4j.engine.ID)

Example 3 with DataSpecificationException

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

the class GeneralEntityService method checkModifications.

/**
 * 系统相关约束检查。此方法有 3 种结果:
 * 1. true - 检查通过
 * 2. false - 检查不通过,但可以忽略的错误(如删除一条不存在的记录)
 * 3. 抛出异常 - 不可忽略的错误
 *
 * @param record
 * @param action [CREATE|UPDATE|DELDETE]
 * @return
 * @throws DataSpecificationException
 */
protected boolean checkModifications(Record record, Permission action) throws DataSpecificationException {
    final Entity entity = record.getEntity();
    final Entity mainEntity = entity.getMainEntity();
    if (action == BizzPermission.CREATE) {
        // 仅验证新建明细(相当于更新主记录)
        if (mainEntity != null && MetadataHelper.hasApprovalField(record.getEntity())) {
            Field dtmField = MetadataHelper.getDetailToMainField(entity);
            ApprovalState state = ApprovalHelper.getApprovalState(record.getID(dtmField.getName()));
            if (state == ApprovalState.APPROVED || state == ApprovalState.PROCESSING) {
                throw new DataSpecificationException(state == ApprovalState.APPROVED ? Language.L("主记录已完成审批,不能添加明细") : Language.L("主记录正在审批中,不能添加明细"));
            }
        }
    } else {
        final Entity checkEntity = mainEntity != null ? mainEntity : entity;
        ID recordId = record.getPrimary();
        if (checkEntity.containsField(EntityHelper.ApprovalId)) {
            // 需要验证主记录
            String recordType = Language.L("记录");
            if (mainEntity != null) {
                recordId = getMainId(entity, recordId);
                recordType = Language.L("主记录");
            }
            ApprovalState currentState;
            ApprovalState changeState = null;
            try {
                currentState = ApprovalHelper.getApprovalState(recordId);
                if (record.hasValue(EntityHelper.ApprovalState)) {
                    changeState = (ApprovalState) ApprovalState.valueOf(record.getInt(EntityHelper.ApprovalState));
                }
            } catch (NoRecordFoundException ignored) {
                log.warn("No record found for check : " + recordId);
                return false;
            }
            boolean unallow = false;
            if (action == BizzPermission.DELETE) {
                unallow = currentState == ApprovalState.APPROVED || currentState == ApprovalState.PROCESSING;
            } else if (action == BizzPermission.UPDATE) {
                unallow = currentState == ApprovalState.APPROVED || currentState == ApprovalState.PROCESSING;
                // 管理员撤销
                if (unallow) {
                    boolean adminCancel = currentState == ApprovalState.APPROVED && changeState == ApprovalState.CANCELED;
                    if (adminCancel)
                        unallow = false;
                }
                // 审批时/已通过强制修改
                if (unallow) {
                    boolean forceUpdate = GeneralEntityServiceContextHolder.isAllowForceUpdateOnce();
                    if (forceUpdate)
                        unallow = false;
                }
            }
            if (unallow) {
                if (RobotTriggerObserver.getTriggerSource() != null) {
                    recordType = Language.L("关联记录");
                }
                throw new DataSpecificationException(currentState == ApprovalState.APPROVED ? Language.L("%s已完成审批,禁止操作", recordType) : Language.L("%s正在审批中,禁止操作", recordType));
            }
        }
    }
    if (action == BizzPermission.CREATE || action == BizzPermission.UPDATE) {
    // TODO 父级级联字段强校验,兼容问题???
    }
    return true;
}
Also used : EasyField(com.rebuild.core.metadata.easymeta.EasyField) NoRecordFoundException(com.rebuild.core.service.NoRecordFoundException) ApprovalState(com.rebuild.core.service.approval.ApprovalState) DataSpecificationException(com.rebuild.core.service.DataSpecificationException) ID(cn.devezhao.persist4j.engine.ID)

Example 4 with DataSpecificationException

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

the class DepartmentService method update.

@Override
public Record update(Record record) {
    checkAdminGuard(BizzPermission.UPDATE, record.getPrimary());
    // 检查父子循环依赖
    if (record.hasValue("parentDept", false)) {
        ID parentDept = record.getID("parentDept");
        if (parentDept.equals(record.getPrimary())) {
            throw new DataSpecificationException(Language.L("父级部门不能选择自己"));
        }
        Department parent = Application.getUserStore().getDepartment(parentDept);
        Department that = Application.getUserStore().getDepartment(record.getPrimary());
        if (that.isChildren(parent, true)) {
            throw new DataSpecificationException(Language.L("子级部门不能同时作为父级部门"));
        }
    }
    record = super.update(record);
    Application.getUserStore().refreshDepartment(record.getPrimary());
    return record;
}
Also used : Department(com.rebuild.core.privileges.bizz.Department) DataSpecificationException(com.rebuild.core.service.DataSpecificationException) ID(cn.devezhao.persist4j.engine.ID)

Example 5 with DataSpecificationException

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

the class BulkBacthUpdate method exec.

@Override
public Integer exec() {
    final ID[] willUpdates = prepareRecords();
    this.setTotal(willUpdates.length);
    JSONArray updateContents = ((JSONObject) context.getExtraParams().get("customData")).getJSONArray("updateContents");
    // 转化成标准 FORM 格式
    JSONObject formJson = new JSONObject();
    for (Object o : updateContents) {
        JSONObject item = (JSONObject) o;
        String field = item.getString("field");
        String op = item.getString("op");
        String value = item.getString("value");
        if (OP_NULL.equalsIgnoreCase(op)) {
            formJson.put(field, StringUtils.EMPTY);
        } else if (OP_SET.equalsIgnoreCase(op)) {
            formJson.put(field, value);
        }
    }
    JSONObject metadata = new JSONObject();
    metadata.put("entity", context.getMainEntity().getName());
    formJson.put(JsonRecordCreator.META_FIELD, metadata);
    if (log.isDebugEnabled()) {
        log.debug("Conversion to : {}", formJson);
    }
    for (ID id : willUpdates) {
        if (Application.getPrivilegesManager().allowUpdate(context.getOpUser(), id)) {
            // 更新记录
            formJson.getJSONObject(JsonRecordCreator.META_FIELD).put("id", id.toLiteral());
            try {
                Record record = EntityHelper.parse(formJson, context.getOpUser());
                ges.update(record);
                this.addSucceeded();
            } catch (DataSpecificationException ex) {
                log.warn("Cannot update `{}` because : {}", id, ex.getLocalizedMessage());
            }
        } else {
            log.warn("No have privileges to UPDATE : {} < {}", id, context.getOpUser());
        }
        this.addCompleted();
    }
    return getSucceeded();
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) JSONArray(com.alibaba.fastjson.JSONArray) JSONObject(com.alibaba.fastjson.JSONObject) Record(cn.devezhao.persist4j.Record) DataSpecificationException(com.rebuild.core.service.DataSpecificationException) ID(cn.devezhao.persist4j.engine.ID)

Aggregations

DataSpecificationException (com.rebuild.core.service.DataSpecificationException)20 ID (cn.devezhao.persist4j.engine.ID)13 Record (cn.devezhao.persist4j.Record)7 JSONObject (com.alibaba.fastjson.JSONObject)7 PostMapping (org.springframework.web.bind.annotation.PostMapping)5 JSON (com.alibaba.fastjson.JSON)3 UserService (com.rebuild.core.privileges.UserService)3 AccessDeniedException (cn.devezhao.bizz.security.AccessDeniedException)2 Field (cn.devezhao.persist4j.Field)2 JSONArray (com.alibaba.fastjson.JSONArray)2 ConfigBean (com.rebuild.core.configuration.ConfigBean)2 EasyField (com.rebuild.core.metadata.easymeta.EasyField)2 Entity (cn.devezhao.persist4j.Entity)1 GenericJdbcException (cn.devezhao.persist4j.exception.jdbc.GenericJdbcException)1 RespBody (com.rebuild.api.RespBody)1 Department (com.rebuild.core.privileges.bizz.Department)1 User (com.rebuild.core.privileges.bizz.User)1 DataSpecificationNoRollbackException (com.rebuild.core.service.DataSpecificationNoRollbackException)1 NoRecordFoundException (com.rebuild.core.service.NoRecordFoundException)1 ApprovalState (com.rebuild.core.service.approval.ApprovalState)1