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("创建定时任务失败");
}
}
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("立即执行定时任务失败");
}
}
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;
}
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());
}
}
}
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);
}
Aggregations