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.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<Menu> listMenu = new ArrayList<Menu>();
		List<Role> roles = new ArrayList<Role>();
		roles = roleMgr.getRoleBySubject(user);
		boolean result = false;
		if(roles.size() > 1){
			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;
								break;
							}
						}
						if(result == false) {
							rolesAuth.add(permission);
						}
					} else {
						rolesAuth.add(permission);
					}					
				}
			}		
			for(String menuAtuh:rolesAuth){
				System.out.println("*****************************" + menuAtuh);
			}		
			listMenu = menuMgr.getMenuListByPermissionAuth(rolesAuth);
			System.out.println("!!!!!!!!!!!!!!!!!!!"+listMenu.size());
		} else if(roles.size() == 1){
			for(String auth:roles.get(0).getPermissionSet()){
				System.out.println("*******************************" + auth);
			}
			listMenu = menuMgr.getMenuListByPermissionAuth(roles.get(0).getPermissionSet());
			System.out.println("!!!!!!!!!!!!!!!!!!!"+listMenu.size());
		}
		return listMenu;
	}


}

@@ -2118,402 +2118,105 @@
 %0A%09%09%09
-for(String menuAtuh:rolesAuth)%7B%0D%0A%09%09%09%09System.out.println(%22*****************************%22 + menuAtuh);%0D%0A%09%09%09%7D%09%09%0D%0A%09%09%09listMenu = menuMgr.getMenuListByPermissionAuth(rolesAuth);%0D%0A%09%09%09System.out.println(%22!!!!!!!!!!!!!!!!!!!%22+listMenu.size());%0D%0A%09%09%7D else if(roles.size() == 1)%7B%0D%0A%09%09%09for(String auth:roles.get(0).getPermissionSet())%7B%0D%0A%09%09%09%09System.out.println(%22*******************************%22 + auth);%0D%0A%09%09%09%7D
+%09%0D%0A%09%09%09listMenu = menuMgr.getMenuListByPermissionAuth(rolesAuth);%0D%0A%09%09%7D else if(roles.size() == 1)%7B
 %0D%0A%09%09
@@ -2302,71 +2302,8 @@
 );%0D%0A
-%09%09%09System.out.println(%22!!!!!!!!!!!!!!!!!!!%22+listMenu.size());%0D%0A
 %09%09%7D%0D

update menuservice