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.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  /**
55   * Provides the generic interface to choose a particular link service
56   */
57  public class InsertServiceSelectorJspBean extends AdminFeaturesPageJspBean
58  {
59      /**
60       * Right to manage this feature
61       */
62      public static final String RIGHT_MANAGE_LINK_SERVICE = "CORE_LINK_SERVICE_MANAGEMENT";
63  
64      /**
65       * Session key to set the content to insert
66       */
67      public static final String SESSION_INSERT = "portal.session.insertServiceSelector.contentToInsert";
68      private static final long serialVersionUID = 3395846045509139922L;
69  
70      // Constants
71      private static final String TEMPLATE_INSERT_TYPE_PAGE = "admin/insert/page_insertservice.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 HTTP request
91       * @return HTML code of the page as String
92       */
93      public String getServicesListPage( HttpServletRequest request )
94      {
95          String strInput = request.getParameter( PARAMETER_INPUT );
96          String strText = request.getParameter( PARAMETER_SELECTED_TEXT );
97  
98          // Encode the HTML code to insert
99          strText = EncodingService.encodeUrl( strText );
100 
101         Collection<InsertService> listServices = InsertServiceManager.getInsertServicesList(  );
102 
103         // building from a template
104         Map<String, Object> model = new HashMap<String, Object>(  );
105         model.put( MARK_INSERT_SERVICES_LIST,
106             RBACService.getAuthorizedCollection( listServices, InsertResourceIdService.PERMISSION_USE, getUser(  ) ) );
107         model.put( MARK_SELECTED_TEXT, strText );
108         model.put( MARK_INPUT, strInput );
109 
110         HtmlTemplate t = AppTemplateService.getTemplate( TEMPLATE_INSERT_TYPE_PAGE, getLocale(  ), model );
111 
112         return t.getHtml(  );
113     }
114 
115     /**
116      * Call the selected service selection UI
117      *
118      * @param request HTTP request
119      * @return the HTML code of the selection page for selected service
120      */
121     public String displayService( HttpServletRequest request )
122     {
123         String strInsertService = request.getParameter( PARAMETER_INSERT_SERVICE_TYPE );
124 
125         if ( ( strInsertService != null ) && !strInsertService.equals( "" ) )
126         {
127             InsertService is = InsertServiceManager.getInsertService( strInsertService );
128 
129             return is.getSelectorUI( request );
130         }
131         else
132         {
133             return AdminMessageService.getMessageUrl( request, MSG_NO_SERVICE_AVAILABLE, AdminMessage.TYPE_STOP );
134         }
135     }
136 
137     /**
138      * Build the JSP that provides the HTML code insertion into the Editor via a javascript call
139      *
140      * @param request HTTP request
141      * @return the HTML code of the page that insert code into the editor
142      */
143     public String doInsertIntoElement( HttpServletRequest request )
144     {
145         String strMode = request.getParameter( PARAMETER_MODE );
146         String strInput = request.getParameter( PARAMETER_INPUT );
147         String strInsert = (String) request.getSession(  ).getAttribute( SESSION_INSERT );
148 
149         if ( strInsert == null )
150         {
151             strInsert = request.getParameter( PARAMETER_INSERT );
152         }
153 
154         request.getSession(  ).removeAttribute( SESSION_INSERT );
155 
156         Map<String, String> model = new HashMap<String, String>(  );
157         model.put( MARK_INPUT, strInput );
158         model.put( MARK_INSERT, strInsert );
159 
160         HtmlTemplate template;
161 
162         if ( strMode.compareTo( "2" ) == 0 )
163         {
164             template = AppTemplateService.getTemplate( TEMPLATE_INSERT_INTO_ELEMENT2, getLocale(  ), model );
165         }
166         else
167         {
168             template = AppTemplateService.getTemplate( TEMPLATE_INSERT_INTO_ELEMENT, getLocale(  ), model );
169         }
170 
171         return template.getHtml(  );
172     }
173 }