View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de 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  /**
61   * This class provides the map of the pages on the site
62   */
63  public class AdminMapJspBean extends AdminFeaturesPageJspBean
64  {
65      // Right
66      public static final String RIGHT_MANAGE_ADMIN_SITE = "CORE_ADMIN_SITE";
67      private static final long serialVersionUID = 2871979154306491511L;
68  
69      // Markers
70      private static final String MARKER_MAP_SITE = "map_site";
71      private static final String MARK_PAGE = "page";
72  
73      // Templates
74      private static final String TEMPLATE_MAP_SITE = "admin/site/site_map.html";
75  
76      // Parameters
77      private static final String PARAMETER_SITE_PATH = "site-path";
78      private static final String PARAMETER_PAGE_ID = "page_id";
79  
80      // Properties
81      private static final String PROPERTY_ADMIN_PATH = "lutece.admin.path";
82  
83      // Xml Tags
84      private static final String TAG_CSS_ID = "css-id";
85      private static final String TAG_PAGE_ROLE = "page-role";
86      private static final int PORTAL_COMPONENT_SITE_MAP_ID = 8;
87      private static final int MODE_ADMIN = 1;
88  
89      /**
90       * Build or get in the cache the page which contains the site map depending on the mode
91       *
92       * @param request 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          StringBuffer strCssId = new StringBuffer(  );
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<String, String>(  );
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<String, Object>(  );
116         XmlTransformerService xmlTransformerService = new XmlTransformerService(  );
117         String map = xmlTransformerService.transformBySourceWithXslCache( strArborescenceXml.toString(  ), xslSource,
118                 mapParamRequest, outputProperties );
119 
120         String strPageId = request.getParameter( PARAMETER_PAGE_ID );
121         int nPageId = ( strPageId != null ) ? Integer.parseInt( strPageId ) : 1;
122         Page page = PageHome.getPage( nPageId );
123 
124         model.put( MARK_PAGE, page );
125         model.put( MARKER_MAP_SITE, map );
126 
127         HtmlTemplate t = AppTemplateService.getTemplate( TEMPLATE_MAP_SITE, getLocale(  ), model );
128 
129         return getAdminPage( t.getHtml(  ) );
130     }
131 
132     /**
133      * Build recursively the XML document containing the arborescence of the site pages
134      * @param request The HttpServletRequest
135      * @param strXmlArborescence The buffer in which adding the current page of the arborescence
136      * @param nPageId The current page of the recursive course
137      * @param nLevel The depth level of the page in the arborescence
138      * @param strCurrentPageId the id of the current page
139      * @param strCssId The id Css for menu tree
140      */
141     private void findPages( HttpServletRequest request, StringBuffer strXmlArborescence, int nPageId, int nLevel,
142         String strCurrentPageId, StringBuffer strCssId )
143     {
144         Page page = PageHome.getPage( nPageId );
145 
146         AdminUser user = AdminUserService.getAdminUser( request );
147         String strPageId = Integer.toString( nPageId );
148 
149         boolean bAuthorizationPage;
150 
151         if ( nPageId == PortalService.getRootPageId(  ) )
152         {
153             bAuthorizationPage = true;
154         }
155         else
156         {
157             // Control the node_status
158             if ( page.getNodeStatus(  ) != 0 )
159             {
160                 Page parentPage = PageHome.getPage( page.getParentPageId(  ) );
161                 int nParentPageNodeStatus = parentPage.getNodeStatus(  );
162                 int nParentPageId = parentPage.getId(  );
163 
164                 // If 0 the page have a node authorization, else
165                 // the parent page node_status must be controlled
166                 // until it is equal to 0
167                 while ( nParentPageNodeStatus != 0 )
168                 {
169                     parentPage = PageHome.getPage( nParentPageId );
170                     nParentPageNodeStatus = parentPage.getNodeStatus(  );
171                     nParentPageId = parentPage.getParentPageId(  );
172                 }
173 
174                 strPageId = Integer.toString( parentPage.getId(  ) );
175             }
176 
177             bAuthorizationPage = RBACService.isAuthorized( Page.RESOURCE_TYPE, strPageId,
178                     PageResourceIdService.PERMISSION_VIEW, user );
179         }
180 
181         XmlUtil.beginElement( strXmlArborescence, XmlContent.TAG_PAGE );
182 
183         if ( bAuthorizationPage )
184         {
185             XmlUtil.addElementHtml( strXmlArborescence, XmlContent.TAG_CURRENT_PAGE_ID, strCurrentPageId );
186             XmlUtil.addElement( strXmlArborescence, XmlContent.TAG_PAGE_ID, page.getId(  ) );
187             XmlUtil.addElementHtml( strXmlArborescence, XmlContent.TAG_PAGE_NAME, page.getName(  ) );
188             XmlUtil.addElement( strXmlArborescence, XmlContent.TAG_PAGE_DESCRIPTION, page.getDescription(  ) );
189             XmlUtil.addElement( strXmlArborescence, XmlContent.TAG_PAGE_LEVEL, nLevel );
190             XmlUtil.addElement( strXmlArborescence, XmlContent.TAG_PARENT_PAGE_ID, page.getParentPageId(  ) );
191             XmlUtil.addElement( strXmlArborescence, TAG_PAGE_ROLE, page.getRole(  ) );
192 
193             AdminPageJspBean adminPage = new AdminPageJspBean(  );
194 
195             if ( page.getImageContent(  ) != null )
196             {
197                 int nImageLength = page.getImageContent(  ).length;
198 
199                 if ( nImageLength >= 1 )
200                 {
201                     XmlUtil.addElement( strXmlArborescence, XmlContent.TAG_PAGE_IMAGE,
202                         adminPage.getResourceImagePage( page, strPageId ) );
203                 }
204             }
205         }
206 
207         XmlUtil.beginElement( strXmlArborescence, XmlContent.TAG_CHILD_PAGES_LIST );
208 
209         for ( Page pageChild : PageHome.getChildPagesMinimalData( nPageId ) )
210         {
211             findPages( request, strXmlArborescence, pageChild.getId(  ), nLevel + 1, strCurrentPageId, strCssId );
212             strCssId.append( "initializeMenu('menu" + pageChild.getId(  ) + "' , 'actuator" + pageChild.getId(  ) +
213                 "');\n" );
214         }
215 
216         XmlUtil.endElement( strXmlArborescence, XmlContent.TAG_CHILD_PAGES_LIST );
217 
218         if ( bAuthorizationPage )
219         {
220             XmlUtil.addElementHtml( strXmlArborescence, TAG_CSS_ID, strCssId.toString(  ) );
221         }
222 
223         XmlUtil.endElement( strXmlArborescence, XmlContent.TAG_PAGE );
224     }
225 }