View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de 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.mappings.web;
35  
36  import fr.paris.lutece.plugins.workflow.modules.mappings.business.CodeMappingFactory;
37  import fr.paris.lutece.plugins.workflow.modules.mappings.business.ICodeMapping;
38  import fr.paris.lutece.plugins.workflow.modules.mappings.business.ICodeMappingFactory;
39  import fr.paris.lutece.plugins.workflow.modules.mappings.service.CodeMappingService;
40  import fr.paris.lutece.plugins.workflow.modules.mappings.service.ICodeMappingService;
41  import fr.paris.lutece.plugins.workflow.modules.mappings.web.component.IMappingTypeComponent;
42  import fr.paris.lutece.portal.service.i18n.I18nService;
43  import fr.paris.lutece.portal.service.message.AdminMessage;
44  import fr.paris.lutece.portal.service.message.AdminMessageService;
45  import fr.paris.lutece.portal.service.spring.SpringContextService;
46  import fr.paris.lutece.portal.service.template.AppTemplateService;
47  import fr.paris.lutece.portal.service.util.AppLogService;
48  import fr.paris.lutece.portal.web.admin.PluginAdminPageJspBean;
49  import fr.paris.lutece.portal.web.constants.Messages;
50  import fr.paris.lutece.util.beanvalidation.BeanValidationUtil;
51  import fr.paris.lutece.util.html.HtmlTemplate;
52  import fr.paris.lutece.util.url.UrlItem;
53  
54  import org.apache.commons.lang.StringUtils;
55  
56  import java.util.HashMap;
57  import java.util.Map;
58  import java.util.Set;
59  
60  import javax.servlet.http.HttpServletRequest;
61  
62  import javax.validation.ConstraintViolation;
63  
64  
65  /**
66   *
67   * MappingJspBean
68   *
69   */
70  public class CodeMappingJspBean extends PluginAdminPageJspBean
71  {
72      public static final String RIGHT_MANAGE_MAPPINGS = "WORKFLOW_MAPPINGS_MANAGEMENT";
73  
74      // PROPERTIES
75      private static final String PROPERTY_MANAGE_MAPPINGS_PAGE_TITLE = "module.workflow.mappings.manageMappings.pageTitle";
76      private static final String PROPERTY_CREATE_MAPPING_PAGE_TITLE = "module.workflow.mappings.createMapping.pageTitle";
77      private static final String PROPERTY_ERRROR_PAGE_TITLE = "module.workflow.mappings.error.pageTitle";
78  
79      // MESSAGES
80      private static final String MESSAGE_ERROR_CODE_MAPPING_NOT_FOUND = "module.workflow.mappings.message.error.codeMapping.notFound";
81      private static final String MESSAGE_ERROR_MAPPING_TYPE_COMPONENT_NOT_FOUND = "module.workflow.mappings.message.error.mappingTypeComponent.notFound";
82      private static final String MESSAGE_ERROR_GENERIC_MESSAGE = "module.workflow.mappings.message.error.genericMessage";
83      private static final String MESSAGE_CONFIRM_REMOVE_CODE_MAPPING = "module.workflow.mappings.message.removeCodeMapping";
84      private static final String MESSAGE_INVALID_CODE_MAPPING = "module.workflow.mappings.message.invalidCodeMapping";
85  
86      // MARKS
87      private static final String MARK_LIST_CODE_MAPPINGS = "listCodeMappings";
88      private static final String MARK_ERROR_MESSAGE = "errorMessage";
89      private static final String MARK_REFERENCE_CODE_FORM = "referenceCodeForm";
90      private static final String MARK_LIST_MAPPING_TYPES = "listMappingTypes";
91      private static final String MARK_MAPPING_TYPE_KEY = "mappingTypeKey";
92      private static final String MARK_LOCALE = "locale";
93      private static final String MARK_CODE_MAPPING = "codeMapping";
94      private static final String MARK_CODE = "code";
95      private static final String MARK_LABEL_CODE = "labelCode";
96  
97      // PARAMETERS
98      private static final String PARAMETER_CODE = "code";
99      private static final String PARAMETER_LABEL_CODE = "labelCode";
100     private static final String PARAMETER_MAPPING_TYPE_KEY = "mappingTypeKey";
101     private static final String PARAMETER_ERROR_MESSAGE = "errorMessage";
102     private static final String PARAMETER_CANCEL = "cancel";
103     private static final String PARAMETER_CREATE = "create";
104     private static final String PARAMETER_MODIFY = "modify";
105     private static final String PARAMETER_ID_CODE = "idCode";
106 
107     // TEMPLATES
108     private static final String TEMPLATE_MANAGE_CODE_MAPPINGS = "/admin/plugins/workflow/modules/mappings/manage_code_mappings.html";
109     private static final String TEMPLATE_CREATE_CODE_MAPPING = "/admin/plugins/workflow/modules/mappings/create_code_mapping.html";
110     private static final String TEMPLATE_MODIFY_CODE_MAPPING = "/admin/plugins/workflow/modules/mappings/modify_code_mapping.html";
111     private static final String TEMPLATE_ERROR = "/admin/plugins/workflow/modules/mappings/error.html";
112 
113     // JSP
114     private static final String JSP_CREATE_CODE_MAPPING = "CreateCodeMapping.jsp";
115     private static final String JSP_MODIFY_CODE_MAPPING = "ModifyCodeMapping.jsp";
116     private static final String JSP_MANAGE_CODE_MAPPINGS = "ManageCodeMappings.jsp";
117     private static final String JSP_URL_DO_REMOVE_CODE_MAPPING = "jsp/admin/plugins/workflow/modules/mappings/DoRemoveCodeMapping.jsp";
118 
119     // SERVICES
120     private ICodeMappingService _codeMappingService = SpringContextService.getBean( CodeMappingService.BEAN_SERVICE );
121     private ICodeMappingFactory _codeMappingFactory = SpringContextService.getBean( CodeMappingFactory.BEAN_FACTORY );
122 
123     // GET
124 
125     /**
126      * Get manage code mappings
127      * @param request the HTTP request
128      * @return the HTML code
129      */
130     public String getManageCodeMappings( HttpServletRequest request )
131     {
132         setPageTitleProperty( PROPERTY_MANAGE_MAPPINGS_PAGE_TITLE );
133 
134         Map<String, Object> model = new HashMap<String, Object>(  );
135         model.put( MARK_LIST_CODE_MAPPINGS, _codeMappingService.getListCodeMappings(  ) );
136         model.put( MARK_LOCALE, getLocale(  ) );
137 
138         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_CODE_MAPPINGS, getLocale(  ), model );
139 
140         return getAdminPage( template.getHtml(  ) );
141     }
142 
143     /**
144      * Get create a code mapping
145      * @param request the HTTP request
146      * @return the HTML code
147      */
148     public String getCreateCodeMapping( HttpServletRequest request )
149     {
150         setPageTitleProperty( PROPERTY_CREATE_MAPPING_PAGE_TITLE );
151 
152         Map<String, Object> model = new HashMap<String, Object>(  );
153         model.put( MARK_LIST_MAPPING_TYPES, _codeMappingFactory.getListMappingTypes( getLocale(  ) ) );
154 
155         String strMappingTypeKey = request.getParameter( PARAMETER_MAPPING_TYPE_KEY );
156 
157         if ( StringUtils.isNotBlank( strMappingTypeKey ) )
158         {
159             IMappingTypeComponent mappingTypeComponent = _codeMappingFactory.getMappingTypeComponent( strMappingTypeKey );
160 
161             if ( mappingTypeComponent != null )
162             {
163                 model.put( MARK_MAPPING_TYPE_KEY, strMappingTypeKey );
164                 model.put( MARK_REFERENCE_CODE_FORM,
165                     mappingTypeComponent.getCreateCodeMappingHtml( request, getLocale(  ) ) );
166             }
167         }
168 
169         String strCode = request.getParameter( PARAMETER_CODE );
170 
171         if ( StringUtils.isNotBlank( strCode ) )
172         {
173             model.put( MARK_CODE, strCode );
174         }
175 
176         String strLabelCode = request.getParameter( PARAMETER_LABEL_CODE );
177 
178         if ( StringUtils.isNotBlank( strLabelCode ) )
179         {
180             model.put( MARK_LABEL_CODE, strLabelCode );
181         }
182 
183         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_CODE_MAPPING, getLocale(  ), model );
184 
185         return getAdminPage( template.getHtml(  ) );
186     }
187 
188     /**
189      * Get modify a code mapping
190      * @param request the HTTP request
191      * @return the HTML code
192      */
193     public String getModifyCodeMapping( HttpServletRequest request )
194     {
195         setPageTitleProperty( PROPERTY_CREATE_MAPPING_PAGE_TITLE );
196 
197         String strIdCode = request.getParameter( PARAMETER_ID_CODE );
198 
199         if ( StringUtils.isNotBlank( strIdCode ) && StringUtils.isNumeric( strIdCode ) )
200         {
201             int nIdCode = Integer.parseInt( strIdCode );
202             ICodeMapping codeMapping = _codeMappingService.getCodeMapping( nIdCode );
203 
204             if ( codeMapping != null )
205             {
206                 Map<String, Object> model = new HashMap<String, Object>(  );
207                 model.put( MARK_CODE_MAPPING, codeMapping );
208                 model.put( MARK_LOCALE, getLocale(  ) );
209 
210                 IMappingTypeComponent mappingTypeComponent = _codeMappingFactory.getMappingTypeComponent( codeMapping.getMappingType(  )
211                                                                                                                      .getKey(  ) );
212 
213                 if ( mappingTypeComponent != null )
214                 {
215                     model.put( MARK_REFERENCE_CODE_FORM,
216                         mappingTypeComponent.getModifyCodeMappingHtml( codeMapping, request, getLocale(  ) ) );
217                 }
218                 else
219                 {
220                     AppLogService.error( "CodeMappingJspBean - MappingTypeComponent is null for key '" +
221                         codeMapping.getMappingType(  ).getKey(  ) + "'" );
222 
223                     String strErrorMessage = I18nService.getLocalizedString( MESSAGE_ERROR_MAPPING_TYPE_COMPONENT_NOT_FOUND,
224                             request.getLocale(  ) );
225 
226                     return getError( request, strErrorMessage );
227                 }
228 
229                 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_CODE_MAPPING, getLocale(  ),
230                         model );
231 
232                 return getAdminPage( template.getHtml(  ) );
233             }
234 
235             AppLogService.debug( "CodeMappingJspBean - CodeMapping is null for id code '" + strIdCode + "'" );
236 
237             String strErrorMessage = I18nService.getLocalizedString( MESSAGE_ERROR_CODE_MAPPING_NOT_FOUND,
238                     request.getLocale(  ) );
239 
240             return getError( request, strErrorMessage );
241         }
242 
243         String strErrorMessage = I18nService.getLocalizedString( Messages.MANDATORY_FIELDS, request.getLocale(  ) );
244 
245         return getError( request, strErrorMessage );
246     }
247 
248     /**
249      * Get confirm remove code mapping
250      * @param request the HTTP request
251      * @return the HTML code
252      */
253     public String getConfirmRemoveCodeMapping( HttpServletRequest request )
254     {
255         String strIdCode = request.getParameter( PARAMETER_ID_CODE );
256 
257         if ( StringUtils.isBlank( strIdCode ) || !StringUtils.isNumeric( strIdCode ) )
258         {
259             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
260         }
261 
262         UrlItem url = new UrlItem( JSP_URL_DO_REMOVE_CODE_MAPPING );
263         url.addParameter( PARAMETER_ID_CODE, strIdCode );
264 
265         return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_CODE_MAPPING, url.getUrl(  ),
266             AdminMessage.TYPE_CONFIRMATION );
267     }
268 
269     /**
270      * Get error
271      * @param request the HTTP request
272      * @return the HTML code
273      */
274     public String getError( HttpServletRequest request )
275     {
276         String strErrorMessage = request.getParameter( PARAMETER_ERROR_MESSAGE );
277 
278         if ( StringUtils.isBlank( strErrorMessage ) )
279         {
280             strErrorMessage = I18nService.getLocalizedString( MESSAGE_ERROR_GENERIC_MESSAGE, request.getLocale(  ) );
281         }
282 
283         return getError( request, strErrorMessage );
284     }
285 
286     /**
287      * Get error
288      * @param request the HTTP request
289      * @param strErrorMessage the error message
290      * @return the HTML code
291      */
292     public String getError( HttpServletRequest request, String strErrorMessage )
293     {
294         setPageTitleProperty( PROPERTY_ERRROR_PAGE_TITLE );
295 
296         Map<String, Object> model = new HashMap<String, Object>(  );
297         model.put( MARK_ERROR_MESSAGE, strErrorMessage );
298 
299         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_ERROR, getLocale(  ), model );
300 
301         return getAdminPage( template.getHtml(  ) );
302     }
303 
304     // DO
305 
306     /**
307      * Do create the code mapping
308      * @param request the HTTP request
309      * @return the JSP return
310      */
311     public String doCreateCodeMapping( HttpServletRequest request )
312     {
313         String strCancel = request.getParameter( PARAMETER_CANCEL );
314 
315         if ( StringUtils.isNotBlank( strCancel ) )
316         {
317             return JSP_MANAGE_CODE_MAPPINGS;
318         }
319 
320         String strMappingTypeKey = request.getParameter( PARAMETER_MAPPING_TYPE_KEY );
321 
322         if ( StringUtils.isBlank( strMappingTypeKey ) )
323         {
324             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
325         }
326 
327         String strCreate = request.getParameter( PARAMETER_CREATE );
328 
329         // Only modify the code mapping if the user has clicked on the submit button "create"
330         if ( StringUtils.isBlank( strCreate ) )
331         {
332             IMappingTypeComponent mappingTypeComponent = _codeMappingFactory.getMappingTypeComponent( strMappingTypeKey );
333 
334             if ( mappingTypeComponent != null )
335             {
336                 UrlItem url = new UrlItem( JSP_CREATE_CODE_MAPPING );
337                 url.addParameter( PARAMETER_MAPPING_TYPE_KEY, strMappingTypeKey );
338                 mappingTypeComponent.addParameter( request, url );
339 
340                 return url.getUrl(  );
341             }
342             else
343             {
344                 return AdminMessageService.getMessageUrl( request, MESSAGE_ERROR_MAPPING_TYPE_COMPONENT_NOT_FOUND,
345                     AdminMessage.TYPE_STOP );
346             }
347         }
348 
349         ICodeMapping codeMapping = _codeMappingFactory.newCodeMapping( strMappingTypeKey );
350         // Populate the bean
351         populate( codeMapping, request );
352 
353         // Check mandatory fields
354         Set<ConstraintViolation<ICodeMapping>> constraintViolations = BeanValidationUtil.validate( codeMapping );
355 
356         if ( constraintViolations.size(  ) > 0 )
357         {
358             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
359         }
360 
361         if ( !_codeMappingService.createCodeMapping( codeMapping ) )
362         {
363             return AdminMessageService.getMessageUrl( request, MESSAGE_INVALID_CODE_MAPPING, AdminMessage.TYPE_STOP );
364         }
365 
366         return JSP_MANAGE_CODE_MAPPINGS;
367     }
368 
369     /**
370      * Do modify the code mapping
371      * @param request the HTTP request
372      * @return the JSP return
373      */
374     public String doModifyCodeMapping( HttpServletRequest request )
375     {
376         String strCancel = request.getParameter( PARAMETER_CANCEL );
377 
378         if ( StringUtils.isNotBlank( strCancel ) )
379         {
380             return JSP_MANAGE_CODE_MAPPINGS;
381         }
382 
383         String strMappingTypeKey = request.getParameter( PARAMETER_MAPPING_TYPE_KEY );
384         String strIdCode = request.getParameter( PARAMETER_ID_CODE );
385 
386         if ( StringUtils.isBlank( strMappingTypeKey ) || StringUtils.isBlank( strIdCode ) ||
387                 !StringUtils.isNumeric( strIdCode ) )
388         {
389             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
390         }
391 
392         String strModify = request.getParameter( PARAMETER_MODIFY );
393 
394         // Only modify the code mapping if the user has clicked on the submit button "modify"
395         if ( StringUtils.isBlank( strModify ) )
396         {
397             IMappingTypeComponent mappingTypeComponent = _codeMappingFactory.getMappingTypeComponent( strMappingTypeKey );
398 
399             if ( mappingTypeComponent != null )
400             {
401                 UrlItem url = new UrlItem( JSP_MODIFY_CODE_MAPPING );
402                 url.addParameter( PARAMETER_MAPPING_TYPE_KEY, strMappingTypeKey );
403                 mappingTypeComponent.addParameter( request, url );
404 
405                 return url.getUrl(  );
406             }
407             else
408             {
409                 return AdminMessageService.getMessageUrl( request, MESSAGE_ERROR_MAPPING_TYPE_COMPONENT_NOT_FOUND,
410                     AdminMessage.TYPE_STOP );
411             }
412         }
413 
414         int nIdCode = Integer.parseInt( strIdCode );
415         ICodeMapping codeMapping = _codeMappingService.getCodeMapping( nIdCode );
416 
417         if ( codeMapping == null )
418         {
419             return AdminMessageService.getMessageUrl( request, MESSAGE_ERROR_CODE_MAPPING_NOT_FOUND,
420                 AdminMessage.TYPE_STOP );
421         }
422 
423         // Populate the bean
424         populate( codeMapping, request );
425 
426         // Check mandatory fields
427         Set<ConstraintViolation<ICodeMapping>> constraintViolations = BeanValidationUtil.validate( codeMapping );
428 
429         if ( constraintViolations.size(  ) > 0 )
430         {
431             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
432         }
433 
434         _codeMappingService.updateCodeMapping( codeMapping );
435 
436         return JSP_MANAGE_CODE_MAPPINGS;
437     }
438 
439     /**
440      * Do remove the code mapping
441      * @param request the HTTP request
442      * @return the JSP return
443      */
444     public String doRemoveCodeMapping( HttpServletRequest request )
445     {
446         String strIdCode = request.getParameter( PARAMETER_ID_CODE );
447 
448         if ( StringUtils.isBlank( strIdCode ) || !StringUtils.isNumeric( strIdCode ) )
449         {
450             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
451         }
452 
453         int nIdCode = Integer.parseInt( strIdCode );
454 
455         _codeMappingService.removeCodeMapping( nIdCode );
456 
457         return JSP_MANAGE_CODE_MAPPINGS;
458     }
459 }