View Javadoc
1   /*
2    * Copyright (c) 2002-2020, 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.plugins.quicklinks.web;
35  
36  import fr.paris.lutece.plugins.quicklinks.business.Quicklinks;
37  import fr.paris.lutece.plugins.quicklinks.business.QuicklinksFilter;
38  import fr.paris.lutece.plugins.quicklinks.business.QuicklinksHome;
39  import fr.paris.lutece.plugins.quicklinks.business.QuicklinksType;
40  import fr.paris.lutece.plugins.quicklinks.service.QuicklinksPlugin;
41  import fr.paris.lutece.portal.service.content.PageData;
42  import fr.paris.lutece.portal.service.i18n.I18nService;
43  import fr.paris.lutece.portal.service.includes.PageInclude;
44  import fr.paris.lutece.portal.service.plugin.Plugin;
45  import fr.paris.lutece.portal.service.plugin.PluginService;
46  import fr.paris.lutece.portal.service.security.LuteceUser;
47  import fr.paris.lutece.portal.service.security.SecurityService;
48  import fr.paris.lutece.portal.service.template.AppTemplateService;
49  import fr.paris.lutece.portal.service.util.AppLogService;
50  import fr.paris.lutece.portal.service.util.AppPropertiesService;
51  import fr.paris.lutece.util.html.HtmlTemplate;
52  
53  import org.xml.sax.InputSource;
54  import org.xml.sax.SAXException;
55  
56  import java.io.ByteArrayInputStream;
57  import java.io.IOException;
58  
59  import java.util.Collection;
60  import java.util.HashMap;
61  import java.util.Locale;
62  import java.util.Map;
63  
64  import javax.servlet.http.HttpServletRequest;
65  
66  import javax.xml.parsers.ParserConfigurationException;
67  
68  /**
69   * This class provides the user interface to manage quicklinks features
70   */
71  public class QuicklinksInclude implements PageInclude
72  {
73      // Properties
74      private static final String PROPERTY_QUICKLINKS_MARKER_PREFIX = "quicklinks.include.marker.prefix";
75  
76      // Templates
77      private static final String TEMPLATE_QUICKLINKS_INCLUDE = "skin/plugins/quicklinks/quicklinks_include.html";
78  
79      // Markers
80      private static final String MARK_QUICKLINKS = "quicklinks";
81      private static final String MARK_PLUGIN = "plugin";
82      private static final String MARK_LOCALE = "locale";
83  
84      // Default Marker prefix if nothing defined in properties file
85      private static final String MARK_DEFAULT_QUICKLINKS_MARKER_PREFIX = "quicklinks_";
86  
87      /**
88       * Substitute specific Freemarker markers in the page template.
89       * 
90       * @param rootModel
91       *            the HashMap containing markers to substitute
92       * @param data
93       *            A PageData object containing applications data
94       * @param nMode
95       *            The current mode
96       * @param request
97       *            The HTTP request
98       */
99      public void fillTemplate( Map<String, Object> rootModel, PageData data, int nMode, HttpServletRequest request )
100     {
101         Plugin plugin = PluginService.getPlugin( QuicklinksPlugin.PLUGIN_NAME );
102         Locale locale = getLocale( request );
103 
104         QuicklinksFilter filter = getFilter( request );
105 
106         Collection<Quicklinks> listQuicklinks = QuicklinksHome.findbyFilter( filter, plugin );
107 
108         // For each quicklinks, generate HTML code and put marker with the associated code in rootModel
109         for ( Quicklinks quicklinks : listQuicklinks )
110         {
111             rootModel.put( getQuicklinksMarkerPrefix( ) + String.valueOf( quicklinks.getId( ) ), getTemplateHtmlForQuicklinks( quicklinks, plugin, locale ) );
112         }
113     }
114 
115     /**
116      * Get the quicklinks marker prefix to use in template
117      * 
118      * @return the {@link Quicklinks} marker prefix
119      */
120     public static String getQuicklinksMarkerPrefix( )
121     {
122         return AppPropertiesService.getProperty( PROPERTY_QUICKLINKS_MARKER_PREFIX, MARK_DEFAULT_QUICKLINKS_MARKER_PREFIX );
123     }
124 
125     /**
126      * Generate the HTML code for the specified {@link Quicklinks}
127      * 
128      * @param quicklinks
129      *            The {@link Quicklinks}
130      * @param plugin
131      *            The {@link Plugin}
132      * @param locale
133      *            The {@link Locale}
134      * @return The HTML code
135      */
136     private String getTemplateHtmlForQuicklinks( Quicklinks quicklinks, Plugin plugin, Locale locale )
137     {
138         HashMap<String, Object> model = new HashMap<>( );
139         model.put( MARK_QUICKLINKS, quicklinks );
140 
141         InputSource in = new InputSource( new ByteArrayInputStream( quicklinks.getXml( plugin, locale ).toString( ).getBytes( ) ) );
142 
143         try
144         {
145             model.put( MARK_QUICKLINKS, freemarker.ext.dom.NodeModel.parse( in ) );
146         }
147         catch( ParserConfigurationException | IOException | SAXException e )
148         {
149             AppLogService.error( e );
150         }
151 
152         model.put( MARK_PLUGIN, plugin );
153         model.put( MARK_LOCALE, locale );
154 
155         HtmlTemplate templateList = AppTemplateService.getTemplate( TEMPLATE_QUICKLINKS_INCLUDE, Locale.getDefault( ), model );
156 
157         return templateList.getHtml( );
158     }
159 
160     /**
161      * Get the locale from request of the default locale if request is null
162      * 
163      * @param request
164      *            The {@link HttpServletRequest}
165      * @return The locale
166      */
167     private Locale getLocale( HttpServletRequest request )
168     {
169         Locale locale = null;
170 
171         if ( request != null )
172         {
173             locale = request.getLocale( );
174         }
175         else
176         {
177             locale = I18nService.getDefaultLocale( );
178         }
179 
180         return locale;
181     }
182 
183     /**
184      * Get the {@link QuicklinksFilter}
185      * 
186      * @param request
187      *            The {@link HttpServletRequest}
188      * @return The {@link QuicklinksFilter}
189      */
190     private QuicklinksFilter getFilter( HttpServletRequest request )
191     {
192         QuicklinksFilterks/business/QuicklinksFilter.html#QuicklinksFilter">QuicklinksFilter filter = new QuicklinksFilter( );
193 
194         filter.setType( QuicklinksType.INCLUDE );
195         filter.setEnabled( true );
196 
197         if ( SecurityService.isAuthenticationEnable( ) && ( request != null ) )
198         {
199             LuteceUser user = SecurityService.getInstance( ).getRegisteredUser( request );
200 
201             if ( user != null )
202             {
203                 String [ ] rolesUser = SecurityService.getInstance( ).getRolesByUser( user );
204                 filter.setRoleKeys( rolesUser );
205             }
206         }
207 
208         return filter;
209     }
210 }