Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

How to create RIchfaces Menu dynamically from database?

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I have to create Menus from database(dynamically).For horizontal menu i am able to create using rich:toolbar.But Vertical menus how to create?If any one come out of the problem help me.From database i am able to read total menu size in menuInterface java file.my files are listed below.

My jsp:

<rich:dropDownMenu binding="#{loginBean.richMenu}">
</rich:dropDownMenu>

Login Bean:

public class LoginBean {

private static Log log = LogFactory.getLog(LoginBean.class);
private ResourceBundle ppsResources = ResourceBundle.getBundle("ApplicationResources");

private List<NavigationMenuItem> menu;
//private HtmlToolBar richMenu;
private HtmlDropDownMenu richMenu;
private MenuInterface menuInterface;


public LoginBean()
{
System.out.print("Calling Cons.............");
menuInterface = new MenuInterface();
}

/* Getter Setter methods */

public List<NavigationMenuItem> getMenu() {
return menu;
}

public void setMenu(List<NavigationMenuItem> menu) {
this.menu = menu;
}

public HtmlDropDownMenu getRichMenu() {
return richMenu;
}

public void setRichMenu(HtmlDropDownMenu richMenu) {
this.richMenu = richMenu;
}

}

MenuInterface.Java:

public class MenuInterface {

private static Log log = LogFactory.getLog(MenuInterface.class);
private List<NavigationMenuItem> menu;
//private HtmlToolBar richToolbar;
private HtmlDropDownMenu richToolbar;

public MenuInterface() {

}

private List<NavigationMenuItem> generateMenu(int userSerial) {
//List cache = ServiceDao.menuListExecute().fetchMenuByRole(userSerial);
List cache = new ArrayList<String>();
return engineMenu(cache);

}

private List<NavigationMenuItem> generateMenu() {
List cache = ServiceDao.menuListExecute().fetchMenuSortData();
return engineMenu(cache);
}

private HtmlDropDownMenu generateRichMenu() {
List cache = ServiceDao.menuListExecute().findByExample();
//List cache = ServiceDao.menuListExecute().fetchMenuSortData();
return engineRichMenu(cache);
}

private HtmlDropDownMenu generateRichMenu(int userSerial) {
//List cache = ServiceDao.menuListExecute().findByExample();
//List cache = ServiceDao.menuListExecute().fetchMenuByRole(userSerial);
List cache = ServiceDao.menuListExecute().findMenuByRoleExample(userSerial);
//List cache = ServiceDao.menuListExecute().fetchMenuSortData();
return engineRichMenu(cache);

}

private HtmlDropDownMenu engineRichMenu(List cache) {

log.debug("engine Rich Menu starting...");
//List<NavigationMenuItem> rootMenu = null;
HtmlDropDownMenu toolbar = new HtmlDropDownMenu();
int tabSpaces=0;


NavigationMenuItem parentMenu;
List<HtmlMenuItem> childMenu;

log.info("Menu List Size = " + cache.size());
//rootMenu = new ArrayList<NavigationMenuItem>();

int i = 0;
while(i<cache.size()){
//while (cache.size() > 0) {
//Menu menu = getDataMenu(cache, i);
Menu menu = (Menu)cache.get(i);

if ((menu.getParent() == 0) && !menu.getStatus().equals("ADDED")){
log.debug("STATUS ==== "+menu.getStatus());
HtmlDropDownMenu rootMenu = new HtmlDropDownMenu();
rootMenu.setValue(menu.getLabel());
rootMenu.setLabelClass("celcomMenuLabel");
rootMenu.setStyleClass("celcomMenuLabel");

log.debug("Root Menu === " + menu.getSerial() + "|"
+ menu.getDescription() + "|" + menu.getParent());

//parentMenu = menuInfo(menu);
//rootMenu.add(parentMenu);
menu.setStatus("ADDED");
cache.set(i,menu);
//cache.remove(i);
//i--;
log.debug("Menu List Size after remove = " + cache.size());
Integer ii = new Integer(i);
childMenu = recursiveChildRichMenu(tabSpaces+10,ii,cache, menu.getSerial());
rootMenu.getChildren().addAll(childMenu);
//parentMenu.setNavigationMenuItems((List<HtmlMenuItem>) childMenu);
toolbar.getChildren().add(rootMenu);

} else {
log.debug("Not Root Menu..."+menu.getLabel());
log.debug("STATUS ==== "+menu.getStatus());

}

i++;
// if (i >= cache.size())
// i = 0;
}
log.debug("engine Menu end...");
return toolbar;
}

private List<HtmlMenuItem> recursiveChildRichMenu(int tabSpaces, Integer upperI, List recursiveData,
int parentID) {

int beforeI;
List parentMenuList = null;

parentMenuList = new ArrayList();

log.debug(whiteSpace(tabSpaces)+"["+ parentID +"]"+ "<--- Parent ID ************************************************ [ START ]");
HtmlMenuItem parentMenu = null;
HtmlMenuGroup parentGroupMenu = null;
List<HtmlMenuItem> childMenu;
int i = 0;

while (i < recursiveData.size()) {
//Menu menu = getDataMenu(recursiveData, i);
Menu menu = (Menu)recursiveData.get(i);
if ((menu.getParent() == parentID) && !menu.getStatus().equals("ADDED")) {

log.debug("STATUS ==== "+menu.getStatus());
log.debug(whiteSpace(tabSpaces)+"Child Menu Found [" + i + "] === " + menu.getSerial() + "|"
+ menu.getDescription() + "|" + menu.getParent());

parentMenu = richMenuInfo(menu);
HtmlActionParameter menuItemParam = new HtmlActionParameter();
menuItemParam.setId("includePage"+Integer.toString(menu.getSerial()));
menuItemParam.setName("includePage");
menuItemParam.setValue(menu.getAction());
parentMenu.getChildren().add(menuItemParam);
parentMenu.setReRender("contentPage");

parentGroupMenu = richMenuGroupInfo(menu);

//recursiveData.remove(i);

menu.setStatus("ADDED");
recursiveData.set(i, menu);
//log.debug(whiteSpace(tabSpaces)+"Menu List Size after remove = "+ recursiveData.size());
log.debug(whiteSpace(tabSpaces)+"upperI ======= "+upperI +" current i ===="+i);


//i--;
beforeI = i;
log.debug(whiteSpace(tabSpaces)+"before i ======= "+i);
childMenu = recursiveChildRichMenu(tabSpaces+10,i,recursiveData, menu.getSerial());

log.debug(whiteSpace(tabSpaces)+"after i ======= "+i);
if(childMenu.size()>0){
//log.debug(whiteSpace(tabSpaces)+"Child Found = "+childMenu.size());
parentMenuList.add(parentGroupMenu);
parentGroupMenu.getChildren().addAll(childMenu);
parentMenu = null;
}else{
//log.debug(whiteSpace(tabSpaces)+"No Child Found... ");
Class[] params = {};

FacesContext ctx = FacesContext.getCurrentInstance();
Application app = ctx.getApplication();
MethodExpression actionExpression = app.getExpressionFactory().createMethodExpression(ctx.getELContext(),"#{menuActionBean.actionInclude}",String.class, params);
parentMenu.setActionExpression(actionExpression);

parentMenuList.add(parentMenu);
parentGroupMenu = null;
}



} else {
log.debug("STATUS ==== "+menu.getStatus());
log.debug(whiteSpace(tabSpaces)+"Not It Child [" + i + "] === " + menu.getSerial() + "|"
+ menu.getDescription() + "|" + menu.getParent());
}
i++;
}
log.debug(whiteSpace(tabSpaces)+"["+ parentID +"]"+ "<--- Parent ID ************************************************ [ END ]");
return parentMenuList;
}

private HtmlMenuItem richMenuInfo(Menu menu) {

HtmlMenuItem htmlMenuItem = new HtmlMenuItem();
//htmlMenuItem.setAction(menu.getAction());
htmlMenuItem.setValue(menu.getLabel());
htmlMenuItem.setSubmitMode("ajax");
htmlMenuItem.setRendered(true);
htmlMenuItem.setAjaxSingle(true);
htmlMenuItem.setLabelClass("celcomMenuLabel");
htmlMenuItem.setStyleClass("celcomMenuLabel");
//htmlMenuItem.setOnclick(_onclick)
//htmlMenuItem.setId(Integer.toString(menu.getSerial()));

//menuItem.setAction(menu.getAction());
//menuItem.setActionListener(menu.getActionListerner());
//menuItem.setLabel(menu.getLabel());
//menuItem.setDescription(menu.getDescription());
// menuItem.setExternalLink();
//menuItem.setActive(true);
//menuItem.setOpen(true);
//menuItem.setTarget(menu.getTarget());

return htmlMenuItem;
}

private HtmlMenuGroup richMenuGroupInfo(Menu menu) {

HtmlMenuGroup htmlMenuGroup = new HtmlMenuGroup();
//htmlMenuItem.setAction(menu.getAction());
htmlMenuGroup.setValue(menu.getLabel());
htmlMenuGroup.setLabelClass("celcomMenuLabel");
htmlMenuGroup.setStyleClass("celcomMenuLabel");
//htmlMenuGroup.setId(Integer.toString(menu.getSerial()));
//menuItem.setAction(menu.getAction());
//menuItem.setActionListener(menu.getActionListerner());
//menuItem.setLabel(menu.getLabel());
//menuItem.setDescription(menu.getDescription());
// menuItem.setExternalLink();
//menuItem.setActive(true);
//menuItem.setOpen(true);
//menuItem.setTarget(menu.getTarget());

return htmlMenuGroup;
}


private String whiteSpace(int count){
String spacing="";
for(int i=0;i<count;i++){
if(i==count-10)
spacing = spacing + "|";
else
spacing = spacing + " ";
}
return spacing;
}
}

