InsertServiceSelectorJspBean.java

  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. import fr.paris.lutece.portal.service.html.EncodingService;
  36. import fr.paris.lutece.portal.service.insert.InsertResourceIdService;
  37. import fr.paris.lutece.portal.service.insert.InsertService;
  38. import fr.paris.lutece.portal.service.insert.InsertServiceManager;
  39. import fr.paris.lutece.portal.service.message.AdminMessage;
  40. import fr.paris.lutece.portal.service.message.AdminMessageService;
  41. import fr.paris.lutece.portal.service.rbac.RBACService;
  42. import fr.paris.lutece.portal.service.template.AppTemplateService;
  43. import fr.paris.lutece.portal.web.admin.AdminFeaturesPageJspBean;
  44. import fr.paris.lutece.util.html.HtmlTemplate;

  45. import java.util.Collection;
  46. import java.util.HashMap;
  47. import java.util.Map;

  48. import javax.servlet.http.HttpServletRequest;

  49. /**
  50.  * Provides the generic interface to choose a particular link service
  51.  */
  52. public class InsertServiceSelectorJspBean extends AdminFeaturesPageJspBean
  53. {
  54.     /**
  55.      * Right to manage this feature
  56.      */
  57.     public static final String RIGHT_MANAGE_LINK_SERVICE = "CORE_LINK_SERVICE_MANAGEMENT";

  58.     /**
  59.      * Session key to set the content to insert
  60.      */
  61.     public static final String SESSION_INSERT = "portal.session.insertServiceSelector.contentToInsert";
  62.     private static final long serialVersionUID = 3395846045509139922L;

  63.     // Constants
  64.     private static final String TEMPLATE_INSERT_TYPE_PAGE = "admin/insert/page_insertservice.html";
  65.     private static final String TEMPLATE_INSERT_TYPE_PAGE_DATA = "admin/insert/page_insertservice_data.html";
  66.     private static final String TEMPLATE_INSERT_INTO_ELEMENT = "admin/insert/insert_into_element.html";
  67.     private static final String TEMPLATE_INSERT_INTO_ELEMENT2 = "admin/insert/insert_into_element2.html";
  68.     private static final String MSG_NO_SERVICE_AVAILABLE = "portal.insert.message.noServiceAvailable";

  69.     /** name of the links type parameter */
  70.     private static final String PARAMETER_INSERT_SERVICE_TYPE = "insert_service_type";
  71.     private static final String PARAMETER_MODE = "mode";
  72.     private static final String PARAMETER_INPUT = "input";
  73.     private static final String PARAMETER_INSERT = "insert";
  74.     private static final String PARAMETER_SELECTED_TEXT = "selected_text";
  75.     private static final String MARK_INSERT_SERVICES_LIST = "insert_services_list";
  76.     private static final String MARK_SELECTED_TEXT = "selected_text";
  77.     private static final String MARK_INPUT = "input";
  78.     private static final String MARK_INSERT = "insert";

  79.     /**
  80.      * build the insert service selection page
  81.      *
  82.      * @param request
  83.      *            HTTP request
  84.      * @return HTML code of the page as String
  85.      */
  86.     public String getServicesListPage( HttpServletRequest request )
  87.     {
  88.         String strInput = request.getParameter( PARAMETER_INPUT );
  89.         String strText = request.getParameter( PARAMETER_SELECTED_TEXT );
  90.         String strMode = request.getParameter( PARAMETER_MODE );

  91.         // Encode the HTML code to insert
  92.         strText = EncodingService.encodeUrl( strText );

  93.         Collection<InsertService> listServices = InsertServiceManager.getInsertServicesList( );

  94.         // building from a template
  95.         Map<String, Object> model = new HashMap<>( );
  96.         model.put( MARK_INSERT_SERVICES_LIST, RBACService.getAuthorizedCollection( listServices, InsertResourceIdService.PERMISSION_USE, getUser( ) ) );
  97.         model.put( MARK_SELECTED_TEXT, strText );
  98.         model.put( MARK_INPUT, strInput );
  99.        
  100.         HtmlTemplate t;
  101.        
  102.         if ("2".equals(strMode))
  103.         {
  104.             t = AppTemplateService.getTemplate( TEMPLATE_INSERT_TYPE_PAGE_DATA, getLocale( ), model );
  105.         }
  106.         else
  107.         {
  108.             t = AppTemplateService.getTemplate( TEMPLATE_INSERT_TYPE_PAGE, getLocale( ), model );
  109.         }
  110.         return t.getHtml( );
  111.     }

  112.     /**
  113.      * Call the selected service selection UI
  114.      *
  115.      * @param request
  116.      *            HTTP request
  117.      * @return the HTML code of the selection page for selected service
  118.      */
  119.     public String displayService( HttpServletRequest request )
  120.     {
  121.         String strInsertService = request.getParameter( PARAMETER_INSERT_SERVICE_TYPE );

  122.         if ( ( strInsertService != null ) && !strInsertService.equals( "" ) )
  123.         {
  124.             InsertService is = InsertServiceManager.getInsertService( strInsertService );

  125.             return is.getSelectorUI( request );
  126.         }
  127.         else
  128.         {
  129.             return AdminMessageService.getMessageUrl( request, MSG_NO_SERVICE_AVAILABLE, AdminMessage.TYPE_STOP );
  130.         }
  131.     }

  132.     /**
  133.      * Build the JSP that provides the HTML code insertion into the Editor via a javascript call
  134.      *
  135.      * @param request
  136.      *            HTTP request
  137.      * @return the HTML code of the page that insert code into the editor
  138.      */
  139.     public String doInsertIntoElement( HttpServletRequest request )
  140.     {
  141.         String strMode = request.getParameter( PARAMETER_MODE );
  142.         String strInput = request.getParameter( PARAMETER_INPUT );
  143.         String strInsert = (String) request.getSession( ).getAttribute( SESSION_INSERT );

  144.         if ( strInsert == null )
  145.         {
  146.             strInsert = request.getParameter( PARAMETER_INSERT );
  147.         }

  148.         request.getSession( ).removeAttribute( SESSION_INSERT );

  149.         Map<String, String> model = new HashMap<>( );
  150.         model.put( MARK_INPUT, strInput );
  151.         model.put( MARK_INSERT, strInsert );

  152.         HtmlTemplate template;

  153.         if ( strMode.compareTo( "2" ) == 0 )
  154.         {
  155.             template = AppTemplateService.getTemplate( TEMPLATE_INSERT_INTO_ELEMENT2, getLocale( ), model );
  156.         }
  157.         else
  158.         {
  159.             template = AppTemplateService.getTemplate( TEMPLATE_INSERT_INTO_ELEMENT, getLocale( ), model );
  160.         }

  161.         return template.getHtml( );
  162.     }
  163. }