package com.zyeeda.framework.ws;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
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.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.xml.xpath.XPathExpressionException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zyeeda.framework.entities.Menu;
import com.zyeeda.framework.entities.Role;
import com.zyeeda.framework.managers.MenuManager;
import com.zyeeda.framework.managers.internal.LdapDepartmentManager;
import com.zyeeda.framework.managers.internal.MenuManagerImpl;
import com.zyeeda.framework.managers.internal.RoleManagerImpl;
import com.zyeeda.framework.ws.base.ResourceService;

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

	@GET
	@Path("/")
	@Produces("application/json")
	public List<Menu> getMenu()throws XPathExpressionException, IOException {
		String user = this.getSecurityService().getCurrentUser();
		MenuManager menuMgr = new MenuManagerImpl();
		RoleManagerImpl roleMgr = new RoleManagerImpl(this
				.getPersistenceService());
		Set<String> rolesAuth = new HashSet<String>();
		List<Role> roles = new ArrayList<Role>();
		roles = roleMgr.getRoleBySubject(user);
		boolean result = false;
		for(Role role:roles){
			logger.debug("the value of the dept subject is = {}  ", role.getPermissions());
			for(String permission:role.getPermissionSet()){
				if(rolesAuth.size()>0){
					for(String haveAuth:rolesAuth){
						if(permission.equals(haveAuth)){
							result = true;
						}
					}
					if(result == false) {
						rolesAuth.add(permission);
					}
				} else {
					rolesAuth.add(permission);
				}
			}
		}
		//Set<String> permissionSet = role.getPermissionSet();
		List<Menu> listMenu = new ArrayList<Menu>();
		listMenu = menuMgr.getMenuListByPermissionAuth(rolesAuth);
		return listMenu;
	}


}

@@ -1582,16 +1582,43 @@
 false;%0D%0A
+%09%09if(roles.size() == 1)%7B%0D%0A%09
 %09%09for(Ro
@@ -1634,16 +1634,17 @@
 oles)%7B%0D%0A
+%09
 %09%09%09logge
@@ -1718,24 +1718,25 @@
 ons());%0D%0A%09%09%09
+%09
 for(String p
@@ -1768,24 +1768,25 @@
 ionSet())%7B%0D%0A
+%09
 %09%09%09%09if(roles
@@ -1800,14 +1800,17 @@
 ze()
-%3E
+ %3E 
 0)%7B%0D%0A
+%09
 %09%09%09%09
@@ -1849,16 +1849,17 @@
 %0D%0A%09%09%09%09%09%09
+%09
 if(permi
@@ -1880,24 +1880,94 @@
 aveAuth))%7B%0D%0A
+%09%09%09%09%09%09%09%09logger.debug(%22the value of the dept permission is agian %22);%0D%0A%09
 %09%09%09%09%09%09%09resul
@@ -1977,16 +1977,33 @@
  true;%0D%0A
+%09%09%09%09%09%09%09%09break;%0D%0A%09
 %09%09%09%09%09%09%7D%0D
@@ -2003,24 +2003,25 @@
 %09%09%09%09%09%7D%0D%0A
+%09
 %09%09%09%09%09%7D%0D%0A
 %09%09%09%09%09if(
@@ -2008,24 +2008,25 @@
 %7D%0D%0A%09%09%09%09%09%09%7D%0D%0A
+%09
 %09%09%09%09%09if(resu
@@ -2043,24 +2043,25 @@
 e) %7B%0D%0A%09%09%09%09%09%09
+%09
 rolesAuth.ad
@@ -2077,27 +2077,29 @@
 ion);%0D%0A%09%09%09%09%09
+%09
 %7D%0D%0A
+%09
 %09%09%09%09%7D else %7B
@@ -2101,24 +2101,25 @@
 lse %7B%0D%0A%09%09%09%09%09
+%09
 rolesAuth.ad
@@ -2142,15 +2142,79 @@
 %09%09%09%09
+%09
 %7D%0D%0A%09%09%09
-%7D
+%09%7D%0D%0A%09%09%09%7D%0D%0A%09%09%7D else %7B%0D%0A%09%09%09rolesAuth.add(role.getPermissionSet());
 %0D%0A%09%09

modify menuService.java