Search in sources :

Example 11 with ServiceException

use of com.ruoyi.common.core.exception.ServiceException in project RuoYi-Cloud-Oracle by yangzongzhuan.

the class GenTableServiceImpl method synchDb.

/**
 * 同步数据库
 *
 * @param tableName 表名称
 */
@Override
@Transactional(rollbackFor = Exception.class)
public void synchDb(String tableName) {
    GenTable table = genTableMapper.selectGenTableByName(tableName);
    List<GenTableColumn> tableColumns = table.getColumns();
    Map<String, GenTableColumn> tableColumnMap = tableColumns.stream().collect(Collectors.toMap(GenTableColumn::getColumnName, Function.identity()));
    List<GenTableColumn> dbTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName);
    if (StringUtils.isEmpty(dbTableColumns)) {
        throw new ServiceException("同步数据失败,原表结构不存在");
    }
    List<String> dbTableColumnNames = dbTableColumns.stream().map(GenTableColumn::getColumnName).collect(Collectors.toList());
    dbTableColumns.forEach(column -> {
        GenUtils.initColumnField(column, table);
        if (tableColumnMap.containsKey(column.getColumnName())) {
            GenTableColumn prevColumn = tableColumnMap.get(column.getColumnName());
            column.setColumnId(prevColumn.getColumnId());
            if (column.isList()) {
                // 如果是列表,继续保留查询方式/字典类型选项
                column.setDictType(prevColumn.getDictType());
                column.setQueryType(prevColumn.getQueryType());
            }
            if (StringUtils.isNotEmpty(prevColumn.getIsRequired()) && !column.isPk() && (column.isInsert() || column.isEdit()) && ((column.isUsableColumn()) || (!column.isSuperColumn()))) {
                // 如果是(新增/修改&非主键/非忽略及父属性),继续保留必填/显示类型选项
                column.setIsRequired(prevColumn.getIsRequired());
                column.setHtmlType(prevColumn.getHtmlType());
            }
            genTableColumnMapper.updateGenTableColumn(column);
        } else {
            genTableColumnMapper.insertGenTableColumn(column);
        }
    });
    List<GenTableColumn> delColumns = tableColumns.stream().filter(column -> !dbTableColumnNames.contains(column.getColumnName())).collect(Collectors.toList());
    if (StringUtils.isNotEmpty(delColumns)) {
        genTableColumnMapper.deleteGenTableColumns(delColumns);
    }
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CharsetKit(com.ruoyi.common.core.text.CharsetKit) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) Constants(com.ruoyi.common.core.constant.Constants) Function(java.util.function.Function) ServiceException(com.ruoyi.common.core.exception.ServiceException) Template(org.apache.velocity.Template) LinkedHashMap(java.util.LinkedHashMap) Service(org.springframework.stereotype.Service) Map(java.util.Map) GenUtils(com.ruoyi.gen.util.GenUtils) GenTableColumnMapper(com.ruoyi.gen.mapper.GenTableColumnMapper) ZipEntry(java.util.zip.ZipEntry) GenTable(com.ruoyi.gen.domain.GenTable) VelocityUtils(com.ruoyi.gen.util.VelocityUtils) Logger(org.slf4j.Logger) GenTableMapper(com.ruoyi.gen.mapper.GenTableMapper) VelocityInitializer(com.ruoyi.gen.util.VelocityInitializer) StringWriter(java.io.StringWriter) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) GenConstants(com.ruoyi.common.core.constant.GenConstants) GenTableColumn(com.ruoyi.gen.domain.GenTableColumn) StringUtils(com.ruoyi.common.core.utils.StringUtils) Collectors(java.util.stream.Collectors) VelocityContext(org.apache.velocity.VelocityContext) File(java.io.File) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) JSON(com.alibaba.fastjson.JSON) JSONObject(com.alibaba.fastjson.JSONObject) Velocity(org.apache.velocity.app.Velocity) SecurityUtils(com.ruoyi.common.security.utils.SecurityUtils) Transactional(org.springframework.transaction.annotation.Transactional) ServiceException(com.ruoyi.common.core.exception.ServiceException) GenTable(com.ruoyi.gen.domain.GenTable) GenTableColumn(com.ruoyi.gen.domain.GenTableColumn) Transactional(org.springframework.transaction.annotation.Transactional)

Example 12 with ServiceException

use of com.ruoyi.common.core.exception.ServiceException in project RuoYi-Cloud-Plus by JavaLionLi.

the class CreateAndUpdateMetaObjectHandler method updateFill.

@Override
public void updateFill(MetaObject metaObject) {
    try {
        if (ObjectUtil.isNotNull(metaObject) && metaObject.getOriginalObject() instanceof BaseEntity) {
            BaseEntity baseEntity = (BaseEntity) metaObject.getOriginalObject();
            Date current = new Date();
            // 更新时间填充(不管为不为空)
            baseEntity.setUpdateTime(current);
            String username = getLoginUsername();
            // 当前已登录 更新人填充(不管为不为空)
            if (StringUtils.isNotBlank(username)) {
                baseEntity.setUpdateBy(username);
            }
        }
    } catch (Exception e) {
        throw new ServiceException("自动注入异常 => " + e.getMessage(), HttpStatus.HTTP_UNAUTHORIZED);
    }
}
Also used : ServiceException(com.ruoyi.common.core.exception.ServiceException) BaseEntity(com.ruoyi.common.core.web.domain.BaseEntity) Date(java.util.Date) ServiceException(com.ruoyi.common.core.exception.ServiceException)

Example 13 with ServiceException

use of com.ruoyi.common.core.exception.ServiceException in project RuoYi-Cloud-Plus by JavaLionLi.

the class PlusDataPermissionHandler method buildDataFilter.

/**
 * 构造数据过滤sql
 */
private String buildDataFilter(DataColumn[] dataColumns, boolean isSelect) {
    StringBuilder sqlString = new StringBuilder();
    // 更新或删除需满足所有条件
    String joinStr = isSelect ? " OR " : " AND ";
    LoginUser loginUser = DataPermissionHelper.getVariable("user");
    StandardEvaluationContext context = new StandardEvaluationContext();
    context.setBeanResolver(beanResolver);
    DataPermissionHelper.getContext().forEach(context::setVariable);
    for (RoleDTO role : loginUser.getRoles()) {
        loginUser.setRoleId(role.getRoleId());
        // 获取角色权限泛型
        DataScopeType type = DataScopeType.findCode(role.getDataScope());
        if (ObjectUtil.isNull(type)) {
            throw new ServiceException("角色数据范围异常 => " + role.getDataScope());
        }
        // 全部数据权限直接返回
        if (type == DataScopeType.ALL) {
            return "";
        }
        boolean isSuccess = false;
        for (DataColumn dataColumn : dataColumns) {
            // 不包含 key 变量 则不处理
            if (!StringUtils.contains(type.getSqlTemplate(), "#" + dataColumn.key())) {
                continue;
            }
            // 设置注解变量 key 为表达式变量 value 为变量值
            context.setVariable(dataColumn.key(), dataColumn.value());
            // 解析sql模板并填充
            String sql = parser.parseExpression(type.getSqlTemplate(), parserContext).getValue(context, String.class);
            sqlString.append(joinStr).append(sql);
            isSuccess = true;
        }
        // 未处理成功则填充兜底方案
        if (!isSuccess) {
            sqlString.append(joinStr).append(type.getElseSql());
        }
    }
    if (StringUtils.isNotBlank(sqlString.toString())) {
        return sqlString.substring(joinStr.length());
    }
    return "";
}
Also used : RoleDTO(com.ruoyi.system.api.model.RoleDTO) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) DataScopeType(com.ruoyi.common.mybatis.enums.DataScopeType) ServiceException(com.ruoyi.common.core.exception.ServiceException) DataColumn(com.ruoyi.common.mybatis.annotation.DataColumn) LoginUser(com.ruoyi.system.api.model.LoginUser)

Example 14 with ServiceException

use of com.ruoyi.common.core.exception.ServiceException in project RuoYi-Cloud-Plus by JavaLionLi.

the class PlusDataPermissionHandler method getSqlSegment.

public Expression getSqlSegment(Expression where, String mappedStatementId, boolean isSelect) {
    DataColumn[] dataColumns = findAnnotation(mappedStatementId);
    if (ArrayUtil.isEmpty(dataColumns)) {
        inavlidCacheSet.add(mappedStatementId);
        return where;
    }
    LoginUser currentUser = DataPermissionHelper.getVariable("user");
    if (ObjectUtil.isNull(currentUser)) {
        currentUser = LoginHelper.getLoginUser();
        DataPermissionHelper.setVariable("user", currentUser);
    }
    // 如果是超级管理员,则不过滤数据
    if (ObjectUtil.isNull(currentUser) || LoginHelper.isAdmin(currentUser.getUserId())) {
        return where;
    }
    String dataFilterSql = buildDataFilter(dataColumns, isSelect);
    if (StringUtils.isBlank(dataFilterSql)) {
        return where;
    }
    try {
        Expression expression = CCJSqlParserUtil.parseExpression(dataFilterSql);
        // 数据权限使用单独的括号 防止与其他条件冲突
        Parenthesis parenthesis = new Parenthesis(expression);
        if (ObjectUtil.isNotNull(where)) {
            return new AndExpression(where, parenthesis);
        } else {
            return parenthesis;
        }
    } catch (JSQLParserException e) {
        throw new ServiceException("数据权限解析异常 => " + e.getMessage());
    }
}
Also used : Parenthesis(net.sf.jsqlparser.expression.Parenthesis) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) ServiceException(com.ruoyi.common.core.exception.ServiceException) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) Expression(net.sf.jsqlparser.expression.Expression) JSQLParserException(net.sf.jsqlparser.JSQLParserException) DataColumn(com.ruoyi.common.mybatis.annotation.DataColumn) LoginUser(com.ruoyi.system.api.model.LoginUser)

Example 15 with ServiceException

use of com.ruoyi.common.core.exception.ServiceException in project RuoYi-Cloud-Plus by JavaLionLi.

the class GenTableServiceImpl method validateEdit.

/**
 * 修改保存参数校验
 *
 * @param genTable 业务信息
 */
@Override
public void validateEdit(GenTable genTable) {
    if (GenConstants.TPL_TREE.equals(genTable.getTplCategory())) {
        String options = JsonUtils.toJsonString(genTable.getParams());
        Dict paramsObj = JsonUtils.parseMap(options);
        if (StringUtils.isEmpty(paramsObj.getStr(GenConstants.TREE_CODE))) {
            throw new ServiceException("树编码字段不能为空");
        } else if (StringUtils.isEmpty(paramsObj.getStr(GenConstants.TREE_PARENT_CODE))) {
            throw new ServiceException("树父编码字段不能为空");
        } else if (StringUtils.isEmpty(paramsObj.getStr(GenConstants.TREE_NAME))) {
            throw new ServiceException("树名称字段不能为空");
        } else if (GenConstants.TPL_SUB.equals(genTable.getTplCategory())) {
            if (StringUtils.isEmpty(genTable.getSubTableName())) {
                throw new ServiceException("关联子表的表名不能为空");
            } else if (StringUtils.isEmpty(genTable.getSubTableFkName())) {
                throw new ServiceException("子表关联的外键名不能为空");
            }
        }
    }
}
Also used : ServiceException(com.ruoyi.common.core.exception.ServiceException) Dict(cn.hutool.core.lang.Dict)

Aggregations

ServiceException (com.ruoyi.common.core.exception.ServiceException)49 IOException (java.io.IOException)10 GenTable (com.ruoyi.gen.domain.GenTable)9 SysUser (com.ruoyi.system.api.domain.SysUser)9 Transactional (org.springframework.transaction.annotation.Transactional)7 GenTableColumn (com.ruoyi.gen.domain.GenTableColumn)6 SysDept (com.ruoyi.system.api.domain.SysDept)6 SysRole (com.ruoyi.system.api.domain.SysRole)6 StringWriter (java.io.StringWriter)6 Template (org.apache.velocity.Template)6 VelocityContext (org.apache.velocity.VelocityContext)6 File (java.io.File)5 JSONObject (com.alibaba.fastjson.JSONObject)4 SysOss (com.ruoyi.resource.domain.SysOss)4 LoginUser (com.ruoyi.system.api.model.LoginUser)4 LambdaQueryWrapper (com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)3 Constants (com.ruoyi.common.core.constant.Constants)3 GenConstants (com.ruoyi.common.core.constant.GenConstants)3 StringUtils (com.ruoyi.common.core.utils.StringUtils)3 GenTableColumnMapper (com.ruoyi.gen.mapper.GenTableColumnMapper)3