Search in sources :

Example 1 with NoRecordFoundException

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

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

the class PrivilegesManager method allowViaShare.

/**
 * 通过共享取得的操作权限(目前只共享了读取权限)
 *
 * @param user
 * @param target 目标记录
 * @param action 权限动作
 * @return
 */
public boolean allowViaShare(ID user, ID target, Permission action) {
    if (!(action == BizzPermission.READ || action == BizzPermission.UPDATE)) {
        return false;
    }
    // TODO 性能优化
    Entity entity = MetadataHelper.getEntity(target.getEntityCode());
    if (entity.getMainEntity() != null) {
        ID mainId = getMainRecordId(target);
        if (mainId == null) {
            throw new NoRecordFoundException("No record found by detail-id : " + target);
        }
        target = mainId;
        entity = entity.getMainEntity();
    }
    Object[] rights = Application.createQueryNoFilter("select rights from ShareAccess where belongEntity = ? and recordId = ? and shareTo = ?").setParameter(1, entity.getName()).setParameter(2, target).setParameter(3, user).unique();
    int rightsMask = rights == null ? 0 : (int) rights[0];
    return (rightsMask & action.getMask()) != 0;
}
Also used : Entity(cn.devezhao.persist4j.Entity) NoRecordFoundException(com.rebuild.core.service.NoRecordFoundException) ID(cn.devezhao.persist4j.engine.ID)

Example 3 with NoRecordFoundException

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

the class FieldValueHelper method getLabel.

/**
 * 获取记录的 NAME/LABEL 字段值
 *
 * @param id
 * @param defaultValue
 * @return
 * @throws NoRecordFoundException If no record found
 */
public static String getLabel(ID id, String defaultValue) throws NoRecordFoundException {
    Assert.notNull(id, "[id] cannot be null");
    Entity entity = MetadataHelper.getEntity(id.getEntityCode());
    if (id.getEntityCode() == EntityHelper.ClassificationData) {
        String hasValue = ClassificationManager.instance.getFullName(id);
        if (hasValue == null) {
            throw new NoRecordFoundException("No ClassificationData found by id : " + id);
        }
        return hasValue;
    } else if (id.getEntityCode() == EntityHelper.PickList) {
        String hasValue = PickListManager.instance.getLabel(id);
        if (hasValue == null) {
            throw new NoRecordFoundException("No PickList found by id : " + id);
        }
        return hasValue;
    } else if (id.equals(ApprovalStepService.APPROVAL_NOID)) {
        return Language.L("自动审批");
    }
    Field nameField = entity.getNameField();
    Object[] nameValue = Application.getQueryFactory().uniqueNoFilter(id, nameField.getName());
    if (nameValue == null) {
        throw new NoRecordFoundException("No record found by id : " + id);
    }
    Object nameLabel = wrapFieldValue(nameValue[0], nameField, true);
    if (nameLabel == null || StringUtils.isBlank(nameLabel.toString())) {
        if (defaultValue == null) {
            defaultValue = NO_LABEL_PREFIX + id.toLiteral().toUpperCase();
        }
        return defaultValue;
    }
    return nameLabel.toString();
}
Also used : Entity(cn.devezhao.persist4j.Entity) EasyField(com.rebuild.core.metadata.easymeta.EasyField) Field(cn.devezhao.persist4j.Field) NoRecordFoundException(com.rebuild.core.service.NoRecordFoundException) JSONObject(com.alibaba.fastjson.JSONObject)

Example 4 with NoRecordFoundException

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

the class RecentlyUsedHelper method gets.

/**
 * 获取最近使用
 *
 * @param user
 * @param entity
 * @param type
 * @param limit
 * @return
 */
