package com.eforce.baby.auth.delegates;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;
import java.util.ResourceBundle;
import org.apache.log4j.Logger;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;
import org.dom4j.Document;
import com.eforce.baby.auth.dao.MenuDAO;
import com.eforce.baby.auth.vo.MenuVO;
import com.eforce.baby.auth.vo.PrivilegeVO;
import com.eforce.baby.auth.vo.UserPrivilegeList;
import com.eforce.baby.common.config.ConfigurationManager;
import com.eforce.baby.common.dao.DAOException;
import com.eforce.baby.common.delegates.BaseDelegate;
import com.eforce.baby.common.factory.DAOFactory;
import com.eforce.baby.utils.IConstants;
public class MenuBD extends BaseDelegate
{
private static ArrayList adminElemns = null;
static
{
adminElemns = new ArrayList();
adminElemns.add(IConstants.REPORT_NAME_INTERNET_LINK);
adminElemns.add(IConstants.REPORT_NAME_SITE);
adminElemns.add(IConstants.REPORT_NAME_REF_DOC);
adminElemns.add(IConstants.REPORT_NAME_EMAILGROUP);
adminElemns.add(IConstants.REPORT_NAME_PROFILE);
adminElemns.add(IConstants.REPORT_NAME_TASK_TEMPLATE);
adminElemns.add(IConstants.REPORT_NAME_ORGANIZATION);
adminElemns.add(IConstants.REPORT_NAME_STAFFING_PLAN);
adminElemns.add(IConstants.REPORT_NAME_DATA_DICTIONARY);
adminElemns.add(IConstants.REPORT_NAME_USER);
adminElemns.add(IConstants.REPORT_NAME_ROLE);
adminElemns.add(IConstants.REPORT_NAME_DIST_GROUP);
adminElemns.add(IConstants.REPORT_NAME_GROUP);
adminElemns.add(IConstants.REPORT_NAME_CONFIG);
adminElemns.add(IConstants.REPORT_NAME_DATA_SHARING_CONF);
}
private Logger log = Logger.getLogger(this.getClass().getName());
public static final String ATTR_ID = "id";
public static final String ATTR_VAL_ID = "ID";
public static final String ATTR_POP = "pop";
public static final String ATTR_HREF = "href";
public static final String ATTR_LEAF = "leaf";
public static final String ATTR_NAME = "name";
public static final String ATTR_Q_NAME = "quickname";
public static final String ATTR_TARGET = "target";
public static final String ATTR_ENABLED = "enabled";
public static final String VALUE_YES = "Y";
public static final String VALUE_NO = "N";
public static final String VALUE_TRUE = "true";
public static final String VALUE_FALSE = "false";
public static final String TAG_ROOT = "ROOT";
public static final String FULL_MENU = "Switch to Full Menu";
public static final String QUICK_MENU = "Switch to Quick Menu";
public static final String WELCOME_PAGE = "welcome.jsp";
public static final String VALUE_CLOSED = "_CLOSED";
public Document createMenu(String dsName, String dbType, String menuType, String userId, UserPrivilegeList privList, Locale locale, String profileId) throws DAOException
{
Document doc = null;
if(menuType == null)
{
return null;
}
else if (menuType.equalsIgnoreCase(IConstants.PARAM_VAL_FULL_MENU))
{
MenuDAO menuDAO = this.getMenuDAO();
ArrayList list = menuDAO.findMenuItemsForUser(dsName, dbType, userId);
// Replace keys with values from the bundle
list = this.getValuesFromBundle(list, locale);
// remove menu items that the user does not have permissions to
list = this.removeMenuItems(list, privList);
log.debug("After first removal SIZE: " + list.size());
//log.debug(list);
//remove menu items that has no child
list = this.removeMenuItemsWithNoChild(list);
log.debug("After second removal SIZE: " + list.size());
doc = this.createFullMenu(dsName, dbType, userId, list);
this.setDefaultMenu(dsName, dbType, profileId, menuType);
}
else if (menuType.equalsIgnoreCase(IConstants.PARAM_VAL_QUICK_MENU))
{
MenuDAO menuDAO = this.getMenuDAO();
ArrayList list = menuDAO.findMenuItemsForUser(dsName, dbType, userId);
// Replace keys with values from the bundle
list = this.getValuesFromBundle(list, locale);
// remove menu items that the user does not have permissions to
list = this.removeMenuItems(list, privList);
//remove menu items that has no child
list = this.removeMenuItemsWithNoChild(list);
doc = this.createQuickMenu(dsName, dbType, userId, list);
this.setDefaultMenu(dsName, dbType, profileId, menuType);
}
else if (menuType.equalsIgnoreCase(IConstants.PARAM_VAL_MODIFY_MENU))
{
MenuDAO menuDAO = this.getMenuDAO();
ArrayList list = menuDAO.findAllMenuItemsForUser(dsName, dbType, userId);
// Replace keys with values from the bundle
list = this.getValuesFromBundle(list, locale);
// remove menu items that the user does not have permissions to
list = this.removeMenuItems(list, privList);
//remove menu items that has no child
list = this.removeMenuItemsWithNoChild(list);
doc = this.createFullMenu(dsName, dbType, userId, list);
}
else if (menuType.equalsIgnoreCase(IConstants.PARAM_VAL_SELECT_DEFAULT_MENU))
{
MenuDAO menuDAO = this.getMenuDAO();
ArrayList list = menuDAO.findMenuItemsForUser(dsName, dbType, userId);
// Replace keys with values from the bundle
list = this.getValuesFromBundle(list, locale);
// remove menu items that the user does not have permissions to
list = this.removeMenuItems(list, privList);
//remove menu items that has no child
list = this.removeMenuItemsWithNoChild(list);
doc = this.createFullMenu(dsName, dbType, userId, list);
}
return doc;
}
private Document createFullMenu(String dsName, String dbType, String userId, ArrayList list) throws DAOException
{
//holds temporary Elements to which child Elements can be added
HashMap map = new HashMap();
DocumentFactory df = DocumentFactory.getInstance();
Document doc = df.createDocument();
int count = 1;
for(int i=0; i < list.size(); i++)
{
MenuVO menu = (MenuVO) list.get(i);
if(menu.getMenuName().equals(TAG_ROOT))
{
Element elem = doc.addElement(menu.getMenuName());
elem = elem.addAttribute(ATTR_ID, ATTR_VAL_ID + 0);
elem = elem.addAttribute(ATTR_HREF, menu.getHyperlink());
map.put(menu.getMenuId(), elem);
count = count + 1;
}
else
{
if(menu.getMenuName().equalsIgnoreCase(FULL_MENU))
{
count = count + 1;
continue;
}
Element elem = ((Element) map.get(menu.getParentMenuId()));
elem = elem.addElement(menu.getTagName());
elem = elem.addAttribute(ATTR_ID, ATTR_VAL_ID + menu.getMenuId() + (menu.getLeaf().equalsIgnoreCase(VALUE_NO) ? VALUE_CLOSED : ""));
elem = elem.addAttribute(ATTR_NAME, menu.getMenuName());
elem = elem.addAttribute(ATTR_Q_NAME, menu.getQuickMenuName());
elem = elem.addAttribute(ATTR_HREF, menu.getHyperlink());
if(menu.getLeaf().equalsIgnoreCase(VALUE_YES))
{
elem = elem.addAttribute(ATTR_LEAF, VALUE_TRUE);
}
else if(menu.getLeaf().equalsIgnoreCase(VALUE_NO))
{
elem = elem.addAttribute(ATTR_LEAF, VALUE_FALSE);
}
elem = elem.addAttribute(ATTR_TARGET, menu.getTarget());
if(menu.getPopup().equalsIgnoreCase(VALUE_YES))
{
elem = elem.addAttribute(ATTR_POP, VALUE_TRUE);
}
else if(menu.getPopup().equalsIgnoreCase(VALUE_NO))
{
elem = elem.addAttribute(ATTR_POP, VALUE_FALSE);
}
if(menu !=null && menu.getEnabled().equalsIgnoreCase(VALUE_NO))
{
elem = elem.addAttribute(ATTR_ENABLED, VALUE_FALSE);
}
else
{
elem = elem.addAttribute(ATTR_ENABLED, VALUE_TRUE);
}
map.put(menu.getMenuId(), elem);
count = count + 1;
}
}
return doc;
}
private Document createQuickMenu(String dsName, String dbType, String userId, ArrayList list) throws DAOException
{
DocumentFactory df = DocumentFactory.getInstance();
Document doc = df.createDocument();
int count = 1;
//create the root element
Element elem1 = doc.addElement(TAG_ROOT);
elem1 = elem1.addAttribute(ATTR_ID, ATTR_VAL_ID + count++);
elem1 = elem1.addAttribute(ATTR_HREF, WELCOME_PAGE);
for(int i=0; i < list.size(); i++)
{
MenuVO menu = (MenuVO) list.get(i);
if(menu.getMenuName().equalsIgnoreCase(QUICK_MENU))
{
continue;
}
if (menu.getLeaf().equalsIgnoreCase(VALUE_YES))
{
Element elem = df.createElement(menu.getTagName());
elem = elem.addAttribute(ATTR_ID, ATTR_VAL_ID + count);
elem = elem.addAttribute(ATTR_NAME, menu.getQuickMenuName());
elem = elem.addAttribute(ATTR_HREF, menu.getHyperlink());
if(menu.getLeaf().equalsIgnoreCase(VALUE_YES))
{
elem = elem.addAttribute(ATTR_LEAF, VALUE_TRUE);
}
else if(menu.getLeaf().equalsIgnoreCase(VALUE_NO))
{
elem = elem.addAttribute(ATTR_LEAF, VALUE_FALSE);
}
elem = elem.addAttribute(ATTR_TARGET, menu.getTarget());
if(menu.getPopup().equalsIgnoreCase(VALUE_YES))
{
elem = elem.addAttribute(ATTR_POP, VALUE_TRUE);
}
else if(menu.getPopup().equalsIgnoreCase(VALUE_NO))
{
elem = elem.addAttribute(ATTR_POP, VALUE_FALSE);
}
if(menu.getEnabled().equalsIgnoreCase(VALUE_NO))
{
elem = elem.addAttribute(ATTR_ENABLED, VALUE_FALSE);
}
else
{
elem = elem.addAttribute(ATTR_ENABLED, VALUE_TRUE);
}
elem1.add(elem);
count = count + 1;
}
}
return doc;
}
public int enableMenuItems(String dsName, String dbType, String userId, String menuList) throws DAOException
{
log.debug("enableMenuItems() : ENTER");
MenuDAO menuDAO = this.getMenuDAO();
log.debug("enableMenuItems() : EXIT");
return menuDAO.enableMenuItems(dsName, dbType, userId, menuList);
}
public int disableMenuItems(String dsName, String dbType, String userId, String menuId) throws DAOException
{
log.debug("disableMenuItems() : ENTER");
MenuDAO menuDAO = this.getMenuDAO();
log.debug("disableMenuItems() : EXIT");
return menuDAO.disableMenuItems(dsName, dbType, userId, menuId);
}
public void setDefaultPage(String dsName, String dbType, String profileId, String page) throws DAOException
{
log.debug("setDefaultPage() : ENTER");
MenuDAO menuDAO = this.getMenuDAO();
menuDAO.setDefaultPage(dsName, dbType, profileId, page);
log.debug("setDefaultPage() : EXIT");
}
public void setDefaultMenu(String dsName, String dbType, String profileId, String menuType) throws DAOException
{
log.debug("setDefaultMenu() : ENTER");
MenuDAO menuDAO = this.getMenuDAO();
menuDAO.setDefaultMenu(dsName, dbType, profileId, menuType);
log.debug("setDefaultMenu() : EXIT");
}
private MenuDAO getMenuDAO()
{
DAOFactory factory = DAOFactory.getInstance();
return ((MenuDAO) factory.getDAO(IConstants.CLASS_MENU_DAO));
}
private boolean hasPrivilege(String tagName, String privilegeName, UserPrivilegeList privList)
{
PrivilegeVO priv = null;
if(privilegeName.equalsIgnoreCase("event"))
{
privilegeName = IConstants.REPORT_NAME_EMERGENCY_EVENT;
}
priv = privList.getPrivilegeByName(privilegeName);
if (priv == null)
{
return false;
}
if(tagName.equalsIgnoreCase("CreateMenu"))
{
if(adminElemns.contains(priv.getPrivilegeName()))
{
if (priv.hasManagerAccess())
{
return true;
}
}
else
{
if ( priv.hasAuthorAccess() || priv.hasEditorAccess() || priv.hasManagerAccess())
{
return true;
}
}
}
else if(tagName.equalsIgnoreCase("SubMenu"))
{
if (priv.hasReadAccess() || priv.hasAuthorAccess() || priv.hasEditorAccess() || priv.hasManagerAccess())
{
return true;
}
}
return false;
}
private ArrayList removeMenuItems(ArrayList list, UserPrivilegeList privList)
{
ArrayList permList = new ArrayList();
// remove those menu items that the user does not have access to
for (int i=0; i < list.size(); i++)
{
MenuVO menu = (MenuVO) list.get(i);
if (menu.getPrivilegeId().length() > 0)
{
if (hasPrivilege(menu.getTagName(), menu.getPrivilegeName(), privList) == false)
{
continue;
}
}
permList.add(menu);
}
return permList;
}
private ArrayList removeMenuItemsWithNoChild(ArrayList list)
{
ArrayList temp = (ArrayList) list.clone();
// remove those menu items that has no child
ArrayList newList = new ArrayList();
for (int i=0; i < list.size(); i++)
{
String menuId = "";
MenuVO menu = (MenuVO) list.get(i);
//log.debug(menu);
if(menu.getLeaf().equalsIgnoreCase(IConstants.NO))
{
menuId = menu.getMenuId();
int count = 0;
for(int j=0; j < temp.size(); j++)
{
String parentId = ((MenuVO)temp.get(j)).getParentMenuId();
if(menuId.equals(parentId))
{
count ++;
}
}
//log.debug(menuId + ", menuName: " + menu.getMenuName() + ", childcount: " + count);
if(count > 0)
{
newList.add(menu);
}
}
else
{
newList.add(menu);
continue;
}
}
ArrayList idsToBeRemoved = new ArrayList();
//check again on the nodes
for (int i=0; i < newList.size(); i++)
{
MenuVO menu = (MenuVO) newList.get(i);
//log.debug("id: " + menu.getMenuId() + ", name: " + menu.getMenuName());
if(menu.getLeaf().equalsIgnoreCase(IConstants.NO))
{
//log.debug("entered....");
String menuId = menu.getMenuId();
int count = 0;
for(int j=0; j < newList.size(); j++)
{
String parentId = ((MenuVO)newList.get(j)).getParentMenuId();
if(menuId.equals(parentId))
{
count ++;
}
}
//log.debug("id: " + menu.getMenuId() + ", name: " + menu.getMenuName() + ", count: " + count);
if(count == 0)
{
idsToBeRemoved.add(newList.get(i));
}
}
}
log.debug("Items to be removed: " + idsToBeRemoved);
//log.debug("newList.size(): " + newList.size());
for(int i=0; i < idsToBeRemoved.size(); i++)
{
newList.remove(idsToBeRemoved.get(i));
}
return newList;
}
private ArrayList getValuesFromBundle(ArrayList list, Locale loc)
{
ConfigurationManager conf = ConfigurationManager.getInstance();
String resFileName = conf.getString(IConstants.EEMS_RESOURCES_FILENAME);
ResourceBundle bundle = ResourceBundle.getBundle(resFileName, loc);
ArrayList bundleList = new ArrayList();
String key = "";
for(int i=0; i < list.size(); i++)
{
MenuVO menu = (MenuVO) list.get(i);
if(menu.getMenuName() != null && menu.getMenuName().trim().length() > 0)
{
key = menu.getMenuName();
menu.setMenuName(bundle.getString(key));
}
if(menu.getQuickMenuName() != null && menu.getQuickMenuName().trim().length() > 0)
{
key = menu.getQuickMenuName();
menu.setQuickMenuName(bundle.getString(key));
}
bundleList.add(menu);
}
return bundleList;
}
}