package com.zyeeda.framework.ws;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.servlet.ServletContext;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.xml.xpath.XPathExpressionException;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zyeeda.framework.entities.Role;
import com.zyeeda.framework.managers.MenuManager;
import com.zyeeda.framework.managers.PermissionManager;
import com.zyeeda.framework.managers.internal.DefaultMenuManager;
import com.zyeeda.framework.managers.internal.DefaultPermissionManager;
import com.zyeeda.framework.managers.internal.DefaultRoleManager;
import com.zyeeda.framework.viewmodels.MenuVo;
import com.zyeeda.framework.viewmodels.PermissionVo;
import com.zyeeda.framework.ws.base.ResourceService;

@Path("/menu")
public class MenuService extends ResourceService {
	
	private static final Logger logger = LoggerFactory.getLogger(MenuService.class);
		
	public MenuService(@Context ServletContext ctx) {
		super(ctx);
	}

	@GET
	@Path("/")
	@Produces("application/json")
	public List<MenuVo> getMenu()throws XPathExpressionException, IOException {
		String user = this.getSecurityService().getCurrentUser();
		MenuManager menuMgr = new DefaultMenuManager();
		DefaultRoleManager roleMgr = new DefaultRoleManager(this
				.getPersistenceService());
		//PermissionManager permissionMgr = new DefaultPermissionManager();
		List<String> rolesAuth = new ArrayList<String>();
		List<MenuVo> listMenu = new ArrayList<MenuVo>();
		List<Role> roles = new ArrayList<Role>();
		roles = roleMgr.getRoleBySubject(user);	
		Set<String> authList = roleMgr.getListAuth(roles);
		Session session = SecurityUtils.getSubject().getSession();
		session.setAttribute("auth", authList);
//		List<PermissionVo> permissionVoList = new ArrayList<PermissionVo>();
//		for(String auth : authList) {
//			PermissionVo permission = permissionMgr.getPermissionByPath(auth);
//			permissionVoList.add(permission);
//		}
//		session.setAttribute("auth", permissionVoList);
		if(roles.size() == 1) {
			logger.debug("the value of the dept subject is = {}  ", roles.get(0).getPermissionList());
			listMenu = menuMgr.getMenuListByPermissionAuth(roles.get(0).getPermissionList());
			return listMenu;
		}
		for(Role role:roles) {
			logger.debug("the value of the dept subject is = {}  ", role.getPermissions());
			for(String permission:role.getPermissionList()){
				if(rolesAuth.size() == 0){
					rolesAuth.add(permission);
					continue;
				}
				if(!(rolesAuth.contains(permission))){
					rolesAuth.add(permission);
				}
				
			}
		}
		listMenu = menuMgr.getMenuListByPermissionAuth(rolesAuth);
		return listMenu;
	}
}

@@ -890,62 +890,177 @@
 Menu
-Vo;%0D%0Aimport com.zyeeda.framework.viewmodels.Permission
+AndPermission;%0D%0Aimport com.zyeeda.framework.viewmodels.MenuVo;%0D%0Aimport com.zyeeda.framework.viewmodels.PermissionVo;%0D%0Aimport com.zyeeda.framework.viewmodels.RoleWithUser
 Vo;%0D
@@ -1407,28 +1407,33 @@
 %09public 
-List%3CMenuVo%3E
+MenuAndPermission
  getMenu
@@ -1688,18 +1688,16 @@
 ());%0D%0A%09%09
-//
 Permissi
@@ -1804,24 +1804,87 @@
 String%3E();%0D%0A
+%09%09MenuAndPermission roleWithUserVo = new MenuAndPermission();%0D%0A
 %09%09List%3CMenuV
@@ -2066,16 +2066,18 @@
 oles);%0D%0A
+//
 %09%09Sessio
@@ -2130,16 +2130,18 @@
 ion();%0D%0A
+//
 %09%09sessio
@@ -2175,20 +2175,20 @@
 List);%0D%0A
-//
 %09%09
+//
 List%3CPer
@@ -2249,18 +2249,16 @@
 Vo%3E();%0D%0A
-//
 %09%09for(St
@@ -2282,18 +2282,16 @@
 ist) %7B%0D%0A
-//
 %09%09%09Permi
@@ -2324,24 +2324,28 @@
 ssionMgr.get
+Raom
 PermissionBy
@@ -2361,29 +2361,45 @@
 );%0D%0A
-//
 %09%09%09
-permissionVoList
+roleWithUserVo.getListPermission()
 .add
@@ -2417,19 +2417,17 @@
 );%0D%0A
-//
 %09%09%7D%0D%0A
-//
 %09%09
+//
 sess
@@ -2683,30 +2683,87 @@
 );%0D%0A%09%09%09r
-eturn listMenu
+oleWithUserVo.getListMenu().addAll(listMenu);%0D%0A%09%09%09return roleWithUserVo
 ;%0D%0A%09%09%7D%0D%0A
@@ -3183,22 +3183,78 @@
 %0A%09%09r
-eturn listMenu
+oleWithUserVo.getListMenu().addAll(listMenu);%0D%0A%09%09return roleWithUserVo
 ;%0D%0A%09

update MenuService.java