View Javadoc
1   /*
2    * Copyright (c) 2002-2023, 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.document.web;
35  
36  import fr.paris.lutece.plugins.document.business.Document;
37  import fr.paris.lutece.plugins.document.business.DocumentHome;
38  import fr.paris.lutece.plugins.document.business.DocumentType;
39  import fr.paris.lutece.plugins.document.business.DocumentTypeHome;
40  import fr.paris.lutece.plugins.document.business.portlet.DocumentListPortlet;
41  import fr.paris.lutece.plugins.document.business.portlet.DocumentListPortletHome;
42  import fr.paris.lutece.plugins.document.service.publishing.PublishingService;
43  import fr.paris.lutece.plugins.document.utils.IntegerUtils;
44  import fr.paris.lutece.portal.business.page.Page;
45  import fr.paris.lutece.portal.business.page.PageHome;
46  import fr.paris.lutece.portal.business.portlet.Portlet;
47  import fr.paris.lutece.portal.business.portlet.PortletHome;
48  import fr.paris.lutece.portal.business.user.AdminUser;
49  import fr.paris.lutece.portal.service.admin.AdminUserService;
50  import fr.paris.lutece.portal.service.message.AdminMessage;
51  import fr.paris.lutece.portal.service.message.AdminMessageService;
52  import fr.paris.lutece.portal.service.template.AppTemplateService;
53  import fr.paris.lutece.portal.service.util.AppPathService;
54  import fr.paris.lutece.portal.web.constants.Messages;
55  import fr.paris.lutece.portal.web.insert.InsertServiceJspBean;
56  import fr.paris.lutece.portal.web.insert.InsertServiceSelectionBean;
57  import fr.paris.lutece.util.ReferenceList;
58  import fr.paris.lutece.util.html.HtmlTemplate;
59  import fr.paris.lutece.util.url.UrlItem;
60  
61  import org.apache.commons.text.StringEscapeUtils;
62  import org.apache.commons.lang3.StringUtils;
63  
64  import java.util.ArrayList;
65  import java.util.Collection;
66  import java.util.HashMap;
67  import java.util.Map;
68  
69  import javax.servlet.http.HttpServletRequest;
70  
71  
72  /**
73   * This class provides the user interface to insert a link to a document
74   *
75   */
76  public class DocumentServiceJspBean extends InsertServiceJspBean implements InsertServiceSelectionBean
77  {
78      private static final long serialVersionUID = 2694692453596836769L;
79  
80      ////////////////////////////////////////////////////////////////////////////
81      // Constants
82      private static final String REGEX_ID = "^[\\d]+$";
83  
84      // Templates
85      private static final String TEMPLATE_SELECTOR_PAGE = "admin/plugins/document/page_selector.html";
86      private static final String TEMPLATE_SELECTOR_PORTLET = "admin/plugins/document/portlet_selector.html";
87      private static final String TEMPLATE_SELECTOR_DOCUMENT = "admin/plugins/document/document_selector.html";
88      private static final String TEMPLATE_LINK = "admin/plugins/document/document_link.html";
89  
90      // JSP
91      private static final String JSP_SELECT_PORTLET = "SelectPortlet.jsp";
92      private static final String JSP_SELECT_DOCUMENT = "SelectDocument.jsp";
93  
94      // Parameters
95      private static final String PARAMETER_PORTLET_ID = "portlet_id";
96      private static final String PARAMETER_PAGE_ID = "page_id";
97      private static final String PARAMETER_DOCUMENT_ID = "document_id";
98      private static final String PARAMETER_ALT = "alt";
99      private static final String PARAMETER_TARGET = "target";
100     private static final String PARAMETER_NAME = "name";
101     private static final String PARAMETER_INPUT = "input";
102     private static final String PARAMETER_SUBCATEGORY = "subcategory";
103 
104     // Marker
105     private static final String MARK_DOCUMENTS_LIST = "documents_list";
106     private static final String MARK_PORTLETS_LIST = "portlets_list";
107     private static final String MARK_PAGES_LIST = "pages_list";
108     private static final String MARK_PORTLET_ID = "portlet_id";
109     private static final String MARK_INPUT = "input";
110     private static final String MARK_TYPE_FILTER = "type_filter";
111     private static final String MARK_URL = "url";
112     private static final String MARK_TARGET = "target";
113     private static final String MARK_ALT = "alt";
114     private static final String MARK_NAME = "name";
115 
116     /** The empty string */
117     private static final String EMPTY_STRING = "";
118 
119     // private
120     private AdminUser _user;
121     private String _input;
122     private String _strTypeFilter;
123 
124     /**
125      * Initialize data
126      *
127      * @param request The HTTP request
128      */
129     public void init( HttpServletRequest request )
130     {
131         _user = AdminUserService.getAdminUser( request );
132         _input = request.getParameter( PARAMETER_INPUT );
133         _strTypeFilter = request.getParameter( PARAMETER_SUBCATEGORY );
134     }
135 
136     /**
137      *
138      *{@inheritDoc}
139      */
140     @Override
141     public ReferenceList getSubCategories(  )
142     {
143         ReferenceList refList = new ReferenceList(  );
144 
145         for ( DocumentType type : DocumentTypeHome.findAll(  ) )
146         {
147             refList.addItem( type.getCode(  ), type.getDescription(  ) );
148         }
149 
150         return refList;
151     }
152 
153     /**
154     * Entry point of the insert service
155     *
156     * @param request The Http Request
157     * @return The html form.
158      */
159     public String getInsertServiceSelectorUI( HttpServletRequest request )
160     {
161         return getSelectPage( request );
162     }
163 
164     /**
165      * Return the html form for page selection.
166      *
167      * @param request The HTTP request
168      * @return The html form of the page selection page
169      */
170     public String getSelectPage( HttpServletRequest request )
171     {
172         init( request );
173 
174         int nPageId = 0;
175         Collection<Page> listPages;
176         String strPageId = request.getParameter( PARAMETER_PAGE_ID );
177 
178         if ( ( strPageId != null ) && strPageId.matches( REGEX_ID ) )
179         {
180             nPageId = IntegerUtils.convert( strPageId );
181         }
182 
183         listPages = PageHome.getChildPages( nPageId );
184 
185         Map<String, Object> model = getDefaultModel(  );
186 
187         model.put( MARK_PAGES_LIST, listPages );
188 
189         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_SELECTOR_PAGE, _user.getLocale(  ), model );
190 
191         return template.getHtml(  );
192     }
193 
194     /**
195      * Select and validate the specified Page
196      *
197      * @param request The http request
198      * @return The url of the portlet selection page
199      */
200     public String doSelectPage( HttpServletRequest request )
201     {
202         init( request );
203 
204         String strPageId = request.getParameter( PARAMETER_PAGE_ID );
205 
206         if ( ( strPageId == null ) || !strPageId.matches( REGEX_ID ) )
207         {
208             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
209         }
210 
211         int nPageId = IntegerUtils.convert( strPageId );
212 
213         Page page = PageHome.findByPrimaryKey( nPageId );
214 
215         if ( page == null )
216         {
217             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
218         }
219 
220         return getSelectPortletUrl( nPageId );
221     }
222 
223     /**
224      * Get the url of the portlet selection page with the specified page id
225      *
226      * @param nPageId Id of the page
227      * @return The url of the portlet selection page
228      */
229     private String getSelectPortletUrl( int nPageId )
230     {
231         UrlItem url = new UrlItem( JSP_SELECT_PORTLET );
232         url.addParameter( PARAMETER_PAGE_ID, nPageId );
233         url.addParameter( PARAMETER_INPUT, _input );
234         url.addParameter( PARAMETER_SUBCATEGORY, getTypeFilter(  ) );
235 
236         return url.getUrl(  );
237     }
238 
239     /**
240      * Return the html form for portlet selection.
241      *
242      * @param request The HTTP request
243      * @return The html form of the portlet selection page
244      */
245     public String getSelectPortlet( HttpServletRequest request )
246     {
247         init( request );
248 
249         String strPageId = request.getParameter( PARAMETER_PAGE_ID );
250 
251         if ( ( strPageId == null ) || !strPageId.matches( REGEX_ID ) )
252         {
253             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
254         }
255 
256         Page page = PageHome.findByPrimaryKey( IntegerUtils.convert( strPageId ) );
257 
258         Collection<Portlet> listPortletsAll = page.getPortlets(  );
259         Collection<Portlet> listPortlets = new ArrayList<Portlet>(  );
260 
261         for ( Portlet portlet : listPortletsAll )
262         {
263             if ( portlet.getPortletTypeId(  ).equals( DocumentListPortletHome.getInstance(  ).getPortletTypeId(  ) ) )
264             {
265                 if ( StringUtils.isNotBlank( getTypeFilter(  ) ) )
266                 {
267                     // filter by type
268                     if ( getTypeFilter(  ).equals( ( (DocumentListPortlet) portlet ).getDocumentTypeCode(  ) ) )
269                     {
270                         listPortlets.add( portlet );
271                     }
272                 }
273                 else
274                 {
275                     // no type filter
276                     listPortlets.add( portlet );
277                 }
278             }
279         }
280 
281         listPortletsAll.clear(  );
282 
283         Map<String, Object> model = getDefaultModel(  );
284 
285         model.put( MARK_PORTLETS_LIST, listPortlets );
286 
287         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_SELECTOR_PORTLET, _user.getLocale(  ), model );
288 
289         return template.getHtml(  );
290     }
291 
292     /**
293      * Select and validate the specified Portlet
294      *
295      * @param request The http request
296      * @return The url of the document selection page if porlet id is valid
297      */
298     public String doSelectPortlet( HttpServletRequest request )
299     {
300         init( request );
301 
302         String strPortletId = request.getParameter( PARAMETER_PORTLET_ID );
303 
304         if ( ( strPortletId == null ) || !strPortletId.matches( REGEX_ID ) )
305         {
306             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
307         }
308 
309         int nPortletId = IntegerUtils.convert( strPortletId );
310 
311         Portlet portlet = PortletHome.findByPrimaryKey( nPortletId );
312 
313         if ( portlet == null )
314         {
315             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
316         }
317 
318         return getSelectDocumentUrl( nPortletId );
319     }
320 
321     /**
322      * Get the url of the document selection page with the specified portlet id
323      *
324      * @param nPortletId Id of the portlet
325      * @return The url of the document selection page
326      */
327     private String getSelectDocumentUrl( int nPortletId )
328     {
329         UrlItem url = new UrlItem( JSP_SELECT_DOCUMENT );
330         url.addParameter( PARAMETER_PORTLET_ID, nPortletId );
331         url.addParameter( PARAMETER_INPUT, _input );
332         url.addParameter( PARAMETER_SUBCATEGORY, getTypeFilter(  ) );
333 
334         return url.getUrl(  );
335     }
336 
337     /**
338      * Return the html form for document selection.
339      *
340      * @param request The HTTP request
341      * @return The html form of the document selection page
342      */
343     public String getSelectDocument( HttpServletRequest request )
344     {
345         init( request );
346 
347         String strPortletId = request.getParameter( PARAMETER_PORTLET_ID );
348 
349         if ( ( strPortletId == null ) || !strPortletId.matches( REGEX_ID ) )
350         {
351             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
352         }
353 
354         int nPortletId = IntegerUtils.convert( strPortletId );
355         Collection<Document> listDocuments = PublishingService.getInstance(  )
356                                                               .getPublishedDocumentsByPortletId( nPortletId );
357 
358         Map<String, Object> model = getDefaultModel(  );
359 
360         model.put( MARK_DOCUMENTS_LIST, listDocuments );
361         model.put( MARK_PORTLET_ID, nPortletId );
362 
363         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_SELECTOR_DOCUMENT, _user.getLocale(  ), model );
364 
365         return template.getHtml(  );
366     }
367 
368     /**
369     * Insert the specified url into HTML content
370     *
371     * @param request The HTTP request
372     * @return The url
373     */
374     public String doInsertUrl( HttpServletRequest request )
375     {
376         init( request );
377 
378         String strDocumentId = request.getParameter( PARAMETER_DOCUMENT_ID );
379         String strPortletId = request.getParameter( PARAMETER_PORTLET_ID );
380         String strTarget = request.getParameter( PARAMETER_TARGET );
381         String strAlt = request.getParameter( PARAMETER_ALT );
382         String strName = request.getParameter( PARAMETER_NAME );
383         HashMap<String, Object> model = new HashMap<String, Object>(  );
384 
385         Document document = null;
386 
387         if ( ( strDocumentId == null ) || !strDocumentId.matches( REGEX_ID ) || ( strPortletId == null ) ||
388                 !strPortletId.matches( REGEX_ID ) )
389         {
390             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
391         }
392 
393         document = DocumentHome.findByPrimaryKeyWithoutBinaries( IntegerUtils.convert( strDocumentId ) );
394 
395         UrlItem url = new UrlItem( AppPathService.getPortalUrl(  ) );
396         url.addParameter( PARAMETER_DOCUMENT_ID, document.getId(  ) );
397         url.addParameter( PARAMETER_PORTLET_ID, strPortletId );
398         model.put( MARK_URL, url.getUrl(  ) );
399         model.put( MARK_TARGET, strTarget );
400         model.put( MARK_ALT, strAlt );
401         model.put( MARK_NAME, ( strName.length(  ) == 0 ) ? document.getTitle(  ) : strName );
402 
403         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_LINK, null, model );
404 
405         return insertUrl( request, _input, StringEscapeUtils.escapeEcmaScript( template.getHtml(  ) ) );
406     }
407 
408     /**
409      * Gets the filter for the document type.
410      * @return the document type, empty string otherwise.
411      */
412     public String getTypeFilter(  )
413     {
414         return ( _strTypeFilter == null ) ? EMPTY_STRING : _strTypeFilter;
415     }
416 
417     /**
418      * Sets the filter for the document type.
419      * @param strTypeFilter the document type, empty string otherwise.
420      */
421     public void setTypeFilter( String strTypeFilter )
422     {
423         _strTypeFilter = strTypeFilter;
424     }
425 
426     /**
427      * Get the default model for selection templates
428      *
429      * @return The default model
430      */
431     private Map<String, Object> getDefaultModel(  )
432     {
433         Map<String, Object> model = new HashMap<String, Object>(  );
434         model.put( MARK_INPUT, _input );
435         model.put( MARK_TYPE_FILTER, getTypeFilter(  ) );
436 
437         return model;
438     }
439 }