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.insert;
35  
36  import fr.paris.lutece.portal.service.html.EncodingService;
37  import fr.paris.lutece.portal.service.insert.InsertResourceIdService;
38  import fr.paris.lutece.portal.service.insert.InsertService;
39  import fr.paris.lutece.portal.service.insert.InsertServiceManager;
40  import fr.paris.lutece.portal.service.message.AdminMessage;
41  import fr.paris.lutece.portal.service.message.AdminMessageService;
42  import fr.paris.lutece.portal.service.rbac.RBACService;
43  import fr.paris.lutece.portal.service.template.AppTemplateService;
44  import fr.paris.lutece.portal.web.admin.AdminFeaturesPageJspBean;
45  import fr.paris.lutece.util.html.HtmlTemplate;
46  
47  import java.util.Collection;
48  import java.util.HashMap;
49  import java.util.Map;
50  
51  import javax.servlet.http.HttpServletRequest;
52  
53  /**
54   * Provides the generic interface to choose a particular link service
55   */
56  public class InsertServiceSelectorJspBean extends AdminFeaturesPageJspBean
57  {
58      /**
59       * Right to manage this feature
60       */
61      public static final String RIGHT_MANAGE_LINK_SERVICE = "CORE_LINK_SERVICE_MANAGEMENT";
62  
63      /**
64       * Session key to set the content to insert
65       */
66      public static final String SESSION_INSERT = "portal.session.insertServiceSelector.contentToInsert";
67      private static final long serialVersionUID = 3395846045509139922L;
68  
69      // Constants
70      private static final String TEMPLATE_INSERT_TYPE_PAGE = "admin/insert/page_insertservice.html";
71      private static final String TEMPLATE_INSERT_TYPE_PAGE_DATA = "admin/insert/page_insertservice_data.html";
72      private static final String TEMPLATE_INSERT_INTO_ELEMENT = "admin/insert/insert_into_element.html";
73      private static final String TEMPLATE_INSERT_INTO_ELEMENT2 = "admin/insert/insert_into_element2.html";
74      private static final String MSG_NO_SERVICE_AVAILABLE = "portal.insert.message.noServiceAvailable";
75  
76      /** name of the links type parameter */
77      private static final String PARAMETER_INSERT_SERVICE_TYPE = "insert_service_type";
78      private static final String PARAMETER_MODE = "mode";
79      private static final String PARAMETER_INPUT = "input";
80      private static final String PARAMETER_INSERT = "insert";
81      private static final String PARAMETER_SELECTED_TEXT = "selected_text";
82      private static final String MARK_INSERT_SERVICES_LIST = "insert_services_list";
83      private static final String MARK_SELECTED_TEXT = "selected_text";
84      private static final String MARK_INPUT = "input";
85      private static final String MARK_INSERT = "insert";
86  
87      /**
88       * build the insert service selection page
89       *
90       * @param request
91       *            HTTP request
92       * @return HTML code of the page as String
93       */
94      public String getServicesListPage( HttpServletRequest request )
95      {
96          String strInput = request.getParameter( PARAMETER_INPUT );
97          String strText = request.getParameter( PARAMETER_SELECTED_TEXT );
98          String strMode = request.getParameter( PARAMETER_MODE );
99  
100         // Encode the HTML code to insert
101         strText = EncodingService.encodeUrl( strText );
102 
103         Collection<InsertService> listServices = InsertServiceManager.getInsertServicesList( );
104 
105         // building from a template
106         Map<String, Object> model = new HashMap<>( );
107         model.put( MARK_INSERT_SERVICES_LIST, RBACService.getAuthorizedCollection( listServices, InsertResourceIdService.PERMISSION_USE, getUser( ) ) );
108         model.put( MARK_SELECTED_TEXT, strText );
109         model.put( MARK_INPUT, strInput );
110         
111         HtmlTemplate t;
112         
113         if ("2".equals(strMode))
114         {
115             t = AppTemplateService.getTemplate( TEMPLATE_INSERT_TYPE_PAGE_DATA, getLocale( ), model );
116         }
117         else
118         {
119             t = AppTemplateService.getTemplate( TEMPLATE_INSERT_TYPE_PAGE, getLocale( ), model );
120         }
121         return t.getHtml( );
122     }
123 
124     /**
125      * Call the selected service selection UI
126      *
127      * @param request
128      *            HTTP request
129      * @return the HTML code of the selection page for selected service
130      */
131     public String displayService( HttpServletRequest request )
132     {
133         String strInsertService = request.getParameter( PARAMETER_INSERT_SERVICE_TYPE );
134 
135         if ( ( strInsertService != null ) && !strInsertService.equals( "" ) )
136         {
137             InsertService is = InsertServiceManager.getInsertService( strInsertService );
138 
139             return is.getSelectorUI( request );
140         }
141         else
142         {
143             return AdminMessageService.getMessageUrl( request, MSG_NO_SERVICE_AVAILABLE, AdminMessage.TYPE_STOP );
144         }
145     }
146 
147     /**
148      * Build the JSP that provides the HTML code insertion into the Editor via a javascript call
149      *
150      * @param request
151      *            HTTP request
152      * @return the HTML code of the page that insert code into the editor
153      */
154     public String doInsertIntoElement( HttpServletRequest request )
155     {
156         String strMode = request.getParameter( PARAMETER_MODE );
157         String strInput = request.getParameter( PARAMETER_INPUT );
158         String strInsert = (String) request.getSession( ).getAttribute( SESSION_INSERT );
159 
160         if ( strInsert == null )
161         {
162             strInsert = request.getParameter( PARAMETER_INSERT );
163         }
164 
165         request.getSession( ).removeAttribute( SESSION_INSERT );
166 
167         Map<String, String> model = new HashMap<>( );
168         model.put( MARK_INPUT, strInput );
169         model.put( MARK_INSERT, strInsert );
170 
171         HtmlTemplate template;
172 
173         if ( strMode.compareTo( "2" ) == 0 )
174         {
175             template = AppTemplateService.getTemplate( TEMPLATE_INSERT_INTO_ELEMENT2, getLocale( ), model );
176         }
177         else
178         {
179             template = AppTemplateService.getTemplate( TEMPLATE_INSERT_INTO_ELEMENT, getLocale( ), model );
180         }
181 
182         return template.getHtml( );
183     }
184 }