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