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.includes;
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.service.content.PageData;
43  import fr.paris.lutece.portal.service.html.XmlTransformerService;
44  import fr.paris.lutece.portal.service.includes.PageInclude;
45  import fr.paris.lutece.portal.service.portal.PortalMenuService;
46  import fr.paris.lutece.portal.service.portal.PortalService;
47  import fr.paris.lutece.portal.service.util.AppLogService;
48  import fr.paris.lutece.portal.service.util.AppPropertiesService;
49  import fr.paris.lutece.portal.web.constants.Markers;
50  import fr.paris.lutece.portal.web.constants.Parameters;
51  import fr.paris.lutece.util.xml.XmlUtil;
52  
53  import java.util.Collection;
54  import java.util.HashMap;
55  import java.util.Map;
56  import java.util.Properties;
57  
58  import javax.servlet.http.HttpServletRequest;
59  
60  
61  /**
62   * This class provides  the list of the page associated by the main menu of the site
63   */
64  public class TreeMenuInclude implements PageInclude
65  {
66      /////////////////////////////////////////////////////////////////////////////////////////////
67      // Constants
68      private static final int PORTAL_COMPONENT_MENU_TREE = 7;
69  
70      // Properties
71      private static final String PROPERTY_ROOT_TREE = "lutece.root.tree";
72  
73      /**
74       * Substitue specific Freemarker markers in the page template.
75       * @param rootModel the HashMap containing markers to substitute
76       * @param data A PageData object containing applications data
77       * @param nMode The current mode
78       * @param request The HTTP request
79       */
80      public void fillTemplate( Map<String, Object> rootModel, PageData data, int nMode, HttpServletRequest request )
81      {
82          if ( request != null )
83          {
84              int nCurrentPageId;
85  
86              try
87              {
88                  nCurrentPageId = ( request.getParameter( Parameters.PAGE_ID ) == null )
89                      ? PortalService.getRootPageId(  ) : Integer.parseInt( request.getParameter( Parameters.PAGE_ID ) );
90              }
91              catch ( NumberFormatException nfe )
92              {
93                  AppLogService.info( "TreeMenuInclude.fillTemplate() : " + nfe.getLocalizedMessage(  ) );
94                  nCurrentPageId = 0;
95              }
96  
97              data.setTreeMenu( buildTreeMenuContent( nCurrentPageId, nMode, request ) );
98              rootModel.put( Markers.PAGE_TREE_MENU, ( data.getTreeMenu(  ) == null ) ? "" : data.getTreeMenu(  ) );
99          }
100     }
101 
102     /**
103      * Builds the tree menu bar
104      *
105      * @param nIdPage The page id
106      * @param nMode the mode id
107          * @param request The HttpServletRequest
108      * @return The list of the tree menus layed out with the stylesheet of the mode
109      */
110     public String buildTreeMenuContent( int nIdPage, int nMode, HttpServletRequest request )
111     {
112         StringBuffer strXml = new StringBuffer(  );
113 
114         String strCurrentPageId = Integer.toString( nIdPage );
115 
116         String strTreeOnRoot = AppPropertiesService.getProperty( PROPERTY_ROOT_TREE );
117         Collection<Page> listPagesMenu;
118 
119         // If the current page is the home page or the string strTreeOnRoot equals false, not display the treeMenu
120         if ( strTreeOnRoot.equalsIgnoreCase( "true" ) )
121         {
122             listPagesMenu = PageHome.getChildPagesMinimalData( getPageTree( nIdPage ) );
123         }
124         else
125         {
126             listPagesMenu = PageHome.getChildPagesMinimalData( nIdPage );
127         }
128 
129         strXml.append( XmlUtil.getXmlHeader(  ) );
130         XmlUtil.beginElement( strXml, XmlContent.TAG_MENU_LIST );
131 
132         int nMenuIndex = 1;
133 
134         for ( Page menuPage : listPagesMenu )
135         {
136             if ( ( menuPage.isVisible( request ) ) || ( nMode == PortalMenuService.MODE_ADMIN ) )
137             {
138                 XmlUtil.beginElement( strXml, XmlContent.TAG_MENU );
139                 XmlUtil.addElement( strXml, XmlContent.TAG_MENU_INDEX, nMenuIndex );
140                 XmlUtil.addElement( strXml, XmlContent.TAG_PAGE_ID, menuPage.getId(  ) );
141                 XmlUtil.addElementHtml( strXml, XmlContent.TAG_PAGE_NAME, menuPage.getName(  ) );
142                 XmlUtil.addElementHtml( strXml, XmlContent.TAG_PAGE_DESCRIPTION, menuPage.getDescription(  ) );
143                 XmlUtil.addElementHtml( strXml, XmlContent.TAG_CURRENT_PAGE_ID, strCurrentPageId );
144 
145                 // Seek of the sub-menus
146                 XmlUtil.beginElement( strXml, XmlContent.TAG_SUBLEVEL_MENU_LIST );
147 
148                 Collection<Page> listSubLevelMenuPages = PageHome.getChildPagesMinimalData( menuPage.getId(  ) );
149                 int nSubLevelMenuIndex = 1;
150 
151                 for ( Page subLevelMenuPage : listSubLevelMenuPages )
152                 {
153                     if ( ( subLevelMenuPage.isVisible( request ) ) || ( nMode == PortalMenuService.MODE_ADMIN ) )
154                     {
155                         XmlUtil.beginElement( strXml, XmlContent.TAG_SUBLEVEL_MENU );
156                         XmlUtil.addElement( strXml, XmlContent.TAG_MENU_INDEX, nMenuIndex );
157                         XmlUtil.addElement( strXml, XmlContent.TAG_SUBLEVEL_INDEX, nSubLevelMenuIndex );
158                         XmlUtil.addElement( strXml, XmlContent.TAG_PAGE_ID, subLevelMenuPage.getId(  ) );
159                         XmlUtil.addElementHtml( strXml, XmlContent.TAG_PAGE_NAME, subLevelMenuPage.getName(  ) );
160                         XmlUtil.addElementHtml( strXml, XmlContent.TAG_PAGE_DESCRIPTION,
161                             subLevelMenuPage.getDescription(  ) );
162                         XmlUtil.addElementHtml( strXml, XmlContent.TAG_CURRENT_PAGE_ID, strCurrentPageId );
163                         XmlUtil.endElement( strXml, XmlContent.TAG_SUBLEVEL_MENU );
164                     }
165                 }
166 
167                 XmlUtil.endElement( strXml, XmlContent.TAG_SUBLEVEL_MENU_LIST );
168                 XmlUtil.endElement( strXml, XmlContent.TAG_MENU );
169                 nMenuIndex++;
170             }
171         }
172 
173         XmlUtil.endElement( strXml, XmlContent.TAG_MENU_LIST );
174 
175         StyleSheet xslSource;
176 
177         // Selection of the XSL stylesheet
178         switch ( nMode )
179         {
180             case PortalMenuService.MODE_NORMAL:
181             case PortalMenuService.MODE_ADMIN:
182                 xslSource = PortalComponentHome.getXsl( PORTAL_COMPONENT_MENU_TREE, PortalMenuService.MODE_NORMAL );
183 
184                 break;
185 
186             default:
187                 xslSource = PortalComponentHome.getXsl( PORTAL_COMPONENT_MENU_TREE, nMode );
188 
189                 break;
190         }
191 
192         Properties outputProperties = ModeHome.getOuputXslProperties( nMode );
193 
194         // Added in v1.3
195         // Add a path param for choose url to use in admin or normal mode
196         Map<String, String> mapParamRequest = new HashMap<String, String>(  );
197         PortalService.setXslPortalPath( mapParamRequest, nMode );
198 
199         XmlTransformerService xmlTransformerService = new XmlTransformerService(  );
200 
201         return xmlTransformerService.transformBySourceWithXslCache( strXml.toString(  ), xslSource, mapParamRequest,
202             outputProperties );
203     }
204 
205     /**
206      *
207      * @param nPageId The page identifier
208      * @return The parent page identifier
209      */
210     private int getPageTree( int nPageId )
211     {
212         Page page = PageHome.getPage( nPageId );
213         int nParentPageId = page.getParentPageId(  );
214 
215         if ( nParentPageId == 0 )
216         {
217             return nPageId;
218         }
219 
220         int nParentTree = nParentPageId;
221 
222         //while ( nParentPageId != 1 )
223         while ( nParentPageId != 0 )
224         {
225             nParentTree = nParentPageId;
226 
227             Page parentPage = PageHome.getPageWithoutImageContent( nParentPageId );
228             nParentPageId = parentPage.getParentPageId(  );
229         }
230 
231         return nParentTree;
232     }
233 }