Search in sources :

Example 6 with BusinessException

use of com.company.project.common.exception.BusinessException in project springboot-manager by aitangbao.

the class ScheduleUtils method createScheduleJob.

/**
 * 创建定时任务
 */
public static void createScheduleJob(Scheduler scheduler, SysJobEntity scheduleJob) {
    try {
        // 构建job信息
        JobDetail jobDetail = JobBuilder.newJob(ScheduleJob.class).withIdentity(getJobKey(scheduleJob.getId())).build();
        // 表达式调度构建器
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(scheduleJob.getCronExpression()).withMisfireHandlingInstructionDoNothing();
        // 按新的cronExpression表达式构建一个新的trigger
        CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(getTriggerKey(scheduleJob.getId())).withSchedule(scheduleBuilder).build();
        // 放入参数,运行时的方法可以获取
        jobDetail.getJobDataMap().put(SysJobEntity.JOB_PARAM_KEY, scheduleJob);
        scheduler.scheduleJob(jobDetail, trigger);
        // 暂停任务
        if (Constant.SCHEDULER_STATUS_PAUSE.equals(scheduleJob.getStatus())) {
            pauseJob(scheduler, scheduleJob.getId());
        }
    } catch (SchedulerException e) {
        throw new BusinessException("创建定时任务失败");
    }
}
Also used : BusinessException(com.company.project.common.exception.BusinessException)

Example 7 with BusinessException

use of com.company.project.common.exception.BusinessException in project springboot-manager by aitangbao.

the class ScheduleUtils method run.

/**
 * 立即执行任务
 */
public static void run(Scheduler scheduler, SysJobEntity scheduleJob) {
    try {
        // 参数
        JobDataMap dataMap = new JobDataMap();
        dataMap.put(SysJobEntity.JOB_PARAM_KEY, scheduleJob);
        scheduler.triggerJob(getJobKey(scheduleJob.getId()), dataMap);
    } catch (SchedulerException e) {
        throw new BusinessException("立即执行定时任务失败");
    }
}
Also used : BusinessException(com.company.project.common.exception.BusinessException)

Example 8 with BusinessException

use of com.company.project.common.exception.BusinessException in project springboot-manager by aitangbao.

the class CustomAccessControlFilter method onAccessDenied.

@Override
protected boolean onAccessDenied(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) servletRequest;
    try {
        Subject subject = getSubject(servletRequest, servletResponse);
        System.out.println(subject.isAuthenticated() + "");
        System.out.println(HttpContextUtils.isAjaxRequest(request));
        log.info(request.getMethod());
        log.info(request.getRequestURL().toString());
        // 从header中获取token
        String token = request.getHeader(Constant.ACCESS_TOKEN);
        // 如果header中不存在token,则从参数中获取token
        if (StringUtils.isEmpty(token)) {
            token = request.getParameter(Constant.ACCESS_TOKEN);
        }
        if (StringUtils.isEmpty(token)) {
            throw new BusinessException(BaseResponseCode.TOKEN_ERROR);
        }
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(token, token);
        getSubject(servletRequest, servletResponse).login(usernamePasswordToken);
    } catch (BusinessException exception) {
        if (HttpContextUtils.isAjaxRequest(request)) {
            customResponse(exception.getMessageCode(), exception.getDetailMessage(), servletResponse);
        } else if (exception.getMessageCode() == BaseResponseCode.TOKEN_ERROR.getCode()) {
            servletRequest.getRequestDispatcher("/index/login").forward(servletRequest, servletResponse);
        } else if (exception.getMessageCode() == BaseResponseCode.UNAUTHORIZED_ERROR.getCode()) {
            servletRequest.getRequestDispatcher("/index/403").forward(servletRequest, servletResponse);
        } else {
            servletRequest.getRequestDispatcher("/index/500").forward(servletRequest, servletResponse);
        }
        return false;
    } catch (AuthenticationException e) {
        if (HttpContextUtils.isAjaxRequest(request)) {
            if (e.getCause() instanceof BusinessException) {
                BusinessException exception = (BusinessException) e.getCause();
                customResponse(exception.getMessageCode(), exception.getDetailMessage(), servletResponse);
            } else {
                customResponse(BaseResponseCode.SYSTEM_BUSY.getCode(), BaseResponseCode.SYSTEM_BUSY.getMsg(), servletResponse);
            }
        } else {
            servletRequest.getRequestDispatcher("/index/403").forward(servletRequest, servletResponse);
        }
        return false;
    } catch (Exception e) {
        if (HttpContextUtils.isAjaxRequest(request)) {
            if (e.getCause() instanceof BusinessException) {
                BusinessException exception = (BusinessException) e.getCause();
                customResponse(exception.getMessageCode(), exception.getDetailMessage(), servletResponse);
            } else {
                customResponse(BaseResponseCode.SYSTEM_BUSY.getCode(), BaseResponseCode.SYSTEM_BUSY.getMsg(), servletResponse);
            }
        } else {
            servletRequest.getRequestDispatcher("/index/500").forward(servletRequest, servletResponse);
        }
        return false;
    }
    return true;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) BusinessException(com.company.project.common.exception.BusinessException) AuthenticationException(org.apache.shiro.authc.AuthenticationException) Subject(org.apache.shiro.subject.Subject) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) AuthenticationException(org.apache.shiro.authc.AuthenticationException) BusinessException(com.company.project.common.exception.BusinessException) UsernamePasswordToken(org.apache.shiro.authc.UsernamePasswordToken)

