Search in sources :

Example 91 with ServiceException

use of com.ruoyi.common.exception.ServiceException in project RuoYi-Flowable-Plus by KonBAI-Q.

the class DataBaseHelper method getDataBaseType.

/**
 * 获取当前数据库类型
 */
public static DataBaseType getDataBaseType() {
    DynamicRoutingDataSource ds = (DynamicRoutingDataSource) SpringUtils.getBean(DataSource.class);
    DataSource dataSource = ds.determineDataSource();
    try (Connection conn = dataSource.getConnection()) {
        DatabaseMetaData metaData = conn.getMetaData();
        String databaseProductName = metaData.getDatabaseProductName();
        return DataBaseType.find(databaseProductName);
    } catch (SQLException e) {
        throw new ServiceException(e.getMessage());
    }
}
Also used : ServiceException(com.ruoyi.common.exception.ServiceException) SQLException(java.sql.SQLException) DynamicRoutingDataSource(com.baomidou.dynamic.datasource.DynamicRoutingDataSource) Connection(java.sql.Connection) DatabaseMetaData(java.sql.DatabaseMetaData) DataSource(javax.sql.DataSource) DynamicRoutingDataSource(com.baomidou.dynamic.datasource.DynamicRoutingDataSource)

Example 92 with ServiceException

use of com.ruoyi.common.exception.ServiceException in project wumei-smart by kerwincui.

the class SysLoginService method login.

/**
 * 登录验证
 *
 * @param username 用户名
 * @param password 密码
 * @param code 验证码
 * @param uuid 唯一标识
 * @return 结果
 */
public String login(String username, String password, String code, String uuid) {
    boolean captchaOnOff = configService.selectCaptchaOnOff();
    // 验证码开关
    if (captchaOnOff) {
        validateCaptcha(username, code, uuid);
    }
    // 用户验证
    Authentication authentication = null;
    try {
        // 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
        authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));
    } catch (Exception e) {
        if (e instanceof BadCredentialsException) {
            AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
            throw new UserPasswordNotMatchException();
        } else {
            AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, e.getMessage()));
            throw new ServiceException(e.getMessage());
        }
    }
    AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
    LoginUser loginUser = (LoginUser) authentication.getPrincipal();
    recordLoginInfo(loginUser.getUserId());
    // 生成token
    return tokenService.createToken(loginUser);
}
Also used : ServiceException(com.ruoyi.common.exception.ServiceException) Authentication(org.springframework.security.core.Authentication) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) UserPasswordNotMatchException(com.ruoyi.common.exception.user.UserPasswordNotMatchException) LoginUser(com.ruoyi.common.core.domain.model.LoginUser) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) ServiceException(com.ruoyi.common.exception.ServiceException) CaptchaExpireException(com.ruoyi.common.exception.user.CaptchaExpireException) CaptchaException(com.ruoyi.common.exception.user.CaptchaException) UserPasswordNotMatchException(com.ruoyi.common.exception.user.UserPasswordNotMatchException)

Example 93 with ServiceException

use of com.ruoyi.common.exception.ServiceException in project wumei-smart by kerwincui.

the class GenTableServiceImpl method synchDb.

/**
 * 同步数据库
 *
 * @param tableName 表名称
 */
@Override
@Transactional
public void synchDb(String tableName) {
    GenTable table = genTableMapper.selectGenTableByName(tableName);
    List<GenTableColumn> tableColumns = table.getColumns();
    List<String> tableColumnNames = tableColumns.stream().map(GenTableColumn::getColumnName).collect(Collectors.toList());
    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 -> {
        if (!tableColumnNames.contains(column.getColumnName())) {
            GenUtils.initColumnField(column, table);
            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) GenTableColumn(com.ruoyi.generator.domain.GenTableColumn) VelocityUtils(com.ruoyi.generator.util.VelocityUtils) CharsetKit(com.ruoyi.common.core.text.CharsetKit) ServiceException(com.ruoyi.common.exception.ServiceException) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) Template(org.apache.velocity.Template) LinkedHashMap(java.util.LinkedHashMap) StringUtils(com.ruoyi.common.utils.StringUtils) Service(org.springframework.stereotype.Service) Map(java.util.Map) SecurityUtils(com.ruoyi.common.utils.SecurityUtils) ZipEntry(java.util.zip.ZipEntry) Logger(org.slf4j.Logger) StringWriter(java.io.StringWriter) GenConstants(com.ruoyi.common.constant.GenConstants) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) GenUtils(com.ruoyi.generator.util.GenUtils) Collectors(java.util.stream.Collectors) VelocityContext(org.apache.velocity.VelocityContext) GenTableMapper(com.ruoyi.generator.mapper.GenTableMapper) File(java.io.File) VelocityInitializer(com.ruoyi.generator.util.VelocityInitializer) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) JSON(com.alibaba.fastjson.JSON) GenTable(com.ruoyi.generator.domain.GenTable) JSONObject(com.alibaba.fastjson.JSONObject) GenTableColumnMapper(com.ruoyi.generator.mapper.GenTableColumnMapper) Velocity(org.apache.velocity.app.Velocity) Transactional(org.springframework.transaction.annotation.Transactional) Constants(com.ruoyi.common.constant.Constants) ServiceException(com.ruoyi.common.exception.ServiceException) GenTable(com.ruoyi.generator.domain.GenTable) GenTableColumn(com.ruoyi.generator.domain.GenTableColumn) Transactional(org.springframework.transaction.annotation.Transactional)

