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.service;
35  
36  import fr.paris.lutece.plugins.workflow.utils.WorkflowUtils;
37  import fr.paris.lutece.plugins.workflowcore.business.state.State;
38  import fr.paris.lutece.plugins.workflowcore.business.state.StateFilter;
39  import fr.paris.lutece.plugins.workflowcore.service.state.IStateService;
40  import fr.paris.lutece.plugins.workflowcore.service.state.StateService;
41  import fr.paris.lutece.plugins.workflowcore.service.workflow.IWorkflowService;
42  import fr.paris.lutece.plugins.workflowcore.service.workflow.WorkflowService;
43  import fr.paris.lutece.portal.service.rbac.Permission;
44  import fr.paris.lutece.portal.service.rbac.ResourceIdService;
45  import fr.paris.lutece.portal.service.rbac.ResourceType;
46  import fr.paris.lutece.portal.service.rbac.ResourceTypeManager;
47  import fr.paris.lutece.portal.service.spring.SpringContextService;
48  import fr.paris.lutece.util.ReferenceList;
49  
50  import java.util.List;
51  import java.util.Locale;
52  
53  /**
54   *
55   * class StateResourceIdService
56   *
57   */
58  public class StateResourceIdService extends ResourceIdService
59  {
60      /** Permission for viewing the resource associated to the state */
61      public static final String PERMISSION_VIEW = "VIEW";
62  
63      /**
64       * Permission to view every workgroups
65       */
66      public static final String PERMISSION_VIEW_ALL_WORKGROUP = "VIEW_ALL_WORKGROUP";
67      private static final String PROPERTY_LABEL_RESOURCE_TYPE = "workflow.permission.label.resource_type_state";
68      private static final String PROPERTY_LABEL_VIEW = "workflow.permission.label.view_state";
69      private static final String PROPERTY_LABEL_VIEW_ALL_WORKGROUP = "workflow.permission.label.view_all_workgroup";
70  
71      /** Creates a new instance of DocumentTypeResourceIdService */
72      public StateResourceIdService( )
73      {
74          setPluginName( WorkflowPlugin.PLUGIN_NAME );
75      }
76  
77      /**
78       * {@inheritDoc}
79       */
80      @Override
81      public void register( )
82      {
83          ResourceType rt = new ResourceType( );
84          rt.setResourceIdServiceClass( StateResourceIdService.class.getName( ) );
85          rt.setPluginName( WorkflowPlugin.PLUGIN_NAME );
86          rt.setResourceTypeKey( State.RESOURCE_TYPE );
87          rt.setResourceTypeLabelKey( PROPERTY_LABEL_RESOURCE_TYPE );
88  
89          Permission p = new Permission( );
90          p.setPermissionKey( PERMISSION_VIEW );
91          p.setPermissionTitleKey( PROPERTY_LABEL_VIEW );
92          rt.registerPermission( p );
93  
94          p = new Permission( );
95          p.setPermissionKey( PERMISSION_VIEW_ALL_WORKGROUP );
96          p.setPermissionTitleKey( PROPERTY_LABEL_VIEW_ALL_WORKGROUP );
97          rt.registerPermission( p );
98  
99          ResourceTypeManager.registerResourceType( rt );
100     }
101 
102     /**
103      * {@inheritDoc}
104      */
105     @Override
106     public ReferenceList getResourceIdList( Locale locale )
107     {
108         IStateService stateService = SpringContextService.getBean( StateService.BEAN_SERVICE );
109         IWorkflowService workflowService = SpringContextService.getBean( WorkflowService.BEAN_SERVICE );
110         List<State> listState = stateService.getListStateByFilter( new StateFilter( ) );
111 
112         ReferenceList reflistState = new ReferenceList( );
113 
114         for ( State state : listState )
115         {
116             state.setWorkflow( workflowService.findByPrimaryKey( state.getWorkflow( ).getId( ) ) );
117             reflistState.addItem( state.getId( ), state.getWorkflow( ).getName( ) + "/" + state.getName( ) );
118         }
119 
120         return reflistState;
121     }
122 
123     /**
124      * {@inheritDoc}
125      */
126     @Override
127     public String getTitle( String strId, Locale locale )
128     {
129         IStateService stateService = SpringContextService.getBean( StateService.BEAN_SERVICE );
130         IWorkflowService workflowService = SpringContextService.getBean( WorkflowService.BEAN_SERVICE );
131         int nId = WorkflowUtils.convertStringToInt( strId );
132         State state = stateService.findByPrimaryKey( nId );
133 
134         if ( state != null )
135         {
136             state.setWorkflow( workflowService.findByPrimaryKey( state.getWorkflow( ).getId( ) ) );
137         }
138 
139         return ( state != null ) ? ( state.getWorkflow( ).getName( ) + "/" + state.getName( ) ) : null;
140     }
141 }