View Javadoc
1   /*
2    * Copyright (c) 2002-2017, 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.plugins.pagelinkservice.web;
35  
36  import fr.paris.lutece.plugins.pagelinkservice.business.PageLinkService;
37  import fr.paris.lutece.plugins.pagelinkservice.business.PageLinkServiceHome;
38  import fr.paris.lutece.portal.business.page.Page;
39  import fr.paris.lutece.portal.business.page.PageHome;
40  import fr.paris.lutece.portal.business.user.AdminUser;
41  import fr.paris.lutece.portal.service.admin.AdminUserService;
42  import fr.paris.lutece.portal.service.message.AdminMessage;
43  import fr.paris.lutece.portal.service.message.AdminMessageService;
44  import fr.paris.lutece.portal.service.page.IPageService;
45  import fr.paris.lutece.portal.service.page.PageResourceIdService;
46  import fr.paris.lutece.portal.service.plugin.Plugin;
47  import fr.paris.lutece.portal.service.plugin.PluginService;
48  import fr.paris.lutece.portal.service.spring.SpringContextService;
49  import fr.paris.lutece.portal.service.template.AppTemplateService;
50  import fr.paris.lutece.portal.service.util.AppPathService;
51  import fr.paris.lutece.portal.service.util.AppPropertiesService;
52  import fr.paris.lutece.portal.web.constants.Messages;
53  import fr.paris.lutece.portal.web.insert.InsertServiceJspBean;
54  import fr.paris.lutece.portal.web.insert.InsertServiceSelectionBean;
55  import fr.paris.lutece.util.html.HtmlTemplate;
56  import fr.paris.lutece.util.url.UrlItem;
57  
58  import org.apache.commons.lang3.StringEscapeUtils;
59  
60  import java.util.ArrayList;
61  import java.util.Collection;
62  import java.util.HashMap;
63  
64  import javax.servlet.http.HttpServletRequest;
65  
66  
67  /**
68   * This class provides the user interface to manage PageLibrary features
69   */
70  public class PageLinkServiceJspBean extends InsertServiceJspBean implements InsertServiceSelectionBean
71  {
72      ////////////////////////////////////////////////////////////////////////////
73      // Constants
74      private static final String REGEX_PAGE_ID = "^[\\d]+$";
75  
76      //Parameters
77      private static final String PARAMETER_PLUGIN_NAME = "plugin_name";
78      private static final String PARAMETER_PAGE_NAME = "page_name";
79      private static final String PARAMETER_PAGE_ID = "page";
80      private static final String PARAMETER_PAGE_ID_URL = "page_id";
81      private static final String PARAMETER_ALT = "alt";
82      private static final String PARAMETER_TARGET = "target";
83      private static final String PARAMETER_NAME = "name";
84      private static final String PARAMETER_INPUT = "input";
85  
86      //Properties
87      private static final String PROPERTY_SEARCH_MESSAGE = "message.warning.resulsearch.empty";
88  
89      //Markers
90      private static final String MARK_PLUGIN_NAME = "plugin_name";
91      private static final String MARK_SEARCH_MESSAGE = "search_message";
92      private static final String MARK_PAGES_LIST = "pages_list";
93      private static final String MARK_LIST_PAGE = "list_page";
94      private static final String MARK_URL = "url";
95      private static final String MARK_TARGET = "target";
96      private static final String MARK_ALT = "alt";
97      private static final String MARK_NAME = "name";
98      private static final String MARK_INPUT = "input";
99  
100     //Templates
101     private static final String TEMPLATE_SELECTOR_PAGE = "admin/plugins/pagelinkservice/pagelinkservice_selector.html";
102     private static final String TEMPLATE_LINK = "admin/plugins/pagelinkservice/pagelinkservice_link.html";
103     private AdminUser _user;
104     private Plugin _plugin;
105     private String _input;
106     private IPageService _pageService = (IPageService) SpringContextService.getBean("pageService");
107 
108     ////////////////////////////////////////////////////////////////////////////
109     // Methods
110 
111     /**
112      * Return the html form for image selection.
113      *
114      * @param request The Http Request
115      * @return The html form.
116      */
117     public String getInsertServiceSelectorUI( HttpServletRequest request )
118     {
119         init( request );
120 
121         AdminUser user = AdminUserService.getAdminUser( request );
122         String strSearch = request.getParameter( PARAMETER_PAGE_NAME );
123         String strResultSearch = ( strSearch == null ) ? "" : strSearch;
124         String strPageName = replaceString( strResultSearch, "'", "''" );
125 
126         HashMap model = getDefaultModel(  );
127 
128         Collection<PageLinkService> listPages = PageLinkServiceHome.getPageListbyName( strPageName );
129         Collection<PageLinkService> listPagesAuthorized = new ArrayList<PageLinkService>(  );
130 
131         for ( PageLinkService pageLinkService : listPages )
132         {
133             if ( _pageService.isAuthorizedAdminPage( pageLinkService.getIdPage(  ),
134                         PageResourceIdService.PERMISSION_VIEW, user ) )
135             {
136                 listPagesAuthorized.add( pageLinkService );
137             }
138         }
139 
140         model.put( MARK_PAGES_LIST, listPagesAuthorized );
141 
142         StringBuilder strListPage = new StringBuilder(  );
143 
144         if ( listPages.isEmpty() )
145         {
146             model.put( MARK_SEARCH_MESSAGE, AppPropertiesService.getProperty( PROPERTY_SEARCH_MESSAGE ) );
147         }
148 
149         String strBaseUrl = AppPathService.getBaseUrl( request );
150 
151         // Search Message
152         model.put( MARK_URL, strBaseUrl );
153         model.put( MARK_LIST_PAGE, strListPage.toString(  ) );
154 
155         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_SELECTOR_PAGE, _user.getLocale(  ), model );
156 
157         return template.getHtml(  );
158     }
159 
160     /**
161      * Replaces a string in an initial string by another string
162      *
163      * @param str the initial string
164      * @param oldStr the string to replace
165      * @param newStr the string which will replace the old string
166      * @return the cleaned string
167      */
168     private String replaceString( String str, String oldStr, String newStr )
169     {
170         // Temporary string to avoid assignment of parameter str
171         String cleanString = str;
172 
173         int index = 0;
174 
175         while ( true )
176         {
177             index = cleanString.lastIndexOf( oldStr );
178 
179             if ( ( index == 2 ) || ( index == -1 ) )
180             {
181                 break;
182             }
183 
184             cleanString = cleanString.substring( 0, index ) + newStr +
185                 cleanString.substring( index + oldStr.length(  ) );
186         }
187 
188         return cleanString;
189     }
190 
191     /**
192      * Insert the specified url into HTML content
193      *
194      * @param request The http request
195      * @return String The url
196      */
197     public String doInsertUrl( HttpServletRequest request )
198     {
199         init( request );
200 
201         String strPageId = request.getParameter( PARAMETER_PAGE_ID );
202         String strTarget = request.getParameter( PARAMETER_TARGET );
203         String strAlt = request.getParameter( PARAMETER_ALT );
204         String strName = request.getParameter( PARAMETER_NAME );
205         HashMap<String, Object> model = new HashMap<String, Object>(  );
206 
207         Page page = null;
208 
209         if ( ( strPageId == null ) || !strPageId.matches( REGEX_PAGE_ID ) )
210         {
211             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
212         }
213 
214         page = PageHome.findByPrimaryKey( Integer.parseInt( strPageId ) );
215 
216         UrlItem url = new UrlItem( AppPathService.getPortalUrl(  ) );
217         url.addParameter( PARAMETER_PAGE_ID_URL, page.getId(  ) );
218         model.put( MARK_URL, url.getUrl(  ) );
219         model.put( MARK_TARGET, strTarget );
220         model.put( MARK_ALT, strAlt );
221         model.put( MARK_NAME, ( strName.length(  ) == 0 ) ? page.getName(  ) : strName );
222 
223         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_LINK, null, model );
224 
225         return insertUrl( request, _input, StringEscapeUtils.escapeEcmaScript( template.getHtml( )) );
226     }
227 
228     private void init( HttpServletRequest request )
229     {
230         String strPluginName = request.getParameter( PARAMETER_PLUGIN_NAME );
231         _user = AdminUserService.getAdminUser( request );
232         _plugin = PluginService.getPlugin( strPluginName );
233         _input = request.getParameter( PARAMETER_INPUT );
234     }
235 
236     private HashMap getDefaultModel(  )
237     {
238         HashMap model = new HashMap(  );
239         model.put( MARK_PLUGIN_NAME, _plugin.getName(  ) );
240         model.put( MARK_INPUT, _input );
241 
242         return model;
243     }
244 }