use of com.ibeiliao.deployment.admin.po.account.MenuPO in project Corgi by kevinYin.
the class MenuServiceImpl method buildTreeMap.
/**
* 建立菜单树
*
* @param appId
* - 应用系统ID
* @param root
* - 根节点
* @param menuList
* - 菜单列表
*/
private void buildTreeMap(int appId, MenuItem root, List<MenuPO> menuList) {
Map<Integer, MenuItem> tmpMap = new HashMap<>(menuList.size() * 4 / 3);
// 2014/8/14 增加,原因:个别程序员增加了几个找不到 parentMenuId 的菜单,导致死循环
final int LOOP_MAX = menuList.size() * menuList.size() + 10;
int loopCount = 0;
int i;
for (i = 0; i < menuList.size() && loopCount < LOOP_MAX; ) {
MenuPO menu = menuList.get(i);
if (menu.getParentMenuId() == 0) {
if (tmpMap.containsKey(menu.getMenuId())) {
// 忽略,不可能出现
;
} else {
MenuItem item = new MenuItem(menu.getMenuName(), "");
item.setId(menu.getMenuId());
item.setSequence(menu.getSequence());
root.createChildren().add(item);
tmpMap.put(menu.getMenuId(), item);
}
i++;
} else {
MenuItem parent = tmpMap.get(menu.getParentMenuId());
if (parent == null) {
// 找不到父菜单,放到队列后面处理
if (i == menuList.size() - 1) {
// 如果已经跑到队列最后,终止,避免死循环
break;
}
// 碰到有问题的菜单,可能有死循环 2014/8/14
menuList.remove(i);
// 放到最后
menuList.add(menu);
} else {
// 增加一个子菜单
MenuItem item = new MenuItem(menu.getMenuName(), "");
item.setId(menu.getMenuId());
item.setSequence(menu.getSequence());
parent.createChildren().add(item);
tmpMap.put(menu.getMenuId(), item);
// next
i++;
}
}
++loopCount;
}
if (loopCount >= LOOP_MAX) {
MenuPO tmp = (i < menuList.size() ? menuList.get(i) : null);
logger.info("菜单死循环, LOOP_MAX=" + LOOP_MAX + ", appId=" + appId + ", 有问题的菜单=" + JsonUtil.toJSONString(tmp));
}
}
use of com.ibeiliao.deployment.admin.po.account.MenuPO in project Corgi by kevinYin.
the class MenuServiceImpl method getMenuTree.
@Override
public MenuItem getMenuTree(long accountId, int appId) {
if (accountId <= 0) {
return new MenuItem("root", "", true);
}
List<MenuPO> menuList = menuDao.listByAppId(appId);
menuList = gainHasPermissionMenu(accountId, menuList);
MenuItem root = new MenuItem("root", "", true);
if (menuList.size() > 0) {
buildTreeMap(appId, root, menuList);
loadMenuResource(appId, root);
}
return root;
}
use of com.ibeiliao.deployment.admin.po.account.MenuPO in project Corgi by kevinYin.
the class MenuServiceImpl method saveMenuResource.
/**
* 保存资源信息。 NOTE:各资源独立,暂时没有 parentResId
*
* @param appId
* - 应用系统ID
* @param item
* - 要保存的节点
* @param parentMenu
* - 属于哪个菜单
*/
private void saveMenuResource(int appId, MenuItem item, MenuItem parentMenu) {
if (parentMenu == null) {
throw new IllegalArgumentException("资源所属的菜单为空, item=" + item.toString());
}
UrlResourcePO res = urlResourceDao.getByAppIdAndUri(appId, item.getUrl());
MenuPO menu = menuDao.getByAppIdAndMenuName(appId, parentMenu.getName());
if (menu == null) {
throw new IllegalArgumentException("资源所属的菜单在数据库里不存在, item=" + item.toString() + ", parent=" + parentMenu.toString());
}
if (res == null) {
// 新增资源
res = new UrlResourcePO();
res.setResId(generateIdForName(false, item.getName(), item.getUrl()));
// 暂时没有 parentResId
res.setParentResId(0);
res.setCreateTime(new Date());
res.setLastModify(res.getCreateTime());
res.setAppId(appId);
res.setUrlName(item.getName());
res.setOperator(AdminContext.getAccountId());
res.setSequence(0);
res.setUri(item.getUrl());
urlResourceDao.save(res);
res = urlResourceDao.getByAppIdAndUri(appId, item.getUrl());
} else {
// 更新资源
// 暂时没有 parentResId
res.setParentResId(0);
res.setUrlName(item.getName());
res.setOperator(AdminContext.getAccountId());
res.setLastModify(new Date());
// 注:这里不能更新uri,如果是新的uri,应该是新的记录
// name,parent_res_id,operator,type,sequence=,last_modify
urlResourceDao.updateRes(res);
}
// 保存菜单和资源的关系
MenuResourcesRelationPO relation = menuResourcesRelationDao.getMenuResource(menu.getMenuId(), res.getResId());
if (relation == null) {
relation = new MenuResourcesRelationPO();
relation.setMenuId(menu.getMenuId());
relation.setResId(res.getResId());
menuResourcesRelationDao.save(relation);
}
}
use of com.ibeiliao.deployment.admin.po.account.MenuPO in project Corgi by kevinYin.
the class MenuServiceImpl method saveMenu.
/**
* 保存菜单信息
*
* @param appId
* - 应用系统ID
* @param item
* - 要保存的节点
* @param parent
* - 父节点
*/
private void saveMenu(int appId, MenuItem item, MenuItem parent) {
MenuPO menu = menuDao.getByAppIdAndMenuName(appId, item.getName());
MenuPO parentMenu = null;
if (parent != null) {
parentMenu = menuDao.getByAppIdAndMenuName(appId, parent.getName());
}
if (menu == null) {
// 新增菜单
menu = new MenuPO();
if (parentMenu != null) {
menu.setParentMenuId(parentMenu.getMenuId());
}
menu.setMenuId(generateIdForName(true, item.getName(), item.getUrl()));
menu.setAppId(appId);
menu.setCreateTime(new Date());
menu.setLastModify(menu.getCreateTime());
menu.setHide((short) 0);
menu.setMenuName(item.getName());
menu.setOperator(AdminContext.getAccountId());
menu.setUrl(item.getUrl());
menu.setSequence(item.getSequence());
menuDao.save(menu);
} else {
// url,parent_menu_id,sequence,operator,hide,last_modify
if (parentMenu != null) {
menu.setParentMenuId(parentMenu.getMenuId());
}
// menu.setUrl(item.getUrl());
menu.setOperator(AdminContext.getAccountId());
menu.setUrl(item.getUrl());
menu.setSequence(item.getSequence());
menu.setLastModify(new Date());
menuDao.updateMenu(menu);
}
}
use of com.ibeiliao.deployment.admin.po.account.MenuPO in project Corgi by kevinYin.
the class MenuServiceImpl method deleteMenu.
/**
* 删除菜单
* @param menu
*/
private void deleteMenu(Menu menu) {
int menuId = menu.getMenuId();
ParameterUtil.assertGreaterThanZero(menuId, "菜单不能为空.");
ParameterUtil.assertGreaterThanZero(menu.getAppId(), "所属应用不能为空.");
// ParameterUtil.assertNonnegativeInt(menu.getParentMenuId(), "所属菜单不能为空.");
// 判断逻辑: 保证所传menuId对应的数据的正确性; 旨在解决传输非法构造数据以及错把资源当菜单的情况
MenuPO menuInDB = menuDao.getById(menu.getMenuId());
ParameterUtil.assertEqual(menu.getAppId(), menuInDB.getAppId(), "该应用系统下不存在此菜单(确认下是否属于资源).");
ParameterUtil.assertEqual(menu.getParentMenuId(), menuInDB.getParentMenuId(), "该父菜单下不存在此菜单(确认下是否属于资源).");
// delete role_menu
logger.info("删除角色菜单关系." + "[menuId=" + menuId + "]");
roleService.deleteMenu(menuId);
// delete menu
logger.info("删除菜单." + "[menuId=" + menuId + "]");
menuDao.deleteById(menuId);
// 获取菜单下面的子菜单(一级)
List<Integer> subMenuIds = menuDao.listMenuIdByParent(menuId);
// 如果子菜单不为空, 则递归删除子菜单; 如果子菜单为空, 则删除菜单资源关系以及对应的资源
if (subMenuIds != null && !subMenuIds.isEmpty()) {
for (Integer subMenuId : subMenuIds) {
MenuPO subMenuPO = menuDao.getById(subMenuId);
Menu subMenu = VOUtil.from(subMenuPO, Menu.class);
deleteMenu(subMenu);
}
} else {
List<Integer> resIds = menuResourcesRelationDao.listResource(Arrays.asList(menuId));
for (Integer resId : resIds) {
// delete menu_resource
logger.info("删除菜单资源关系." + "[resId=" + resId + "]");
menuResourcesRelationDao.deleteByResource(resId);
// delete resource
logger.info("删除资源." + "[resId=" + resId + "]");
urlResourceDao.deleteById(resId);
}
}
}
Aggregations