View Javadoc
1   /*
2    * Copyright (c) 2002-2021, 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;
35  
36  import java.util.HashMap;
37  import java.util.List;
38  import java.util.Map;
39  
40  import javax.servlet.http.HttpServletRequest;
41  
42  import org.apache.commons.lang3.StringUtils;
43  
44  import fr.paris.lutece.api.user.User;
45  import fr.paris.lutece.plugins.blog.business.Blog;
46  import fr.paris.lutece.plugins.blog.service.BlogService;
47  import fr.paris.lutece.portal.business.rbac.RBAC;
48  import fr.paris.lutece.portal.business.right.Right;
49  import fr.paris.lutece.portal.business.right.RightHome;
50  import fr.paris.lutece.portal.business.user.AdminUser;
51  import fr.paris.lutece.portal.service.dashboard.DashboardComponent;
52  import fr.paris.lutece.portal.service.database.AppConnectionService;
53  import fr.paris.lutece.portal.service.datastore.DatastoreService;
54  import fr.paris.lutece.portal.service.plugin.Plugin;
55  import fr.paris.lutece.portal.service.plugin.PluginService;
56  import fr.paris.lutece.portal.service.rbac.RBACService;
57  import fr.paris.lutece.portal.service.template.AppTemplateService;
58  import fr.paris.lutece.util.html.HtmlTemplate;
59  import fr.paris.lutece.util.url.UrlItem;
60  
61  /**
62   * Calendar Dashboard Component This component displays directories
63   */
64  public class BlogDashboardComponent extends DashboardComponent
65  {
66      // PARAMETERS
67      private static final String PARAMETER_PLUGIN_NAME = "plugin_name";
68  
69      // MARKS
70      private static final String MARK_URL = "url";
71      private static final String MARK_ICON = "icon";
72      private static final String MARK_LAST_MODIFIED_DOCUMENT = "last_modified_document";
73      protected static final String MARK_PERMISSION_CREATE_BLOG = "permission_manage_create_blog";
74      protected static final String MARK_PERMISSION_MODIFY_BLOG = "permission_manage_modify_blog";
75      protected static final String MARK_PERMISSION_PUBLISH_BLOG = "permission_manage_publish_blog";
76      protected static final String MARK_PERMISSION_DELETE_BLOG = "permission_manage_delete_blog";
77  
78      private static final String PROPERTY_NIMBER_DOCUMENT_LOADED = "number.documents.to.be.loaded";
79  
80      // TEMPALTES
81      private static final String TEMPLATE_DASHBOARD = "/admin/plugins/blog/dashboard/blogs_dashboard.html";
82  
83      /**
84       * {@inheritDoc}
85       */
86      @Override
87      public String getDashboardData( AdminUser user, HttpServletRequest request )
88      {
89          Right right = RightHome.findByPrimaryKey( getRight( ) );
90          Plugin plugin = PluginService.getPlugin( right.getPluginName( ) );
91  
92          if ( !( ( plugin.getDbPoolName( ) != null ) && !AppConnectionService.NO_POOL_DEFINED.equals( plugin.getDbPoolName( ) ) ) )
93          {
94              return StringUtils.EMPTY;
95          }
96  
97          String strValue = DatastoreService.getDataValue( PROPERTY_NIMBER_DOCUMENT_LOADED, "5" );
98          List<Blog> lastModifiedDocument = BlogService.getInstance( ).getLastModifiedBlogsList( Integer.parseInt( strValue ) );
99  
100         UrlItem url = new UrlItem( right.getUrl( ) );
101         url.addParameter( PARAMETER_PLUGIN_NAME, right.getPluginName( ) );
102 
103         boolean bPermissionCreate = RBACService.isAuthorized( Blog.PROPERTY_RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID, Blog.PERMISSION_CREATE, (User) user );
104         boolean bPermissionModify = RBACService.isAuthorized( Blog.PROPERTY_RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID, Blog.PERMISSION_MODIFY, (User) user );
105         boolean bPermissionDelete = RBACService.isAuthorized( Blog.PROPERTY_RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID, Blog.PERMISSION_DELETE, (User) user );
106         boolean bPermissionPublish = RBACService.isAuthorized( Blog.PROPERTY_RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID, Blog.PERMISSION_PUBLISH, (User) user );
107 
108         Map<String, Object> model = new HashMap<>( );
109 
110         model.put( MARK_PERMISSION_CREATE_BLOG, bPermissionCreate );
111         model.put( MARK_PERMISSION_MODIFY_BLOG, bPermissionModify );
112         model.put( MARK_PERMISSION_DELETE_BLOG, bPermissionDelete );
113         model.put( MARK_PERMISSION_PUBLISH_BLOG, bPermissionPublish );
114 
115         model.put( MARK_LAST_MODIFIED_DOCUMENT, lastModifiedDocument );
116         model.put( MARK_URL, url.getUrl( ) );
117         model.put( MARK_ICON, plugin.getIconUrl( ) );
118 
119         HtmlTemplate t = AppTemplateService.getTemplate( TEMPLATE_DASHBOARD, user.getLocale( ), model );
120 
121         return t.getHtml( );
122     }
123 }