Example 9 with BusinessException

use of com.company.project.common.exception.BusinessException in project springboot-manager by aitangbao.

the class GenUtils method generatorCode.

/**
 * 生成代码
 */
public static void generatorCode(Map<String, String> table, List<Map<String, String>> columns, ZipOutputStream zip) {
    // 配置信息
    Configuration config = getConfig();
    boolean hasBigDecimal = false;
    // 表信息
    TableEntity tableEntity = new TableEntity();
    tableEntity.setTableName(table.get("tableName"));
    tableEntity.setComments(table.get("tableComment"));
    // 表名转换成Java类名
    String className = tableToJava(tableEntity.getTableName(), config.getStringArray("tablePrefix"));
    tableEntity.setClassName(className);
    tableEntity.setClassname(StringUtils.uncapitalize(className));
    tableEntity.setClassNameLower(className.toLowerCase());
    // 列信息
    List<ColumnEntity> columsList = new ArrayList<>();
    for (Map<String, String> column : columns) {
        ColumnEntity columnEntity = new ColumnEntity();
        columnEntity.setColumnName(column.get("columnName"));
        columnEntity.setDataType(column.get("dataType"));
        columnEntity.setComments(column.get("columnComment"));
        columnEntity.setExtra(column.get("extra"));
        // 列名转换成Java属性名
        String attrName = columnToJava(columnEntity.getColumnName());
        columnEntity.setAttrName(attrName);
        columnEntity.setAttrname(StringUtils.uncapitalize(attrName));
        // 列的数据类型,转换成Java类型
        String attrType = config.getString(columnEntity.getDataType(), "unknowType");
        columnEntity.setAttrType(attrType);
        if (!hasBigDecimal && "BigDecimal".equals(attrType)) {
            hasBigDecimal = true;
        }
        // 是否主键
        if ("PRI".equalsIgnoreCase(column.get("columnKey")) && tableEntity.getPk() == null) {
            tableEntity.setPk(columnEntity);
        }
        columsList.add(columnEntity);
    }
    tableEntity.setColumns(columsList);
    // 没主键,则第一个字段为主键
    if (tableEntity.getPk() == null) {
        tableEntity.setPk(tableEntity.getColumns().get(0));
    }
    // 设置velocity资源加载器
    Properties prop = new Properties();
    prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
    Velocity.init(prop);
    String mainPath = config.getString("mainPath");
    mainPath = StringUtils.isBlank(mainPath) ? "com.company" : mainPath;
    // 封装模板数据
    Map<String, Object> map = new HashMap<>(15);
    map.put("tableName", tableEntity.getTableName());
    map.put("comments", tableEntity.getComments());
    map.put("pk", tableEntity.getPk());
    map.put("className", tableEntity.getClassName());
    map.put("classname", tableEntity.getClassname());
    map.put("pathName", tableEntity.getClassname().toLowerCase());
    map.put("columns", tableEntity.getColumns());
    map.put("classNameLower", tableEntity.getClassNameLower());
    map.put("hasBigDecimal", hasBigDecimal);
    map.put("mainPath", mainPath);
    map.put("package", config.getString("package"));
    map.put("author", config.getString("author"));
    map.put("email", config.getString("email"));
    map.put("datetime", DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN));
    map.put("identity", IdWorker.getId());
    map.put("addId", IdWorker.getId());
    map.put("updateId", IdWorker.getId());
    map.put("deleteId", IdWorker.getId());
    map.put("selectId", IdWorker.getId());
    map.put("identityJoinId", IdWorker.getId());
    map.put("addIdJoinId", IdWorker.getId());
    map.put("updateIdJoinId", IdWorker.getId());
    map.put("deleteIdJoinId", IdWorker.getId());
    map.put("selectIdJoinId", IdWorker.getId());
    VelocityContext context = new VelocityContext(map);
    // 获取模板列表
    List<String> templates = getTemplates();
    for (String template : templates) {
        // 渲染模板
        StringWriter sw = new StringWriter();
        Template tpl = Velocity.getTemplate(template, "UTF-8");
        tpl.merge(context, sw);
        try {
            // 添加到zip
            zip.putNextEntry(new ZipEntry(Objects.requireNonNull(getFileName(template, tableEntity.getClassName(), config.getString("package")))));
            IOUtils.write(sw.toString(), zip, "UTF-8");
            IOUtils.closeQuietly(sw);
            zip.closeEntry();
        } catch (IOException e) {
            throw new BusinessException("渲染模板失败,表名:" + tableEntity.getTableName());
        }
    }
}
Also used : ColumnEntity(com.company.project.entity.ColumnEntity) Configuration(org.apache.commons.configuration.Configuration) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) VelocityContext(org.apache.velocity.VelocityContext) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) Template(org.apache.velocity.Template) BusinessException(com.company.project.common.exception.BusinessException) StringWriter(java.io.StringWriter) TableEntity(com.company.project.entity.TableEntity)

