View Javadoc
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  
36  import fr.paris.lutece.portal.business.XmlContent;
37  import fr.paris.lutece.portal.business.page.Page;
38  import fr.paris.lutece.portal.business.page.PageHome;
39  import fr.paris.lutece.portal.business.portalcomponent.PortalComponentHome;
40  import fr.paris.lutece.portal.business.style.ModeHome;
41  import fr.paris.lutece.portal.business.stylesheet.StyleSheet;
42  import fr.paris.lutece.portal.business.user.AdminUser;
43  import fr.paris.lutece.portal.service.admin.AdminUserService;
44  import fr.paris.lutece.portal.service.html.XmlTransformerService;
45  import fr.paris.lutece.portal.service.page.PageResourceIdService;
46  import fr.paris.lutece.portal.service.portal.PortalService;
47  import fr.paris.lutece.portal.service.rbac.RBACService;
48  import fr.paris.lutece.portal.service.template.AppTemplateService;
49  import fr.paris.lutece.portal.service.util.AppPropertiesService;
50  import fr.paris.lutece.util.html.HtmlTemplate;
51  import fr.paris.lutece.util.xml.XmlUtil;
52  
53  import java.util.HashMap;
54  import java.util.Map;
55  import java.util.Properties;
56  
57  import javax.servlet.http.HttpServletRequest;
58  
59  /**
60   * This class provides the map of the pages on the site
61   */
62  public class AdminMapJspBean extends AdminFeaturesPageJspBean
63  {
64      // Right
65      public static final String RIGHT_MANAGE_ADMIN_SITE = "CORE_ADMIN_SITE";
66      private static final long serialVersionUID = 2871979154306491511L;
67  
68      // Markers
69      private static final String MARKER_MAP_SITE = "map_site";
70      private static final String MARK_PAGE = "page";
71  
72      // Templates
73      private static final String TEMPLATE_MAP_SITE = "admin/site/site_map.html";
74  
75      // Parameters
76      private static final String PARAMETER_SITE_PATH = "site-path";
77      private static final String PARAMETER_PAGE_ID = "page_id";
78  
79      // Properties
80      private static final String PROPERTY_ADMIN_PATH = "lutece.admin.path";
81  
82      // Xml Tags
83      private static final String TAG_CSS_ID = "css-id";
84      private static final String TAG_PAGE_ROLE = "page-role";
85      private static final int PORTAL_COMPONENT_SITE_MAP_ID = 8;
86      private static final int MODE_ADMIN = 1;
87  
88      /**
89       * Build or get in the cache the page which contains the site map depending on the mode
90       *
91       * @param request
92       *            The Http request
93       * @return The content of the site map
94       */
95      public String getMap( HttpServletRequest request )
96      {
97          StringBuffer strArborescenceXml = new StringBuffer( );
98  
99          StringBuilder strCssId = new StringBuilder( );
100         int nLevel = 0;
101 
102         String strCurrentPageId = request.getParameter( PARAMETER_PAGE_ID );
103 
104         findPages( request, strArborescenceXml, PortalService.getRootPageId( ), nLevel, strCurrentPageId, strCssId );
105 
106         StyleSheet xslSource = PortalComponentHome.getXsl( PORTAL_COMPONENT_SITE_MAP_ID, MODE_ADMIN );
107 
108         // Added in v1.3
109         // Add a path param for choose url to use in admin or normal mode
110         Map<String, String> mapParamRequest = new HashMap<>( );
111         mapParamRequest.put( PARAMETER_SITE_PATH, AppPropertiesService.getProperty( PROPERTY_ADMIN_PATH ) );
112 
113         Properties outputProperties = ModeHome.getOuputXslProperties( MODE_ADMIN );
114 
115         Map<String, Object> model = new HashMap<>( );
116         XmlTransformerServicervice.html#XmlTransformerService">XmlTransformerService xmlTransformerService = new XmlTransformerService( );
117         String map = xmlTransformerService.transformBySourceWithXslCache( strArborescenceXml.toString( ), xslSource, mapParamRequest, outputProperties );
118 
119         String strPageId = request.getParameter( PARAMETER_PAGE_ID );
120         int nPageId = ( strPageId != null ) ? Integer.parseInt( strPageId ) : 1;
121         Page page = PageHome.getPage( nPageId );
122 
123         model.put( MARK_PAGE, page );
124         model.put( MARKER_MAP_SITE, map );
125 
126         HtmlTemplate t = AppTemplateService.getTemplate( TEMPLATE_MAP_SITE, getLocale( ), model );
127 
128         return t.getHtml();
129     }
130 
131     /**
132      * Build recursively the XML document containing the arborescence of the site pages
133      * 
134      * @param request
135      *            The HttpServletRequest
136      * @param strXmlArborescence
137      *            The buffer in which adding the current page of the arborescence
138      * @param nPageId
139      *            The current page of the recursive course
140      * @param nLevel
141      *            The depth level of the page in the arborescence
142      * @param strCurrentPageId
143      *            the id of the current page
144      * @param strCssId
145      *            The id Css for menu tree
146      */
147     private void findPages( HttpServletRequest request, StringBuffer strXmlArborescence, int nPageId, int nLevel, String strCurrentPageId,
148             StringBuilder strCssId )
149     {
150         Page page = PageHome.getPage( nPageId );
151 
152         AdminUser user = AdminUserService.getAdminUser( request );
153         String strPageId = Integer.toString( nPageId );
154 
155         boolean bAuthorizationPage;
156 
157         if ( nPageId == PortalService.getRootPageId( ) )
158         {
159             bAuthorizationPage = true;
160         }
161         else
162         {
163             // Control the node_status
164             if ( page.getNodeStatus( ) != 0 )
165             {
166                 Page parentPage = PageHome.getPage( page.getParentPageId( ) );
167                 int nParentPageNodeStatus = parentPage.getNodeStatus( );
168                 int nParentPageId = parentPage.getId( );
169 
170                 // If 0 the page have a node authorization, else
171                 // the parent page node_status must be controlled
172                 // until it is equal to 0
173                 while ( nParentPageNodeStatus != 0 )
174                 {
175                     parentPage = PageHome.getPage( nParentPageId );
176                     nParentPageNodeStatus = parentPage.getNodeStatus( );
177                     nParentPageId = parentPage.getParentPageId( );
178                 }
179 
180                 strPageId = Integer.toString( parentPage.getId( ) );
181             }
182 
183             bAuthorizationPage = RBACService.isAuthorized( Page.RESOURCE_TYPE, strPageId, PageResourceIdService.PERMISSION_VIEW, user );
184         }
185 
186         XmlUtil.beginElement( strXmlArborescence, XmlContent.TAG_PAGE );
187 
188         if ( bAuthorizationPage )
189         {
190             XmlUtil.addElementHtml( strXmlArborescence, XmlContent.TAG_CURRENT_PAGE_ID, strCurrentPageId );
191             XmlUtil.addElement( strXmlArborescence, XmlContent.TAG_PAGE_ID, page.getId( ) );
192             XmlUtil.addElementHtml( strXmlArborescence, XmlContent.TAG_PAGE_NAME, page.getName( ) );
193             XmlUtil.addElement( strXmlArborescence, XmlContent.TAG_PAGE_DESCRIPTION, page.getDescription( ) );
194             XmlUtil.addElement( strXmlArborescence, XmlContent.TAG_PAGE_LEVEL, nLevel );
195             XmlUtil.addElement( strXmlArborescence, XmlContent.TAG_PARENT_PAGE_ID, page.getParentPageId( ) );
196             XmlUtil.addElement( strXmlArborescence, TAG_PAGE_ROLE, page.getRole( ) );
197 
198             AdminPageJspBeanminPageJspBean.html#AdminPageJspBean">AdminPageJspBean adminPage = new AdminPageJspBean( );
199 
200             if ( page.getImageContent( ) != null )
201             {
202                 int nImageLength = page.getImageContent( ).length;
203 
204                 if ( nImageLength >= 1 )
205                 {
206                     XmlUtil.addElement( strXmlArborescence, XmlContent.TAG_PAGE_IMAGE, adminPage.getResourceImagePage( page, strPageId ) );
207                 }
208             }
209         }
210 
211         XmlUtil.beginElement( strXmlArborescence, XmlContent.TAG_CHILD_PAGES_LIST );
212 
213         for ( Page pageChild : PageHome.getChildPagesMinimalData( nPageId ) )
214         {
215             findPages( request, strXmlArborescence, pageChild.getId( ), nLevel + 1, strCurrentPageId, strCssId );
216             strCssId.append( "initializeMenu('menu" + pageChild.getId( ) + "' , 'actuator" + pageChild.getId( ) + "');\n" );
217         }
218 
219         XmlUtil.endElement( strXmlArborescence, XmlContent.TAG_CHILD_PAGES_LIST );
220 
221         if ( bAuthorizationPage )
222         {
223             XmlUtil.addElementHtml( strXmlArborescence, TAG_CSS_ID, strCssId.toString( ) );
224         }
225 
226         XmlUtil.endElement( strXmlArborescence, XmlContent.TAG_PAGE );
227     }
228 }