com.alogient.cameleon.sdk.content.service.impl.MenuServiceImpl Maven / Gradle / Ivy
The newest version!
package com.alogient.cameleon.sdk.content.service.impl;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alogient.cameleon.sdk.common.dao.model.Culture;
import com.alogient.cameleon.sdk.content.dao.CultureDao;
import com.alogient.cameleon.sdk.content.dao.MenuDao;
import com.alogient.cameleon.sdk.content.dao.model.Element;
import com.alogient.cameleon.sdk.content.dao.model.Nav;
import com.alogient.cameleon.sdk.content.dao.model.Page;
import com.alogient.cameleon.sdk.content.service.MenuService;
import com.alogient.cameleon.sdk.content.url.UrlMenuGenerator;
import com.alogient.cameleon.sdk.content.util.InternalCameleonConfig;
import com.alogient.cameleon.sdk.content.util.exception.UnknownMenuException;
import com.alogient.cameleon.sdk.content.vo.CultureVo;
import com.alogient.cameleon.sdk.content.vo.menu.BreadcrumbsVo;
import com.alogient.cameleon.sdk.content.vo.menu.MenuVo;
import com.alogient.cameleon.sdk.membership.dao.MembershipDao;
import com.alogient.cameleon.sdk.membership.dao.model.Pagegroupaccess;
public class MenuServiceImpl implements MenuService {
/**
* Logger for this class
*/
private final transient Logger log = LoggerFactory.getLogger(getClass());
/**
* FIXME Temporary Root identifier
*/
public static final Integer ROOT_MENU_NAVID = 0;
/**
* Menu data access object
*/
private MenuDao menuDao;
/**
* Culture data access object
*/
private CultureDao cultureDao;
/**
* Membership data access object
*/
private MembershipDao membershipDao;
/**
* @param menuDao the menuDao to set
*/
public void setMenuDao(MenuDao menuDao) {
this.menuDao = menuDao;
}
/**
* @param cultureDao cultureDao to set
*/
public void setCultureDao(CultureDao cultureDao) {
this.cultureDao = cultureDao;
}
/**
* @param membershipDao membershipDao to set
*/
public void setMembershipDao(MembershipDao membershipDao) {
this.membershipDao = membershipDao;
}
/**
* {@inheritDoc}
*/
@Override
public MenuVo getMenu(String name, CultureVo cultureVo, Boolean isLiveMode, Boolean isSecurityAware) throws UnknownMenuException {
/**
* FIXME Reimplement with the correct DB structure (once it exists) This is a temporary implementation while we
* wait for a menu structure to be created in the data base.
*/
Nav menuRoot;
if (name == null) {
menuRoot = menuDao.getNav(ROOT_MENU_NAVID);
} else {
throw new UnknownMenuException("Menu '" + name + "' doesn't exist.");
}
if (menuRoot == null) {
throw new UnknownMenuException("Root menu doesn't exist. (Something smells fishy, possible DB corruction ?)");
}
Culture culture = cultureDao.getCulture(cultureVo.getCultureId());
if (log.isDebugEnabled()) {
log.debug("About to build menu");
}
UrlMenuGenerator urlMenuGenerator = null;
try {
urlMenuGenerator = (UrlMenuGenerator) Class.forName(
InternalCameleonConfig.getString(InternalCameleonConfig.URL_MENU_GENERATOR_CLASS, "com.alogient.cameleon.sdk.content.url.SEO2Generator")).newInstance();
} catch (InstantiationException e) {
log.error(e.getMessage(), e);
throw new UnknownMenuException(e);
} catch (IllegalAccessException e) {
log.error(e.getMessage(), e);
throw new UnknownMenuException(e);
} catch (ClassNotFoundException e) {
log.error(e.getMessage(), e);
throw new UnknownMenuException(e);
}
MenuVo menu = getMenuFromNav(urlMenuGenerator, null, menuRoot, culture, isLiveMode, isSecurityAware);
// BEARWATCH : Don't ask
if (menu.getSubMenu() != null && menu.getSubMenu().size() > 0) {
MenuVo home = menu.getSubMenu().get(0);
Set toRemove = new HashSet();
for (MenuVo sub : home.getSubMenu()) {
if (sub.getBreadcrumbs().getIsPageInMenu()) {
menu.getSubMenu().add(sub);
toRemove.add(sub);
}
}
home.getSubMenu().removeAll(toRemove);
for (MenuVo subMenu : menu.getSubMenu()) {
subMenu.setParent(menu);
}
}
return menu;
}
/**
* Utility method used to transform the navigation structure into a menu structure. It is obsolete on first creation
*
* @param parent What is the parent of this menu item
* @param srcNav What navigation node do we build the navigation from
* @param culture What culture should the menu be built for
* @param isLiveMode Should the menu be built for the live mode or the preview mode
* @param isSecurityAware Should the security information be embeded into the menu
* @return The corresponding menu
*/
@Deprecated
private MenuVo getMenuFromNav(UrlMenuGenerator urlMenuGenerator, MenuVo parent, Nav srcNav, Culture culture, boolean isLiveMode, boolean isSecurityAware) {
MenuVo menuVo = new MenuVo();
menuVo.setParent(parent);
BreadcrumbsVo breadcrumbs = null;
Page srcPage = null;
if (srcNav.getPages() != null) {
for (Page page : srcNav.getPages()) {
if (culture.equals(page.getCulture())) {
if (isPageVisible(page, isLiveMode) || parent == null) {
srcPage = page;
}
}
}
}
boolean isVirtual = false;
Nav realNav = null;
if (srcNav != null && srcNav.getTemplate() != null && "Virtual".equals(srcNav.getTemplate().getTemplateName())) {
isVirtual = true;
if (srcNav.getPages() != null) {
for (Page page : srcNav.getPages()) {
if (culture.equals(page.getCulture())) {
if (isPageVisible(page, isLiveMode) || parent == null) {
for (Element element : page.getElements()) {
if ("targetNavId".equals(element.getPart().getPartName())) {
if (StringUtils.isNotBlank(isLiveMode ? element.getValue() : element.getPreviewValue())
&& StringUtils.isNumeric(isLiveMode ? element.getValue() : element.getPreviewValue())) {
realNav = menuDao.getNav(new Integer(isLiveMode ? element.getValue() : element.getPreviewValue()));
break;
}
}
}
}
}
}
}
}
Page realPage = null;
if (realNav != null && realNav.getPages() != null) {
for (Page page : realNav.getPages()) {
if (culture.equals(page.getCulture())) {
if (isPageVisible(page, isLiveMode) || parent == null) {
realPage = page;
break;
}
}
}
}
if (srcPage != null) {
breadcrumbs = new BreadcrumbsVo();
breadcrumbs.setNavId(srcPage.getNav().getNavId());
breadcrumbs.setPageName(isLiveMode ? srcPage.getPageName() : srcPage.getPageNamePreview());
breadcrumbs.setIsPageInMenu(srcPage.getPageNavEnd() != null && !srcPage.getPageNavEnd());
if (isVirtual && realPage != null) {
breadcrumbs.setPageId(realPage.getPageId());
breadcrumbs.setLastModified(realPage.getLastModified());
} else {
breadcrumbs.setPageId(srcPage.getPageId());
breadcrumbs.setLastModified(srcPage.getLastModified());
}
if (isSecurityAware) {
Nav toProcess = srcNav;
do {
List pageGroupAccesses = membershipDao.getRolesForNavId(toProcess.getNavId());
for (Pagegroupaccess pageGroupAccess : pageGroupAccesses) {
breadcrumbs.addRole(pageGroupAccess.getPk().getRolename());
}
toProcess = toProcess.getParent();
} while (toProcess != null);
}
// Template should never be null except for the root
// nav.
if (isVirtual && realNav != null) {
if (realNav.getTemplate() != null) {
breadcrumbs.setTemplateName(realNav.getTemplate().getTemplateName());
}
breadcrumbs.setIsVisible(isPageVisible(realPage, isLiveMode));
} else {
if (srcNav.getTemplate() != null) {
breadcrumbs.setTemplateName(srcNav.getTemplate().getTemplateName());
}
breadcrumbs.setIsVisible(isPageVisible(srcPage, isLiveMode));
}
if (parent == null) {
breadcrumbs.setNavigateUrl("ROOT");
} else if (parent.getBreadcrumbs().getNavigateUrl().equals("ROOT")) {
breadcrumbs.setNavigateUrl(culture.getCultureCode());
} else {
if ("NoNav".equals(srcPage.getNav().getTemplate().getTemplateName())) {
breadcrumbs.setNavigateUrl(parent.getBreadcrumbs().getNavigateUrl());
// String url = urlMenuGenerator
// .generatePageUrl(parent.getBreadcrumbs().getNavigateUrl(), isLiveMode ? srcPage.getPageName() :
// srcPage.getPageNamePreview(), null);
// breadcrumbs.setNavigateUrl(url);
} else {
String url = urlMenuGenerator
.generatePageUrl(parent.getBreadcrumbs().getNavigateUrl(), isLiveMode ? srcPage.getPageName() : srcPage.getPageNamePreview(), null);
breadcrumbs.setNavigateUrl(url);
}
}
}
if (log.isDebugEnabled()) {
StringBuffer sb = new StringBuffer("NavId: ");
sb.append(srcNav.getNavId());
sb.append(" Culture: ");
sb.append(culture.getCultureCode());
sb.append(" IsLive: ");
sb.append(isLiveMode);
if (breadcrumbs != null) {
sb.append(" Url: ");
sb.append(breadcrumbs.getNavigateUrl());
}
log.debug("Building menu for: " + sb.toString());
}
if (breadcrumbs == null && parent != null) {
return null;
} else {
menuVo.setBreadcrumbs(breadcrumbs);
// Build the sub menu
if (isVirtual && realNav != null) {
if (realNav.getChildrenNav() != null) {
for (Nav childNav : realNav.getChildrenNav()) {
MenuVo childMenu = getMenuFromNav(urlMenuGenerator, menuVo, childNav, culture, isLiveMode, isSecurityAware);
if (childMenu != null) {
menuVo.addMenu(childMenu);
}
}
}
} else {
if (srcNav.getChildrenNav() != null) {
for (Nav childNav : srcNav.getChildrenNav()) {
MenuVo childMenu = getMenuFromNav(urlMenuGenerator, menuVo, childNav, culture, isLiveMode, isSecurityAware);
if (childMenu != null) {
menuVo.addMenu(childMenu);
}
}
}
}
if (srcNav != null && srcNav.getTemplate() != null && "NoNav".equals(srcPage.getNav().getTemplate().getTemplateName())) {
if (menuVo.getSubMenu() != null && menuVo.getSubMenu().size() != 0) {
breadcrumbs.setNavigateUrl(menuVo.getSubMenu().get(0).getBreadcrumbs().getNavigateUrl());
} else {
breadcrumbs.setNavigateUrl("#");
}
}
return menuVo;
}
}
/**
* Checks if the page is visible
*
* @param page The page to check
* @param isLiveMode The mode to check in
* @return True if the page is visible
*/
private boolean isPageVisible(Page page, boolean isLiveMode) {
if (page == null) {
return false;
}
return page.isVisible(isLiveMode);
}
/**
* {@inheritDoc}
*/
@Override
public MenuVo getRootMenu(CultureVo cultureVo, Boolean isLiveMode, Boolean isSecurityAware) throws UnknownMenuException {
return getMenu(null, cultureVo, isLiveMode, isSecurityAware);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy