AdminMapJspBean.java

  1. /*
  2.  * Copyright (c) 2002-2022, City of Paris
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  *  1. Redistributions of source code must retain the above copyright notice
  10.  *     and the following disclaimer.
  11.  *
  12.  *  2. Redistributions in binary form must reproduce the above copyright notice
  13.  *     and the following disclaimer in the documentation and/or other materials
  14.  *     provided with the distribution.
  15.  *
  16.  *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
  17.  *     contributors may be used to endorse or promote products derived from
  18.  *     this software without specific prior written permission.
  19.  *
  20.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
  24.  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30.  * POSSIBILITY OF SUCH DAMAGE.
  31.  *
  32.  * License 1.0
  33.  */
  34. package fr.paris.lutece.portal.web.admin;

  35. import fr.paris.lutece.portal.business.XmlContent;
  36. import fr.paris.lutece.portal.business.page.Page;
  37. import fr.paris.lutece.portal.business.page.PageHome;
  38. import fr.paris.lutece.portal.business.portalcomponent.PortalComponentHome;
  39. import fr.paris.lutece.portal.business.style.ModeHome;
  40. import fr.paris.lutece.portal.business.stylesheet.StyleSheet;
  41. import fr.paris.lutece.portal.business.user.AdminUser;
  42. import fr.paris.lutece.portal.service.admin.AdminUserService;
  43. import fr.paris.lutece.portal.service.html.XmlTransformerService;
  44. import fr.paris.lutece.portal.service.page.PageResourceIdService;
  45. import fr.paris.lutece.portal.service.portal.PortalService;
  46. import fr.paris.lutece.portal.service.rbac.RBACService;
  47. import fr.paris.lutece.portal.service.template.AppTemplateService;
  48. import fr.paris.lutece.portal.service.util.AppPropertiesService;
  49. import fr.paris.lutece.util.html.HtmlTemplate;
  50. import fr.paris.lutece.util.xml.XmlUtil;

  51. import java.util.HashMap;
  52. import java.util.Map;
  53. import java.util.Properties;

  54. import javax.servlet.http.HttpServletRequest;

  55. /**
  56.  * This class provides the map of the pages on the site
  57.  */
  58. public class AdminMapJspBean extends AdminFeaturesPageJspBean
  59. {
  60.     // Right
  61.     public static final String RIGHT_MANAGE_ADMIN_SITE = "CORE_ADMIN_SITE";
  62.     private static final long serialVersionUID = 2871979154306491511L;

  63.     // Markers
  64.     private static final String MARKER_MAP_SITE = "map_site";
  65.     private static final String MARK_PAGE = "page";

  66.     // Templates
  67.     private static final String TEMPLATE_MAP_SITE = "admin/site/site_map.html";

  68.     // Parameters
  69.     private static final String PARAMETER_SITE_PATH = "site-path";
  70.     private static final String PARAMETER_PAGE_ID = "page_id";

  71.     // Properties
  72.     private static final String PROPERTY_ADMIN_PATH = "lutece.admin.path";

  73.     // Xml Tags
  74.     private static final String TAG_CSS_ID = "css-id";
  75.     private static final String TAG_PAGE_ROLE = "page-role";
  76.     private static final int PORTAL_COMPONENT_SITE_MAP_ID = 8;
  77.     private static final int MODE_ADMIN = 1;

  78.     /**
  79.      * Build or get in the cache the page which contains the site map depending on the mode
  80.      *
  81.      * @param request
  82.      *            The Http request
  83.      * @return The content of the site map
  84.      */
  85.     public String getMap( HttpServletRequest request )
  86.     {
  87.         StringBuffer strArborescenceXml = new StringBuffer( );

  88.         StringBuilder strCssId = new StringBuilder( );
  89.         int nLevel = 0;

  90.         String strCurrentPageId = request.getParameter( PARAMETER_PAGE_ID );

  91.         findPages( request, strArborescenceXml, PortalService.getRootPageId( ), nLevel, strCurrentPageId, strCssId );

  92.         StyleSheet xslSource = PortalComponentHome.getXsl( PORTAL_COMPONENT_SITE_MAP_ID, MODE_ADMIN );

  93.         // Added in v1.3
  94.         // Add a path param for choose url to use in admin or normal mode
  95.         Map<String, String> mapParamRequest = new HashMap<>( );
  96.         mapParamRequest.put( PARAMETER_SITE_PATH, AppPropertiesService.getProperty( PROPERTY_ADMIN_PATH ) );

  97.         Properties outputProperties = ModeHome.getOuputXslProperties( MODE_ADMIN );

  98.         Map<String, Object> model = new HashMap<>( );
  99.         XmlTransformerService xmlTransformerService = new XmlTransformerService( );
  100.         String map = xmlTransformerService.transformBySourceWithXslCache( strArborescenceXml.toString( ), xslSource, mapParamRequest, outputProperties );

  101.         String strPageId = request.getParameter( PARAMETER_PAGE_ID );
  102.         int nPageId = ( strPageId != null ) ? Integer.parseInt( strPageId ) : 1;
  103.         Page page = PageHome.getPage( nPageId );

  104.         model.put( MARK_PAGE, page );
  105.         model.put( MARKER_MAP_SITE, map );

  106.         HtmlTemplate t = AppTemplateService.getTemplate( TEMPLATE_MAP_SITE, getLocale( ), model );

  107.         return t.getHtml();
  108.     }

  109.     /**
  110.      * Build recursively the XML document containing the arborescence of the site pages
  111.      *
  112.      * @param request
  113.      *            The HttpServletRequest
  114.      * @param strXmlArborescence
  115.      *            The buffer in which adding the current page of the arborescence
  116.      * @param nPageId
  117.      *            The current page of the recursive course
  118.      * @param nLevel
  119.      *            The depth level of the page in the arborescence
  120.      * @param strCurrentPageId
  121.      *            the id of the current page
  122.      * @param strCssId
  123.      *            The id Css for menu tree
  124.      */
  125.     private void findPages( HttpServletRequest request, StringBuffer strXmlArborescence, int nPageId, int nLevel, String strCurrentPageId,
  126.             StringBuilder strCssId )
  127.     {
  128.         Page page = PageHome.getPage( nPageId );

  129.         AdminUser user = AdminUserService.getAdminUser( request );
  130.         String strPageId = Integer.toString( nPageId );

  131.         boolean bAuthorizationPage;

  132.         if ( nPageId == PortalService.getRootPageId( ) )
  133.         {
  134.             bAuthorizationPage = true;
  135.         }
  136.         else
  137.         {
  138.             // Control the node_status
  139.             if ( page.getNodeStatus( ) != 0 )
  140.             {
  141.                 Page parentPage = PageHome.getPage( page.getParentPageId( ) );
  142.                 int nParentPageNodeStatus = parentPage.getNodeStatus( );
  143.                 int nParentPageId = parentPage.getId( );

  144.                 // If 0 the page have a node authorization, else
  145.                 // the parent page node_status must be controlled
  146.                 // until it is equal to 0
  147.                 while ( nParentPageNodeStatus != 0 )
  148.                 {
  149.                     parentPage = PageHome.getPage( nParentPageId );
  150.                     nParentPageNodeStatus = parentPage.getNodeStatus( );
  151.                     nParentPageId = parentPage.getParentPageId( );
  152.                 }

  153.                 strPageId = Integer.toString( parentPage.getId( ) );
  154.             }

  155.             bAuthorizationPage = RBACService.isAuthorized( Page.RESOURCE_TYPE, strPageId, PageResourceIdService.PERMISSION_VIEW, user );
  156.         }

  157.         XmlUtil.beginElement( strXmlArborescence, XmlContent.TAG_PAGE );

  158.         if ( bAuthorizationPage )
  159.         {
  160.             XmlUtil.addElementHtml( strXmlArborescence, XmlContent.TAG_CURRENT_PAGE_ID, strCurrentPageId );
  161.             XmlUtil.addElement( strXmlArborescence, XmlContent.TAG_PAGE_ID, page.getId( ) );
  162.             XmlUtil.addElementHtml( strXmlArborescence, XmlContent.TAG_PAGE_NAME, page.getName( ) );
  163.             XmlUtil.addElement( strXmlArborescence, XmlContent.TAG_PAGE_DESCRIPTION, page.getDescription( ) );
  164.             XmlUtil.addElement( strXmlArborescence, XmlContent.TAG_PAGE_LEVEL, nLevel );
  165.             XmlUtil.addElement( strXmlArborescence, XmlContent.TAG_PARENT_PAGE_ID, page.getParentPageId( ) );
  166.             XmlUtil.addElement( strXmlArborescence, TAG_PAGE_ROLE, page.getRole( ) );

  167.             AdminPageJspBean adminPage = new AdminPageJspBean( );

  168.             if ( page.getImageContent( ) != null )
  169.             {
  170.                 int nImageLength = page.getImageContent( ).length;

  171.                 if ( nImageLength >= 1 )
  172.                 {
  173.                     XmlUtil.addElement( strXmlArborescence, XmlContent.TAG_PAGE_IMAGE, adminPage.getResourceImagePage( page, strPageId ) );
  174.                 }
  175.             }
  176.         }

  177.         XmlUtil.beginElement( strXmlArborescence, XmlContent.TAG_CHILD_PAGES_LIST );

  178.         for ( Page pageChild : PageHome.getChildPagesMinimalData( nPageId ) )
  179.         {
  180.             findPages( request, strXmlArborescence, pageChild.getId( ), nLevel + 1, strCurrentPageId, strCssId );
  181.             strCssId.append( "initializeMenu('menu" + pageChild.getId( ) + "' , 'actuator" + pageChild.getId( ) + "');\n" );
  182.         }

  183.         XmlUtil.endElement( strXmlArborescence, XmlContent.TAG_CHILD_PAGES_LIST );

  184.         if ( bAuthorizationPage )
  185.         {
  186.             XmlUtil.addElementHtml( strXmlArborescence, TAG_CSS_ID, strCssId.toString( ) );
  187.         }

  188.         XmlUtil.endElement( strXmlArborescence, XmlContent.TAG_PAGE );
  189.     }
  190. }