View Javadoc
1   /*
2    * Copyright (c) 2002-2022, 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.workflow.web;
35  
36  import fr.paris.lutece.api.user.User;
37  import fr.paris.lutece.plugins.workflow.utils.WorkflowUtils;
38  import fr.paris.lutece.plugins.workflowcore.business.workflow.Workflow;
39  import fr.paris.lutece.plugins.workflowcore.business.workflow.WorkflowFilter;
40  import fr.paris.lutece.plugins.workflowcore.service.workflow.IWorkflowService;
41  import fr.paris.lutece.plugins.workflowcore.service.workflow.WorkflowService;
42  import fr.paris.lutece.portal.business.right.Right;
43  import fr.paris.lutece.portal.business.right.RightHome;
44  import fr.paris.lutece.portal.business.user.AdminUser;
45  import fr.paris.lutece.portal.service.dashboard.DashboardComponent;
46  import fr.paris.lutece.portal.service.database.AppConnectionService;
47  import fr.paris.lutece.portal.service.plugin.Plugin;
48  import fr.paris.lutece.portal.service.plugin.PluginService;
49  import fr.paris.lutece.portal.service.security.SecurityTokenService;
50  import fr.paris.lutece.portal.service.spring.SpringContextService;
51  import fr.paris.lutece.portal.service.template.AppTemplateService;
52  import fr.paris.lutece.portal.service.workgroup.AdminWorkgroupService;
53  import fr.paris.lutece.util.html.HtmlTemplate;
54  import fr.paris.lutece.util.url.UrlItem;
55  
56  import org.apache.commons.lang3.StringUtils;
57  
58  import java.util.HashMap;
59  import java.util.List;
60  import java.util.Map;
61  
62  import javax.servlet.http.HttpServletRequest;
63  
64  /**
65   * Calendar Dashboard Component This component displays directories
66   */
67  public class WorkflowDashboardComponent extends DashboardComponent
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_WORKFLOW_LIST = "workflow_list";
73  
74      // PARAMETERS
75      private static final String PARAMETER_PLUGIN_NAME = "plugin_name";
76  
77      // TEMPLATES
78      private static final String TEMPLATE_DASHBOARD = "/admin/plugins/workflow/workflow_dashboard.html";
79  
80      // OTHER CONSTANTS
81      private static final int FILTER_NO_STATUS = -1;
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          IWorkflowService workflowService = SpringContextService.getBean( WorkflowService.BEAN_SERVICE );
98  
99          UrlItem url = new UrlItem( right.getUrl( ) );
100         url.addParameter( PARAMETER_PLUGIN_NAME, right.getPluginName( ) );
101 
102         WorkflowFilter filter = new WorkflowFilter( );
103         filter.setIsEnabled( FILTER_NO_STATUS );
104         filter.setWorkGroup( AdminWorkgroupService.ALL_GROUPS );
105 
106         List<Workflow> listWorkflow = workflowService.getListWorkflowsByFilter( filter );
107         listWorkflow = (List<Workflow>) AdminWorkgroupService.getAuthorizedCollection( listWorkflow, (User) user );
108 
109         Map<String, Object> model = new HashMap<>( );
110         model.put( MARK_WORKFLOW_LIST, listWorkflow );
111         model.put( MARK_URL, url.getUrl( ) );
112         model.put( MARK_ICON, plugin.getIconUrl( ) );
113         // Add CSRF Token
114         model.put( SecurityTokenService.MARK_TOKEN , SecurityTokenService.getInstance( ).getToken( request, WorkflowUtils.CONSTANT_ACTION_MODIFY_WORKFLOW ) );
115 
116         HtmlTemplate t = AppTemplateService.getTemplate( TEMPLATE_DASHBOARD, user.getLocale( ), model );
117 
118         return t.getHtml( );
119     }
120 }