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.action.Action;
38  import fr.paris.lutece.plugins.workflowcore.business.action.ActionFilter;
39  import fr.paris.lutece.plugins.workflowcore.business.state.State;
40  import fr.paris.lutece.plugins.workflowcore.service.action.ActionService;
41  import fr.paris.lutece.plugins.workflowcore.service.action.IActionService;
42  import fr.paris.lutece.plugins.workflowcore.service.state.IStateService;
43  import fr.paris.lutece.plugins.workflowcore.service.state.StateService;
44  import fr.paris.lutece.plugins.workflowcore.service.workflow.IWorkflowService;
45  import fr.paris.lutece.plugins.workflowcore.service.workflow.WorkflowService;
46  import fr.paris.lutece.portal.service.rbac.Permission;
47  import fr.paris.lutece.portal.service.rbac.ResourceIdService;
48  import fr.paris.lutece.portal.service.rbac.ResourceType;
49  import fr.paris.lutece.portal.service.rbac.ResourceTypeManager;
50  import fr.paris.lutece.portal.service.spring.SpringContextService;
51  import fr.paris.lutece.util.ReferenceList;
52  
53  import java.util.List;
54  import java.util.Locale;
55  
56  /**
57   *
58   * class ActionResourceIdService
59   *
60   */
61  public class ActionResourceIdService extends ResourceIdService
62  {
63      /** Permission for viewing a action */
64      public static final String PERMISSION_VIEW = "VIEW";
65  
66      /** Permission for managing advanced parameters */
67      public static final String PERMISSION_MANAGE_ADVANCED_PARAMETERS = "MANAGE_ADVANCED_PARAMETERS";
68      private static final String PROPERTY_LABEL_RESOURCE_TYPE = "workflow.permission.label.resource_type_action";
69      private static final String PROPERTY_LABEL_VIEW = "workflow.permission.label.view_action";
70      private static final String PROPERTY_LABEL_MANAGE_ADVANCED_PARAMETERS = "workflow.permission.label.manage_advanced_parameters";
71  
72      /** Creates a new instance of DocumentTypeResourceIdService */
73      public ActionResourceIdService( )
74      {
75          setPluginName( WorkflowPlugin.PLUGIN_NAME );
76      }
77  
78      /**
79       * {@inheritDoc}
80       */
81      @Override
82      public void register( )
83      {
84          ResourceType rt = new ResourceType( );
85          rt.setResourceIdServiceClass( ActionResourceIdService.class.getName( ) );
86          rt.setPluginName( WorkflowPlugin.PLUGIN_NAME );
87          rt.setResourceTypeKey( Action.RESOURCE_TYPE );
88          rt.setResourceTypeLabelKey( PROPERTY_LABEL_RESOURCE_TYPE );
89  
90          Permission p = new Permission( );
91          p.setPermissionKey( PERMISSION_VIEW );
92          p.setPermissionTitleKey( PROPERTY_LABEL_VIEW );
93          rt.registerPermission( p );
94  
95          p = new Permission( );
96          p.setPermissionKey( PERMISSION_MANAGE_ADVANCED_PARAMETERS );
97          p.setPermissionTitleKey( PROPERTY_LABEL_MANAGE_ADVANCED_PARAMETERS );
98          rt.registerPermission( p );
99  
100         ResourceTypeManager.registerResourceType( rt );
101     }
102 
103     /**
104      * Returns a list of action resource ids
105      * 
106      * @param locale
107      *            The current locale
108      * @return A list of resource ids
109      */
110     public ReferenceList getResourceIdList( Locale locale )
111     {
112         IActionService actionService = SpringContextService.getBean( ActionService.BEAN_SERVICE );
113         ActionFilter actionFilter = new ActionFilter( );
114         actionFilter.setAutomaticReflexiveAction( false );
115 
116         List<Action> listAction = actionService.getListActionByFilter( actionFilter );
117         ReferenceList reflistAction = new ReferenceList( );
118 
119         for ( Action action : listAction )
120         {
121             reflistAction.addItem( action.getId( ), getActionLabel( action ) );
122         }
123 
124         return reflistAction;
125     }
126 
127     /**
128      * Returns the Title of a given resource
129      * 
130      * @param strId
131      *            The Id of the resource
132      * @param locale
133      *            The current locale
134      * @return The Title of a given resource
135      */
136     public String getTitle( String strId, Locale locale )
137     {
138         IActionService actionService = SpringContextService.getBean( ActionService.BEAN_SERVICE );
139         int nId = WorkflowUtils.convertStringToInt( strId );
140         Action action = actionService.findByPrimaryKey( nId );
141 
142         if ( action == null )
143         {
144             return null;
145         }
146 
147         return getActionLabel( action );
148     }
149 
150     private String getActionLabel( Action action )
151     {
152         IWorkflowService workflowService = SpringContextService.getBean( WorkflowService.BEAN_SERVICE );
153         IStateService stateService = SpringContextService.getBean( StateService.BEAN_SERVICE );
154 
155         action.setWorkflow( workflowService.findByPrimaryKey( action.getWorkflow( ).getId( ) ) );
156         action.setStateAfter( stateService.findByPrimaryKey( action.getStateAfter( ).getId( ) ) );
157 
158         StringBuilder sbTitle = new StringBuilder( );
159         sbTitle.append( action.getWorkflow( ).getName( ) );
160         sbTitle.append( "/" );
161         sbTitle.append( action.getName( ) );
162         sbTitle.append( " ( " );
163         for ( Integer idStateBefore : action.getListIdStateBefore( ) )
164         {
165         	State stateBefore = stateService.findByPrimaryKey( idStateBefore );
166         	sbTitle.append( stateBefore.getName( ) );
167         	sbTitle.append( " " );
168         }
169         sbTitle.append( " -> " );
170         sbTitle.append( action.getStateAfter( ).getName( ) );
171         sbTitle.append( " ) " );
172 
173         return sbTitle.toString( );
174     }
175 }