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