Example 10 with BusinessException

use of com.company.project.common.exception.BusinessException in project springboot-manager by aitangbao.

the class DeptServiceImpl method addDept.

@Override
public void addDept(SysDept vo) {
    String relationCode;
    String deptCode = this.getNewDeptCode();
    SysDept parent = sysDeptMapper.selectById(vo.getPid());
    if ("0".equals(vo.getPid())) {
        relationCode = deptCode;
    } else if (null == parent) {
        throw new BusinessException(BaseResponseCode.DATA_ERROR);
    } else {
        relationCode = parent.getRelationCode() + deptCode;
    }
    vo.setDeptNo(deptCode);
    vo.setRelationCode(relationCode);
    vo.setStatus(1);
    sysDeptMapper.insert(vo);
}
Also used : BusinessException(com.company.project.common.exception.BusinessException) SysDept(com.company.project.entity.SysDept)

Aggregations

BusinessException (com.company.project.common.exception.BusinessException)24 SysUser (com.company.project.entity.SysUser)7 SysDept (com.company.project.entity.SysDept)5 SysPermission (com.company.project.entity.SysPermission)3 SysRolePermission (com.company.project.entity.SysRolePermission)3 Transactional (org.springframework.transaction.annotation.Transactional)3 SysRole (com.company.project.entity.SysRole)2 DeptRespNodeVO (com.company.project.vo.resp.DeptRespNodeVO)2 IOException (java.io.IOException)2 JSONObject (com.alibaba.fastjson.JSONObject)1 LogAnnotation (com.company.project.common.aop.annotation.LogAnnotation)1 ColumnEntity (com.company.project.entity.ColumnEntity)1 SysDictDetailEntity (com.company.project.entity.SysDictDetailEntity)1 SysDictEntity (com.company.project.entity.SysDictEntity)1 SysFilesEntity (com.company.project.entity.SysFilesEntity)1 SysJobEntity (com.company.project.entity.SysJobEntity)1 SysRoleDeptEntity (com.company.project.entity.SysRoleDeptEntity)1 TableEntity (com.company.project.entity.TableEntity)1 RolePermissionOperationReqVO (com.company.project.vo.req.RolePermissionOperationReqVO)1 UserRoleOperationReqVO (com.company.project.vo.req.UserRoleOperationReqVO)1