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.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   * JspBean to manage prerequisite of automatic actions
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       * Creates a new prerequisite. If the prerequisite needs to display a configuration form, then the form is returned. Otherwise, redirects the user to the
92       * action management screen.
93       * 
94       * @param request
95       *            The request
96       * @param response
97       *            The response
98       * @return The HTML content to display, or null if the reponse has been redirected
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      * Do create a prerequisite
162      * 
163      * @param request
164      *            The request
165      * @return The next URL to redirect to
166      * @throws AccessDeniedException 
167      */
168     public String doCreatePrerequisite( HttpServletRequest request ) throws AccessDeniedException
169     {
170         // Control the validity of the CSRF Token
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      * Get the page to modify a prerequisite configuration
228      * 
229      * @param request
230      *            the request
231      * @param response
232      *            the response
233      * @return the HTML content to display, or null if the response has been redirected
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      * Do modify a prerequisite configuration
301      * 
302      * @param request
303      *            The request
304      * @return The next URL to redirect to
305      * @throws AccessDeniedException 
306      */
307     public String doModifyPrerequisite( HttpServletRequest request ) throws AccessDeniedException
308     {
309         // Control the validity of the CSRF Token
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      * Get a confirmation message before removing a prerequisite
361      * 
362      * @param request
363      *            The request
364      * @return The next URL to redirect to
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      * Do remove a prerequisite
378      * 
379      * @param request
380      *            the request
381      * @return the next URL to redirect to
382      * @throws AccessDeniedException 
383      */
384     public String doRemovePrerequisite( HttpServletRequest request ) throws AccessDeniedException
385     {
386         // Control the validity of the CSRF Token
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 }