View Javadoc
1   /*
2    * Copyright (c) 2002-2021, 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.regularexpression.web;
35  
36  import fr.paris.lutece.plugins.regularexpression.business.RegularExpressionHome;
37  import fr.paris.lutece.portal.business.regularexpression.RegularExpression;
38  import fr.paris.lutece.portal.service.admin.AdminUserService;
39  import fr.paris.lutece.portal.service.i18n.I18nService;
40  import fr.paris.lutece.portal.service.message.AdminMessage;
41  import fr.paris.lutece.portal.service.message.AdminMessageService;
42  import fr.paris.lutece.portal.service.plugin.Plugin;
43  import fr.paris.lutece.portal.service.regularexpression.IRegularExpressionService;
44  import fr.paris.lutece.portal.service.regularexpression.RegularExpressionRemovalListenerService;
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.service.util.AppPathService;
49  import fr.paris.lutece.portal.service.util.AppPropertiesService;
50  import fr.paris.lutece.portal.web.admin.PluginAdminPageJspBean;
51  import fr.paris.lutece.util.html.AbstractPaginator;
52  import fr.paris.lutece.util.html.HtmlTemplate;
53  import fr.paris.lutece.util.html.Paginator;
54  import fr.paris.lutece.util.url.UrlItem;
55  
56  import java.util.ArrayList;
57  import java.util.HashMap;
58  import java.util.List;
59  import java.util.Locale;
60  import java.util.Map;
61  
62  import javax.servlet.http.HttpServletRequest;
63  
64  import org.apache.commons.lang3.StringUtils;
65  
66  /**
67   * This class provides the user interface to manage regular expression( manage, create, modify, remove)
68   */
69  public class RegularExpressionJspBean extends PluginAdminPageJspBean
70  {
71      private static final long serialVersionUID = 4005299802051287019L;
72  
73      public static final String RIGHT_REGULAR_EXPRESSION_MANAGEMENT = "REGULAR_EXPRESSION_MANAGEMENT";
74  
75      // templates
76      private static final String TEMPLATE_MANAGE_REGULAR_EXPRESSION = "admin/plugins/regularexpression/manage_regular_expression.html";
77      private static final String TEMPLATE_CREATE_REGULAR_EXPRESSION = "admin/plugins/regularexpression/create_regular_expression.html";
78      private static final String TEMPLATE_MODIFY_REGULAR_EXPRESSION = "admin/plugins/regularexpression/modify_regular_expression.html";
79  
80      // Markers
81      private static final String MARK_EXPRESSION_LIST = "expression_list";
82      private static final String MARK_EXPPRESSION = "expression";
83      private static final String MARK_PAGINATOR = "paginator";
84      private static final String MARK_NB_ITEMS_PER_PAGE = "nb_items_per_page";
85      private static final String MARK_WEBAPP_URL = "webapp_url";
86      private static final String MARK_LOCALE = "locale";
87  
88      // parameters form
89      private static final String PARAMETER_ID_EXPRESSION = "id_expression";
90      private static final String PARAMETER_TITLE = "title";
91      private static final String PARAMETER_VALUE = "value";
92      private static final String PARAMETER_VALID_EXEMPLE = "valid_exemple";
93      private static final String PARAMETER_INFORMATION_MESSAGE = "information_message";
94      private static final String PARAMETER_ERROR_MESSAGE = "error_message";
95      private static final String PARAMETER_PAGE_INDEX = "page_index";
96  
97      // other constants
98      private static final String EMPTY_STRING = "";
99  
100     // message
101     private static final String MESSAGE_CONFIRM_REMOVE_EXPRESSION = "regularexpression.message.confirm_remove_regular_expression";
102     private static final String MESSAGE_MANDATORY_FIELD = "regularexpression.message.mandatory.field";
103     private static final String MESSAGE_REGULAR_EXPRESSION_FORMAT_NOT_VALIDE = "regularexpression.message.regular_expression_format_not_valide";
104     private static final String MESSAGE_REGULAR_EXPRESSION_EXEMPLE_NOT_VALIDE = "regularexpression.message.regular_expression_exemple_not_valide";
105     private static final String MESSAGE_CAN_NOT_REMOVE_REGULAR_EXPRESSION = "regularexpression.message.can_not_remove_regular_expression";
106     private static final String FIELD_TITLE = "regularexpression.create_regular_expression.label_title";
107     private static final String FIELD_VALUE = "regularexpression.create_regular_expression.label_value";
108     private static final String FIELD_VALID_EXEMPLE = "regularexpression.create_regular_expression.label_valid_exemple";
109     private static final String FIELD_INFORMATION_MESSAGE = "regularexpression.create_regular_expression.label_information_message";
110     private static final String FIELD_ERROR_MESSAGE = "regularexpression.create_regular_expression.label_error_message";
111 
112     // properties
113     private static final String PROPERTY_ITEM_PER_PAGE = "regularexpression.itemsPerPage";
114     private static final String PROPERTY_MANAGE_REGULAR_EXPRESSION_TITLE = "regularexpression.manage_regular_expression.page_title";
115     private static final String PROPERTY_MODIFY_REGULAR_EXPRESSION_TITLE = "regularexpression.modify_regular_expression.title";
116     private static final String PROPERTY_CREATE_REGULAR_EXPRESSION_TITLE = "regularexpression.create_regular_expression.title";
117 
118     // Jsp Definition
119     private static final String JSP_MANAGE_REGULAR_EXPRESSION = "jsp/admin/plugins/regularexpression/ManageRegularExpression.jsp";
120     private static final String JSP_DO_REMOVE_REGULAR_EXPRESSION = "jsp/admin/plugins/regularexpression/DoRemoveRegularExpression.jsp";
121 
122     // session fields
123     private int _nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_ITEM_PER_PAGE, 15 );
124     private String _strCurrentPageIndexExport;
125     private int _nItemsPerPageForm;
126 
127     /**
128      * Return management regular expression ( list of regular expression )
129      * 
130      * @param request
131      *            The Http request
132      * @return Html form
133      */
134     public String getManageRegularExpression( HttpServletRequest request )
135     {
136         Plugin plugin = getPlugin( );
137         Locale locale = getLocale( );
138         Map<String, Object> model = new HashMap<>( );
139         List<RegularExpression> listRegularExpression = RegularExpressionHome.getList( plugin );
140 
141         _strCurrentPageIndexExport = AbstractPaginator.getPageIndex( request, AbstractPaginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndexExport );
142         _nItemsPerPageForm = AbstractPaginator.getItemsPerPage( request, AbstractPaginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPageForm,
143                 _nDefaultItemsPerPage );
144 
145         Paginator<RegularExpression> paginator = new Paginator<>( listRegularExpression, _nItemsPerPageForm, getJspManageRegularExpression( request ),
146                 PARAMETER_PAGE_INDEX, _strCurrentPageIndexExport );
147         model.put( MARK_PAGINATOR, paginator );
148         model.put( MARK_NB_ITEMS_PER_PAGE, EMPTY_STRING + _nItemsPerPageForm );
149         model.put( MARK_EXPRESSION_LIST, paginator.getPageItems( ) );
150         setPageTitleProperty( PROPERTY_MANAGE_REGULAR_EXPRESSION_TITLE );
151 
152         HtmlTemplate templateList = AppTemplateService.getTemplate( TEMPLATE_MANAGE_REGULAR_EXPRESSION, locale, model );
153         return getAdminPage( templateList.getHtml( ) );
154     }
155 
156     /**
157      * Gets the regular expressioncreation page
158      * 
159      * @param request
160      *            The HTTP request
161      * @return The regular expression creation page
162      */
163     public String getCreateRegularExpression( HttpServletRequest request )
164     {
165         Locale locale = getLocale( );
166         Map<String, Object> model = new HashMap<>( );
167         model.put( MARK_WEBAPP_URL, AppPathService.getBaseUrl( request ) );
168         model.put( MARK_LOCALE, AdminUserService.getLocale( request ).getLanguage( ) );
169         setPageTitleProperty( PROPERTY_CREATE_REGULAR_EXPRESSION_TITLE );
170 
171         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_REGULAR_EXPRESSION, locale, model );
172 
173         return getAdminPage( template.getHtml( ) );
174     }
175 
176     /**
177      * Perform the regular expression creation
178      * 
179      * @param request
180      *            The HTTP request
181      * @return The URL to go after performing the action
182      */
183     public String doCreateRegularExpression( HttpServletRequest request )
184     {
185         RegularExpression regularExpression = new RegularExpression( );
186         String strError = getRegularExpressionData( request, regularExpression );
187 
188         if ( strError != null )
189         {
190             return strError;
191         }
192 
193         RegularExpressionHome.create( regularExpression, getPlugin( ) );
194 
195         return getJspManageRegularExpression( request );
196     }
197 
198     /**
199      * Gets the regular expression modification page
200      * 
201      * @param request
202      *            The HTTP request
203      * @return The regular expression creation page
204      */
205     public String getModifyRegularExpression( HttpServletRequest request )
206     {
207         Plugin plugin = getPlugin( );
208         Locale locale = getLocale( );
209         RegularExpression regularExpression;
210         String strIdExpression = request.getParameter( PARAMETER_ID_EXPRESSION );
211         Map<String, Object> model = new HashMap<>( );
212 
213         int nIdExpression = -1;
214 
215         if ( ( strIdExpression != null ) && !strIdExpression.equals( EMPTY_STRING ) )
216         {
217             try
218             {
219                 nIdExpression = Integer.parseInt( strIdExpression );
220             }
221             catch( NumberFormatException ne )
222             {
223                 AppLogService.error( ne );
224 
225                 return getManageRegularExpression( request );
226             }
227         }
228         else
229         {
230             return getManageRegularExpression( request );
231         }
232 
233         regularExpression = RegularExpressionHome.findByPrimaryKey( nIdExpression, plugin );
234         model.put( MARK_WEBAPP_URL, AppPathService.getBaseUrl( request ) );
235         model.put( MARK_LOCALE, AdminUserService.getLocale( request ).getLanguage( ) );
236         model.put( MARK_EXPPRESSION, regularExpression );
237         setPageTitleProperty( PROPERTY_MODIFY_REGULAR_EXPRESSION_TITLE );
238 
239         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_REGULAR_EXPRESSION, locale, model );
240 
241         return getAdminPage( template.getHtml( ) );
242     }
243 
244     /**
245      * Perform the regular expression modification
246      * 
247      * @param request
248      *            The HTTP request
249      * @return The URL to go after performing the action
250      */
251     public String doModifyRegularExpression( HttpServletRequest request )
252     {
253         Plugin plugin = getPlugin( );
254         RegularExpression regularExpression;
255         String strIdExpression = request.getParameter( PARAMETER_ID_EXPRESSION );
256         int nIdExpression = -1;
257 
258         if ( ( strIdExpression != null ) && !strIdExpression.equals( EMPTY_STRING ) )
259         {
260             try
261             {
262                 nIdExpression = Integer.parseInt( strIdExpression );
263             }
264             catch( NumberFormatException ne )
265             {
266                 AppLogService.error( ne );
267 
268                 return getJspManageRegularExpression( request );
269             }
270         }
271         else
272         {
273             return getJspManageRegularExpression( request );
274         }
275 
276         regularExpression = new RegularExpression( );
277         regularExpression.setIdExpression( nIdExpression );
278 
279         String strError = getRegularExpressionData( request, regularExpression );
280 
281         if ( strError != null )
282         {
283             return strError;
284         }
285 
286         RegularExpressionHome.update( regularExpression, plugin );
287 
288         return getJspManageRegularExpression( request );
289     }
290 
291     /**
292      * Gets the confirmation page of delete regular expression
293      * 
294      * @param request
295      *            The HTTP request
296      * @return the confirmation page of delete regular expression
297      */
298     public String getConfirmRemoveRegularExpression( HttpServletRequest request )
299     {
300         String strIdExpression = request.getParameter( PARAMETER_ID_EXPRESSION );
301 
302         if ( strIdExpression == null )
303         {
304             return getHomeUrl( request );
305         }
306 
307         UrlItem url = new UrlItem( JSP_DO_REMOVE_REGULAR_EXPRESSION );
308         url.addParameter( PARAMETER_ID_EXPRESSION, strIdExpression );
309 
310         return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_EXPRESSION, url.getUrl( ), AdminMessage.TYPE_CONFIRMATION );
311     }
312 
313     /**
314      * Perform the regular expression supression
315      * 
316      * @param request
317      *            The HTTP request
318      * @return The URL to go after performing the action
319      */
320     public String doRemoveRegularExpression( HttpServletRequest request )
321     {
322         String strIdExpression = request.getParameter( PARAMETER_ID_EXPRESSION );
323         ArrayList<String> listErrors = new ArrayList<>( );
324 
325         int nIdExpression = -1;
326 
327         if ( ( strIdExpression != null ) && !strIdExpression.equals( EMPTY_STRING ) )
328         {
329             try
330             {
331                 nIdExpression = Integer.parseInt( strIdExpression );
332             }
333             catch( NumberFormatException ne )
334             {
335                 AppLogService.error( ne );
336             }
337         }
338 
339         if ( !RegularExpressionRemovalListenerService.getService( ).checkForRemoval( strIdExpression, listErrors, getLocale( ) ) )
340         {
341             String strCause = AdminMessageService.getFormattedList( listErrors, getLocale( ) );
342             Object [ ] args = {
343                     strCause
344             };
345 
346             return AdminMessageService.getMessageUrl( request, MESSAGE_CAN_NOT_REMOVE_REGULAR_EXPRESSION, args, AdminMessage.TYPE_STOP );
347         }
348 
349         if ( nIdExpression != -1 )
350         {
351             RegularExpressionHome.remove( nIdExpression, getPlugin( ) );
352         }
353 
354         return getJspManageRegularExpression( request );
355     }
356 
357     /**
358      * Get the request data and if there is no error insert the data in the regularExpression object specified in parameter. return null if there is no error or
359      * else return the error page url
360      * 
361      * @param request
362      *            the request
363      * @param regularExpression
364      *            the regularExpression Object
365      * @return null if there is no error or else return the error page url
366      */
367     private String getRegularExpressionData( HttpServletRequest request, RegularExpression regularExpression )
368     {
369         IRegularExpressionService service = SpringContextService.getBean( "regularExpressionService" );
370         String strTitle = ( request.getParameter( PARAMETER_TITLE ) == null ) ? null : request.getParameter( PARAMETER_TITLE ).trim( );
371         String strValue = ( request.getParameter( PARAMETER_VALUE ) == null ) ? null : request.getParameter( PARAMETER_VALUE ).trim( );
372         String strValidExemple = ( request.getParameter( PARAMETER_VALID_EXEMPLE ) == null ) ? null : request.getParameter( PARAMETER_VALID_EXEMPLE ).trim( );
373         String strInformationMessage = ( request.getParameter( PARAMETER_INFORMATION_MESSAGE ) == null ) ? null
374                 : request.getParameter( PARAMETER_INFORMATION_MESSAGE ).trim( );
375         String strErrorMessage = ( request.getParameter( PARAMETER_ERROR_MESSAGE ) == null ) ? null : request.getParameter( PARAMETER_ERROR_MESSAGE ).trim( );
376 
377         String strFieldError = EMPTY_STRING;
378 
379         if ( StringUtils.isEmpty( strTitle ) )
380         {
381             strFieldError = FIELD_TITLE;
382         }
383 
384         else
385             if ( StringUtils.isEmpty( strValue ) )
386             {
387                 strFieldError = FIELD_VALUE;
388             }
389 
390             else
391                 if ( StringUtils.isEmpty( strValidExemple ) )
392                 {
393                     strFieldError = FIELD_VALID_EXEMPLE;
394                 }
395                 else
396                     if ( StringUtils.isEmpty( strInformationMessage ) )
397                     {
398                         strFieldError = FIELD_INFORMATION_MESSAGE;
399                     }
400                     else
401                         if ( StringUtils.isEmpty( strErrorMessage ) )
402                         {
403                             strFieldError = FIELD_ERROR_MESSAGE;
404                         }
405 
406         // Mandatory fields
407         if ( !strFieldError.equals( EMPTY_STRING ) )
408         {
409             Object [ ] tabRequiredFields = {
410                     I18nService.getLocalizedString( strFieldError, getLocale( ) )
411             };
412 
413             return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
414         }
415 
416         // teste if the regular expression is valid
417         if ( !service.isPatternValide( strValue ) )
418         {
419             return AdminMessageService.getMessageUrl( request, MESSAGE_REGULAR_EXPRESSION_FORMAT_NOT_VALIDE, AdminMessage.TYPE_STOP );
420         }
421 
422         regularExpression.setTitle( strTitle );
423         regularExpression.setValue( strValue );
424         regularExpression.setValidExemple( strValidExemple );
425         regularExpression.setInformationMessage( strInformationMessage );
426         regularExpression.setErrorMessage( strErrorMessage );
427 
428         if ( !service.isMatches( strValidExemple, strValue ) )
429         {
430             return AdminMessageService.getMessageUrl( request, MESSAGE_REGULAR_EXPRESSION_EXEMPLE_NOT_VALIDE, AdminMessage.TYPE_STOP );
431         }
432 
433         return null;
434     }
435 
436     /**
437      * return the url of manage regular expression
438      * 
439      * @param request
440      *            the request
441      * @return the url of manage regular expression
442      */
443     private String getJspManageRegularExpression( HttpServletRequest request )
444     {
445         return AppPathService.getBaseUrl( request ) + JSP_MANAGE_REGULAR_EXPRESSION;
446     }
447 }