public static ID[] gets(ID user, String entity, String type, int limit) {
    final String key = formatKey(user, entity, type);
    @SuppressWarnings("unchecked") LinkedList<ID> cached = (LinkedList<ID>) Application.getCommonsCache().getx(key);
    if (cached == null || cached.isEmpty()) {
        return ID.EMPTY_ID_ARRAY;
    }
    Set<ID> missed = new HashSet<>();
    List<ID> data = new ArrayList<>();
    for (int i = 0; i < limit && i < cached.size(); i++) {
        final ID raw = cached.get(i);
        if (!(raw.getEntityCode() == EntityHelper.ClassificationData || Application.getPrivilegesManager().allowRead(user, raw))) {
            continue;
        }
        try {
            String label = FieldValueHelper.getLabel(raw);
            ID clone = ID.valueOf(raw.toLiteral());
            clone.setLabel(label);
            data.add(clone);
        } catch (NoRecordFoundException ex) {
            missed.add(raw);
        }
    }
    if (!missed.isEmpty()) {
        cached.removeAll(missed);
        Application.getCommonsCache().putx(key, cached);
    }
    return data.toArray(new ID[0]);
}
Also used : NoRecordFoundException(com.rebuild.core.service.NoRecordFoundException) ID(cn.devezhao.persist4j.engine.ID)

Example 5 with NoRecordFoundException

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

the class RecordOwningCache method getOwningUser.

/**
 * 获取记录的所属人。若是明细实体则获取其主记录的所属人
 *
 * @param record
 * @param tryIfNotExists 记录不存在是否抛出异常
 * @return
 * @throws PrivilegesException
 * @throws NoRecordFoundException
 */
public ID getOwningUser(ID record, boolean tryIfNotExists) throws PrivilegesException, NoRecordFoundException {
    final String recordKey = record.toLiteral();
    ID hits = getx(recordKey);
    if (hits != null) {
        return hits;
    }
    Entity entity = MetadataHelper.getEntity(record.getEntityCode());
    Entity useMain = null;
    if (!MetadataHelper.hasPrivilegesField(entity)) {
        useMain = entity.getMainEntity();
        if (!(useMain != null && MetadataHelper.hasPrivilegesField(useMain))) {
            throw new PrivilegesException("None privileges entity : " + entity.getName());
        }
    }
    String sql = "select owningUser from %s where %s = '%s'";
    // 使用主记录
    if (useMain != null) {
        Field dtmField = MetadataHelper.getDetailToMainField(entity);
        sql = sql.replaceFirst("owningUser", dtmField.getName() + ".owningUser");
    }
    sql = String.format(sql, entity.getName(), entity.getPrimaryField().getName(), record.toLiteral());
    Object[] owningUser = aPMFactory.createQuery(sql).unique();
    if (owningUser == null || owningUser[0] == null) {
        String error = "No Record found : " + record;
        if (tryIfNotExists) {
            throw new NoRecordFoundException(error);
        } else {
            log.warn(error);
            return null;
        }
    }
    putx(recordKey, (ID) owningUser[0]);
    return (ID) owningUser[0];
}
Also used : Entity(cn.devezhao.persist4j.Entity) Field(cn.devezhao.persist4j.Field) NoRecordFoundException(com.rebuild.core.service.NoRecordFoundException) ID(cn.devezhao.persist4j.engine.ID) PrivilegesException(cn.devezhao.bizz.privileges.PrivilegesException)

Aggregations

NoRecordFoundException (com.rebuild.core.service.NoRecordFoundException)9 ID (cn.devezhao.persist4j.engine.ID)8 Entity (cn.devezhao.persist4j.Entity)4 EasyField (com.rebuild.core.metadata.easymeta.EasyField)3 Field (cn.devezhao.persist4j.Field)2 ApprovalState (com.rebuild.core.service.approval.ApprovalState)2 PrivilegesException (cn.devezhao.bizz.privileges.PrivilegesException)1 Record (cn.devezhao.persist4j.Record)1 JSON (com.alibaba.fastjson.JSON)1 JSONObject (com.alibaba.fastjson.JSONObject)1 DataSpecificationException (com.rebuild.core.service.DataSpecificationException)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1