@Override public User createUserWithRoles(UserDto userDto) { Collection<Role> roles = new ArrayList<Role>(); Collection<Menu> menus = new ArrayList<Menu>(); User user = new User(); user.setTheme("home"); user.setUserName(userDto.getUsername()); user.setPassword(userDto.getPassword()); for (String roleName : userDto.getUserRoles()) { Role role = new Role(); role.setRoleEnum(RoleEnum.valueOf(roleName)); role.getRoleUsers().add(user); roles.add(role); } for (String menuName : userDto.getMenus()) { Menu menu = new Menu(); menu.setMenuAction("www.google.com"); menu.setMenuType(MenuType.valueOf(menuName)); SubMenu subMenu = new SubMenu(); subMenu.setActionUrl("www.google.com"); subMenu.setSunMenuType(SubMenuType.CONTENT); subMenu.setMenu(menu); for (Role role : roles) { menu.setRole(role); } menu.getSubMenus().add(subMenu); menus.add(menu); } for (Role role : roles) { role.setMenus(menus); } user.setUserRoles(roles); sessionFactory.getCurrentSession().persist(user); return user; }
@Override public List<Menu> getUserMenuBasedOnRoles(User user) { List<Menu> userMenus = new ArrayList<Menu>(); try { User persistUser = (User) sessionFactory.getCurrentSession().get(User.class, user.getUserId()); Collection<Role> roles = persistUser.getUserRoles(); for (Role role : roles) { if (role != null) { for (Menu menu : role.getMenus()) { userMenus.add(menu); } } } } catch (HibernateException e) { e.printStackTrace(); } return userMenus; }