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.modules.actionsbatch.web;
35  
36  import java.util.ArrayList;
37  import java.util.Collection;
38  import java.util.HashMap;
39  import java.util.List;
40  import java.util.Locale;
41  import java.util.Map;
42  import java.util.Set;
43  
44  import javax.servlet.http.HttpServletRequest;
45  import javax.validation.ConstraintViolation;
46  
47  import org.apache.commons.collections.CollectionUtils;
48  import org.apache.commons.lang3.StringUtils;
49  
50  import com.fasterxml.jackson.databind.ObjectMapper;
51  import com.fasterxml.jackson.databind.node.ArrayNode;
52  import com.fasterxml.jackson.databind.node.ObjectNode;
53  
54  import fr.paris.lutece.api.user.User;
55  import fr.paris.lutece.plugins.workflow.modules.actionsbatch.task.ActionsBatchTaskConfig;
56  import fr.paris.lutece.plugins.workflow.web.task.NoFormTaskComponent;
57  import fr.paris.lutece.plugins.workflowcore.business.action.Action;
58  import fr.paris.lutece.plugins.workflowcore.business.action.ActionFilter;
59  import fr.paris.lutece.plugins.workflowcore.business.config.ITaskConfig;
60  import fr.paris.lutece.plugins.workflowcore.business.state.State;
61  import fr.paris.lutece.plugins.workflowcore.service.action.ActionService;
62  import fr.paris.lutece.plugins.workflowcore.service.action.IActionService;
63  import fr.paris.lutece.plugins.workflowcore.service.task.ITask;
64  import fr.paris.lutece.portal.service.admin.AdminUserService;
65  import fr.paris.lutece.portal.service.message.AdminMessage;
66  import fr.paris.lutece.portal.service.message.AdminMessageService;
67  import fr.paris.lutece.portal.service.spring.SpringContextService;
68  import fr.paris.lutece.portal.service.template.AppTemplateService;
69  import fr.paris.lutece.portal.service.util.AppException;
70  import fr.paris.lutece.portal.service.workflow.WorkflowService;
71  import fr.paris.lutece.portal.web.constants.Messages;
72  import fr.paris.lutece.util.ReferenceItem;
73  import fr.paris.lutece.util.ReferenceList;
74  import fr.paris.lutece.util.beanvalidation.BeanValidationUtil;
75  import fr.paris.lutece.util.html.HtmlTemplate;
76  
77  /**
78   * ActionsBatchTaskComponent Class
79   * 
80   * @author MDP,ACN
81   *
82   */
83  public class ActionsBatchTaskComponent extends NoFormTaskComponent
84  {
85      // MARKERS
86      private static final String MARK_WORKFLOWS = "workflow_list";
87      private static final String MARK_STATUS = "status_list";
88      private static final String MARK_ACTION = "action_list";
89      private static final String MARK_RESOURCE_TYPE = "resource_type";
90  
91      // PARAMETERS
92      private static final String PARAM_STATUS = "state";
93      private static final String PARAM_ACTION = "action";
94      private static final String PARAM_WORKFLOW = "workflow";
95      private static final String PARAM_RESOURCE_TYPE = "resource_type";
96  
97      // TEMPLATES
98      private static final String TEMPLATE_TASK_DEMANDE_MASS_ACTION_CONFIG = "admin/plugins/workflow/modules/actionsbatch/actionsbatch_task_config.html";
99      private static final String MARK_WORKFLOW_ACTIONS = "json_workflow_actions";
100     private static final String MARK_WORKFLOW_ID = "workflow_id";
101     private static final String MARK_STATE_ID = "state_id";
102     private static final String MARK_ACTION_ID = "action_id";
103 
104     @Override
105     public String getDisplayConfigForm( HttpServletRequest request, Locale locale, ITask task )
106     {
107         User user = AdminUserService.getAdminUser( request );
108         if ( user == null )
109         {
110             throw new AppException( "Access Denied" );
111         }
112 
113         // service
114         IActionService _actionService = SpringContextService.getBean( ActionService.BEAN_SERVICE );
115 
116         // model
117         final Map<String, Object> model = new HashMap<>( );
118 
119         // get config
120         final ActionsBatchTaskConfig config = findTaskConfig( task.getId( ) );
121 
122         // get enabled workflow list
123         final ReferenceList workflowsRefList = WorkflowService.getInstance( ).getWorkflowsEnabled( user, locale );
124 
125         // remove first blank item
126         workflowsRefList.remove( 0 );
127 
128         // get all available states and actions
129         Map<String, Collection<State>> mapStates = new HashMap<>( );
130         Map<String, List<Action>> mapActions = new HashMap<>( );
131 
132         for ( ReferenceItem workflowItem : workflowsRefList )
133         {
134             // STATES
135             Collection<State> workflowStates = WorkflowService.getInstance( ).getAllStateByWorkflow( Integer.valueOf( workflowItem.getCode( ) ), user );
136 
137             // put in global map
138             mapStates.put( workflowItem.getCode( ), workflowStates );
139 
140             // ACTIONS
141             for ( State state : workflowStates )
142             {
143 
144                 // get actions
145                 final ActionFilter filter = new ActionFilter( );
146                 filter.setIdStateBefore( state.getId( ) );
147                 final List<Action> stateActions = _actionService.getListActionByFilter( filter );
148 
149                 // put in map
150                 mapActions.put( String.valueOf( state.getId( ) ), stateActions );
151             }
152         }
153 
154         model.put( MARK_WORKFLOW_ACTIONS, getJsonActions( workflowsRefList, mapStates, mapActions ) );
155 
156         model.put( MARK_WORKFLOW_ID, config.getIdWorkflow( ) );
157         model.put( MARK_STATE_ID, config.getIdState( ) );
158         model.put( MARK_ACTION_ID, config.getIdAction( ) );
159         model.put( MARK_RESOURCE_TYPE, config.getResourceType( ) );
160 
161         final HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_TASK_DEMANDE_MASS_ACTION_CONFIG, locale, model );
162         return template.getHtml( );
163     }
164 
165     @Override
166     public String validateConfig( ITaskConfig config, HttpServletRequest request )
167     {
168         String workflow = request.getParameter( PARAM_WORKFLOW );
169         String status = request.getParameter( PARAM_STATUS );
170         String action = request.getParameter( PARAM_ACTION );
171         String resourceType = request.getParameter( PARAM_RESOURCE_TYPE );
172 
173         if ( StringUtils.isBlank( workflow ) || StringUtils.isBlank( status ) || StringUtils.isBlank( action ) || StringUtils.isBlank( resourceType ) )
174         {
175             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
176         }
177         else
178             if ( config instanceof ActionsBatchTaskConfig )
179             {
180                 final ActionsBatchTaskConfig/../fr/paris/lutece/plugins/workflow/modules/actionsbatch/task/ActionsBatchTaskConfig.html#ActionsBatchTaskConfig">ActionsBatchTaskConfig taskConfig = (ActionsBatchTaskConfig) config;
181                 taskConfig.setIdWorkflow( Integer.parseInt( workflow ) );
182                 taskConfig.setIdState( Integer.parseInt( status ) );
183                 taskConfig.setIdAction( Integer.parseInt( action ) );
184                 taskConfig.setResourceType( resourceType );
185 
186                 // Check mandatory fields
187                 Set<ConstraintViolation<ITaskConfig>> constraintViolations = BeanValidationUtil.validate( taskConfig );
188 
189                 if ( CollectionUtils.isNotEmpty( constraintViolations ) )
190                 {
191                     return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
192                 }
193             }
194 
195         return StringUtils.EMPTY;
196     }
197 
198     /**
199      * get config
200      * 
201      * @param id
202      *            id Config
203      * @return ActionsBatchTaskConfig
204      */
205     private ActionsBatchTaskConfig findTaskConfig( int id )
206     {
207         final ActionsBatchTaskConfig config = this.getTaskConfigService( ).findByPrimaryKey( id );
208         return config == null ? new ActionsBatchTaskConfig( ) : config;
209     }
210 
211     @Override
212     public String getDisplayTaskInformation( int pNIdHistory, HttpServletRequest pRequest, Locale pLocale, ITask pTask )
213     {
214         return null;
215     }
216 
217     /**
218      * generate Json for cascade selects in template
219      * 
220      * example : {"workflows":[{"id":1, "name":"dotation", "states":[{"id":12, "name":"statut11", "actions":[{"id":111, "name": "action111"}, ...
221      * 
222      * @param workflowsRefList
223      * @param mapStates
224      * @param mapActions
225      * @return json
226      */
227     private String getJsonActions( ReferenceList workflowsRefList, Map<String, Collection<State>> mapStates, Map<String, List<Action>> mapActions )
228     {
229         ObjectMapper mapper = new ObjectMapper( );
230         ObjectNode root = mapper.createObjectNode( );
231 
232         ArrayNode jsonWfList = mapper.createArrayNode( );
233         for ( ReferenceItem workflowItem : workflowsRefList )
234         {
235             ObjectNode jsonWf = mapper.createObjectNode( );
236             jsonWf.put( "id", workflowItem.getCode( ) );
237             jsonWf.put( "name", workflowItem.getName( ) );
238 
239             ArrayNode jsonStatesList = mapper.createArrayNode( );
240             for ( State state : mapStates.get( workflowItem.getCode( ) ) )
241             {
242                 ObjectNode jsonState = mapper.createObjectNode( );
243                 jsonState.put( "id", state.getId( ) );
244                 jsonState.put( "name", state.getName( ) );
245 
246                 ArrayNode jsonActionsList = mapper.createArrayNode( );
247                 for ( Action action : mapActions.get( String.valueOf( state.getId( ) ) ) )
248                 {
249                     ObjectNode jsonAction = mapper.createObjectNode( );
250                     jsonAction.put( "id", action.getId( ) );
251                     jsonAction.put( "name", action.getName( ) );
252 
253                     jsonActionsList.add( jsonAction );
254                 }
255 
256                 jsonState.set( "actions", jsonActionsList );
257                 jsonStatesList.add( jsonState );
258             }
259 
260             jsonWf.set( "states", jsonStatesList );
261             jsonWfList.add( jsonWf );
262         }
263 
264         root.set( "workflows", jsonWfList );
265 
266         return root.toPrettyString( );
267     }
268 }