diff --git a/src/java/fr/paris/lutece/plugins/menus/web/AbstractMainTreeMenuInclude.java b/src/java/fr/paris/lutece/plugins/menus/web/AbstractMainTreeMenuInclude.java new file mode 100644 index 0000000..b649363 --- /dev/null +++ b/src/java/fr/paris/lutece/plugins/menus/web/AbstractMainTreeMenuInclude.java @@ -0,0 +1,295 @@ +/* + * Copyright (c) 2002-2015, Mairie de Paris + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright notice + * and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice + * and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * License 1.0 + */ +package fr.paris.lutece.plugins.menus.web; + +import fr.paris.lutece.plugins.menus.business.MenuItem; +import fr.paris.lutece.plugins.menus.service.MenusService; +import fr.paris.lutece.portal.business.page.Page; +import fr.paris.lutece.portal.business.page.PageHome; +import fr.paris.lutece.portal.service.cache.AbstractCacheableService; +import fr.paris.lutece.portal.service.content.PageData; +import fr.paris.lutece.portal.service.includes.PageInclude; +import fr.paris.lutece.portal.service.page.PageEvent; +import fr.paris.lutece.portal.service.page.PageEventListener; +import fr.paris.lutece.portal.service.page.PageService; +import fr.paris.lutece.portal.service.portal.PortalService; +import fr.paris.lutece.portal.service.template.AppTemplateService; +import fr.paris.lutece.portal.service.util.AppLogService; +import fr.paris.lutece.portal.service.util.AppPropertiesService; +import fr.paris.lutece.portal.web.constants.Parameters; +import fr.paris.lutece.util.html.HtmlTemplate; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; + + +/** + * MainTreeMenuInclude + */ +public abstract class AbstractMainTreeMenuInclude extends AbstractCacheableService implements PageInclude, PageEventListener +{ + ///////////////////////////////////////////////////////////////////////////////////////////// + // Constants + + //Templates + private static final String TEMPLATE_MENU_PAGES = "skin/plugins/menus/main_tree_pages_list.html"; + private static final String TEMPLATE_MENU_PAGES_TREE = "skin/plugins/menus/main_tree_pages_list_tree.html"; + + // Parameters + private final static String PARAMETER_CURRENT_PAGE_ID = "current_page_id"; + + // Markers + private static final String MARK_MENU = "menu"; + private static final String MARK_CURRENT_PAGE_ID = "current_page_id"; + private static final String MARK_ROOT_PAGE_ID = "root_page_id"; + + protected static final String DEFAULT_CACHE_ENABLED = "true"; + private static final String KEY_MAIN = "main"; + private static final String KEY_TREE = "menu"; + + /** + * Constructor + */ + public AbstractMainTreeMenuInclude( ) + { + String strCacheEnabled = AppPropertiesService.getProperty( getPropertyCacheEnabled( ), DEFAULT_CACHE_ENABLED ); + + if ( strCacheEnabled.equalsIgnoreCase( DEFAULT_CACHE_ENABLED ) ) + { + initCache( getName( ) ); + } + + PageService.addPageEventListener( this ); + } + + protected abstract String getPropertyCacheEnabled( ); + + /** + * Substitute specific Freemarker markers in the page template. + * @param rootModel the HashMap containing markers to substitute + * @param data A PageData object containing applications data + * @param nMode The current mode + * @param request The HTTP request + */ + public void fillTemplate( Map rootModel, PageData data, int nMode, HttpServletRequest request ) + { + if ( request != null ) + { + int nCurrentPageId; + + /* test parameter name: page_id parameter for a PageContentService, current_page_id for a DocumentContentService */ + String strParameterPageId = ( request.getParameter( PARAMETER_CURRENT_PAGE_ID ) == null ) ? Parameters.PAGE_ID : PARAMETER_CURRENT_PAGE_ID; + + try + { + nCurrentPageId = ( request.getParameter( strParameterPageId ) == null ) ? 0 : Integer.parseInt( request.getParameter( strParameterPageId ) ); + } + catch ( NumberFormatException nfe ) + { + AppLogService.info( this.getClass( ).getSimpleName( ) + ".fillTemplate() : " + nfe.getLocalizedMessage( ) ); + nCurrentPageId = 0; + } + + rootModel.put( getMarkPageMenuMain( ), getMainPageList( nCurrentPageId, nMode, request ) ); + rootModel.put( getMarkPageMenuTree( ), getTreePageList( nCurrentPageId, nMode, request ) ); + } + } + + protected abstract String getMarkPageMenuMain( ); + + protected abstract String getMarkPageMenuTree( ); + + /** + * Display the list of childpages pages for first level of childpages + * @param nCurrentPageId The current page id + * @param request The HTTP request + * @return the list of childpages + */ + private String getMainPageList( int nCurrentPageId, int nMode, HttpServletRequest request ) + { + HashMap modelList = new HashMap( ); + Locale locale = ( request == null ) ? null : request.getLocale( ); + + // Define the root tree for each childpages of root page + int nRootParentTree = getRootParentTree( nCurrentPageId ); + + MenuItem root = null; + + // Search the list of childpages from the root page, the number of levels defined by nDepth + // and store it in root object + if ( isCacheEnable( ) ) + { + root = (MenuItem) getFromCache( KEY_MAIN ); + } + + if ( root == null ) + { + root = new MenuItem( ); + buildMenuTree( root, PortalService.getRootPageId( ), getMainPageListDepth( ) ); + + if ( isCacheEnable( ) ) + { + putInCache( KEY_MAIN, root ); + } + } + + modelList.put( MARK_MENU, root ); + modelList.put( MARK_ROOT_PAGE_ID, nRootParentTree ); + modelList.put( MARK_CURRENT_PAGE_ID, Integer.toString( nCurrentPageId ) ); + + // Define the site path from url, by mode + modelList.put( MenusService.MARKER_SITE_PATH, MenusService.getInstance( ).getSitePath( nMode ) ); + + HtmlTemplate templateList = AppTemplateService.getTemplate( TEMPLATE_MENU_PAGES, locale, modelList ); + + return templateList.getHtml( ); + } + + // Define the level of tree + protected abstract int getMainPageListDepth( ); + + /** + * Display the list of childpages pages for other levels (nDepth define how many level to add on the list) + * @param nCurrentPageId The current page id + * @param request The HTTP request + * @return the list of chilpages + */ + private String getTreePageList( int nCurrentPageId, int nMode, HttpServletRequest request ) + { + HashMap modelList = new HashMap( ); + Locale locale = ( request == null ) ? null : request.getLocale( ); + + // Define the root tree for each childpages of root page + int nRootParentTree = getRootParentTree( nCurrentPageId ); + + MenuItem root = null; + String strCacheKey = KEY_TREE + nCurrentPageId; + + if ( isCacheEnable( ) ) + { + root = (MenuItem) getFromCache( strCacheKey ); + } + + if ( root == null ) + { + root = new MenuItem( ); + + buildMenuTree( root, getPageId( nRootParentTree, nCurrentPageId ), getTreePageListDepth( nCurrentPageId ) ); + + if ( isCacheEnable( ) ) + { + putInCache( strCacheKey, root ); + } + } + + modelList.put( MARK_MENU, root ); + modelList.put( MARK_ROOT_PAGE_ID, nRootParentTree ); + modelList.put( MARK_CURRENT_PAGE_ID, Integer.toString( nCurrentPageId ) ); + + // Define the site path from url, by mode + modelList.put( MenusService.MARKER_SITE_PATH, MenusService.getInstance( ).getSitePath( nMode ) ); + + HtmlTemplate templateList = AppTemplateService.getTemplate( TEMPLATE_MENU_PAGES_TREE, locale, modelList ); + + return templateList.getHtml( ); + } + + protected abstract int getPageId( int nRootParentTree, int nCurrentPageId ); + + protected abstract int getTreePageListDepth( int pageId ); + + /** + * Define the root tree id of a page + * @param nPageId The page identifier + * @return The parent page identifier or root tree + */ + private int getRootParentTree( int nPageId ) + { + Page page = PageHome.getPage( nPageId ); + int nParentPageId = page.getParentPageId( ); + + if ( nParentPageId == 0 ) + { + return nPageId; + } + + int nParentTree = nParentPageId; + + int nPageRootId = PortalService.getRootPageId( ); + + while ( nParentPageId != nPageRootId ) + { + nParentTree = nParentPageId; + + Page parentPage = PageHome.getPage( nParentPageId ); + nParentPageId = parentPage.getParentPageId( ); + } + + return nParentTree; + } + + /** + * Build the menu tree from nPageId, the number of levels defined by nDepth + * @param item The MenunItem object + * @param nPageId The page identifier + * @param nDepth The page level + */ + private void buildMenuTree( MenuItem item, int nPageId, int nDepth ) + { + if ( nDepth > 0 ) + { + Collection listPages = PageHome.getChildPages( nPageId ); + + for ( Page page : listPages ) + { + MenuItem mi = new MenuItem( ); + mi.setPage( PageHome.findByPrimaryKey( page.getId( ) ) ); + item.addChild( mi ); + buildMenuTree( mi, page.getId( ), nDepth - 1 ); + } + } + } + + @Override + public void processPageEvent( PageEvent event ) + { + // a page was added, removed or updated + resetCache( ); + } + +} diff --git a/src/java/fr/paris/lutece/plugins/menus/web/MainTreeMenuInclude.java b/src/java/fr/paris/lutece/plugins/menus/web/MainTreeMenuInclude.java index 002d8bf..aef8abb 100644 --- a/src/java/fr/paris/lutece/plugins/menus/web/MainTreeMenuInclude.java +++ b/src/java/fr/paris/lutece/plugins/menus/web/MainTreeMenuInclude.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2002-2014, Mairie de Paris + * Copyright (c) 2002-2015, Mairie de Paris * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -33,47 +33,18 @@ */ package fr.paris.lutece.plugins.menus.web; -import fr.paris.lutece.plugins.menus.business.MenuItem; -import fr.paris.lutece.plugins.menus.service.MenusService; -import fr.paris.lutece.portal.business.page.Page; -import fr.paris.lutece.portal.business.page.PageHome; -import fr.paris.lutece.portal.service.cache.AbstractCacheableService; -import fr.paris.lutece.portal.service.content.PageData; import fr.paris.lutece.portal.service.includes.PageInclude; import fr.paris.lutece.portal.service.portal.PortalService; -import fr.paris.lutece.portal.service.template.AppTemplateService; -import fr.paris.lutece.portal.service.util.AppLogService; import fr.paris.lutece.portal.service.util.AppPropertiesService; -import fr.paris.lutece.portal.web.constants.Parameters; -import fr.paris.lutece.util.html.HtmlTemplate; - -import java.util.Collection; -import java.util.HashMap; -import java.util.Locale; -import java.util.Map; - -import javax.servlet.http.HttpServletRequest; - /** * MainTreeMenuInclude */ -public class MainTreeMenuInclude extends AbstractCacheableService implements PageInclude +public class MainTreeMenuInclude extends AbstractMainTreeMenuInclude implements PageInclude { ///////////////////////////////////////////////////////////////////////////////////////////// // Constants - //Templates - private static final String TEMPLATE_MENU_PAGES = "skin/plugins/menus/main_tree_pages_list.html"; - private static final String TEMPLATE_MENU_PAGES_TREE = "skin/plugins/menus/main_tree_pages_list_tree.html"; - - // Parameters - private final static String PARAMETER_CURRENT_PAGE_ID = "current_page_id"; - - // Markers - private static final String MARK_MENU = "menu"; - private static final String MARK_CURRENT_PAGE_ID = "current_page_id"; - private static final String MARK_ROOT_PAGE_ID = "root_page_id"; private static final String MARK_PAGE_MENU_MAIN = "page_tree_menu_main"; private static final String MARK_PAGE_MENU_TREE = "page_tree_menu_tree"; @@ -83,230 +54,79 @@ public class MainTreeMenuInclude extends AbstractCacheableService implements Pag private static final String PROPERTY_DEPTH_TREE_LEVEL = "menus.mainTreeMenu.depth.tree"; private static final String CACHE_NAME = "Plugin Menus - Main Tree Menu Cache"; - private static final String DEFAULT_CACHE_ENABLED = "true"; - private static final String KEY_MAIN = "main"; - private static final String KEY_TREE = "menu"; /** * Constructor */ public MainTreeMenuInclude( ) { - String strCacheEnabled = AppPropertiesService.getProperty( PROPERTY_CACHE_ENABLED, DEFAULT_CACHE_ENABLED ); - - if ( strCacheEnabled.equalsIgnoreCase( DEFAULT_CACHE_ENABLED ) ) - { - initCache( CACHE_NAME ); - } + super( ); } - /** - * Substitue specific Freemarker markers in the page template. - * @param rootModel the HashMap containing markers to substitute - * @param data A PageData object containing applications data - * @param nMode The current mode - * @param request The HTTP request - */ - public void fillTemplate( Map rootModel, PageData data, int nMode, HttpServletRequest request ) + @Override + protected String getPropertyCacheEnabled( ) { - if ( request != null ) - { - int nCurrentPageId; - - /* test parameter name: page_id parameter for a PageContentService, current_page_id for a DocumentContentService */ - String strParameterPageId = ( request.getParameter( PARAMETER_CURRENT_PAGE_ID ) == null ) ? Parameters.PAGE_ID : PARAMETER_CURRENT_PAGE_ID; - - try - { - nCurrentPageId = ( request.getParameter( strParameterPageId ) == null ) ? 0 : Integer.parseInt( request.getParameter( strParameterPageId ) ); - } - catch ( NumberFormatException nfe ) - { - AppLogService.info( "MainMenuInclude.fillTemplate() : " + nfe.getLocalizedMessage( ) ); - nCurrentPageId = 0; - } - - rootModel.put( MARK_PAGE_MENU_MAIN, getMainPageList( nCurrentPageId, nMode, request ) ); - rootModel.put( MARK_PAGE_MENU_TREE, getTreePageList( nCurrentPageId, nMode, request ) ); - } + return PROPERTY_CACHE_ENABLED; } /** * Returns the cache name * @return The cache name */ + @Override public String getName( ) { return CACHE_NAME; } - /** - * Display the list of childpages pages for first level of childpages - * @param nCurrentPageId The current page id - * @param request The HTTP request - * @return the list of childpages - */ - private String getMainPageList( int nCurrentPageId, int nMode, HttpServletRequest request ) + @Override + protected String getMarkPageMenuMain( ) { - HashMap modelList = new HashMap( ); - Locale locale = ( request == null ) ? null : request.getLocale( ); - - // Define the root tree for each childpages of root page - int nRootParentTree = getRootParentTree( nCurrentPageId ); - - MenuItem root = null; - - // Define the level of tree - int nDepth = AppPropertiesService.getPropertyInt( PROPERTY_DEPTH_MAIN_LEVEL, 1); - - // Search the list of childpages from the root page, the number of levels defined by nDepth - // and store it in root object - if ( isCacheEnable( ) ) - { - root = (MenuItem) getFromCache( KEY_MAIN ); - } - - if ( root == null ) - { - root = new MenuItem( ); - buildMenuTree( root, PortalService.getRootPageId( ), nDepth ); - - if ( isCacheEnable( ) ) - { - putInCache( KEY_MAIN, root ); - } - } - - modelList.put( MARK_MENU, root ); - modelList.put( MARK_ROOT_PAGE_ID, nRootParentTree ); - modelList.put( MARK_CURRENT_PAGE_ID, Integer.toString( nCurrentPageId ) ); - - // Define the site path from url, by mode - modelList.put( MenusService.MARKER_SITE_PATH, MenusService.getInstance( ).getSitePath( nMode ) ); - - HtmlTemplate templateList = AppTemplateService.getTemplate( TEMPLATE_MENU_PAGES, locale, modelList ); - - return templateList.getHtml( ); + return MARK_PAGE_MENU_MAIN; } - /** - * Display the list of childpages pages for other levels (nDepth define how many level to add on the list) - * @param nCurrentPageId The current page id - * @param request The HTTP request - * @return the list of chilpages - */ - private String getTreePageList( int nCurrentPageId, int nMode, HttpServletRequest request ) + @Override + protected String getMarkPageMenuTree( ) { - HashMap modelList = new HashMap( ); - Locale locale = ( request == null ) ? null : request.getLocale( ); - - // Define the root tree for each childpages of root page - int nRootParentTree = getRootParentTree( nCurrentPageId ); - - MenuItem root = null; - String strCacheKey = KEY_TREE + nCurrentPageId; - - if ( isCacheEnable( ) ) - { - root = (MenuItem) getFromCache( strCacheKey ); - } - - if ( root == null ) - { - root = new MenuItem( ); - - // Define the level of tree - int nDepth = AppPropertiesService.getPropertyInt( PROPERTY_DEPTH_TREE_LEVEL, 0); - - // if the current page isn't root (1), we need two levels of childpages, other 0 - if ( nCurrentPageId != PortalService.getRootPageId( ) ) - { - // if the current page isn't 0, the we need two levels of childpages, other 0 - if ( nCurrentPageId != 0 ) - { - nDepth = 2; - } - } - - // If the root tree isn't site root tree (1), search the list of childpages from this nRootTree, the number of levels defined by nDepth - if ( nRootParentTree != PortalService.getRootPageId( ) ) - { - buildMenuTree( root, nRootParentTree, nDepth ); - } - - // If the root tree is the site root tree (1), search the list of childpages from the current page, the number of levels defined by nDepth - else - { - buildMenuTree( root, nCurrentPageId, nDepth ); - } - - if ( isCacheEnable( ) ) - { - putInCache( strCacheKey, root ); - } - } - - modelList.put( MARK_MENU, root ); - modelList.put( MARK_ROOT_PAGE_ID, nRootParentTree ); - modelList.put( MARK_CURRENT_PAGE_ID, Integer.toString( nCurrentPageId ) ); - - // Define the site path from url, by mode - modelList.put( MenusService.MARKER_SITE_PATH, MenusService.getInstance( ).getSitePath( nMode ) ); - - HtmlTemplate templateList = AppTemplateService.getTemplate( TEMPLATE_MENU_PAGES_TREE, locale, modelList ); - - return templateList.getHtml( ); + return MARK_PAGE_MENU_TREE; } - /** - * Define the root tree id of a page - * @param nPageId The page identifier - * @return The parent page identifier or root tree - */ - private int getRootParentTree( int nPageId ) + @Override + protected int getMainPageListDepth( ) { - Page page = PageHome.getPage( nPageId ); - int nParentPageId = page.getParentPageId( ); + return AppPropertiesService.getPropertyInt( PROPERTY_DEPTH_MAIN_LEVEL, 1); + } - if ( nParentPageId == 0 ) + @Override + protected int getPageId( int nRootParentTree, int nCurrentPageId ) + { + if ( nRootParentTree != PortalService.getRootPageId( ) ) { - return nPageId; + return nRootParentTree; } - int nParentTree = nParentPageId; - - int nPageRootId = PortalService.getRootPageId( ); - - while ( nParentPageId != nPageRootId ) + // If the root tree is the site root tree (1), search the list of childpages from the current page, the number of levels defined by nDepth + else { - nParentTree = nParentPageId; - - Page parentPage = PageHome.getPage( nParentPageId ); - nParentPageId = parentPage.getParentPageId( ); + return nCurrentPageId; } - - return nParentTree; } - /** - * Build the menu tree from nPageId, the number of levels defined by nDepth - * @param item The MenunItem object - * @param nPageId The page identifier - * @param nDepth The page level - */ - private void buildMenuTree( MenuItem item, int nPageId, int nDepth ) + @Override + protected int getTreePageListDepth( int pageId ) { - if ( nDepth > 0 ) - { - Collection listPages = PageHome.getChildPages( nPageId ); + int nDepth = AppPropertiesService.getPropertyInt( PROPERTY_DEPTH_TREE_LEVEL, 0); - for ( Page page : listPages ) + // if the current page isn't root (1), we need two levels of childpages, other 0 + if ( pageId != PortalService.getRootPageId( ) ) + { + // if the current page isn't 0, the we need two levels of childpages, other 0 + if ( pageId != 0 ) { - MenuItem mi = new MenuItem( ); - mi.setPage( PageHome.findByPrimaryKey( page.getId( ) ) ); - item.addChild( mi ); - buildMenuTree( mi, page.getId( ), nDepth - 1 ); + nDepth = 2; } } + + return nDepth; } } diff --git a/src/java/fr/paris/lutece/plugins/menus/web/MainTreeMenuIncludeAllPages.java b/src/java/fr/paris/lutece/plugins/menus/web/MainTreeMenuIncludeAllPages.java index a0749f7..ab493f6 100644 --- a/src/java/fr/paris/lutece/plugins/menus/web/MainTreeMenuIncludeAllPages.java +++ b/src/java/fr/paris/lutece/plugins/menus/web/MainTreeMenuIncludeAllPages.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2002-2014, Mairie de Paris + * Copyright (c) 2002-2015, Mairie de Paris * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -33,47 +33,18 @@ */ package fr.paris.lutece.plugins.menus.web; -import fr.paris.lutece.plugins.menus.business.MenuItem; -import fr.paris.lutece.plugins.menus.service.MenusService; -import fr.paris.lutece.portal.business.page.Page; -import fr.paris.lutece.portal.business.page.PageHome; -import fr.paris.lutece.portal.service.cache.AbstractCacheableService; -import fr.paris.lutece.portal.service.content.PageData; import fr.paris.lutece.portal.service.includes.PageInclude; import fr.paris.lutece.portal.service.portal.PortalService; -import fr.paris.lutece.portal.service.template.AppTemplateService; -import fr.paris.lutece.portal.service.util.AppLogService; import fr.paris.lutece.portal.service.util.AppPropertiesService; -import fr.paris.lutece.portal.web.constants.Parameters; -import fr.paris.lutece.util.html.HtmlTemplate; - -import java.util.Collection; -import java.util.HashMap; -import java.util.Locale; -import java.util.Map; - -import javax.servlet.http.HttpServletRequest; - /** * MainTreeMenuInclude */ -public class MainTreeMenuIncludeAllPages extends AbstractCacheableService implements PageInclude +public class MainTreeMenuIncludeAllPages extends AbstractMainTreeMenuInclude implements PageInclude { ///////////////////////////////////////////////////////////////////////////////////////////// // Constants - //Templates - private static final String TEMPLATE_MENU_PAGES = "skin/plugins/menus/main_tree_pages_list.html"; - private static final String TEMPLATE_MENU_PAGES_TREE = "skin/plugins/menus/main_tree_pages_list_tree.html"; - - // Parameters - private final static String PARAMETER_CURRENT_PAGE_ID = "current_page_id"; - - // Markers - private static final String MARK_MENU = "menu"; - private static final String MARK_CURRENT_PAGE_ID = "current_page_id"; - private static final String MARK_ROOT_PAGE_ID = "root_page_id"; private static final String MARK_PAGE_MENU_MAIN_ALL_PAGES = "page_tree_menu_main_all_pages"; private static final String MARK_PAGE_MENU_TREE_ALL_PAGES = "page_tree_menu_tree_all_pages"; private static final String CACHE_NAME = "Plugin Menus - Main Tree Menu All Pages Cache"; @@ -83,220 +54,54 @@ public class MainTreeMenuIncludeAllPages extends AbstractCacheableService implem private static final String PROPERTY_DEPTH_MAIN_LEVEL_ALLPAGES = "menus.mainTreeMenu.depth.main.allpages"; private static final String PROPERTY_DEPTH_TREE_LEVEL_ALLPAGES = "menus.mainTreeMenu.depth.tree.allpages"; - private static final String DEFAULT_CACHE_ENABLED = "true"; - private static final String KEY_MAIN = "main"; - private static final String KEY_TREE = "menu"; - public MainTreeMenuIncludeAllPages( ) { - String strCacheEnabled = AppPropertiesService.getProperty( PROPERTY_CACHE_ENABLED_ALLPAGES, DEFAULT_CACHE_ENABLED ); - - if ( strCacheEnabled.equalsIgnoreCase( DEFAULT_CACHE_ENABLED ) ) - { - initCache( CACHE_NAME ); - } - } - - /** - * Substitue specific Freemarker markers in the page template. - * @param rootModel the HashMap containing markers to substitute - * @param data A PageData object containing applications data - * @param nMode The current mode - * @param request The HTTP request - */ - public void fillTemplate( Map rootModel, PageData data, int nMode, HttpServletRequest request ) - { - if ( request != null ) - { - int nCurrentPageId; - - /* test parameter name: page_id parameter for a PageContentService, current_page_id for a DocumentContentService */ - String strParameterPageId = ( request.getParameter( PARAMETER_CURRENT_PAGE_ID ) == null ) ? Parameters.PAGE_ID : - PARAMETER_CURRENT_PAGE_ID; - - try - { - nCurrentPageId = ( request.getParameter( strParameterPageId ) == null ) ? 0 - : Integer.parseInt( request.getParameter( - strParameterPageId ) ); - } - catch ( NumberFormatException nfe ) - { - AppLogService.info( "MainMenuInclude.fillTemplate() : " + nfe.getLocalizedMessage( ) ); - nCurrentPageId = 0; - } - - rootModel.put( MARK_PAGE_MENU_MAIN_ALL_PAGES, getMainPageList( nCurrentPageId, nMode, request ) ); - rootModel.put( MARK_PAGE_MENU_TREE_ALL_PAGES, getTreePageList( nCurrentPageId, nMode, request ) ); - } + super( ); } /** * Returns the cache name * @return The cache name */ + @Override public String getName( ) { return CACHE_NAME; } - /** - * Display the list of childpages pages for first level of childpages - * @param nCurrentPageId The current page id - * @param request The HTTP request - * @return the list of childpages - */ - private String getMainPageList( int nCurrentPageId, int nMode, HttpServletRequest request ) + @Override + protected String getPropertyCacheEnabled( ) { - HashMap modelList = new HashMap( ); - Locale locale = ( request == null ) ? null : request.getLocale( ); - - // Define the root tree for each childpages of root page - int nRootParentTree = getRootParentTree( nCurrentPageId ); - - MenuItem root = null; - - // Define the level of tree - int nDepth = AppPropertiesService.getPropertyInt( PROPERTY_DEPTH_MAIN_LEVEL_ALLPAGES, 0); - - // Search the list of childpages from the root page, the number of levels defined by nDepth - // and store it in root object - if ( isCacheEnable( ) ) - { - root = (MenuItem) getFromCache( KEY_MAIN ); - } - - if ( root == null ) - { - root = new MenuItem( ); - buildMenuTree( root, PortalService.getRootPageId( ), nDepth ); - - if ( isCacheEnable( ) ) - { - putInCache( KEY_MAIN, root ); - } - } - - modelList.put( MARK_MENU, root ); - modelList.put( MARK_ROOT_PAGE_ID, nRootParentTree ); - modelList.put( MARK_CURRENT_PAGE_ID, Integer.toString( nCurrentPageId ) ); - - // Define the site path from url, by mode - modelList.put( MenusService.MARKER_SITE_PATH, MenusService.getInstance( ).getSitePath( nMode ) ); - - HtmlTemplate templateList = AppTemplateService.getTemplate( TEMPLATE_MENU_PAGES, locale, modelList ); - - return templateList.getHtml( ); + return PROPERTY_CACHE_ENABLED_ALLPAGES; } - /** - * Display the list of childpages pages for other levels (nDepth define how many level to add on the list) - * @param nCurrentPageId The current page id - * @param request The HTTP request - * @return the list of chilpages - */ - private String getTreePageList( int nCurrentPageId, int nMode, HttpServletRequest request ) + @Override + protected String getMarkPageMenuMain( ) { - HashMap modelList = new HashMap( ); - Locale locale = ( request == null ) ? null : request.getLocale( ); - - // Define the root tree for each childpages of root page - int nRootParentTree = getRootParentTree( nCurrentPageId ); - - MenuItem root = null; - String strCacheKey = KEY_TREE + nCurrentPageId; - - if ( isCacheEnable( ) ) - { - root = (MenuItem) getFromCache( strCacheKey ); - } - - if ( root == null ) - { - root = new MenuItem( ); - - // Define the level of tree - int nDepth = AppPropertiesService.getPropertyInt( PROPERTY_DEPTH_TREE_LEVEL_ALLPAGES, 3); - - // If the root tree isn't site root tree (1), search the list of childpages from this nRootTree, the number of levels defined by nDepth - //if ( nRootParentTree != PortalService.getRootPageId( ) ) - // { - buildMenuTree( root, PortalService.getRootPageId( ), nDepth ); - //} - - // If the root tree is the site root tree (1), search the list of childpages from the current page, the number of levels defined by nDepth - /* else - { - buildMenuTree( root, nCurrentPageId, nDepth ); - }*/ - - if ( isCacheEnable( ) ) - { - putInCache( strCacheKey, root ); - } - } - - modelList.put( MARK_MENU, root ); - modelList.put( MARK_ROOT_PAGE_ID, nRootParentTree ); - modelList.put( MARK_CURRENT_PAGE_ID, Integer.toString( nCurrentPageId ) ); - - // Define the site path from url, by mode - modelList.put( MenusService.MARKER_SITE_PATH, MenusService.getInstance( ).getSitePath( nMode ) ); - - HtmlTemplate templateList = AppTemplateService.getTemplate( TEMPLATE_MENU_PAGES_TREE, locale, modelList ); - - return templateList.getHtml( ); + return MARK_PAGE_MENU_MAIN_ALL_PAGES; } - /** - * Define the root tree id of a page - * @param nPageId The page identifier - * @return The parent page identifier or root tree - */ - private int getRootParentTree( int nPageId ) + @Override + protected String getMarkPageMenuTree( ) { - Page page = PageHome.getPage( nPageId ); - int nParentPageId = page.getParentPageId( ); - - if ( nParentPageId == 0 ) - { - return nPageId; - } - - int nParentTree = nParentPageId; - - int nPageRootId = PortalService.getRootPageId( ); - - while ( nParentPageId != nPageRootId ) - { - nParentTree = nParentPageId; - - Page parentPage = PageHome.getPage( nParentPageId ); - nParentPageId = parentPage.getParentPageId( ); - } + return MARK_PAGE_MENU_TREE_ALL_PAGES; + } - return nParentTree; + @Override + protected int getMainPageListDepth( ) + { + return AppPropertiesService.getPropertyInt( PROPERTY_DEPTH_MAIN_LEVEL_ALLPAGES, 0); } - /** - * Build the menu tree from nPageId, the number of levels defined by nDepth - * @param item The MenunItem object - * @param nPageId The page identifier - * @param nDepth The page level - */ - private void buildMenuTree( MenuItem item, int nPageId, int nDepth ) + @Override + protected int getPageId( int nRootParentTree, int nCurrentPageId ) { - if ( nDepth > 0 ) - { - Collection listPages = PageHome.getChildPages( nPageId ); + return PortalService.getRootPageId( ); + } - for ( Page page : listPages ) - { - MenuItem mi = new MenuItem( ); - mi.setPage( PageHome.findByPrimaryKey( page.getId( ) ) ); - item.addChild( mi ); - buildMenuTree( mi, page.getId( ), nDepth - 1 ); - } - } + @Override + protected int getTreePageListDepth( int pageId ) + { + return AppPropertiesService.getPropertyInt( PROPERTY_DEPTH_TREE_LEVEL_ALLPAGES, 3); } } \ No newline at end of file diff --git a/src/test/java/fr/paris/lutece/plugins/menus/web/AbstractMainTreeMenuIncludeTest.java b/src/test/java/fr/paris/lutece/plugins/menus/web/AbstractMainTreeMenuIncludeTest.java new file mode 100644 index 0000000..022be54 --- /dev/null +++ b/src/test/java/fr/paris/lutece/plugins/menus/web/AbstractMainTreeMenuIncludeTest.java @@ -0,0 +1,247 @@ +/* + * Copyright (c) 2002-2015, Mairie de Paris + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright notice + * and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice + * and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * License 1.0 + */ +package fr.paris.lutece.plugins.menus.web; + +import java.security.SecureRandom; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Stack; + +import org.apache.commons.lang.StringUtils; +import org.junit.Ignore; +import org.junit.Test; + +import fr.paris.lutece.portal.business.page.Page; +import fr.paris.lutece.portal.business.page.PageHome; +import fr.paris.lutece.portal.business.style.PageTemplateHome; +import fr.paris.lutece.portal.service.cache.AbstractCacheableService; +import fr.paris.lutece.portal.service.content.PageData; +import fr.paris.lutece.portal.service.includes.PageInclude; +import fr.paris.lutece.portal.service.includes.PageIncludeService; +import fr.paris.lutece.portal.service.page.IPageService; +import fr.paris.lutece.portal.service.portal.PortalService; +import fr.paris.lutece.portal.service.spring.SpringContextService; +import fr.paris.lutece.portal.web.constants.Parameters; +import fr.paris.lutece.test.LuteceTestCase; +import fr.paris.lutece.test.MokeHttpServletRequest; + +public abstract class AbstractMainTreeMenuIncludeTest extends LuteceTestCase +{ + + @Test + public void testFillTemplate( ) + { + int depth = 4; + int width = 2; + String randPageNamePart = createPageTree( depth, width ); + try + { + PageInclude mainTreeMenuInclude = findMainTreeMenuInclude( getMainTreeMenuIncludeClass( ) ); + // create a stack of pages to test the menu on + Stack parentPages = new Stack( ); + // begin with the root + Page parentPage = PageHome.findByPrimaryKey( PortalService.getRootPageId( ) ); + parentPages.push( parentPage ); + MokeHttpServletRequest request = new MokeHttpServletRequest( ); + while ( !parentPages.isEmpty( ) ) + { + parentPage = parentPages.pop( ); + request.addMokeParameters( Parameters.PAGE_ID, Integer.toString( parentPage.getId( ) ) ); + Map rootModel = new HashMap( ); + PageData data = new PageData( ); + mainTreeMenuInclude.fillTemplate( rootModel, data, 0, request ); + checkPageTreeMenuMain( rootModel.get( getPageTreeMenuMainMark( ) ), randPageNamePart, depth, width ); + checkPageTreeMenuTree( rootModel.get( getPageTreeMenuTreeMark( ) ), parentPage, randPageNamePart, + depth, width ); + // add children to test + Collection children = PageHome.getChildPagesMinimalData( parentPage.getId( ) ); + for ( Page aPage : children ) + { + if ( aPage.getName( ).contains( randPageNamePart ) ) + { + // only take into account pages created for the test + parentPages.push( aPage ); + } + } + } + } finally + { + // cleanup pages created for the test + deletePageTree( randPageNamePart ); + } + } + + protected abstract String getPageTreeMenuTreeMark( ); + + protected abstract String getPageTreeMenuMainMark( ); + + protected abstract Class getMainTreeMenuIncludeClass( ); + + protected abstract void checkPageTreeMenuTree( Object object, Page parentPage, String randPageNamePart, int depth, + int width ); + + protected abstract void checkPageTreeMenuMain( Object object, String randPageNamePart, int depth, int width ); + + private PageInclude findMainTreeMenuInclude( Class clazz ) + { + PageInclude mainTreeMenuInclude = null; + List includes = PageIncludeService.getIncludes( ); + for ( PageInclude anInclude : includes ) + { + if ( clazz.isInstance( anInclude ) ) + { + mainTreeMenuInclude = anInclude; + break; + } + } + assertNotNull( "Did not find MainTreeMenuInclude", mainTreeMenuInclude ); + return mainTreeMenuInclude; + } + + private void deletePageTree( String randPageNamePart ) + { + Collection children = PageHome.getChildPagesMinimalData( PortalService.getRootPageId( ) ); + for ( Page aPage : children ) + { + if ( aPage.getName( ).contains( randPageNamePart ) ) + { + deletePageTree( aPage ); + } + } + } + + private void deletePageTree( Page page ) + { + Collection children = PageHome.getChildPagesMinimalData( page.getId( ) ); + for ( Page aPage : children ) + { + deletePageTree( aPage ); + } + IPageService pageService = ( IPageService ) SpringContextService.getBean( "pageService" ); + pageService.removePage( page.getId( ) ); + } + + private String createPageTree( int depth, int width ) + { + Page root = PageHome.findByPrimaryKey( PortalService.getRootPageId( ) ); + String randomPageName = "page" + new SecureRandom( ).nextLong( ); + List ancestry = Collections.emptyList( ); + createPageTree( root, ancestry, randomPageName, depth, width ); + return randomPageName; + } + + private void createPageTree( Page root, List ancestry, String randomPageName, int depth, int width ) + { + if ( depth == 0 ) + { + return; + } + IPageService pageService = ( IPageService ) SpringContextService.getBean( "pageService" ); + for ( int i = 0; i < width; i++ ) + { + Page page = new Page( ); + page.setParentPageId( root.getId( ) ); + List path = new ArrayList( ancestry ); + path.add( depth + "_" + i ); + String name = randomPageName + "_" + StringUtils.join( path, '|' ); + page.setName( name ); + page.setDescription( name + "_desc" ); + page.setPageTemplateId( PageTemplateHome.getPageTemplatesList( ).get( 0 ).getId( ) ); + pageService.createPage( page ); + createPageTree( page, path, randomPageName, depth - 1, width ); + } + } + + @Test + public void testGetMenuContent( ) + { + + PageInclude mainTreeMenuInclude = findMainTreeMenuInclude( getMainTreeMenuIncludeClass( ) ); + + // activate the cache + assertTrue( mainTreeMenuInclude instanceof AbstractCacheableService ); + boolean cacheStatus = ( ( AbstractCacheableService ) mainTreeMenuInclude ).isCacheEnable( ); + ( ( AbstractCacheableService ) mainTreeMenuInclude ).enableCache( true ); + + try { + Map model = new HashMap( ); + PageData data = new PageData( ); + // determine a random page name + String randomPageName = "page" + new SecureRandom( ).nextLong( ); + MokeHttpServletRequest request = new MokeHttpServletRequest( ); + // get the menu + mainTreeMenuInclude.fillTemplate( model, data, 0, request ); + String mark = getTestGetMenuContentMark( ); + String menu = ( String ) model.get( mark ); + assertFalse( mark + " should not contain not yet created page with name " + randomPageName, menu.contains( randomPageName ) ); + // create the page + Page page = new Page( ); + page.setParentPageId( PortalService.getRootPageId( ) ); + page.setName( randomPageName ); + page.setDescription( randomPageName + "_desc"); + page.setPageTemplateId( PageTemplateHome.getPageTemplatesList( ).get( 0 ).getId( ) ); + IPageService pageService = (IPageService) SpringContextService.getBean( "pageService" ); + pageService.createPage( page ); + // get the menu + mainTreeMenuInclude.fillTemplate( model, data, 0, request ); + menu = ( String ) model.get( mark ); + assertTrue( mark + " should contain page with name " + randomPageName, menu.contains( randomPageName ) ); + // change the page name + randomPageName = randomPageName + "_mod"; + page.setName( randomPageName ); + pageService.updatePage( page ); + // get the menu + mainTreeMenuInclude.fillTemplate( model, data, 0, request ); + menu = ( String ) model.get( mark ); + assertTrue( mark + " should contain page with the modified name " + randomPageName, menu.contains( randomPageName ) ); + // remove the page + pageService.removePage( page.getId( ) ); + // get the menu + mainTreeMenuInclude.fillTemplate( model, data, 0, request ); + menu = ( String ) model.get( mark ); + assertFalse( mark + " should not contain page with name " + randomPageName + " anymore", menu.contains( randomPageName ) ); + } finally + { + // restore the cache status + ( ( AbstractCacheableService ) mainTreeMenuInclude ).enableCache( cacheStatus ); + } + } + + protected abstract String getTestGetMenuContentMark( ); + +} diff --git a/src/test/java/fr/paris/lutece/plugins/menus/web/MainTreeMenuIncludeAllPagesTest.java b/src/test/java/fr/paris/lutece/plugins/menus/web/MainTreeMenuIncludeAllPagesTest.java new file mode 100644 index 0000000..ecbca24 --- /dev/null +++ b/src/test/java/fr/paris/lutece/plugins/menus/web/MainTreeMenuIncludeAllPagesTest.java @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2002-2015, Mairie de Paris + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright notice + * and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice + * and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * License 1.0 + */ +package fr.paris.lutece.plugins.menus.web; + +import org.apache.commons.lang.StringUtils; + +import fr.paris.lutece.portal.business.page.Page; +import fr.paris.lutece.portal.service.includes.PageInclude; + +public class MainTreeMenuIncludeAllPagesTest extends AbstractMainTreeMenuIncludeTest +{ + @Override + protected String getPageTreeMenuTreeMark( ) + { + return "page_tree_menu_tree_all_pages"; + } + + @Override + protected String getPageTreeMenuMainMark( ) + { + return "page_tree_menu_main_all_pages"; + } + + @Override + protected Class getMainTreeMenuIncludeClass( ) + { + return MainTreeMenuIncludeAllPages.class; + } + + protected void checkPageTreeMenuTree( Object object, Page parentPage, String randPageNamePart, int depth, int width ) + { + assertTrue( getPageTreeMenuTreeMark( ) + " should be a String", object instanceof String ); + String menu = ( String ) object; + for ( int i = 0; i < width; i++ ) + { + String name = randPageNamePart + "_" + depth + "_" + i; + assertTrue( "menu should contain page named " + name, menu.contains( name + " " ) ); + checkPageTreeMenuTreePages( menu, name, width, depth, 1 /* + * 2 is the + * depth for + * menus + */); + } + } + + private void checkPageTreeMenuTreePages( String menu, String prefix, int width, int depth, int menuDepth ) + { + if ( menuDepth == 0 ) + { + return; + } + for ( int i = 0; i < width; i++ ) + { + String name = prefix + "|" + ( menuDepth + depth - 2 ) + "_" + i; + assertTrue( "menu should contain page named " + name, menu.contains( name + " " ) ); + checkPageTreeMenuTreePages( menu, name, width, depth, menuDepth - 1 ); + } + } + + protected void checkPageTreeMenuMain( Object object, String randPageNamePart, int depth, int width ) + { + assertTrue( getPageTreeMenuMainMark( ) + " should be a String", object instanceof String ); + String menu = ( String ) object; + assertTrue( StringUtils.isBlank( menu ) ); + } + + @Override + protected String getTestGetMenuContentMark( ) + { + return getPageTreeMenuTreeMark( ); + } + +} diff --git a/src/test/java/fr/paris/lutece/plugins/menus/web/MainTreeMenuIncludeTest.java b/src/test/java/fr/paris/lutece/plugins/menus/web/MainTreeMenuIncludeTest.java new file mode 100644 index 0000000..4de395a --- /dev/null +++ b/src/test/java/fr/paris/lutece/plugins/menus/web/MainTreeMenuIncludeTest.java @@ -0,0 +1,126 @@ +/* + * Copyright (c) 2002-2015, Mairie de Paris + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright notice + * and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice + * and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * License 1.0 + */ +package fr.paris.lutece.plugins.menus.web; + +import org.apache.commons.lang.StringUtils; + +import fr.paris.lutece.portal.business.page.Page; +import fr.paris.lutece.portal.service.includes.PageInclude; +import fr.paris.lutece.portal.service.portal.PortalService; + +public class MainTreeMenuIncludeTest extends AbstractMainTreeMenuIncludeTest +{ + + @Override + protected String getPageTreeMenuTreeMark( ) + { + return "page_tree_menu_tree"; + } + + @Override + protected String getPageTreeMenuMainMark( ) + { + return "page_tree_menu_main"; + } + + @Override + protected Class getMainTreeMenuIncludeClass( ) + { + return MainTreeMenuInclude.class; + } + + protected void checkPageTreeMenuTree( Object object, Page parentPage, String randPageNamePart, int depth, int width ) + { + assertTrue( getPageTreeMenuTreeMark( ) + " should be a String", object instanceof String ); + String menu = ( String ) object; + if ( parentPage.getId( ) == PortalService.getRootPageId( ) ) + { + assertTrue( StringUtils.isBlank( menu ) ); + } else + { + int indexOfPipe = parentPage.getName( ).indexOf( '|' ); + String prefix; + if ( indexOfPipe == -1 ) + { + prefix = parentPage.getName( ); + } else + { + prefix = parentPage.getName( ).substring( 0, indexOfPipe ); + } + checkPageTreeMenuTreePages( menu, prefix, width, depth, 2 /* + * 2 is + * the + * depth + * for + * menus + */); + } + } + + private void checkPageTreeMenuTreePages( String menu, String prefix, int width, int depth, int menuDepth ) + { + if ( menuDepth == 0 ) + { + return; + } + for ( int i = 0; i < width; i++ ) + { + String name = prefix + "|" + ( menuDepth + depth - 3 ) + "_" + i; + assertTrue( "menu should contain page named " + name, menu.contains( name + " " ) ); + checkPageTreeMenuTreePages( menu, name, width, depth, menuDepth - 1 ); + } + } + + protected void checkPageTreeMenuMain( Object object, String randPageNamePart, int depth, int width ) + { + assertTrue( getPageTreeMenuMainMark( ) + " should be a String", object instanceof String ); + String menu = ( String ) object; + // int menuDepth = AppPropertiesService.getPropertyInt( + // "menus.mainTreeMenu.depth.main", 1); + int menuDepth = 1; // the default template is hardcoded to a depth of 1 + for ( int i = 0; i < width; i++ ) + { + String name = randPageNamePart + "_" + depth + "_" + i; + assertTrue( "menu should contain page named " + name, menu.contains( name + " " ) ); + checkPageTreeMenuTreePages( menu, name, width, depth, menuDepth - 1 ); + } + } + + @Override + protected String getTestGetMenuContentMark( ) + { + return getPageTreeMenuMainMark( ); + } + +}