Example 94 with ServiceException

use of com.ruoyi.common.exception.ServiceException in project wumei-smart by kerwincui.

the class GenTableServiceImpl method importGenTable.

/**
 * 导入表结构
 *
 * @param tableList 导入表列表
 */
@Override
@Transactional
public void importGenTable(List<GenTable> tableList) {
    String operName = SecurityUtils.getUsername();
    try {
        for (GenTable table : tableList) {
            String tableName = table.getTableName();
            GenUtils.initTable(table, operName);
            int row = genTableMapper.insertGenTable(table);
            if (row > 0) {
                // 保存列信息
                List<GenTableColumn> genTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName);
                for (GenTableColumn column : genTableColumns) {
                    GenUtils.initColumnField(column, table);
                    genTableColumnMapper.insertGenTableColumn(column);
                }
            }
        }
    } catch (Exception e) {
        throw new ServiceException("导入失败:" + e.getMessage());
    }
}
Also used : ServiceException(com.ruoyi.common.exception.ServiceException) GenTable(com.ruoyi.generator.domain.GenTable) GenTableColumn(com.ruoyi.generator.domain.GenTableColumn) ServiceException(com.ruoyi.common.exception.ServiceException) IOException(java.io.IOException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 95 with ServiceException

use of com.ruoyi.common.exception.ServiceException in project wumei-smart by kerwincui.

the class GenTableServiceImpl method generatorCode.

/**
 * 生成代码(自定义路径)
 *
 * @param tableName 表名称
 */
@Override
public void generatorCode(String tableName) {
    // 查询表信息
    GenTable table = genTableMapper.selectGenTableByName(tableName);
    // 设置主子表信息
    setSubTable(table);
    // 设置主键列信息
    setPkColumn(table);
    VelocityInitializer.initVelocity();
    VelocityContext context = VelocityUtils.prepareContext(table);
    // 获取模板列表
    List<String> templates = VelocityUtils.getTemplateList(table.getTplCategory());
    for (String template : templates) {
        if (!StringUtils.containsAny(template, "sql.vm", "api.js.vm", "index.vue.vm", "index-tree.vue.vm")) {
            // 渲染模板
            StringWriter sw = new StringWriter();
            Template tpl = Velocity.getTemplate(template, Constants.UTF8);
            tpl.merge(context, sw);
            try {
                String path = getGenPath(table, template);
                FileUtils.writeStringToFile(new File(path), sw.toString(), CharsetKit.UTF_8);
            } catch (IOException e) {
                throw new ServiceException("渲染模板失败,表名:" + table.getTableName());
            }
        }
    }
}
Also used : StringWriter(java.io.StringWriter) ServiceException(com.ruoyi.common.exception.ServiceException) GenTable(com.ruoyi.generator.domain.GenTable) VelocityContext(org.apache.velocity.VelocityContext) IOException(java.io.IOException) File(java.io.File) Template(org.apache.velocity.Template)

Aggregations

ServiceException (com.ruoyi.common.exception.ServiceException)109 IOException (java.io.IOException)21 Transactional (org.springframework.transaction.annotation.Transactional)21 GenTable (com.ruoyi.generator.domain.GenTable)12 StringWriter (java.io.StringWriter)12 Template (org.apache.velocity.Template)12 VelocityContext (org.apache.velocity.VelocityContext)12 SysRole (com.ruoyi.common.core.domain.entity.SysRole)10 File (java.io.File)10 SysUser (com.ruoyi.common.core.domain.entity.SysUser)9 GenTableColumn (com.ruoyi.generator.domain.GenTableColumn)8 Before (org.aspectj.lang.annotation.Before)8 JSONObject (com.alibaba.fastjson.JSONObject)6 LambdaQueryWrapper (com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)6 SysDept (com.ruoyi.common.core.domain.entity.SysDept)6 LoginUser (com.ruoyi.common.core.domain.model.LoginUser)6 Constants (com.ruoyi.common.constant.Constants)5 GenConstants (com.ruoyi.common.constant.GenConstants)5 StringUtils (com.ruoyi.common.utils.StringUtils)5 SysOss (com.ruoyi.system.domain.SysOss)5