View Javadoc
1   /*
2    * Copyright (c) 2002-2020, 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.blog.web.portlet;
35  
36  import java.sql.Date;
37  import java.text.ParseException;
38  import java.text.SimpleDateFormat;
39  import java.util.ArrayList;
40  import java.util.HashMap;
41  import java.util.List;
42  
43  import javax.servlet.http.HttpServletRequest;
44  
45  import org.apache.commons.lang.StringUtils;
46  import org.apache.commons.lang3.ArrayUtils;
47  
48  import fr.paris.lutece.plugins.blog.business.Blog;
49  import fr.paris.lutece.plugins.blog.business.BlogHome;
50  import fr.paris.lutece.plugins.blog.business.BlogSearchFilter;
51  import fr.paris.lutece.plugins.blog.business.TagHome;
52  import fr.paris.lutece.plugins.blog.business.portlet.BlogListPortlet;
53  import fr.paris.lutece.plugins.blog.business.portlet.BlogListPortletHome;
54  import fr.paris.lutece.plugins.blog.business.portlet.BlogPublication;
55  import fr.paris.lutece.plugins.blog.business.portlet.BlogPublicationHome;
56  import fr.paris.lutece.plugins.blog.service.BlogService;
57  import fr.paris.lutece.plugins.blog.service.PublishingService;
58  import fr.paris.lutece.plugins.blog.service.docsearch.BlogSearchService;
59  import fr.paris.lutece.plugins.blog.web.BlogPublicationJspBean;
60  import fr.paris.lutece.portal.business.portlet.PortletHome;
61  import fr.paris.lutece.portal.business.user.AdminUser;
62  import fr.paris.lutece.portal.service.admin.AdminUserService;
63  import fr.paris.lutece.portal.service.util.AppPathService;
64  import fr.paris.lutece.portal.service.util.AppPropertiesService;
65  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
66  import fr.paris.lutece.portal.web.portlet.PortletJspBean;
67  import fr.paris.lutece.portal.web.util.LocalizedPaginator;
68  import fr.paris.lutece.util.date.DateUtil;
69  import fr.paris.lutece.util.html.AbstractPaginator;
70  import fr.paris.lutece.util.html.HtmlTemplate;
71  import fr.paris.lutece.util.json.JsonResponse;
72  import fr.paris.lutece.util.json.JsonUtil;
73  import java.util.Map;
74  
75  /**
76   * This class provides the user interface to manage BlogList Portlet
77   */
78  public class BlogListPortletJspBean extends PortletJspBean
79  {
80      // Right
81      public static final String RIGHT_MANAGE_ADMIN_SITE = "CORE_ADMIN_SITE";
82  
83      public static final String MARK_WEBAPP_URL = "webapp_url";
84      public static final String MARK_LIST_HTMLDOC = "blog_list";
85      public static final String MARK_LIST_PAGES = "pages_list";
86      public static final String MARK_LIST_HTMLDOC_PUBLISHED = "blog_list_published";
87  
88      public static final String PARAMETER_ACTION_PORTLET_ADD = "add";
89      public static final String PARAMETER_ACTION_PORTLET_REMOVE = "remove";
90      public static final String PARAMETER_ACTION_PORTLET = "action";
91      private static final String PARAMETER_PAGE_TEMPLATE_CODE = "page_template_code";
92  
93      private static final String PARAMETER_DOCUMENT_ID = "idDocument";
94      private static final String PARAMETER_DOCUMENT_ORDER = "orderDocument";
95      private static final String PARAMETER_REFRESH_BUTTON = "refresh";
96      protected static final String PARAMETER_TAG = "tag_doc";
97  
98      protected static final String PARAMETER_SEARCH_TEXT = "search_text";
99      protected static final String PARAMETER_UNPUBLISHED = "unpublished";
100     protected static final String PARAMETER_DATE_UPDATE_BLOG_AFTER = "dateUpdateBlogAfter";
101     protected static final String PARAMETER_DATE_UPDATE_BLOG_BEFOR = "dateUpdateBlogBefor";
102 
103     protected static final String PARAMETER_BUTTON_SEARCH = "button_search";
104     protected static final String PARAMETER_BUTTON_RESET = "button_reset";
105 
106     protected static final String MARK_IS_CHECKED = "is_checked";
107     protected static final String MARK_CURRENT_USER = "current_user";
108     protected static final String MARK_ID_BLOG = "id";
109     protected static final String MARK_SEARCH_TEXT = "search_text";
110 
111     protected static final String MARK_PAGINATOR = "paginator";
112     protected static final String MARK_NB_ITEMS_PER_PAGE = "nb_items_per_page";
113     protected static final String MARK_DATE_UPDATE_BLOG_AFTER = "dateUpdateBlogAfter";
114     protected static final String MARK_DATE_UPDATE_BLOG_BEFOR = "dateUpdateBlogBefor";
115     protected static final String MARK_UNPUBLISHED = "unpublished";
116     protected static final String MARK_LIST_TAG = "list_tag";
117 
118     // Properties
119     private static final String PROPERTY_DEFAULT_LIST_ITEM_PER_PAGE = "blog.listItems.itemsPerPage";
120 
121     private static final String VIEW_MODIFY_PORTLET = "getModify";
122 
123     private static final String RESPONSE_SUCCESS = "SUCCESS";
124 
125     // //////////////////////////////////////////////////////////////////////////
126     // Constants
127     private static final long serialVersionUID = 1L;
128 
129     // //////////////////////////////////////////////////////////////////////////
130     // Class attributes
131 
132     private BlogListPortlet _portlet;
133 
134     protected String _strCurrentPageIndex;
135     protected int _nItemsPerPage;
136 
137     protected int _nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_DEFAULT_LIST_ITEM_PER_PAGE, 20 );
138 
139     // Session variable to store working values
140     protected boolean _bIsChecked = false;
141     protected String _strSearchText;
142     protected boolean _bIsUnpulished = false;
143     protected String _dateUpdateBlogAfter;
144     protected String _dateUpdateBlogBefor;
145     protected boolean _bIsSorted = false;
146     protected String _strSortedAttributeName;
147     protected Boolean _bIsAscSort;
148     protected String [ ] _strTag;
149 
150     /**
151      * Returns portlet's properties prefix
152      *
153      * @return prefix
154      */
155     public String getPropertiesPrefix( )
156     {
157         return "portlet.blogdocument";
158     }
159 
160     /**
161      * Return a model that contains the list and paginator infos
162      * 
163      * @param request
164      *            The HTTP request
165      * @return The map
166      */
167     protected Map<String, Object> getPaginatedListModel( HttpServletRequest request )
168     {
169 
170         List<Integer> listBlogsId = new ArrayList<>( );
171         AdminUser user = AdminUserService.getAdminUser( request );
172 
173         if ( StringUtils.isNotBlank( _strSearchText ) || ArrayUtils.isNotEmpty( _strTag ) || _bIsChecked || _bIsUnpulished || _dateUpdateBlogAfter != null
174                 || _dateUpdateBlogBefor != null )
175         {
176             BlogSearchFilterbusiness/BlogSearchFilter.html#BlogSearchFilter">BlogSearchFilter filter = new BlogSearchFilter( );
177             if ( StringUtils.isNotBlank( _strSearchText ) )
178             {
179                 filter.setKeywords( _strSearchText );
180             }
181             if ( ArrayUtils.isNotEmpty( _strTag ) )
182             {
183                 filter.setTag( _strTag );
184             }
185             if ( _bIsChecked )
186             {
187                 filter.setUser( user.getAccessCode( ) );
188             }
189             if ( _bIsUnpulished )
190             {
191                 filter.setIsUnpulished( _bIsUnpulished );
192             }
193             if ( _dateUpdateBlogAfter != null )
194             {
195                 filter.setUpdateDateAfter( DateUtil.formatDate( _dateUpdateBlogAfter, request.getLocale( ) ) );
196             }
197             if ( _dateUpdateBlogBefor != null )
198             {
199                 filter.setUpdateDateBefor( DateUtil.formatDate( _dateUpdateBlogBefor, request.getLocale( ) ) );
200             }
201 
202             BlogSearchService.getInstance( ).getSearchResults( filter, listBlogsId );
203 
204         }
205         else
206         {
207 
208             listBlogsId = BlogHome.getIdBlogsList( );
209         }
210 
211         _strCurrentPageIndex = AbstractPaginator.getPageIndex( request, AbstractPaginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
212         _nItemsPerPage = AbstractPaginator.getItemsPerPage( request, AbstractPaginator.PARAMETER_ITEMS_PER_PAGE, _nDefaultItemsPerPage, _nItemsPerPage );
213         List<Blog> listBlogPublished = new ArrayList<>( );
214         List<Blog> listBlogNotPublished = new ArrayList<>( );
215 
216         for ( BlogPublication i : _portlet.getArrayBlogs( ) )
217         {
218             Blog blog = BlogService.getInstance( ).findByPrimaryKeyWithoutBinaries( i.getIdBlog( ) );
219             listBlogPublished.add( blog );
220             listBlogsId.removeIf( blg -> blg.equals( blog.getId( ) ) );
221         }
222 
223         LocalizedPaginator<Integer> paginator = new LocalizedPaginator<>( listBlogsId, _nItemsPerPage, getCurrentUrlFromRequest( request ),
224                 AbstractPaginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex, getLocale( ) );
225         List<Blog> listBlog = new ArrayList<>( );
226 
227         for ( Integer documentId : paginator.getPageItems( ) )
228         {
229             Blog blog = BlogService.getInstance( ).findByPrimaryKeyWithoutBinaries( documentId );
230 
231             if ( blog != null )
232             {
233                 listBlog.add( blog );
234             }
235         }
236 
237         listBlogNotPublished.addAll( listBlog );
238 
239         HashMap<String, Object> model = new HashMap<>( );
240         model.put( MARK_PAGINATOR, paginator );
241         model.put( MARK_NB_ITEMS_PER_PAGE, "" + _nItemsPerPage );
242         model.put( MARK_WEBAPP_URL, AppPathService.getBaseUrl( request ) );
243         model.put( MARK_LIST_HTMLDOC, listBlogNotPublished );
244         model.put( MARK_LIST_HTMLDOC_PUBLISHED, listBlogPublished );
245 
246         model.put( MARK_LIST_TAG, TagHome.getTagsReferenceList( ) );
247         model.put( MARK_IS_CHECKED, _bIsChecked );
248         model.put( MARK_SEARCH_TEXT, _strSearchText );
249         model.put( MARK_DATE_UPDATE_BLOG_AFTER, _dateUpdateBlogAfter );
250         model.put( MARK_DATE_UPDATE_BLOG_BEFOR, _dateUpdateBlogBefor );
251         model.put( MARK_UNPUBLISHED, _bIsUnpulished );
252 
253         model.put( MARK_LIST_PAGES, BlogListPortletHome.loadPages( BlogListPortlet.RESOURCE_ID ) );
254 
255         return model;
256     }
257 
258     /**
259      * 
260      * @param request
261      */
262     private void setSearchBlog( HttpServletRequest request, String strButtonSearch, String strButtonReset )
263     {
264 
265         if ( strButtonSearch != null )
266         {
267             // CURRENT USER
268             _bIsChecked = request.getParameter( MARK_CURRENT_USER ) != null;
269             _strSearchText = request.getParameter( PARAMETER_SEARCH_TEXT );
270             _strTag = request.getParameterValues( PARAMETER_TAG );
271             _bIsUnpulished = request.getParameter( PARAMETER_UNPUBLISHED ) != null;
272             _dateUpdateBlogAfter = request.getParameter( PARAMETER_DATE_UPDATE_BLOG_AFTER );
273             _dateUpdateBlogBefor = request.getParameter( PARAMETER_DATE_UPDATE_BLOG_BEFOR );
274 
275         }
276         else
277             if ( strButtonReset != null )
278             {
279 
280                 _bIsChecked = false;
281                 _strSearchText = null;
282                 _strTag = null;
283                 _bIsUnpulished = false;
284                 _dateUpdateBlogAfter = null;
285                 _dateUpdateBlogBefor = null;
286 
287             }
288     }
289 
290     /**
291      * Returns the Download portlet creation form
292      *
293      * @param request
294      *            The http request
295      * @return The HTML form
296      */
297     @Override
298     public String getCreate( HttpServletRequest request )
299     {
300         String strIdPage = request.getParameter( PARAMETER_PAGE_ID );
301         String strIdPortletType = request.getParameter( PARAMETER_PORTLET_TYPE_ID );
302         _portlet = ( _portlet != null && request.getParameter( AbstractPaginator.PARAMETER_PAGE_INDEX ) != null ) ? _portlet : new BlogListPortlet( );
303         Map<String, Object> model = getPaginatedListModel( request );
304         HtmlTemplate template = getCreateTemplate( strIdPage, strIdPortletType, model );
305 
306         return template.getHtml( );
307     }
308 
309     /**
310      * Returns the Download portlet modification form
311      *
312      * @param request
313      *            The Http request
314      * @return The HTML form
315      */
316     @View( VIEW_MODIFY_PORTLET )
317     @Override
318     public String getModify( HttpServletRequest request )
319     {
320         String strPortletId = request.getParameter( PARAMETER_PORTLET_ID );
321         int nPortletId = Integer.parseInt( strPortletId );
322         _portlet = ( _portlet != null && request.getParameter( AbstractPaginator.PARAMETER_PAGE_INDEX ) != null ) ? _portlet
323                 : (BlogListPortlet) PortletHome.findByPrimaryKey( nPortletId );
324         Map<String, Object> model = getPaginatedListModel( request );
325 
326         HtmlTemplate template = getModifyTemplate( _portlet, model );
327 
328         return template.getHtml( );
329     }
330 
331     /**
332      * Process portlet's creation
333      *
334      * @param request
335      *            The Http request
336      * @return The Jsp management URL of the process result
337      */
338     @Override
339     public String doCreate( HttpServletRequest request )
340     {
341 
342         String button = request.getParameter( PARAMETER_REFRESH_BUTTON );
343         _nDefaultItemsPerPage = AbstractPaginator.getItemsPerPage( request, AbstractPaginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage, _nDefaultItemsPerPage );
344         String strIdPage = request.getParameter( PARAMETER_PAGE_ID );
345         String strButtonSearch = request.getParameter( PARAMETER_BUTTON_SEARCH );
346         String strButtonReset = request.getParameter( PARAMETER_BUTTON_RESET );
347         int nIdPage = Integer.parseInt( strIdPage );
348 
349         if ( ( button != null && button.equals( PARAMETER_REFRESH_BUTTON ) ) || strButtonSearch != null || strButtonReset != null )
350         {
351 
352             setSearchBlog( request, strButtonSearch, strButtonReset );
353             return "../../DoCreatePortlet.jsp?portlet_type_id=" + BlogListPortlet.RESOURCE_ID + "&page_id=" + strIdPage + "&"
354                     + AbstractPaginator.PARAMETER_PAGE_INDEX + "=1";
355         }
356 
357         int order = 1;
358 
359         // gets the identifier of the parent page
360         String strTemplateCode = request.getParameter( PARAMETER_PAGE_TEMPLATE_CODE );
361 
362         // get portlet common attributes
363         String strErrorUrl = setPortletCommonData( request, _portlet );
364 
365         if ( strErrorUrl != null )
366         {
367             return strErrorUrl;
368         }
369 
370         _portlet.setPageId( nIdPage );
371 
372         // gets the specific parameters
373         _portlet.setPageTemplateDocument( Integer.parseInt( strTemplateCode ) );
374 
375         for ( BlogPublication doc : _portlet.getArrayBlogs( ) )
376         {
377 
378             doc.setBlogOrder( order );
379             order++;
380 
381         }
382         // Portlet creation
383         BlogListPortletHome.getInstance( ).create( _portlet );
384 
385         for ( BlogPublication doc : _portlet.getArrayBlogs( ) )
386         {
387             int nbPublication = BlogPublicationHome.countPublicationByIdBlogAndDate( doc.getIdBlog( ), new java.util.Date( ) );
388             // First publication of this blog -> indexing needed
389             if ( nbPublication == 1 )
390             {
391                 BlogService.getInstance( ).fireCreateBlogEvent( doc.getIdBlog( ) );
392             }
393         }
394 
395         // Displays the page with the new Portlet
396         return getPageUrl( nIdPage );
397     }
398 
399     /**
400      * Process portlet's modification
401      *
402      * @param request
403      *            The http request
404      * @return Management's Url
405      */
406     @Override
407     public String doModify( HttpServletRequest request )
408     {
409 
410         _nDefaultItemsPerPage = AbstractPaginator.getItemsPerPage( request, AbstractPaginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage, _nDefaultItemsPerPage );
411         String button = request.getParameter( PARAMETER_REFRESH_BUTTON );
412         String strButtonSearch = request.getParameter( PARAMETER_BUTTON_SEARCH );
413         String strButtonReset = request.getParameter( PARAMETER_BUTTON_RESET );
414 
415         if ( ( button != null && button.equals( PARAMETER_REFRESH_BUTTON ) ) || strButtonSearch != null || strButtonReset != null )
416         {
417 
418             setSearchBlog( request, strButtonSearch, strButtonReset );
419             return "../../DoModifyPortlet.jsp?portlet_id=" + _portlet.getId( ) + "&" + AbstractPaginator.PARAMETER_PAGE_INDEX + "=1";
420         }
421 
422         int order = 1;
423 
424         // recovers portlet attributes
425         String strDocumentTypeCode = request.getParameter( PARAMETER_PAGE_TEMPLATE_CODE );
426         // retrieve portlet common attributes
427         String strErrorUrl = setPortletCommonData( request, _portlet );
428 
429         if ( strErrorUrl != null )
430         {
431             return strErrorUrl;
432         }
433 
434         _portlet.setPageTemplateDocument( Integer.parseInt( strDocumentTypeCode ) );
435 
436         for ( BlogPublication doc : _portlet.getArrayBlogs( ) )
437         {
438 
439             doc.setBlogOrder( order );
440             order++;
441 
442         }
443         // updates the portlet
444         _portlet.update( );
445 
446         for ( Integer removedBlog : _portlet.getRemovedBlogsId( ) )
447         {
448             int nbPublication = BlogPublicationHome.countPublicationByIdBlogAndDate( removedBlog, new java.util.Date( ) );
449             // Last publication of this blog removed -> removing from index
450             if ( nbPublication == 0 )
451             {
452                 BlogService.getInstance( ).fireDeleteBlogEvent( removedBlog );
453             }
454         }
455 
456         for ( BlogPublication doc : _portlet.getArrayBlogs( ) )
457         {
458             int nbPublication = BlogPublicationHome.countPublicationByIdBlogAndDate( doc.getIdBlog( ), new java.util.Date( ) );
459             // First publication of this blog -> indexing needed
460             if ( nbPublication == 1 )
461             {
462                 BlogService.getInstance( ).fireCreateBlogEvent( doc.getIdBlog( ) );
463             }
464         }
465 
466         // displays the page withe the potlet updated
467         return getPageUrl( _portlet.getPageId( ) );
468     }
469 
470     /**
471      * Update blog portlet
472      * 
473      * @param request
474      * @return Json The Json succes or echec
475      * @throws ParseException
476      */
477     public String updatePortletDocument( HttpServletRequest request ) throws ParseException
478     {
479         // recovers portlet attributes
480 
481         String strAction = request.getParameter( PARAMETER_ACTION_PORTLET );
482         String strIdDocument = request.getParameter( PARAMETER_DOCUMENT_ID );
483         String strOrderDocument = request.getParameter( PARAMETER_DOCUMENT_ORDER );
484 
485         int nIdDocument = Integer.parseInt( strIdDocument );
486         BlogPublication doc = PublishingService.getInstance( ).getBlogPublication( _portlet.getId( ), nIdDocument );
487         if ( doc == null )
488         {
489             doc = new BlogPublication( );
490             SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
491             doc.setDateEndPublishing( new Date( sdf.parse( BlogPublicationJspBean.DATE_END_PUBLICATION ).getTime( ) ) );
492             doc.setIdBlog( nIdDocument );
493         }
494         if ( strAction != null && !strAction.isEmpty( ) && strAction.equals( PARAMETER_ACTION_PORTLET_ADD ) )
495         {
496 
497             int nDocumentOrder = Integer.parseInt( strOrderDocument );
498             _portlet.addIdBlogs( nDocumentOrder, doc );
499 
500         }
501         else
502             if ( strAction != null && !strAction.isEmpty( ) && strAction.equals( PARAMETER_ACTION_PORTLET_REMOVE ) )
503             {
504 
505                 _portlet.removeBlogs( doc );
506 
507             }
508 
509         return JsonUtil.buildJsonResponse( new JsonResponse( RESPONSE_SUCCESS ) );
510     }
511 
512     /**
513      * 
514      * @param request
515      * @return
516      */
517     public static String getCurrentUrlFromRequest( HttpServletRequest request )
518     {
519         StringBuffer sbRequestURL = request.getRequestURL( );
520         String queryString = request.getQueryString( );
521 
522         if ( queryString == null )
523         {
524 
525             return sbRequestURL.toString( );
526 
527         }
528         else
529             if ( request.getParameter( AbstractPaginator.PARAMETER_PAGE_INDEX ) != null )
530             {
531 
532                 String [ ] query = queryString.split( "&" + AbstractPaginator.PARAMETER_PAGE_INDEX );
533                 queryString = query [0];
534             }
535 
536         return sbRequestURL.append( '?' ).append( queryString ).toString( );
537     }
538 
539 }