My MenuDao:

public List findMenuByRoleExample(int userSerial) {
log.debug("finding Menu instance by example");
List results=null;
List menu = new ArrayList();
try {

/*results = getSession().createCriteria(UserProfile.class)
.add(Restrictions.eq("serial", userSerial))
.list();

UserProfile userProfile = (UserProfile)results.get(0);
log.debug("userProfile.getUserRole().getId() = " + userProfile.getUserRole().getId());*/
results = getSession().createCriteria(Menu.class)
.addOrder(Order.asc("parent"))
.addOrder(Order.asc("sequence"))
//.createCriteria("menuRoleAccesses")
//.add(Restrictions.eq("userRole.id", ))
.list();

log.debug("find by example successful, result size: " + results.size());


return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}finally{
getCloseSession();
}
//return results;
}

....................................

public String systemGatewayLogin() {
System.out.println("Loading systemGatewayLogin.....");
log.debug("Entering into systemGatewayLogin....");
setRichMenu(menuInterface.getRichMenu(2));
log.debug("ChildCount:"+getRichMenu().getChildCount());
//setInteractionType(menuInterface.getInteractionType());
AppSession.CreateNewSession();
AppSession.AppTimeOut();
AppSession.AppSessionSetAttribute("username","siva");
//AppSession.AppSessionSetAttribute("fullname",login.getUserProfile().getFullname());
AppSession.AppSessionSetAttribute("userserial","0");
return "success";
}



In Jsp page it is displaying empty.Nothing it is diaplaying.Any one help me about this problem.
Menus.JPG
[Thumbnail for Menus.JPG]
Menus
Filename: menuwithsub.bmp
Description: Origial Menus
File size: 218 Kbytes
 
All of the following truths are shameless lies. But what about this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic