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.modules.extendcomment.web;
35  
36  import java.util.ArrayList;
37  import java.util.HashMap;
38  import java.util.List;
39  import java.util.Map;
40  import java.util.stream.Collectors;
41  
42  import javax.servlet.http.HttpServletRequest;
43  
44  import org.apache.commons.lang3.StringUtils;
45  
46  import fr.paris.lutece.plugins.blog.business.Blog;
47  import fr.paris.lutece.plugins.blog.business.BlogSearchFilter;
48  import fr.paris.lutece.plugins.blog.modules.extendcomment.utils.BlogCommentConstants;
49  import fr.paris.lutece.plugins.blog.service.docsearch.BlogSearchService;
50  import fr.paris.lutece.plugins.extend.business.extender.ResourceExtenderDTO;
51  import fr.paris.lutece.plugins.extend.business.extender.ResourceExtenderDTOFilter;
52  import fr.paris.lutece.plugins.extend.modules.comment.business.Comment;
53  import fr.paris.lutece.plugins.extend.modules.comment.business.CommentFilter;
54  import fr.paris.lutece.plugins.extend.modules.comment.business.config.CommentExtenderConfig;
55  import fr.paris.lutece.plugins.extend.modules.comment.service.CommentPlugin;
56  import fr.paris.lutece.plugins.extend.modules.comment.service.CommentService;
57  import fr.paris.lutece.plugins.extend.modules.comment.service.ICommentService;
58  import fr.paris.lutece.plugins.extend.modules.comment.util.constants.CommentConstants;
59  import fr.paris.lutece.plugins.extend.service.extender.IResourceExtender;
60  import fr.paris.lutece.plugins.extend.service.extender.IResourceExtenderService;
61  import fr.paris.lutece.plugins.extend.service.extender.ResourceExtenderService;
62  import fr.paris.lutece.plugins.extend.service.extender.config.IResourceExtenderConfigService;
63  import fr.paris.lutece.portal.business.user.AdminUser;
64  import fr.paris.lutece.portal.service.admin.AdminUserService;
65  import fr.paris.lutece.portal.service.message.AdminMessage;
66  import fr.paris.lutece.portal.service.message.AdminMessageService;
67  import fr.paris.lutece.portal.service.resource.IExtendableResource;
68  import fr.paris.lutece.portal.service.spring.SpringContextService;
69  import fr.paris.lutece.portal.service.template.AppTemplateService;
70  import fr.paris.lutece.portal.service.util.AppPropertiesService;
71  import fr.paris.lutece.portal.service.workflow.WorkflowService;
72  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
73  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
74  import fr.paris.lutece.portal.web.constants.Parameters;
75  import fr.paris.lutece.portal.web.util.LocalizedDelegatePaginator;
76  import fr.paris.lutece.util.html.HtmlTemplate;
77  import fr.paris.lutece.util.html.IPaginator;
78  import fr.paris.lutece.util.html.Paginator;
79  
80  /**
81   * 
82   * CommentResourceExtenderComponent
83   * 
84   */
85  @Controller( controllerJsp = "BlogExtendCommentJspBean.jsp", controllerPath = "jsp/admin/plugins/blog/modules/extendcomment", right = "BLOG_EXTENDCOMMENT_MANAGEMENT" )
86  public class BlogExtendCommentJspBean extends AbstractManageBlogExtendcommentJspBean
87  {
88      // TEMPLATES
89      private static final String JSP_VIEW_EXTENDER_BLOG = "../../../blog/modules/extendcomment/ManageBlogExtendcomment.jsp";
90      private static final String TEMPLATE_MANAGE_COMMENTS = "admin/plugins/blog/modules/extendcomment/manage_blog_extendcomment.html";
91      private static final String VIEW_MANAGE_COMMENT = "manageComment";
92  
93      // Properties
94      private static final String PROPERTY_DEFAULT_LIST_ITEM_PER_PAGE = "blog.extendcomment.listItems.itemsPerPage";
95  
96      protected String _strCurrentPageIndex = "1";
97      protected int _nItemsPerPage;
98      protected int _nDefaultItemsPerPage;
99  
100     private IResourceExtenderService _extenderService = SpringContextService.getBean( ResourceExtenderService.BEAN_SERVICE );
101 
102     private ICommentService _commentService = SpringContextService.getBean( CommentService.BEAN_SERVICE );
103     private IResourceExtenderConfigService _configService = SpringContextService.getBean( CommentConstants.BEAN_CONFIG_SERVICE );
104     private IResourceExtenderService _resourceExtenderService = SpringContextService.getBean( ResourceExtenderService.BEAN_SERVICE );;
105 
106     /**
107      * Build the Manage View
108      *
109      * @param request
110      *            The HTTP request
111      * @return The page
112      */
113     @View( value = VIEW_MANAGE_COMMENT, defaultView = true )
114     public String getCommentBlog( HttpServletRequest request )
115     {
116 
117         _strCurrentPageIndex = Paginator.getPageIndex( request, Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
118         _nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_DEFAULT_LIST_ITEM_PER_PAGE, 50 );
119         _nItemsPerPage = Paginator.getItemsPerPage( request, Paginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage, _nDefaultItemsPerPage );
120         List<Integer> listBlogsId = new ArrayList<>( );
121         AdminUser adminUser = AdminUserService.getAdminUser( request );
122 
123         int nIdExtender = 0;
124         ResourceExtenderDTO resourceExtender = null;
125 
126         String strIdExtender = request.getParameter( BlogCommentConstants.PARAMETER_ID_EXTENDER );
127         if ( strIdExtender == null || strIdExtender.isEmpty( ) )
128         {
129 
130             ResourceExtenderDTOFilter filter = new ResourceExtenderDTOFilter( );
131             filter.setFilterExtendableResourceType( Blog.PROPERTY_RESOURCE_TYPE );
132             filter.setFilterExtenderType( "comment" );
133             filter.setIncludeWildcardResource( true );
134             List<ResourceExtenderDTO> listExtender = _extenderService.findByFilter( filter );
135             if ( listExtender != null && !listExtender.isEmpty( ) )
136                 resourceExtender = listExtender.get( 0 );
137 
138         }
139         else
140         {
141             nIdExtender = Integer.parseInt( strIdExtender );
142             resourceExtender = _extenderService.findByPrimaryKey( nIdExtender );
143         }
144 
145         if ( resourceExtender == null )
146         {
147             return redirect( request, AdminMessageService.getMessageUrl( request, BlogCommentConstants.MESSAGE_UNVAILABLE_EXTENDER, AdminMessage.TYPE_STOP ) );
148         }
149 
150         IResourceExtender extender = _extenderService.getResourceExtender( resourceExtender.getExtenderType( ) );
151         CommentExtenderConfig config = _configService.find( extender.getKey( ), resourceExtender.getIdExtendableResource( ), Blog.PROPERTY_RESOURCE_TYPE );
152 
153         // We get the pagination info from the session
154         Boolean bIsAscSort = false;
155 
156         String strIsAscSort = request.getParameter( BlogCommentConstants.COMMENT_ADMIN_IS_ASC_SORT );
157         if ( strIsAscSort != null )
158         {
159             bIsAscSort = Boolean.parseBoolean( strIsAscSort );
160         }
161 
162         String strSortedAttributeName = request.getParameter( BlogCommentConstants.COMMENT_ADMIN_SORTED_ATTRIBUTE_NAME );
163         String strFilterState = request.getParameter( BlogCommentConstants.COMMENT_ADMIN_FILTER_STATE );
164         String strFilterPinned = request.getParameter( BlogCommentConstants.COMMENT_ADMIN_FILTER_PINNED );
165         String strFilterMarkAsImportant = request.getParameter( BlogCommentConstants.COMMENT_ADMIN_FILTER_MARK_AS_IMPORTANT );
166 
167         String strNewSortedAttributeName = request.getParameter( Parameters.SORTED_ATTRIBUTE_NAME );
168         if ( strNewSortedAttributeName != null )
169         {
170             // We update sort properties
171             strSortedAttributeName = strNewSortedAttributeName;
172             bIsAscSort = Boolean.parseBoolean( request.getParameter( Parameters.SORTED_ASC ) );
173         }
174 
175         if ( request.getParameter( CommentConstants.PARAMETER_FILTER_STATE ) != null )
176         {
177             strFilterState = request.getParameter( CommentConstants.PARAMETER_FILTER_STATE );
178         }
179 
180         if ( request.getParameter( CommentConstants.PARAMETER_FILTER_PINNED ) != null )
181         {
182             strFilterPinned = request.getParameter( CommentConstants.PARAMETER_FILTER_PINNED );
183         }
184 
185         if ( request.getParameter( CommentConstants.PARAMETER_FILTER_MARK_AS_IMPORTANT ) != null )
186         {
187             strFilterMarkAsImportant = request.getParameter( CommentConstants.PARAMETER_FILTER_MARK_AS_IMPORTANT );
188         }
189 
190         int nItemsOffset = _nItemsPerPage * ( Integer.parseInt( _strCurrentPageIndex ) - 1 );
191 
192         CommentFilter commentFilter = new CommentFilter( );
193         if ( StringUtils.isNotBlank( strFilterState ) && StringUtils.isNumeric( strFilterState ) )
194         {
195             commentFilter.setCommentState( Integer.parseInt( strFilterState ) );
196         }
197         if ( StringUtils.isNotBlank( strFilterPinned ) )
198         {
199             commentFilter.setPinned( new Boolean( strFilterPinned ) );
200         }
201         if ( StringUtils.isNotBlank( strFilterMarkAsImportant ) )
202         {
203             commentFilter.setImportant( new Boolean( strFilterMarkAsImportant ) );
204         }
205         commentFilter.setSortedAttributeName( strSortedAttributeName );
206         commentFilter.setAscSort( bIsAscSort );
207 
208         BlogSearchFilter filter = new BlogSearchFilter( );
209         filter.setUserEditedBlogVersion( adminUser.getAccessCode( ) );
210 
211         BlogSearchService.getInstance( ).getSearchResults( filter, listBlogsId );
212 
213         List<Comment> listComments = _commentService.findByResource( resourceExtender.getIdExtendableResource( ), Blog.PROPERTY_RESOURCE_TYPE, commentFilter,
214                 nItemsOffset, _nItemsPerPage, config.getAuthorizeSubComments( ) );
215         listComments= listComments.stream( ).filter( comment -> listBlogsId.stream( ).anyMatch( p -> String.valueOf( p ).equals( comment.getIdExtendableResource( ) ) ) )
216         						  .collect( Collectors.toList( ) );
217         int nItemsCount = listComments.size( );
218 
219         // We get the paginator
220         IPaginator<Comment> paginator = new LocalizedDelegatePaginator<Comment>( listComments, _nItemsPerPage, getHomeUrl( request ),
221                 Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex, nItemsCount, AdminUserService.getLocale( request ) );
222 
223         Map<String, Object> model = new HashMap<String, Object>( );
224         List<Comment> listCommentDisplay = paginator.getPageItems( );
225         if ( adminUser != null && WorkflowService.getInstance( ).isAvailable( ) )
226         {
227             for ( Comment comment : listCommentDisplay )
228             {
229 
230                 comment.setListWorkflowActions( WorkflowService.getInstance( ).getActions( comment.getIdComment( ),
231                         _commentService.getResourceType( comment.getExtendableResourceType( ) ), config.getIdWorkflow( ), adminUser ) );
232 
233             }
234         }
235 
236         HashMap<String, IExtendableResource> mapResourceExtender = new HashMap<>( );
237         for ( Comment comment : listCommentDisplay )
238         {
239             if ( !mapResourceExtender.containsKey( comment.getIdExtendableResource( ) ) )
240             {
241 
242                 mapResourceExtender.put( comment.getIdExtendableResource( ),
243                         _resourceExtenderService.getExtendableResource( comment.getIdExtendableResource( ), resourceExtender.getExtendableResourceType( ) ) );
244             }
245 
246         }
247       
248         request.getSession( ).setAttribute(
249                 CommentPlugin.PLUGIN_NAME + CommentConstants.SESSION_COMMENT_ADMIN_POST_BACK_URL, JSP_VIEW_EXTENDER_BLOG );
250         
251         model.put( CommentConstants.MARK_ALL_RESOURCES, true );
252         model.put( CommentConstants.MARK_RESOURCE_EXTENDER_MAP, mapResourceExtender );
253 
254         model.put( CommentConstants.MARK_ID_EXTENDABLE_RESOURCE, resourceExtender.getIdExtendableResource( ) );
255         model.put( CommentConstants.MARK_EXTENDABLE_RESOURCE_TYPE, resourceExtender.getExtendableResourceType( ) );
256 
257         model.put( CommentConstants.MARK_LIST_COMMENTS, listCommentDisplay );
258         model.put( CommentConstants.MARK_PAGINATOR, paginator );
259         model.put( CommentConstants.MARK_NB_ITEMS_PER_PAGE, Integer.toString( paginator.getItemsPerPage( ) ) );
260         model.put( CommentConstants.MARK_ASC_SORT, bIsAscSort );
261         model.put( Parameters.SORTED_ATTRIBUTE_NAME, strSortedAttributeName );
262         model.put( CommentConstants.PARAMETER_ID_COMMENT, request.getParameter( CommentConstants.PARAMETER_ID_COMMENT ) );
263         model.put( CommentConstants.MARK_USE_BBCODE, config.getUseBBCodeEditor( ) );
264         model.put( CommentConstants.MARK_ADMIN_BADGE, config.getAdminBadge( ) );
265         model.put( CommentConstants.MARK_ALLOW_SUB_COMMENTS, config.getAuthorizeSubComments( ) );
266         model.put( CommentConstants.MARK_FILTER_STATE, strFilterState );
267         model.put( CommentConstants.MARK_FILTER_MARK_AS_IMPORTANT, strFilterMarkAsImportant );
268         model.put( CommentConstants.MARK_FILTER_PINNED, strFilterPinned );
269         model.put( CommentConstants.MARK_LIST_COMMENT_STATES, _commentService.getRefListCommentStates( request.getLocale( ) ) );
270         model.put( CommentConstants.MARK_LIST_MARK_AS_IMPORTANT_FILTER, _commentService.getRefListFilterAsImportant( request.getLocale( ) ) );
271         model.put( CommentConstants.MARK_LIST_PINNED_FILTER, _commentService.getRefListFilterAsPinned( request.getLocale( ) ) );
272 
273         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_COMMENTS, request.getLocale( ), model );
274 
275         String strContent = template.getHtml( );
276 
277         return strContent;
278 
279     }
280   
281 }