Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
eu.ginere.base.web.menu.MenuManager Maven / Gradle / Ivy
package eu.ginere.base.web.menu;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import eu.ginere.base.util.i18n.Language;
import eu.ginere.base.util.properties.FileProperties.PropertiesChangedLister;
import eu.ginere.base.util.properties.GlobalFileProperties;
import eu.ginere.base.web.connectors.i18n.I18NConnector;
import eu.ginere.base.web.connectors.rights.RightConnector;
public class MenuManager {
private static PropertiesChangedListerImpl SINGLETON=new PropertiesChangedListerImpl();
private static final Hashtable > cache=new Hashtable >();
private static final Hashtable >> i18nCache=new Hashtable >>();
private static final String MENU_SECTION = "MENU_LABELS_SECTION";
private static List getI18nCacheMenuItem(String id,Language lang){
Map > langCache;
if (i18nCache.containsKey(lang)){
langCache=i18nCache.get(lang);
} else {
langCache=new Hashtable >();
i18nCache.put(lang,langCache);
}
if (langCache.containsKey(id)){
return langCache.get(id);
} else {
List orig=getCacheMenuItem(id);
List translated=translateMenuItemList(orig,lang);
langCache.put(id,translated);
return translated;
}
}
public static void clearI18NCache() {
i18nCache.clear();
}
private static List getCacheMenuItem(String id){
if (cache.containsKey(id)){
return cache.get(id);
} else {
String subMenuArray[]=GlobalFileProperties.getPropertyList(MenuManager.class,"MenuItemList_"+id);
List menu=new ArrayList(subMenuArray.length);
for(String element:subMenuArray){
String label=GlobalFileProperties.getStringValue(MenuManager.class, element+"_Label","Item Not Found");
String right=GlobalFileProperties.getStringValue(MenuManager.class, element+"_Right",null);
String url=GlobalFileProperties.getStringValue(MenuManager.class, element+"_URL",null);
String cls=GlobalFileProperties.getStringValue(MenuManager.class, element+"_CLS",null);
boolean window=GlobalFileProperties.getBooleanValue(MenuManager.class, element+"_WINDOW",false);
boolean isSeparator=GlobalFileProperties.getBooleanValue(MenuManager.class, element+"_SEP",false);
List itemList=getItemList(element);
MenuItem menuItem=new MenuItem(element,label,right,url,cls,isSeparator,window,itemList);
menu.add(menuItem);
}
cache.put(id,menu);
return menu;
}
}
private static List getItemList(String id){
String itemsArray[]=GlobalFileProperties.getPropertyList(MenuManager.class,id+"_ItemList");
List menu=new ArrayList(itemsArray.length);
for(String element:itemsArray){
String label=GlobalFileProperties.getStringValue(MenuManager.class, element+"_Label","Item Not Found");
String right=GlobalFileProperties.getStringValue(MenuManager.class, element+"_Right",null);
String url=GlobalFileProperties.getStringValue(MenuManager.class, element+"_URL",null);
String cls=GlobalFileProperties.getStringValue(MenuManager.class, element+"_CLS",null);
boolean isSeparator=GlobalFileProperties.getBooleanValue(MenuManager.class, element+"_SEP",false);
boolean window=GlobalFileProperties.getBooleanValue(MenuManager.class, element+"_WINDOW",false);
List itemList=getItemList(element);
MenuItem menuItem=new MenuItem(element,label,right,url,cls,isSeparator,window,itemList);
menu.add(menuItem);
}
return menu;
}
public static List getMenuItem(String id,String userId){
List menu = getCacheMenuItem(id);
return checkRights(menu, userId);
}
public static List getI18nMenuItem(String id, String userId,
Language langId) {
List menu = getI18nCacheMenuItem(id,langId);
return checkRights(menu, userId);
}
private static List checkRights(List menu,String userId){
// compruebo si el usuario tiene los derechos
List menuRight = new ArrayList(menu.size());
for (MenuItem element : menu) {
if (element.hasChilds()){
List childs=checkRights(element.getItemList(), userId);
if (childs.size()>0){
menuRight.add(new MenuItem(element,childs));
} else if (RightConnector.hasRight(userId, element.getRight())) {
menuRight.add(new MenuItem(element));
}
} else if (RightConnector.hasRight(userId, element.getRight())) {
menuRight.add(element);
}
}
return menuRight;
}
public static List translateMenuItemList(List list,Language langId){
List ret=new ArrayList(list.size());
for (MenuItem menuItem:list){
MenuItem translatedMenu=translateMenuItem(menuItem,langId);
ret.add(translatedMenu);
}
return ret;
}
public static MenuItem translateMenuItem(MenuItem menuItem,Language langId){
List childs=menuItem.getItemList();
List translatedChilds=translateMenuItemList(childs,langId);
String trasnlatedlabel=I18NConnector.getLabel(langId, MENU_SECTION, menuItem.getLabel());
return new MenuItem(menuItem.getId(),trasnlatedlabel,menuItem.getRight(),menuItem.getUrl(),menuItem.getCls(),menuItem.isSeparator(),menuItem.isWindow(),translatedChilds);
}
static private class PropertiesChangedListerImpl implements PropertiesChangedLister{
private PropertiesChangedListerImpl(){
GlobalFileProperties.subscriveToPropertiesChanged(this);
}
/**
* @param lastModified fecha de la modificacion precedente.
* Si no ha habido, es la primera vez que se carga el fichero esta fecha es 0.
* Todos los listeners son llamados desde el mismo hilo si alguno se bloquea o
* tarda demasiado la aplicacion quedara bloqueada. Por favor si tienes que hacer
* muchas cosas crea un Hilo
*/
public void propertiesChanged(long lastModified){
cache.clear();
}
}
}