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.web.prerequisite;
35
36 import java.io.IOException;
37 import java.util.HashMap;
38 import java.util.Map;
39 import java.util.Set;
40
41 import javax.servlet.http.HttpServletRequest;
42 import javax.servlet.http.HttpServletResponse;
43 import javax.validation.ConstraintViolation;
44
45 import org.apache.commons.collections.CollectionUtils;
46 import org.apache.commons.lang3.StringUtils;
47
48 import fr.paris.lutece.plugins.workflow.service.prerequisite.PrerequisiteManagementService;
49 import fr.paris.lutece.plugins.workflow.utils.WorkflowUtils;
50 import fr.paris.lutece.plugins.workflowcore.business.prerequisite.IPrerequisiteConfig;
51 import fr.paris.lutece.plugins.workflowcore.business.prerequisite.Prerequisite;
52 import fr.paris.lutece.plugins.workflowcore.service.prerequisite.IAutomaticActionPrerequisiteService;
53 import fr.paris.lutece.plugins.workflowcore.service.prerequisite.IPrerequisiteManagementService;
54 import fr.paris.lutece.portal.service.admin.AccessDeniedException;
55 import fr.paris.lutece.portal.service.i18n.I18nService;
56 import fr.paris.lutece.portal.service.message.AdminMessage;
57 import fr.paris.lutece.portal.service.message.AdminMessageService;
58 import fr.paris.lutece.portal.service.security.SecurityTokenService;
59 import fr.paris.lutece.portal.service.spring.SpringContextService;
60 import fr.paris.lutece.portal.service.template.AppTemplateService;
61 import fr.paris.lutece.portal.service.util.AppLogService;
62 import fr.paris.lutece.portal.service.util.AppPathService;
63 import fr.paris.lutece.portal.web.admin.PluginAdminPageJspBean;
64 import fr.paris.lutece.util.bean.BeanUtil;
65 import fr.paris.lutece.util.html.HtmlTemplate;
66 import fr.paris.lutece.util.url.UrlItem;
67
68
69
70
71 public class PrerequisiteJspBean extends PluginAdminPageJspBean
72 {
73 private static final long serialVersionUID = 2375784363589766595L;
74 private static final String MESSAGE_CONFIRM_REMOVE_PREREQUISITE = "workflow.modify_action.manage_prerequisite.confirmRemovePrerequisite";
75 private static final String MARK_PREREQUISITE_TYPE = "prerequisite_type";
76 private static final String MARK_ID_ACTION = "id_action";
77 private static final String MARK_CONTENT = "content";
78 private static final String MARK_ERRORS = "errors";
79 private static final String MARK_ID_PREREQUISITE = "id_prerequisite";
80 private static final String MARK_CANCEL = "cancel";
81 private static final String TEMPLATE_CREATE_MODIFY_PREREQUISITE = "admin/plugins/workflow/create_modify_prerequisite.html";
82 private static final String JSP_URL_MODIFY_ACTION = "jsp/admin/plugins/workflow/ModifyAction.jsp";
83 private static final String JSP_URL_CREATE_PREREQUISITE = "jsp/admin/plugins/workflow/prerequisite/GetCreatePrerequisite.jsp";
84 private static final String JSP_URL_MODIFY_PREREQUISITE = "jsp/admin/plugins/workflow/prerequisite/GetModifyPrerequisite.jsp";
85 private static final String JSP_URL_DO_REMOVE_PREREQUISITE = "jsp/admin/plugins/workflow/prerequisite/DoRemovePrerequisite.jsp";
86 private static final String SESSION_ERRORS = "workflow.prerequisite.session.errors";
87 private static final String SESSION_CONFIG = "workflow.prerequisite.session.config";
88 private IPrerequisiteManagementService _prerequisiteManagementService = SpringContextService.getBean( PrerequisiteManagementService.BEAN_NAME );
89
90
91
92
93
94
95
96
97
98
99
100 public String getCreatePrerequisite( HttpServletRequest request, HttpServletResponse response )
101 {
102 String strPrerequisiteType = request.getParameter( MARK_PREREQUISITE_TYPE );
103 int nIdAction = Integer.parseInt( request.getParameter( MARK_ID_ACTION ) );
104
105 IAutomaticActionPrerequisiteService service = _prerequisiteManagementService.getPrerequisiteService( strPrerequisiteType );
106
107 if ( !service.hasConfiguration( ) )
108 {
109 Prerequisite prerequisite = new Prerequisite( );
110 prerequisite.setIdAction( nIdAction );
111 prerequisite.setPrerequisiteType( strPrerequisiteType );
112 _prerequisiteManagementService.createPrerequisite( prerequisite );
113
114 UrlItem url = new UrlItem( AppPathService.getBaseUrl( request ) + JSP_URL_MODIFY_ACTION );
115 url.addParameter( MARK_ID_ACTION, nIdAction );
116
117 try
118 {
119 response.sendRedirect( url.getUrl( ) );
120 }
121 catch( IOException e )
122 {
123 AppLogService.error( e.getMessage( ), e );
124 }
125
126 return null;
127 }
128
129 Map<String, Object> model = new HashMap<>( );
130 model.put( MARK_PREREQUISITE_TYPE, strPrerequisiteType );
131 model.put( MARK_ID_ACTION, nIdAction );
132
133 Set<ConstraintViolation<IPrerequisiteConfig>> listErrors = (Set<ConstraintViolation<IPrerequisiteConfig>>) request.getSession( )
134 .getAttribute( SESSION_ERRORS );
135 IPrerequisiteConfig config;
136
137 if ( listErrors != null )
138 {
139 model.put( MARK_ERRORS, listErrors );
140 request.getSession( ).removeAttribute( SESSION_ERRORS );
141 config = (IPrerequisiteConfig) request.getSession( ).getAttribute( SESSION_CONFIG );
142 request.getSession( ).removeAttribute( SESSION_CONFIG );
143 }
144 else
145 {
146 config = service.getEmptyConfiguration( );
147 }
148
149 config.setPrerequisiteType( strPrerequisiteType );
150
151 String strContent = service.getConfigHtml( config, request, getLocale( ) );
152 model.put( MARK_CONTENT, strContent );
153 model.put( SecurityTokenService.MARK_TOKEN , SecurityTokenService.getInstance( ).getToken( request, TEMPLATE_CREATE_MODIFY_PREREQUISITE ) );
154
155 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_MODIFY_PREREQUISITE, getLocale( ), model );
156
157 return getAdminPage( template.getHtml( ) );
158 }
159
160
161
162
163
164
165
166
167
168 public String doCreatePrerequisite( HttpServletRequest request ) throws AccessDeniedException
169 {
170
171 if ( !SecurityTokenService.getInstance( ).validate( request, TEMPLATE_CREATE_MODIFY_PREREQUISITE ) )
172 {
173 throw new AccessDeniedException( I18nService.getLocalizedString( WorkflowUtils.MESSAGE_ERROR_INVALID_SECURITY_TOKEN, getLocale( ) ) );
174 }
175
176 String strPrerequisiteType = request.getParameter( MARK_PREREQUISITE_TYPE );
177 int nIdAction = Integer.parseInt( request.getParameter( MARK_ID_ACTION ) );
178
179 if ( StringUtils.isEmpty( request.getParameter( MARK_CANCEL ) ) )
180 {
181 IAutomaticActionPrerequisiteService service = _prerequisiteManagementService.getPrerequisiteService( strPrerequisiteType );
182
183 if ( service != null )
184 {
185 IPrerequisiteConfig config = service.getEmptyConfiguration( );
186
187 if ( config != null )
188 {
189 BeanUtil.populate( config, request );
190
191 Set<ConstraintViolation<IPrerequisiteConfig>> listErrors = validate( config );
192
193 if ( CollectionUtils.isNotEmpty( listErrors ) )
194 {
195 request.getSession( ).setAttribute( SESSION_ERRORS, listErrors );
196 request.getSession( ).setAttribute( SESSION_CONFIG, config );
197
198 UrlItem url = new UrlItem( AppPathService.getBaseUrl( request ) + JSP_URL_CREATE_PREREQUISITE );
199 url.addParameter( MARK_ID_ACTION, nIdAction );
200 url.addParameter( MARK_PREREQUISITE_TYPE, strPrerequisiteType );
201
202 return url.getUrl( );
203 }
204 }
205
206 Prerequisite prerequisite = new Prerequisite( );
207 prerequisite.setIdAction( nIdAction );
208 prerequisite.setPrerequisiteType( strPrerequisiteType );
209
210 _prerequisiteManagementService.createPrerequisite( prerequisite );
211
212 if ( config != null )
213 {
214 config.setIdPrerequisite( prerequisite.getIdPrerequisite( ) );
215 _prerequisiteManagementService.createPrerequisiteConfiguration( config, service );
216 }
217 }
218 }
219
220 UrlItem url = new UrlItem( AppPathService.getBaseUrl( request ) + JSP_URL_MODIFY_ACTION );
221 url.addParameter( MARK_ID_ACTION, nIdAction );
222
223 return url.getUrl( );
224 }
225
226
227
228
229
230
231
232
233
234
235 public String getModifyPrerequisite( HttpServletRequest request, HttpServletResponse response )
236 {
237 int nIdAction = Integer.parseInt( request.getParameter( MARK_ID_ACTION ) );
238 int nIdPrerequisite = Integer.parseInt( request.getParameter( MARK_ID_PREREQUISITE ) );
239 Prerequisite prerequisite = _prerequisiteManagementService.findPrerequisite( nIdPrerequisite );
240
241 IAutomaticActionPrerequisiteService service = _prerequisiteManagementService.getPrerequisiteService( prerequisite.getPrerequisiteType( ) );
242
243 if ( !service.hasConfiguration( ) )
244 {
245 UrlItem url = new UrlItem( AppPathService.getBaseUrl( request ) + JSP_URL_MODIFY_ACTION );
246 url.addParameter( MARK_ID_ACTION, nIdAction );
247
248 try
249 {
250 response.sendRedirect( url.getUrl( ) );
251 }
252 catch( IOException e )
253 {
254 AppLogService.error( e.getMessage( ), e );
255 }
256
257 return null;
258 }
259
260 IPrerequisiteConfig config;
261
262 Map<String, Object> model = new HashMap<>( );
263 model.put( MARK_PREREQUISITE_TYPE, prerequisite.getPrerequisiteType( ) );
264 model.put( MARK_ID_ACTION, nIdAction );
265 model.put( MARK_ID_PREREQUISITE, nIdPrerequisite );
266
267 Set<ConstraintViolation<IPrerequisiteConfig>> listErrors = (Set<ConstraintViolation<IPrerequisiteConfig>>) request.getSession( )
268 .getAttribute( SESSION_ERRORS );
269
270 if ( listErrors != null )
271 {
272 model.put( MARK_ERRORS, listErrors );
273 request.getSession( ).removeAttribute( SESSION_ERRORS );
274 config = (IPrerequisiteConfig) request.getSession( ).getAttribute( SESSION_CONFIG );
275 request.getSession( ).removeAttribute( SESSION_CONFIG );
276 }
277 else
278 {
279 config = _prerequisiteManagementService.getPrerequisiteConfiguration( nIdPrerequisite, service );
280 }
281
282 if ( config == null )
283 {
284 config = service.getEmptyConfiguration( );
285 }
286
287 config.setIdPrerequisite( nIdPrerequisite );
288 config.setPrerequisiteType( prerequisite.getPrerequisiteType( ) );
289
290 String strContent = service.getConfigHtml( config, request, getLocale( ) );
291 model.put( MARK_CONTENT, strContent );
292 model.put( SecurityTokenService.MARK_TOKEN , SecurityTokenService.getInstance( ).getToken( request, TEMPLATE_CREATE_MODIFY_PREREQUISITE ) );
293
294 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_MODIFY_PREREQUISITE, getLocale( ), model );
295
296 return getAdminPage( template.getHtml( ) );
297 }
298
299
300
301
302
303
304
305
306
307 public String doModifyPrerequisite( HttpServletRequest request ) throws AccessDeniedException
308 {
309
310 if ( !SecurityTokenService.getInstance( ).validate( request, TEMPLATE_CREATE_MODIFY_PREREQUISITE ) )
311 {
312 throw new AccessDeniedException( I18nService.getLocalizedString( WorkflowUtils.MESSAGE_ERROR_INVALID_SECURITY_TOKEN, getLocale( ) ) );
313 }
314
315 int nIdAction = Integer.parseInt( request.getParameter( MARK_ID_ACTION ) );
316
317 if ( StringUtils.isEmpty( request.getParameter( MARK_CANCEL ) ) )
318 {
319 int nIdPrerequisite = Integer.parseInt( request.getParameter( MARK_ID_PREREQUISITE ) );
320 Prerequisite prerequisite = _prerequisiteManagementService.findPrerequisite( nIdPrerequisite );
321 IAutomaticActionPrerequisiteService service = _prerequisiteManagementService.getPrerequisiteService( prerequisite.getPrerequisiteType( ) );
322
323 IPrerequisiteConfig config = _prerequisiteManagementService.getPrerequisiteConfiguration( nIdPrerequisite, service );
324
325 if ( config == null )
326 {
327 config = service.getEmptyConfiguration( );
328 config.setIdPrerequisite( nIdPrerequisite );
329 config.setPrerequisiteType( prerequisite.getPrerequisiteType( ) );
330 _prerequisiteManagementService.createPrerequisiteConfiguration( config, service );
331 }
332
333 BeanUtil.populate( config, request );
334
335 Set<ConstraintViolation<IPrerequisiteConfig>> listErrors = validate( config );
336
337 if ( CollectionUtils.isNotEmpty( listErrors ) )
338 {
339 request.getSession( ).setAttribute( SESSION_ERRORS, listErrors );
340 request.getSession( ).setAttribute( SESSION_CONFIG, config );
341
342 UrlItem url = new UrlItem( AppPathService.getBaseUrl( request ) + JSP_URL_MODIFY_PREREQUISITE );
343 url.addParameter( MARK_ID_ACTION, nIdAction );
344 url.addParameter( MARK_PREREQUISITE_TYPE, prerequisite.getPrerequisiteType( ) );
345 url.addParameter( MARK_ID_PREREQUISITE, nIdPrerequisite );
346
347 return url.getUrl( );
348 }
349
350 _prerequisiteManagementService.updatePrerequisiteConfiguration( config, service );
351 }
352
353 UrlItem url = new UrlItem( AppPathService.getBaseUrl( request ) + JSP_URL_MODIFY_ACTION );
354 url.addParameter( MARK_ID_ACTION, nIdAction );
355
356 return url.getUrl( );
357 }
358
359
360
361
362
363
364
365
366 public String getConfirmRemovePrerequisite( HttpServletRequest request )
367 {
368 UrlItem urlItem = new UrlItem( AppPathService.getBaseUrl( request ) + JSP_URL_DO_REMOVE_PREREQUISITE );
369 urlItem.addParameter( MARK_ID_ACTION, request.getParameter( MARK_ID_ACTION ) );
370 urlItem.addParameter( MARK_ID_PREREQUISITE, request.getParameter( MARK_ID_PREREQUISITE ) );
371 urlItem.addParameter( SecurityTokenService.PARAMETER_TOKEN, SecurityTokenService.getInstance( ).getToken( request, JSP_URL_DO_REMOVE_PREREQUISITE ) );
372
373 return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_PREREQUISITE, urlItem.getUrl( ), AdminMessage.TYPE_CONFIRMATION );
374 }
375
376
377
378
379
380
381
382
383
384 public String doRemovePrerequisite( HttpServletRequest request ) throws AccessDeniedException
385 {
386
387 if ( !SecurityTokenService.getInstance( ).validate( request, JSP_URL_DO_REMOVE_PREREQUISITE ) )
388 {
389 throw new AccessDeniedException( I18nService.getLocalizedString( WorkflowUtils.MESSAGE_ERROR_INVALID_SECURITY_TOKEN, getLocale( ) ) );
390 }
391
392 int nIdAction = Integer.parseInt( request.getParameter( MARK_ID_ACTION ) );
393 int nIdPrerequisite = Integer.parseInt( request.getParameter( MARK_ID_PREREQUISITE ) );
394
395 _prerequisiteManagementService.deletePrerequisite( nIdPrerequisite );
396
397 UrlItem url = new UrlItem( AppPathService.getBaseUrl( request ) + JSP_URL_MODIFY_ACTION );
398 url.addParameter( MARK_ID_ACTION, nIdAction );
399
400 return url.getUrl( );
401 }
402 }