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;
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.Collection;
41 import java.util.HashMap;
42 import java.util.Map;
43
44 import javax.servlet.http.HttpServletRequest;
45
46 import fr.paris.lutece.plugins.blog.service.BlogParameterService;
47 import org.apache.commons.lang3.StringUtils;
48
49 import fr.paris.lutece.plugins.blog.business.portlet.BlogListPortlet;
50 import fr.paris.lutece.plugins.blog.business.portlet.BlogListPortletHome;
51 import fr.paris.lutece.plugins.blog.business.portlet.BlogPortlet;
52 import fr.paris.lutece.plugins.blog.business.portlet.BlogPortletHome;
53 import fr.paris.lutece.plugins.blog.business.portlet.BlogPublication;
54 import fr.paris.lutece.plugins.blog.business.portlet.BlogPublicationHome;
55 import fr.paris.lutece.plugins.blog.business.portlet.PortletFilter;
56 import fr.paris.lutece.plugins.blog.business.portlet.PortletOrder;
57 import fr.paris.lutece.plugins.blog.service.BlogService;
58 import fr.paris.lutece.plugins.blog.service.PublishingService;
59 import fr.paris.lutece.portal.business.portlet.Portlet;
60 import fr.paris.lutece.portal.business.portlet.PortletHome;
61 import fr.paris.lutece.portal.business.portlet.PortletType;
62 import fr.paris.lutece.portal.service.i18n.I18nService;
63 import fr.paris.lutece.portal.service.portal.PortalService;
64 import fr.paris.lutece.portal.service.portlet.PortletResourceIdService;
65 import fr.paris.lutece.portal.service.portlet.PortletService;
66 import fr.paris.lutece.portal.service.rbac.RBACService;
67 import fr.paris.lutece.portal.service.template.AppTemplateService;
68 import fr.paris.lutece.portal.service.util.AppPropertiesService;
69 import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
70 import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
71 import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
72 import fr.paris.lutece.util.ReferenceItem;
73 import fr.paris.lutece.util.html.HtmlTemplate;
74 import fr.paris.lutece.api.user.User;
75 import fr.paris.lutece.util.html.AbstractPaginator;
76 import fr.paris.lutece.portal.web.util.LocalizedPaginator;
77
78
79
80
81 @Controller( controllerJsp = "ManagePublicationBlogs.jsp", controllerPath = "jsp/admin/plugins/blog/", right = "BLOG_MANAGEMENT" )
82 public class BlogPublicationJspBean extends BlogJspBean
83 {
84
85 private static final long serialVersionUID = -2768032240064881165L;
86 private static final String TEMPLATE_PUBLICATION_HTMLDOC = "/admin/plugins/blog/publication_blog.html";
87 private static final String TEMPLATE_PORTLET_PAGE_PATH = "/admin/plugins/blog/portlet_page_path.html";
88
89
90
91
92 private static final String PROPERTY_PAGE_TITLE_PUBLICATION_HTMLDOC = "blog.publication_blogpageTitle";
93
94
95 public static final String MARK_PORTLET_LIST = "portlet_list";
96 private static final String MARK_PORTLET_FILTER_ERROR = "portlet_filter_error";
97 private static final String MARK_ORDER_PORTLET = "order_portlet";
98 private static final String MARK_ORDER_PORTLET_ASC = "order_portlet_asc";
99 private static final String MARK_PORTLET = "portlet";
100 private static final String MARK_LIST_PAGE = "page_list";
101
102 private static final String MARK_DOCUMENT_PORTLET_LIST = "document_portlet_list";
103 private static final String MARK_DOCUMENT_LIST_PORTLET_LIST = "document_list_portlet_list";
104 private static final String MARK_PORTLET_FILTER = "portlet_filter";
105
106
107
108
109
110
111 private static final String VIEW_MANAGE_PUBLICATION = "manageBlogsPublication";
112
113
114 private static final String ACTION_PUBLISHE_DOCUMENT = "publishDocument";
115 private static final String ACTION_UNPUBLISHE_DOCUMENT = "unPublishDocument";
116
117
118
119
120
121 private static final String PARAMETER_PUBLISHED_DATE = "dateBeginPublishing";
122 private static final String PARAMETER_UNPUBLISHED_DATE = "dateEndPublishing";
123 private static final String PARAMETER_PORTLET_ID = "idPortlet";
124
125 private static final String PARAMETER_ORDER_PORTLET = "order_portlet";
126 private static final String PARAMETER_ORDER_PORTLET_ASC = "order_portlet_asc";
127 private static final String PARAMETER_PORTLET_FILTER_TYPE = "portlet_filter_type";
128 private static final String PARAMETER_PORTLET_FILTER_VALUE = "portlet_filter_value";
129 private static final String MARK_NB_ITEMS_PER_PAGE = "nb_items_per_page";
130 private static final String PROPERTY_DEFAULT_LIST_ITEM_PER_PAGE = "blog.listItems.itemsPerPage";
131 private static final String PARAMETER_PAGE_INDEX = "page_index";
132
133 private static final String PARAMETER_IS_DISPLAY_LATEST_PORTLETS = "is_display_latest_portlets";
134
135
136 private static final String ERROR_MESSAGE_NOT_NUMERIC_FIELD = "blog.message.notNumericField";
137 private static final String ERROR_MESSAGE_MESSAGE_MANDATORY_SEARCH_FIELD = "blog.message.mandatory.searchField";
138
139
140 protected BlogPublication _blogPublication;
141
142
143
144
145
146
147
148
149 @View( value = VIEW_MANAGE_PUBLICATION )
150 public String getManageBlogPublication( HttpServletRequest request )
151 {
152 int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_BLOG ) );
153 String strErrorFilter = null;
154 _blogPublication = ( _blogPublication != null ) ? _blogPublication : new BlogPublication( );
155
156 if ( _blog == null || ( _blog.getId( ) != nId ) )
157 {
158
159 _blog = BlogService.getInstance( ).findByPrimaryKeyWithoutBinaries( nId );
160
161 }
162 PortletOrder/business/portlet/PortletOrder.html#PortletOrder">PortletOrder pOrder = new PortletOrder( );
163 String strOrderPortlet = request.getParameter( PARAMETER_ORDER_PORTLET );
164 boolean bIsDisplayPortlets = ( ( request.getParameter( PARAMETER_IS_DISPLAY_LATEST_PORTLETS ) == null )
165 || Boolean.valueOf( request.getParameter( PARAMETER_IS_DISPLAY_LATEST_PORTLETS ) ) );
166
167 String strOrderPortletAsc = request.getParameter( PARAMETER_ORDER_PORTLET_ASC );
168 int nOrderPortlet = -1;
169 int nOrderPortletAsc = -1;
170
171 Map<String, Object> model = getModel( );
172 PortletFilter portletFilter = null;
173 if ( !bIsDisplayPortlets )
174 {
175
176 portletFilter = new PortletFilter( );
177 portletFilter.setPortletFilterType( request.getParameter( PARAMETER_PORTLET_FILTER_TYPE ) );
178 portletFilter.setDisplayLatestPortlets( bIsDisplayPortlets );
179 portletFilter.setSearchValue( request.getParameter( PARAMETER_PORTLET_FILTER_VALUE ) );
180 strErrorFilter = setFillFilter( portletFilter );
181
182 if ( strErrorFilter != null )
183 {
184 model.put( MARK_PORTLET_FILTER_ERROR, strErrorFilter );
185 }
186
187 }
188
189 if ( StringUtils.isNotBlank( strOrderPortlet ) && StringUtils.isNumeric( strOrderPortlet ) && StringUtils.isNotBlank( strOrderPortletAsc )
190 && StringUtils.isNumeric( strOrderPortletAsc ) )
191 {
192 nOrderPortlet = Integer.parseInt( strOrderPortlet );
193 nOrderPortletAsc = Integer.parseInt( strOrderPortletAsc );
194 pOrder.setTypeOrder( nOrderPortlet );
195 pOrder.setSortAsc( nOrderPortletAsc == PortletOrder.SORT_ASC );
196 }
197
198 Collection<ReferenceItem> listDocumentListPortlets = getListAuthorizedDocumentListPortlets( _blog.getId( ), pOrder,
199 ( strErrorFilter == null ) ? portletFilter : null );
200 Collection<ReferenceItem> listDocumentPortlets = getListAuthorizedDocumentPortlets( _blog.getId( ), pOrder,
201 ( strErrorFilter == null ) ? portletFilter : null );
202
203 java.util.List<ReferenceItem> listDocumentPortletsRef = new ArrayList<>( listDocumentListPortlets );
204 model = getPaginatedListModel( request, MARK_DOCUMENT_LIST_PORTLET_LIST, listDocumentPortletsRef, getHomeUrl( request ), model );
205 model.put( MARK_DOCUMENT_PORTLET_LIST, listDocumentPortlets );
206 model.put( MARK_BLOG, _blog );
207 model.put( MARK_PORTLET_LIST, PublishingService.getInstance( ).getBlogsPortletstoPublish( ) );
208 model.put( MARK_ORDER_PORTLET, nOrderPortlet );
209 model.put( MARK_ORDER_PORTLET_ASC, nOrderPortletAsc );
210 model.put( MARK_PORTLET_FILTER, portletFilter );
211 return getPage( PROPERTY_PAGE_TITLE_PUBLICATION_HTMLDOC, TEMPLATE_PUBLICATION_HTMLDOC, model );
212 }
213
214
215
216
217
218
219
220
221 @Action( ACTION_PUBLISHE_DOCUMENT )
222 public String doPublishDocument( HttpServletRequest request ) throws ParseException
223 {
224 populateBlogPublication( _blogPublication, request );
225
226 if ( _blogPublication.getDateBeginPublishing( ) == null )
227 {
228
229 _blogPublication.setDateBeginPublishing( new Date( System.currentTimeMillis( ) ) );
230 }
231 if ( _blogPublication.getDateEndPublishing( ) == null )
232 {
233 _blogPublication.setDateEndPublishing(BlogParameterService.getInstance().getDefaultDateEndPublishing());
234 }
235 if ( _blogPublication.getIdPortlet( ) != 0 )
236 {
237
238 BlogPublicationHome.create( _blogPublication );
239
240 _blog = BlogService.getInstance( ).findByPrimaryKeyWithoutBinaries( _blogPublication.getIdBlog( ) );
241 }
242
243 return getManageBlogPublication( request );
244
245 }
246
247
248
249
250
251
252
253
254 @Action( ACTION_UNPUBLISHE_DOCUMENT )
255 public String doUnPublishDocument( HttpServletRequest request )
256 {
257 _blogPublication = new BlogPublication( );
258 populate( _blogPublication, request );
259
260 BlogPublicationHome.remove( _blogPublication.getIdBlog( ), _blogPublication.getIdPortlet( ) );
261 _blog = BlogService.getInstance( ).findByPrimaryKeyWithoutBinaries( _blogPublication.getIdBlog( ) );
262
263 return getManageBlogPublication( request );
264
265 }
266
267
268
269
270
271
272
273
274
275
276 private void populateBlogPublication( BlogPublication htmldocPublication, HttpServletRequest request ) throws ParseException
277 {
278
279 int nIdDoc = Integer.parseInt( request.getParameter( PARAMETER_ID_BLOG ) );
280 String strIdPortlet = request.getParameter( PARAMETER_PORTLET_ID );
281 int nIdPortlet = Integer.parseInt( strIdPortlet != null ? strIdPortlet : "0" );
282
283 String dateBeginPublishingStr = request.getParameter( PARAMETER_PUBLISHED_DATE );
284 String dateEndPublishingStr = request.getParameter( PARAMETER_UNPUBLISHED_DATE );
285 java.sql.Date dateBeginPublishing = null;
286 java.sql.Date dateEndPublishing = null;
287
288 SimpleDateFormat sdf = new SimpleDateFormat( "dd/MM/yyyy" );
289 java.util.Date parsed;
290
291 if ( dateBeginPublishingStr != null && !dateBeginPublishingStr.isEmpty( ) )
292 {
293 parsed = sdf.parse( dateBeginPublishingStr );
294 dateBeginPublishing = new java.sql.Date( parsed.getTime( ) );
295 }
296 if ( dateEndPublishingStr != null && !dateEndPublishingStr.isEmpty( ) )
297 {
298 parsed = sdf.parse( dateEndPublishingStr );
299 dateEndPublishing = new java.sql.Date( parsed.getTime( ) );
300 }
301 int nBlogOrder = BlogListPortletHome.getMinDocBlogOrder( );
302 nBlogOrder = nBlogOrder - 1;
303
304 htmldocPublication.setIdBlog( nIdDoc );
305 htmldocPublication.setIdPortlet( nIdPortlet );
306 htmldocPublication.setDateBeginPublishing( dateBeginPublishing );
307 htmldocPublication.setDateEndPublishing( dateEndPublishing );
308 htmldocPublication.setBlogOrder( nBlogOrder );
309
310 }
311
312
313
314
315
316
317
318
319 public String setFillFilter( PortletFilter portletFilter )
320 {
321 String strErrorMessage = null;
322 String strValue;
323
324 if ( ( portletFilter.getSearchValue( ) != null ) && !portletFilter.getSearchValue( ).trim( ).equals( "" )
325 && ( portletFilter.getPortletFilterType( ) != null ) )
326 {
327 strValue = portletFilter.getSearchValue( ).trim( );
328
329 if ( portletFilter.getPortletFilterType( ).equals( PortletFilter.PAGE_NAME ) )
330 {
331 portletFilter.setPageName( strValue.split( PortletFilter.CONSTANTE_SPACE_STRING ) );
332 }
333 else
334 if ( portletFilter.getPortletFilterType( ).equals( PortletFilter.PORTLET_NAME ) )
335 {
336 portletFilter.setPortletName( strValue.split( PortletFilter.CONSTANTE_SPACE_STRING ) );
337 }
338 else
339 if ( portletFilter.getPortletFilterType( ).equals( PortletFilter.PAGE_ID ) )
340 {
341 if ( StringUtils.isNumeric( strValue ) )
342 {
343 portletFilter.setIdPage( Integer.parseInt( strValue ) );
344 }
345 else
346 {
347 strErrorMessage = I18nService.getLocalizedString( ERROR_MESSAGE_NOT_NUMERIC_FIELD, getLocale( ) );
348 }
349 }
350 }
351
352 else
353 {
354 strErrorMessage = I18nService.getLocalizedString( ERROR_MESSAGE_MESSAGE_MANDATORY_SEARCH_FIELD, getLocale( ) );
355 }
356
357 return strErrorMessage;
358 }
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383 private Collection<ReferenceItem> getListAuthorizedDocumentListPortlets( int nDocumentId, PortletOrder pOrder, PortletFilter pFilter )
384 {
385 Collection<ReferenceItem> listPortlets = new ArrayList<>( );
386
387
388 if ( RBACService.isAuthorized( PortletType.RESOURCE_TYPE, BlogListPortlet.RESOURCE_ID, PortletResourceIdService.PERMISSION_MANAGE, (User) getUser( ) ) )
389 {
390 listPortlets.addAll( BlogListPortletHome.findByFilter( nDocumentId, pOrder, pFilter ) );
391 }
392
393
394 return filterByWorkgroup( listPortlets, pFilter );
395 }
396
397
398
399
400
401
402
403
404
405
406 private Collection<ReferenceItem> filterByWorkgroup( Collection<ReferenceItem> listPortlets, PortletFilter pFilter )
407 {
408
409 Collection<ReferenceItem> listFilteredPortlets = new ArrayList<>( );
410
411
412
413 for ( ReferenceItem item : listPortlets )
414 {
415 Portlet portlet = PortletHome.findByPrimaryKey( Integer.parseInt( item.getCode( ) ) );
416
417 if ( ( portlet != null ) && PortletService.getInstance( ).isAuthorized( portlet, getUser( ) ) )
418 {
419 Map<String, Object> subModel = new HashMap<>( );
420 subModel.put( MARK_LIST_PAGE, PortalService.getPagePath( PortletHome.findByPrimaryKey( Integer.parseInt( item.getCode( ) ) ).getPageId( ) ) );
421 subModel.put( MARK_PORTLET, item );
422
423 HtmlTemplate subTemplate = AppTemplateService.getTemplate( TEMPLATE_PORTLET_PAGE_PATH, getLocale( ), subModel );
424 item.setName( subTemplate.getHtml( ) );
425
426 listFilteredPortlets.add( item );
427
428 }
429 }
430
431 return listFilteredPortlets;
432 }
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458 private Collection<ReferenceItem> getListAuthorizedDocumentPortlets( int nDocumentId, PortletOrder pOrder, PortletFilter pFilter )
459 {
460 Collection<ReferenceItem> listPortlets = new ArrayList<>( );
461
462
463 if ( RBACService.isAuthorized( PortletType.RESOURCE_TYPE, BlogPortlet.RESOURCE_ID, PortletResourceIdService.PERMISSION_MANAGE, (User) getUser( ) ) )
464 {
465 listPortlets.addAll( BlogPortletHome.findByFilter( nDocumentId, pOrder, pFilter ) );
466 }
467
468 return filterByWorkgroup( listPortlets, pFilter );
469 }
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484 protected Map<String, Object> getPaginatedListModel( HttpServletRequest request, String strBookmark, java.util.List list, String strManageJsp, Map<String, Object> model)
485 {
486 _strCurrentPageIndex = AbstractPaginator.getPageIndex( request, AbstractPaginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
487 int defaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_DEFAULT_LIST_ITEM_PER_PAGE, 50 );
488 _nItemsPerPage = AbstractPaginator.getItemsPerPage( request, AbstractPaginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage, defaultItemsPerPage );
489
490 fr.paris.lutece.util.url.UrlItem url = new fr.paris.lutece.util.url.UrlItem( strManageJsp );
491 String strUrl = url.getUrl( );
492
493
494 String strSortedAttributeName = request.getParameter( MARK_SORTED_ATTRIBUTE );
495
496 if ( strSortedAttributeName != null || _bIsSorted )
497 {
498 if ( strSortedAttributeName == null )
499 {
500 strSortedAttributeName = _strSortedAttributeName;
501 }
502 String strAscSort = request.getParameter( MARK_ASC_SORT );
503
504 boolean bIsAscSort = Boolean.parseBoolean( strAscSort );
505 if ( strAscSort == null )
506 {
507 bIsAscSort = _bIsAscSort;
508 }
509
510 java.util.Collections.sort( list, new fr.paris.lutece.util.sort.AttributeComparator( strSortedAttributeName, bIsAscSort ) );
511
512 _bIsSorted = true;
513
514 _strSortedAttributeName = strSortedAttributeName;
515 _bIsAscSort = bIsAscSort;
516 }
517
518
519 LocalizedPaginator paginator = new LocalizedPaginator( list, _nItemsPerPage, strUrl, PARAMETER_PAGE_INDEX, _strCurrentPageIndex, getLocale( ) );
520
521
522 model.put( MARK_NB_ITEMS_PER_PAGE, _nItemsPerPage );
523 model.put( MARK_PAGINATOR, paginator );
524 model.put( strBookmark, paginator.getPageItems( ) );
525
526 return model;
527 }
528
529 }