Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jimin.zheng committed May 28, 2016
1 parent 887ea8a commit 68b030a
Show file tree
Hide file tree
Showing 9 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/app/mvc/acl/service/SysAclService.java
Original file line number Diff line number Diff line change
@@ -1 +1 @@
package com.app.mvc.acl.service;import com.app.mvc.acl.convert.AclConvert;import com.app.mvc.acl.convert.BaseConvert;import com.app.mvc.acl.dao.SysAclDao;import com.app.mvc.acl.domain.SysAcl;import com.app.mvc.acl.vo.AclPara;import com.app.mvc.beans.PageQuery;import com.app.mvc.beans.PageResult;import com.app.mvc.exception.ParaException;import com.app.mvc.util.DateTimeUtil;import com.google.common.base.Preconditions;import lombok.extern.slf4j.Slf4j;import org.joda.time.DateTime;import org.springframework.stereotype.Service;import javax.annotation.Resource;import java.util.List;/** * Created by jimin on 16/1/18. */@Slf4j@Servicepublic class SysAclService implements SysService { @Resource private SysAclDao sysAclDao; @Resource private SysLogService sysLogService; /** * 新增权限点 */ public void save(AclPara para) { BaseConvert.checkPara(para); SysAcl acl = AclConvert.of(para); if (checkExist(para.getAclModuleId(), para.getName(), null)) { throw new ParaException("当前模块下存在相同名称的权限点"); } acl.setCode(buildCode()); sysAclDao.save(acl); sysLogService.saveAclLog(null, acl); } /** * 更新权限点 */ public void update(AclPara para) { BaseConvert.checkPara(para); SysAcl before = sysAclDao.findById(para.getId()); Preconditions.checkNotNull(before, "待更新权限点不存在"); if (checkExist(para.getAclModuleId(), para.getName(), para.getId())) { throw new ParaException("当前模块下存在相同名称的权限点"); } SysAcl after = AclConvert.of(para); sysAclDao.update(after); sysLogService.saveAclLog(before, after); } private String buildCode() { return DateTimeUtil.allFrom(DateTime.now()) + "_" + (int) (Math.random() * 100); } /** * 校验指定模块下是否存在指定名称的权限点 * * @param aclModuleId 权限模块id * @param name 权限点名称 * @param id 待排除的权限点id, 更新时要忽略掉自己 * @return */ private boolean checkExist(int aclModuleId, String name, Integer id) { return sysAclDao.countByNameAndAclModuleId(aclModuleId, name, id) > 0; } /** * 分页获取权限点列表 */ public PageResult<SysAcl> getPageByAclModuleId(int aclModuleId, PageQuery page) { BaseConvert.checkPara(page); int count = sysAclDao.countByAclModuleId(aclModuleId); if (count > 0) { List<SysAcl> list = sysAclDao.getPageByAclModuleId(aclModuleId, page); return PageResult.<SysAcl>builder().total(count).data(list).build(); } else { return PageResult.<SysAcl>builder().total(0).build(); } } public SysAcl findById(int id) { return sysAclDao.findById(id); } @Override public void recover(int targetId, Object o) { SysAcl acl = (SysAcl) o; Preconditions.checkNotNull(acl, "还原前的权限点为空,无法还原"); SysAcl before = sysAclDao.findById(targetId); Preconditions.checkNotNull(before, "待还原的权限点不存在"); if (checkExist(acl.getAclModuleId(), acl.getName(), acl.getId())) { throw new ParaException("当前模块下存在相同名称的权限点"); } sysAclDao.update(acl); sysLogService.saveAclLog(before, acl); } /** * 根据url获取能正则匹配到的权限点列表 */ public List<SysAcl> getByUrlRegexp(String url) { Preconditions.checkNotNull(url, "url不可以为空"); return sysAclDao.getByUrlRegexp(url); }}
package com.app.mvc.acl.service;import com.app.mvc.acl.convert.AclConvert;import com.app.mvc.acl.convert.BaseConvert;import com.app.mvc.acl.dao.SysAclDao;import com.app.mvc.acl.domain.SysAcl;import com.app.mvc.acl.vo.AclPara;import com.app.mvc.beans.PageQuery;import com.app.mvc.beans.PageResult;import com.app.mvc.exception.ParaException;import com.app.mvc.util.DateTimeUtil;import com.google.common.base.Preconditions;import lombok.extern.slf4j.Slf4j;import org.joda.time.DateTime;import org.springframework.stereotype.Service;import javax.annotation.Resource;import java.util.List;/** * Created by jimin on 16/1/18. */@Slf4j@Servicepublic class SysAclService implements SysService { @Resource private SysAclDao sysAclDao; @Resource private SysLogService sysLogService; /** * 新增权限点 */ public void save(AclPara para) { BaseConvert.checkPara(para); SysAcl acl = AclConvert.of(para); if (checkExist(para.getAclModuleId(), para.getName(), null)) { throw new ParaException("当前模块下存在相同名称的权限点"); } acl.setCode(buildCode()); sysAclDao.save(acl); sysLogService.saveAclLog(null, acl); } /** * 更新权限点 */ public void update(AclPara para) { BaseConvert.checkPara(para); SysAcl before = sysAclDao.findById(para.getId()); Preconditions.checkNotNull(before, "待更新权限点不存在"); if (checkExist(para.getAclModuleId(), para.getName(), para.getId())) { throw new ParaException("当前模块下存在相同名称的权限点"); } SysAcl after = AclConvert.of(para); sysAclDao.update(after); sysLogService.saveAclLog(before, after); } private String buildCode() { return DateTimeUtil.allFrom(DateTime.now()) + "_" + (int) (Math.random() * 100); } /** * 校验指定模块下是否存在指定名称的权限点 * * @param aclModuleId 权限模块id * @param name 权限点名称 * @param id 待排除的权限点id, 更新时要忽略掉自己 * @return */ private boolean checkExist(int aclModuleId, String name, Integer id) { return sysAclDao.countByNameAndAclModuleId(aclModuleId, name, id) > 0; } /** * 分页获取权限点列表 */ public PageResult<SysAcl> getPageByAclModuleId(int aclModuleId, PageQuery page) { BaseConvert.checkPara(page); int count = sysAclDao.countByAclModuleId(aclModuleId); if (count > 0) { List<SysAcl> list = sysAclDao.getPageByAclModuleId(aclModuleId, page); return PageResult.<SysAcl>builder().total(count).data(list).build(); } else { return PageResult.<SysAcl>builder().build(); } } public SysAcl findById(int id) { return sysAclDao.findById(id); } @Override public void recover(int targetId, Object o) { SysAcl acl = (SysAcl) o; Preconditions.checkNotNull(acl, "还原前的权限点为空,无法还原"); SysAcl before = sysAclDao.findById(targetId); Preconditions.checkNotNull(before, "待还原的权限点不存在"); if (checkExist(acl.getAclModuleId(), acl.getName(), acl.getId())) { throw new ParaException("当前模块下存在相同名称的权限点"); } sysAclDao.update(acl); sysLogService.saveAclLog(before, acl); } /** * 根据url获取能正则匹配到的权限点列表 */ public List<SysAcl> getByUrlRegexp(String url) { Preconditions.checkNotNull(url, "url不可以为空"); return sysAclDao.getByUrlRegexp(url); }}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/app/mvc/acl/service/SysLogService.java
Original file line number Diff line number Diff line change
@@ -1 +1 @@
package com.app.mvc.acl.service;import com.app.mvc.acl.convert.BaseConvert;import com.app.mvc.acl.convert.LogConvert;import com.app.mvc.acl.dao.SysLogDao;import com.app.mvc.acl.domain.SysAcl;import com.app.mvc.acl.domain.SysAclModule;import com.app.mvc.acl.domain.SysBase;import com.app.mvc.acl.domain.SysDept;import com.app.mvc.acl.domain.SysLog;import com.app.mvc.acl.domain.SysRole;import com.app.mvc.acl.domain.SysUser;import com.app.mvc.acl.dto.LogSearchDto;import com.app.mvc.acl.enums.LogType;import com.app.mvc.acl.vo.LogPara;import com.app.mvc.beans.JsonMapper;import com.app.mvc.beans.PageQuery;import com.app.mvc.beans.PageResult;import com.google.common.base.Joiner;import lombok.extern.slf4j.Slf4j;import org.springframework.stereotype.Service;import javax.annotation.Resource;import java.util.List;/** * Created by jimin on 16/1/18. */@Slf4j@Servicepublic class SysLogService { @Resource private SysLogDao sysLogDao; public PageResult<SysLog> getPageByFuzzySearch(LogPara para, PageQuery page) { BaseConvert.checkPara(page); LogSearchDto dto = LogConvert.of(para); int count = sysLogDao.countFuzzySearch(dto); if (count > 0) { List<SysLog> list = sysLogDao.fuzzySearch(dto, page); return PageResult.<SysLog>builder().total(count).data(list).build(); } return PageResult.<SysLog>builder().total(0).build(); } public SysLog findById(int id) { return sysLogDao.findById(id); } public void saveAclLog(SysAcl before, SysAcl after) { SysBase base = BaseConvert.of(); SysLog sysLog = SysLog.builder().targetId(after.getId()).type(LogType.ACL.getCode()).oldValue(JsonMapper.obj2String(before)) .newValue(JsonMapper.obj2String(after)).operator(base.getOperator()).operateIp(base.getOperateIp()).build(); safetySaveLog(sysLog); } public void saveAclModuleLog(SysAclModule before, SysAclModule after) { SysBase base = BaseConvert.of(); SysLog sysLog = SysLog.builder().targetId(after == null ? before.getId() : after.getId()).type(LogType.ACL_MODULE.getCode()) .oldValue(JsonMapper.obj2String(before)).newValue(JsonMapper.obj2String(after)).operator(base.getOperator()).operateIp(base.getOperateIp()) .build(); safetySaveLog(sysLog); } public void saveUserLog(SysUser before, SysUser after) { SysBase base = BaseConvert.of(); SysLog sysLog = SysLog.builder().targetId(after.getId()).type(LogType.USER.getCode()).oldValue(JsonMapper.obj2String(before)) .newValue(JsonMapper.obj2String(after)).operator(base.getOperator()).operateIp(base.getOperateIp()).build(); safetySaveLog(sysLog); } public void saveRoleLog(SysRole before, SysRole after) { SysBase base = BaseConvert.of(); SysLog sysLog = SysLog.builder().targetId(after == null ? before.getId() : after.getId()).type(LogType.ROLE.getCode()) .oldValue(JsonMapper.obj2String(before)).newValue(JsonMapper.obj2String(after)).operator(base.getOperator()).operateIp(base.getOperateIp()) .build(); safetySaveLog(sysLog); } public void saveDeptLog(SysDept before, SysDept after) { SysBase base = BaseConvert.of(); SysLog sysLog = SysLog.builder().targetId(after == null ? before.getId() : after.getId()).type(LogType.DEPT.getCode()) .oldValue(JsonMapper.obj2String(before)).newValue(JsonMapper.obj2String(after)).operator(base.getOperator()).operateIp(base.getOperateIp()) .build(); safetySaveLog(sysLog); } public void saveRoleAclLog(int roleId, List<Integer> beforeList, List<Integer> afterList) { SysBase base = BaseConvert.of(); SysLog sysLog = SysLog.builder().targetId(roleId).type(LogType.ROLE_ACL.getCode()).oldValue(Joiner.on(",").join(beforeList)) .newValue(JsonMapper.obj2String(Joiner.on(",").join(afterList))).operator(base.getOperator()).operateIp(base.getOperateIp()).build(); safetySaveLog(sysLog); } public void saveRoleUserLog(int roleId, List<Integer> beforeList, List<Integer> afterList) { SysBase base = BaseConvert.of(); SysLog sysLog = SysLog.builder().targetId(roleId).type(LogType.ROLE_USER.getCode()).oldValue(Joiner.on(",").join(beforeList)) .newValue(JsonMapper.obj2String(Joiner.on(",").join(afterList))).operator(base.getOperator()).operateIp(base.getOperateIp()).build(); safetySaveLog(sysLog); } private void safetySaveLog(SysLog sysLog) { try { sysLogDao.save(sysLog); } catch (Throwable e) { log.error("add log exception, {}", JsonMapper.obj2String(sysLog), e); } }}
package com.app.mvc.acl.service;import com.app.mvc.acl.convert.BaseConvert;import com.app.mvc.acl.convert.LogConvert;import com.app.mvc.acl.dao.SysLogDao;import com.app.mvc.acl.domain.SysAcl;import com.app.mvc.acl.domain.SysAclModule;import com.app.mvc.acl.domain.SysBase;import com.app.mvc.acl.domain.SysDept;import com.app.mvc.acl.domain.SysLog;import com.app.mvc.acl.domain.SysRole;import com.app.mvc.acl.domain.SysUser;import com.app.mvc.acl.dto.LogSearchDto;import com.app.mvc.acl.enums.LogType;import com.app.mvc.acl.vo.LogPara;import com.app.mvc.beans.JsonMapper;import com.app.mvc.beans.PageQuery;import com.app.mvc.beans.PageResult;import com.google.common.base.Joiner;import lombok.extern.slf4j.Slf4j;import org.springframework.stereotype.Service;import javax.annotation.Resource;import java.util.List;/** * Created by jimin on 16/1/18. */@Slf4j@Servicepublic class SysLogService { @Resource private SysLogDao sysLogDao; public PageResult<SysLog> getPageByFuzzySearch(LogPara para, PageQuery page) { BaseConvert.checkPara(page); LogSearchDto dto = LogConvert.of(para); int count = sysLogDao.countFuzzySearch(dto); if (count > 0) { List<SysLog> list = sysLogDao.fuzzySearch(dto, page); return PageResult.<SysLog>builder().total(count).data(list).build(); } return PageResult.<SysLog>builder().build(); } public SysLog findById(int id) { return sysLogDao.findById(id); } public void saveAclLog(SysAcl before, SysAcl after) { SysBase base = BaseConvert.of(); SysLog sysLog = SysLog.builder().targetId(after.getId()).type(LogType.ACL.getCode()).oldValue(JsonMapper.obj2String(before)) .newValue(JsonMapper.obj2String(after)).operator(base.getOperator()).operateIp(base.getOperateIp()).build(); safetySaveLog(sysLog); } public void saveAclModuleLog(SysAclModule before, SysAclModule after) { SysBase base = BaseConvert.of(); SysLog sysLog = SysLog.builder().targetId(after == null ? before.getId() : after.getId()).type(LogType.ACL_MODULE.getCode()) .oldValue(JsonMapper.obj2String(before)).newValue(JsonMapper.obj2String(after)).operator(base.getOperator()).operateIp(base.getOperateIp()) .build(); safetySaveLog(sysLog); } public void saveUserLog(SysUser before, SysUser after) { SysBase base = BaseConvert.of(); SysLog sysLog = SysLog.builder().targetId(after.getId()).type(LogType.USER.getCode()).oldValue(JsonMapper.obj2String(before)) .newValue(JsonMapper.obj2String(after)).operator(base.getOperator()).operateIp(base.getOperateIp()).build(); safetySaveLog(sysLog); } public void saveRoleLog(SysRole before, SysRole after) { SysBase base = BaseConvert.of(); SysLog sysLog = SysLog.builder().targetId(after == null ? before.getId() : after.getId()).type(LogType.ROLE.getCode()) .oldValue(JsonMapper.obj2String(before)).newValue(JsonMapper.obj2String(after)).operator(base.getOperator()).operateIp(base.getOperateIp()) .build(); safetySaveLog(sysLog); } public void saveDeptLog(SysDept before, SysDept after) { SysBase base = BaseConvert.of(); SysLog sysLog = SysLog.builder().targetId(after == null ? before.getId() : after.getId()).type(LogType.DEPT.getCode()) .oldValue(JsonMapper.obj2String(before)).newValue(JsonMapper.obj2String(after)).operator(base.getOperator()).operateIp(base.getOperateIp()) .build(); safetySaveLog(sysLog); } public void saveRoleAclLog(int roleId, List<Integer> beforeList, List<Integer> afterList) { SysBase base = BaseConvert.of(); SysLog sysLog = SysLog.builder().targetId(roleId).type(LogType.ROLE_ACL.getCode()).oldValue(Joiner.on(",").join(beforeList)) .newValue(JsonMapper.obj2String(Joiner.on(",").join(afterList))).operator(base.getOperator()).operateIp(base.getOperateIp()).build(); safetySaveLog(sysLog); } public void saveRoleUserLog(int roleId, List<Integer> beforeList, List<Integer> afterList) { SysBase base = BaseConvert.of(); SysLog sysLog = SysLog.builder().targetId(roleId).type(LogType.ROLE_USER.getCode()).oldValue(Joiner.on(",").join(beforeList)) .newValue(JsonMapper.obj2String(Joiner.on(",").join(afterList))).operator(base.getOperator()).operateIp(base.getOperateIp()).build(); safetySaveLog(sysLog); } private void safetySaveLog(SysLog sysLog) { try { sysLogDao.save(sysLog); } catch (Throwable e) { log.error("add log exception, {}", JsonMapper.obj2String(sysLog), e); } }}
Expand Down
Loading

0 comments on commit 68b030a

Please sign in to comment.