1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
79
80
81
82
83 public class ActionsBatchTaskComponent extends NoFormTaskComponent
84 {
85
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
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
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
114 IActionService _actionService = SpringContextService.getBean( ActionService.BEAN_SERVICE );
115
116
117 final Map<String, Object> model = new HashMap<>( );
118
119
120 final ActionsBatchTaskConfig config = findTaskConfig( task.getId( ) );
121
122
123 final ReferenceList workflowsRefList = WorkflowService.getInstance( ).getWorkflowsEnabled( user, locale );
124
125
126 workflowsRefList.remove( 0 );
127
128
129 Map<String, Collection<State>> mapStates = new HashMap<>( );
130 Map<String, List<Action>> mapActions = new HashMap<>( );
131
132 for ( ReferenceItem workflowItem : workflowsRefList )
133 {
134
135 Collection<State> workflowStates = WorkflowService.getInstance( ).getAllStateByWorkflow( Integer.valueOf( workflowItem.getCode( ) ), user );
136
137
138 mapStates.put( workflowItem.getCode( ), workflowStates );
139
140
141 for ( State state : workflowStates )
142 {
143
144
145 final ActionFilter filter = new ActionFilter( );
146 filter.setIdStateBefore( state.getId( ) );
147 final List<Action> stateActions = _actionService.getListActionByFilter( filter );
148
149
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
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
200
201
202
203
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
219
220
221
222
223
224
225
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 }