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.modules.alertgru.web;
35  
36  import fr.paris.lutece.plugins.workflow.modules.alertgru.business.AlertGruTaskConfig;
37  import fr.paris.lutece.plugins.workflow.modules.alertgru.service.AlertGruService;
38  import fr.paris.lutece.plugins.workflow.modules.alertgru.service.AlertGruTaskConfigService;
39  import fr.paris.lutece.plugins.workflow.modules.alertgru.service.IAlertGruService;
40  import fr.paris.lutece.plugins.workflow.modules.alertgru.service.ServiceConfigTaskForm;
41  import fr.paris.lutece.plugins.workflow.modules.alertgru.utils.constants.Constants;
42  import fr.paris.lutece.plugins.workflow.modules.alertgru.web.alertconfig.impl.AgentAlertConfig;
43  import fr.paris.lutece.plugins.workflow.modules.alertgru.web.alertconfig.impl.BillingAccountBasedSMSAlertConfig;
44  import fr.paris.lutece.plugins.workflow.modules.alertgru.web.alertconfig.impl.BroadcastAlertConfig;
45  import fr.paris.lutece.plugins.workflow.modules.alertgru.web.alertconfig.impl.EmailAlertConfig;
46  import fr.paris.lutece.plugins.workflow.modules.alertgru.web.alertconfig.impl.GuichetAlertConfig;
47  import fr.paris.lutece.plugins.workflow.modules.alertgru.web.alertconfig.impl.SMSAlertConfig;
48  import fr.paris.lutece.plugins.workflow.service.provider.MarkerProviderService;
49  import fr.paris.lutece.plugins.workflow.service.provider.ProviderManagerUtil;
50  import fr.paris.lutece.plugins.workflowcore.service.config.ITaskConfigService;
51  import fr.paris.lutece.plugins.workflowcore.service.provider.AbstractProviderManager;
52  import fr.paris.lutece.plugins.workflowcore.service.provider.IMarkerProvider;
53  import fr.paris.lutece.plugins.workflowcore.service.provider.InfoMarker;
54  import fr.paris.lutece.plugins.workflowcore.service.task.ITask;
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.spring.SpringContextService;
59  import fr.paris.lutece.portal.service.template.AppTemplateService;
60  import fr.paris.lutece.portal.service.util.AppPathService;
61  import fr.paris.lutece.portal.service.util.AppPropertiesService;
62  import fr.paris.lutece.portal.util.mvc.utils.MVCMessage;
63  import fr.paris.lutece.util.ErrorMessage;
64  import fr.paris.lutece.util.ReferenceList;
65  import fr.paris.lutece.util.html.HtmlTemplate;
66  import java.util.ArrayList;
67  import java.util.Arrays;
68  import java.util.Collection;
69  import java.util.HashMap;
70  import java.util.List;
71  import java.util.Map;
72  import javax.servlet.http.HttpServletRequest;
73  import org.apache.commons.lang3.StringUtils;
74  
75  public class AlertGruTaskConfigController
76  {
77  
78      // Templates
79      private static final String TEMPLATE_FIRST_STEP = "admin/plugins/workflow/modules/alertgru/task_alert_gru_config_first_step.html";
80      private static final String TEMPLATE_SECOND_STEP = "admin/plugins/workflow/modules/alertgru/task_alert_gru_config_second_step.html";
81  
82      // Marks
83      private static final String MARK_SELECT_PROVIDER = "list_provider";
84      private static final String MARK_LIST_MARKER_PROVIDER = "list_marker_provider";
85      private static final String MARK_LIST_ALERT_CONFIG = "list_alert_config";
86      private static final String MARK_LOCALE = "locale";
87      private static final String MARK_WEBAPP_URL = "webapp_url";
88  
89      // Services
90      private static final ITaskConfigService _taskAlertGruConfigService = SpringContextService.getBean( AlertGruTaskConfigService.BEAN_SERVICE );
91      private static final IAlertGruService _alertGruService = SpringContextService.getBean( AlertGruService.BEAN_SERVICE );
92  
93      private final ITask _task;
94      private AlertGruTaskConfig _config;
95  
96      // Parameters
97      private static final String PARAMETER_APPLY = "apply";
98      private static final String PARAMETER_APPLY_MARKER = "apply_marker";
99  
100     // Actions
101     private static final String ACTION_FIRST_STEP_SAVE = "saveFirstStep";
102     private static final String ACTION_SECOND_STEP_SAVE = "saveSecondStep";
103     private static final String ACTION_ADVANCED_CONFIG_SAVE = "saveAdvancedConfig";
104     private static final String ACTION_ADVANCED_CONFIG_CANCEL = "cancelAdvancedConfig";
105     private static final String ACTION_ALERT_CONFIG_ADD = "AddAlertConfig";
106     private static final String ACTION_ALERT_CONFIG_REMOVE_PREFIX = "RemoveAlertConfig_";
107     private static final String ACTION_ALERT_CONFIG_MARKERS = "loadMarkers";
108 
109     // Sessions
110     private static final String SESSION_ID_PROVIDER = "alertProviderSession";
111 
112     /**
113      * Constructor
114      *
115      * @param task
116      *            the task associated to the configuration
117      */
118     public AlertGruTaskConfigController( ITask task )
119     {
120         _task = task;
121 
122         findConfig( );
123     }
124 
125     /**
126      * Finds the configuration
127      */
128     private void findConfig( )
129     {
130         _config = _taskAlertGruConfigService.findByPrimaryKey( _task.getId( ) );
131 
132         if ( _config == null )
133         {
134             // no config stored yet for this task, setting a empty one
135             _config = new AlertGruTaskConfig( );
136         }
137     }
138 
139     /**
140      * Initializes the alert configurations
141      *
142      * @param request
143      *            the request used to initialize the configurations
144      * @return the list of configurations
145      */
146     private List<IAlertConfig> initAlertConfigs( HttpServletRequest request )
147     {
148         List<IAlertConfig> listAlertConfig = new ArrayList<>( );
149 
150         listAlertConfig.add( new AgentAlertConfig( request, _config ) );
151         listAlertConfig.add( new BroadcastAlertConfig( request, _config ) );
152         listAlertConfig.add( new EmailAlertConfig( request, _config ) );
153         listAlertConfig.add( new GuichetAlertConfig( request, _config ) );
154         if ( _config.isBillingAccountBasedSmsNotification( ) )
155         {
156             listAlertConfig.add( new BillingAccountBasedSMSAlertConfig( request, _config ) );
157         }
158         else
159         {
160             listAlertConfig.add( new SMSAlertConfig( request, _config ) );
161         }
162 
163         return listAlertConfig;
164     }
165 
166     /**
167      * Builds the view depending on the specified request and locale
168      *
169      * @param request
170      *            the request the locale
171      * @return the {@link HtmlTemplate} representing the view
172      */
173     public HtmlTemplate buildView( HttpServletRequest request )
174     {
175         HtmlTemplate template = null;
176         View view = new View( request );
177 
178         if ( _config.getIdSpringProvider( ) == null || _config.getMarkerAlert( ) == null )
179         {
180             template = view.buildFirstStep( );
181         }
182         else
183         {
184             template = view.buildSecondStep( );
185         }
186 
187         return template;
188     }
189 
190     /**
191      * Performs an action triggered by the user
192      *
193      * @param request
194      *            the request containing the action
195      * @return the URL of the error page if there is an error during the action, {@code null} otherwise
196      */
197     public String performAction( HttpServletRequest request )
198     {
199         String strErrorUrl = null;
200         String strAction = findAction( request );
201         Action action = new Action( request, strAction );
202 
203         if ( ACTION_ALERT_CONFIG_MARKERS.equals( strAction ) )
204         {
205             strErrorUrl = action.loadMarkers( );
206         }
207 
208         if ( ACTION_FIRST_STEP_SAVE.equals( strAction ) )
209         {
210             strErrorUrl = action.saveFirstStep( );
211         }
212 
213         if ( ACTION_SECOND_STEP_SAVE.equals( strAction ) )
214         {
215             strErrorUrl = action.saveSecondStep( );
216         }
217 
218         if ( strAction.startsWith( ACTION_ALERT_CONFIG_REMOVE_PREFIX ) )
219         {
220             strErrorUrl = action.removeAlertConf( );
221         }
222 
223         if ( ACTION_ALERT_CONFIG_ADD.equals( strAction ) )
224         {
225             strErrorUrl = action.addAlertConf( );
226         }
227 
228         if ( ACTION_ADVANCED_CONFIG_CANCEL.equals( strAction ) )
229         {
230             strErrorUrl = action.cancel( );
231         }
232 
233         if ( ACTION_ADVANCED_CONFIG_SAVE.equals( strAction ) )
234         {
235             strErrorUrl = action.saveAdvancedConfig( );
236         }
237 
238         return strErrorUrl;
239     }
240 
241     /**
242      * Finds the action contained in the specified request
243      *
244      * @param request
245      *            the request
246      * @return the action
247      */
248     private String findAction( HttpServletRequest request )
249     {
250         String strAction;
251         if ( !StringUtils.isBlank( request.getParameter( PARAMETER_APPLY_MARKER ) ) )
252         {
253             strAction = request.getParameter( PARAMETER_APPLY_MARKER );
254         }
255         else
256         {
257             strAction = request.getParameter( PARAMETER_APPLY );
258         }
259         if ( StringUtils.isBlank( strAction ) )
260         {
261             strAction = ACTION_SECOND_STEP_SAVE;
262         }
263 
264         return strAction;
265     }
266 
267     /**
268      * This class represents a view of the task configuration
269      *
270      */
271     private final class View
272     {
273         private final HttpServletRequest _request;
274         private final List<IAlertConfig> _listAlertConfig;
275         private final Map<String, Object> _model;
276 
277         /**
278          * Constructor
279          *
280          * @param request
281          *            the request used by the view
282          */
283         private View( HttpServletRequest request )
284         {
285             _request = request;
286             _model = new HashMap<>( );
287 
288             _listAlertConfig = initAlertConfigs( request );
289         }
290 
291         /**
292          * Builds the first step of the configuration
293          *
294          * @return the {@link HtmlTemplate} representing the view
295          */
296         private HtmlTemplate buildFirstStep( )
297         {
298             fillModelWithConfig( );
299             fillModelWithGlobalConfig( );
300             if ( _config.getIdSpringProvider( ) != null )
301             {
302                 manageAlertGruMarkersInModel( );
303             }
304 
305             return AppTemplateService.getTemplate( TEMPLATE_FIRST_STEP, _request.getLocale( ), _model );
306         }
307 
308         /**
309          * Builds the second step of the configuration
310          *
311          * @return the {@link HtmlTemplate} representing the view
312          */
313         private HtmlTemplate buildSecondStep( )
314         {
315             fillModelWithConfig( );
316             fillModelWithRichTextEditorConfig( );
317             fillModelWithAlertConfigs( );
318             fillModelWithMailSenderName( );
319             fillModelWithMailingList( );
320             fillModelWithGlobalConfig( );
321             manageAlertGruMarkersInModel( );
322 
323             return AppTemplateService.getTemplate( TEMPLATE_SECOND_STEP, _request.getLocale( ), _model );
324         }
325 
326         /**
327          * Fills the model with the task configuration
328          */
329         private void fillModelWithConfig( )
330         {
331             if ( _request.getSession( ).getAttribute( SESSION_ID_PROVIDER ) != null )
332             {
333                 _config.setIdSpringProvider( (String) _request.getSession( ).getAttribute( SESSION_ID_PROVIDER ) );
334             }
335             _model.put( Constants.MARK_CONFIG, _config );
336         }
337 
338         /**
339          * Fills the model with the configuration of the rich text editor
340          */
341         private void fillModelWithRichTextEditorConfig( )
342         {
343             _model.put( MARK_LOCALE, _request.getLocale( ) );
344             _model.put( MARK_WEBAPP_URL, AppPathService.getBaseUrl( _request ) );
345         }
346 
347         /**
348          * Fills the model the alert configurations (as reference list)
349          */
350         private void fillModelWithAlertConfigs( )
351         {
352             ReferenceList listAlertConfig = ServiceConfigTaskForm.buildReferenceListOfInactiveAlertConfigs( _listAlertConfig, _request.getLocale( ) );
353 
354             _model.put( MARK_LIST_ALERT_CONFIG, listAlertConfig );
355         }
356 
357         /**
358          * Fills the model with the global configuration of the task
359          */
360         private void fillModelWithGlobalConfig( )
361         {
362             fillModelWithProviders( );
363             fillModelWithMarkerProviders( );
364             fillModelWithAlertInfos( );
365         }
366 
367         /**
368          * Fills the model with the providers
369          */
370         private void fillModelWithProviders( )
371         {
372             _model.put( MARK_SELECT_PROVIDER, ServiceConfigTaskForm.getProviderReferenceList( _task ) );
373         }
374 
375         /**
376          * Fills the model with the marker providers
377          */
378         private void fillModelWithMarkerProviders( )
379         {
380             _model.put( MARK_LIST_MARKER_PROVIDER, MarkerProviderService.getInstance( ).getMarkerProviders( ) );
381         }
382 
383         /**
384          * Fills the model with the mailing list
385          */
386         private void fillModelWithMailingList( )
387         {
388             _model.put( Constants.MARK_MAILING_LIST, _alertGruService.getMailingList( _request ) );
389         }
390 
391         /**
392          * Fills the model with the mail sender name
393          */
394         private void fillModelWithMailSenderName( )
395         {
396             String strDefaultSenderName = AppPropertiesService.getProperty( Constants.PROPERTY_NOTIFY_MAIL_DEFAULT_SENDER_NAME );
397 
398             _model.put( Constants.MARK_DEFAULT_SENDER_NAME, strDefaultSenderName );
399         }
400 
401         private void fillModelWithAlertInfos( )
402         {
403             _model.put( Constants.MARK_STATE_LIST, _alertGruService.getListStates( _task.getAction( ).getId( ) ) );
404 
405         }
406 
407         /**
408          * Manages the AlertyGru markers in the model
409          */
410         private void manageAlertGruMarkersInModel( )
411         {
412             String strIdSpringProvider = _request.getSession( ).getAttribute( SESSION_ID_PROVIDER ) != null
413                     ? (String) _request.getSession( ).getAttribute( SESSION_ID_PROVIDER )
414                     : _config.getIdSpringProvider( );
415             String strProviderManagerId = ProviderManagerUtil.fetchProviderManagerId( strIdSpringProvider );
416             String strProviderId = ProviderManagerUtil.fetchProviderId( strIdSpringProvider );
417             AbstractProviderManager providerManager = ProviderManagerUtil.fetchProviderManager( strProviderManagerId );
418 
419             if ( providerManager != null )
420             {
421                 _model.put( Constants.MARK_NOTIFYGRU_MARKERS, findMarkers( providerManager, strProviderId, _config.getMarkerProviders( ) ) );
422             }
423             else
424             {
425                 List<ErrorMessage> listErrorMessages = new ArrayList<>( );
426                 listErrorMessages.add( new MVCMessage( I18nService.getLocalizedString( Constants.MESSAGE_ERROR_PROVIDER_NOT_FOUND, _request.getLocale( ) ) ) );
427                 _model.put( Constants.MARK_MESSAGES_ERROR, listErrorMessages );
428             }
429         }
430 
431         /**
432          * Finds the AlertGru markers
433          *
434          * @param providerManager
435          *            the provider manager
436          * @param strProviderId
437          *            the provider id
438          * @param listMarkerProviderIds
439          *            the list of marker provider ids
440          * @return the AlertGru markers
441          */
442         private Collection<InfoMarker> findMarkers( AbstractProviderManager providerManager, String strProviderId, List<String> listMarkerProviderIds )
443         {
444             Collection<InfoMarker> collectionMarkers = providerManager.getProviderDescription( strProviderId ).getMarkerDescriptions( );
445 
446             for ( String strMarkerProviderId : listMarkerProviderIds )
447             {
448                 IMarkerProvider markerProvider = MarkerProviderService.getInstance( ).find( strMarkerProviderId );
449 
450                 if ( markerProvider != null )
451                 {
452                     collectionMarkers.addAll( markerProvider.provideMarkerDescriptions( ) );
453                 }
454             }
455 
456             return collectionMarkers;
457         }
458 
459     }
460 
461     /**
462      * This class represents an action in the task configuration
463      *
464      *
465      *
466      *
467      *
468      *
469      */
470     private final class Action
471     {
472         private static final String VALUE_CHECKBOX = "on";
473         private static final String PARAMETER_ADDED_ALERT_CONFIG = "added_alert_config";
474         private static final String PARAMETER_REMOVE_SEPARATOR = "_";
475         private static final int PARAMETER_REMOVE_ALERT_CONFIG_NAME_PART = 1;
476 
477         private final HttpServletRequest _request;
478         private final List<IAlertConfig> _listAlertConfig;
479         private final AbstractProviderManager _providerManager;
480         private final String _strAction;
481 
482         /**
483          * Constructor
484          *
485          * @param request
486          *            the request used to perform the action
487          * @param strAction
488          *            the action to perform
489          */
490         private Action( HttpServletRequest request, String strAction )
491         {
492             _request = request;
493             _strAction = strAction;
494 
495             String strProviderManagerId = ProviderManagerUtil.fetchProviderManagerId( _config.getIdSpringProvider( ) );
496             _providerManager = ProviderManagerUtil.fetchProviderManager( strProviderManagerId );
497 
498             _listAlertConfig = initAlertConfigs( request );
499 
500         }
501 
502         /**
503          * Load markers from chosen provider
504          *
505          * @return the URL of the error page if there is an error during the action, {@code null} otherwise
506          */
507         private String loadMarkers( )
508         {
509             fillConfigWithGlobalConfig( );
510             putProviderInSession( );
511             return null;
512         }
513 
514         private void putProviderInSession( )
515         {
516             _request.getSession( ).setAttribute( SESSION_ID_PROVIDER, _request.getParameter( Constants.PARAMETER_SELECT_PROVIDER ) );
517         }
518 
519         /**
520          * Saves the first step of the task configuration
521          *
522          * @return the URL of the error page if there is an error during the action, {@code null} otherwise
523          */
524         private String saveFirstStep( )
525         {
526             String strErrorUrl = validateGlobalConfig( );
527 
528             if ( !StringUtils.isEmpty( strErrorUrl ) )
529             {
530                 return strErrorUrl;
531             }
532 
533             fillConfigWithGlobalConfig( );
534             putProviderInSession( );
535             saveConfig( );
536 
537             return null;
538         }
539 
540         /**
541          * Saves the second step of the task configuration
542          *
543          * @return the URL of the error page if there is an error during the action, {@code null} otherwise
544          */
545         private String saveSecondStep( )
546         {
547             String strResult = validateSecondStepConfig( );
548 
549             if ( !StringUtils.isEmpty( strResult ) )
550             {
551                 return strResult;
552             }
553 
554             saveAlertConfigs( );
555             saveConfig( );
556 
557             return null;
558 
559         }
560 
561         /**
562          * Saves the advanced configuration of the task
563          *
564          * @return the URL of the error page if there is an error during the action, {@code null} otherwise
565          */
566         private String saveAdvancedConfig( )
567         {
568             String strErrorUrl = validateGlobalConfig( );
569 
570             if ( !StringUtils.isEmpty( strErrorUrl ) )
571             {
572                 return strErrorUrl;
573             }
574             fillConfigWithGlobalConfig( );
575             fillConfigWithCrmStatus( );
576             putProviderInSession( );
577             saveConfig( );
578             return null;
579         }
580 
581         /**
582          * Cancels the action
583          *
584          * @return {@code null}
585          */
586         private String cancel( )
587         {
588             _request.getSession( ).removeAttribute( SESSION_ID_PROVIDER );
589             return null;
590         }
591 
592         /**
593          * Adds an alert configuration in the task
594          *
595          * @return the URL of the error page if there is an error during the action, {@code null} otherwise
596          */
597         private String addAlertConf( )
598         {
599             String strErrorUrl = validateAlertConfigs( );
600 
601             if ( !StringUtils.isEmpty( strErrorUrl ) )
602             {
603                 return strErrorUrl;
604             }
605 
606             String strNameOfAddedAlertConfig = findNameOfAddedAlertConfig( );
607             IAlertConfig alertConfigAdded = findAlertConfigByName( strNameOfAddedAlertConfig );
608 
609             alertConfigAdded.setActive( true );
610 
611             fillConfigWithCurrentAlertConfig( alertConfigAdded );
612 
613             saveAlertConfigs( );
614             saveConfig( );
615 
616             return null;
617         }
618 
619         /**
620          * Removes a alert configuration from the task
621          *
622          * @return the URL of the error page if there is an error during the action, {@code null} otherwise
623          */
624         private String removeAlertConf( )
625         {
626             String strNameOfRemovedAlertConfig = findNameOfRemovedAlertConfig( );
627             IAlertConfig alertConfigRemoved = findAlertConfigByName( strNameOfRemovedAlertConfig );
628 
629             alertConfigRemoved.setActive( false );
630             alertConfigRemoved.removeConfig( );
631 
632             saveConfig( );
633 
634             return null;
635         }
636 
637         /**
638          * Finds the name of the added alert configuration
639          *
640          * @return the name of the added alert configuration
641          */
642         private String findNameOfAddedAlertConfig( )
643         {
644             return _request.getParameter( PARAMETER_ADDED_ALERT_CONFIG );
645         }
646 
647         /**
648          * Finds the name of the removed alert configuration
649          *
650          * @return the name of the removed alert configuration
651          */
652         private String findNameOfRemovedAlertConfig( )
653         {
654             return _strAction.split( PARAMETER_REMOVE_SEPARATOR ) [PARAMETER_REMOVE_ALERT_CONFIG_NAME_PART];
655         }
656 
657         /**
658          * Finds a alert configuration by its name
659          *
660          * @param strName
661          *            the name of the alert configuration to find
662          * @return the found alert configuration, or {@code null} if not found
663          */
664         private IAlertConfig findAlertConfigByName( String strName )
665         {
666             IAlertConfig alertConfigFound = null;
667 
668             for ( IAlertConfig alertConfig : _listAlertConfig )
669             {
670                 if ( alertConfig.getName( ).equals( strName ) )
671                 {
672                     alertConfigFound = alertConfig;
673                     break;
674                 }
675             }
676 
677             return alertConfigFound;
678         }
679 
680         /**
681          * Validates the global configuration of the task
682          *
683          * @return the URL of the error page if there is a validation error, {@code null} otherwise
684          */
685         private String validateGlobalConfig( )
686         {
687             String errUrl;
688             errUrl = validateFieldIsNotNull( Constants.PARAMETER_SUBJECT_ALERT, Constants.MESSAGE_MANDATORY_SUBJECT );
689             if ( StringUtils.isEmpty( errUrl ) )
690             {
691                 errUrl = validateFieldIsNotNull( Constants.PARAMETER_SELECT_PROVIDER, Constants.MESSAGE_MANDATORY_PROVIDER );
692                 if ( StringUtils.isEmpty( errUrl ) )
693                 {
694                     errUrl = validateFieldIsNotNull( Constants.PARAMETER_MARKER_ALERT, Constants.MESSAGE_MANDATORY_MARKER );
695                 }
696                 if ( StringUtils.isEmpty( errUrl ) )
697                 {
698                     errUrl = validateFieldIsNotNull( Constants.PARAMETER_ALERT_AFTER_BEFORE, Constants.MESSAGE_MANDATORY_ALERT_AFTER );
699                     if ( StringUtils.isEmpty( errUrl ) )
700                     {
701                         errUrl = validateFieldIsNotNull( Constants.PARAMETER_STATE_AFTER, Constants.MESSAGE_MANDATORY_STATE_AFTER );
702                     }
703                 }
704             }
705             return errUrl;
706         }
707 
708         /**
709          * Validates that the field is not {@code null}
710          *
711          * @return the URL of the error page if there is a validation error, {@code null} otherwise
712          */
713         private String validateFieldIsNotNull( String paramater, String errMessage )
714         {
715             String strErrorUrl = null;
716 
717             if ( StringUtils.isEmpty( _request.getParameter( paramater ) ) )
718             {
719                 strErrorUrl = buildUrlForMissingField( errMessage );
720             }
721 
722             return strErrorUrl;
723         }
724 
725         /**
726          * Validates the second step of the task configuration
727          *
728          * @return the URL of the error page if there is a validation error, {@code null} otherwise
729          */
730         private String validateSecondStepConfig( )
731         {
732             String strErrorUrl = validateOneAlertConfigIsActive( );
733 
734             if ( !StringUtils.isEmpty( strErrorUrl ) )
735             {
736                 return strErrorUrl;
737             }
738 
739             strErrorUrl = validateAlertConfigs( );
740 
741             if ( !StringUtils.isEmpty( strErrorUrl ) )
742             {
743                 return strErrorUrl;
744             }
745 
746             return null;
747         }
748 
749         /**
750          * Validates that at least one alert configuration is active
751          *
752          * @return the URL of the error page if there is a validation error, {@code null} otherwise
753          */
754         private String validateOneAlertConfigIsActive( )
755         {
756             String strErrorUrl = null;
757 
758             if ( filterActiveAlertConfigs( ).isEmpty( ) )
759             {
760                 strErrorUrl = buildUrlForNoAlertConfig( );
761             }
762 
763             return strErrorUrl;
764         }
765 
766         /**
767          * Validates the alert configurations
768          *
769          * @return the URL of the error page if there is a validation error, {@code null} otherwise
770          */
771         private String validateAlertConfigs( )
772         {
773             return validateAlertConfigs( buildModelForValidation( ) );
774         }
775 
776         /**
777          * Validates the alert configurations with the specified model
778          *
779          * @param model
780          *            the model
781          * @return the URL of the error page if there is a validation error, {@code null} otherwise
782          */
783         private String validateAlertConfigs( Map<String, Object> model )
784         {
785             String strErrorUrl = null;
786 
787             for ( IAlertConfig alertConfig : filterActiveAlertConfigs( ) )
788             {
789                 strErrorUrl = alertConfig.getValidator( ).validate( model );
790 
791                 if ( !StringUtils.isEmpty( strErrorUrl ) )
792                 {
793                     break;
794                 }
795             }
796 
797             return strErrorUrl;
798         }
799 
800         /**
801          * Filters the active alert configurations
802          *
803          * @return the list of active alert configurations
804          */
805         private List<IAlertConfig> filterActiveAlertConfigs( )
806         {
807             List<IAlertConfig> listActiveAlertConfigs = new ArrayList<>( );
808 
809             for ( IAlertConfig alertConfig : _listAlertConfig )
810             {
811                 if ( alertConfig.isActive( ) )
812                 {
813                     listActiveAlertConfigs.add( alertConfig );
814                 }
815             }
816 
817             return listActiveAlertConfigs;
818         }
819 
820         /**
821          * Builds the URL for missing field
822          *
823          * @return the URL
824          */
825         private String buildUrlForMissingField( String errMessage )
826         {
827             return AdminMessageService.getMessageUrl( _request, errMessage, AdminMessage.TYPE_STOP );
828         }
829 
830         /**
831          * Builds the URL when there is no alert configuration
832          *
833          * @return the URL
834          */
835         private String buildUrlForNoAlertConfig( )
836         {
837             return AdminMessageService.getMessageUrl( _request, Constants.MESSAGE_MANDATORY_ONGLET, AdminMessage.TYPE_STOP );
838         }
839 
840         /**
841          * Builds the model used to validate the task
842          *
843          * @return the model
844          */
845         private Map<String, Object> buildModelForValidation( )
846         {
847             String strProviderId = ProviderManagerUtil.fetchProviderId( _config.getIdSpringProvider( ) );
848 
849             return markersToModel( _providerManager.getProviderDescription( strProviderId ).getMarkerDescriptions( ) );
850         }
851 
852         /**
853          * Converts the specified collection of AlertGru markers into a model
854          *
855          * @param collectionAlertMarkers
856          *            the collection to convert
857          * @return the model
858          */
859         private Map<String, Object> markersToModel( Collection<InfoMarker> collectionAlertMarkers )
860         {
861             Map<String, Object> model = new HashMap<>( );
862 
863             for ( InfoMarker alertMarker : collectionAlertMarkers )
864             {
865                 model.put( alertMarker.getMarker( ), alertMarker.getDescription( ) );
866             }
867 
868             return model;
869         }
870 
871         /**
872          * Fills the configuration with the global configuration of the task
873          */
874         private void fillConfigWithGlobalConfig( )
875         {
876             fillConfigWithProvider( );
877             fillConfigWithMarkerProviders( );
878             fillConfigWithDemandStatus( );
879             fillConfigWithAlertInfos( );
880             fillConfigWithMarkerAlert( );
881         }
882 
883         /**
884          * Fills the configuration with the provider
885          */
886         private void fillConfigWithProvider( )
887         {
888             _config.setIdSpringProvider( _request.getParameter( Constants.PARAMETER_SELECT_PROVIDER ) );
889         }
890 
891         /**
892          * Fills the configuration with the marker providers
893          */
894         private void fillConfigWithMarkerProviders( )
895         {
896             String [ ] listMarkerProviders = _request.getParameterValues( Constants.PARAMETER_MARKER_PROVIDERS );
897 
898             if ( listMarkerProviders != null )
899             {
900                 _config.setMarkerProviders( Arrays.asList( listMarkerProviders ) );
901             }
902             else
903             {
904                 _config.setMarkerProviders( null );
905             }
906         }
907 
908         /**
909          * Fills the configuration with the demand status
910          */
911         private void fillConfigWithDemandStatus( )
912         {
913             int nDemandStatus = ( VALUE_CHECKBOX.equals( _request.getParameter( Constants.PARAMETER_DEMAND_STATUS ) ) ) ? 1 : 0;
914 
915             _config.setDemandStatus( nDemandStatus );
916         }
917 
918         /**
919          * Fills the configuration with alert information
920          */
921         private void fillConfigWithAlertInfos( )
922         {
923             _config.setDaysToAlert( StringUtils.isNotBlank( _request.getParameter( Constants.PARAMETER_DAYS_TO_ALERT ) )
924                     ? Integer.parseInt( _request.getParameter( Constants.PARAMETER_DAYS_TO_ALERT ) )
925                     : 0 );
926             _config.setAlertSubject( _request.getParameter( Constants.PARAMETER_SUBJECT_ALERT ) );
927             _config.setIdStateAfter( StringUtils.isNotBlank( _request.getParameter( Constants.PARAMETER_STATE_AFTER ) )
928                     ? Integer.parseInt( _request.getParameter( Constants.PARAMETER_STATE_AFTER ) )
929                     : 0 );
930             _config.setAlertAfterBefore( _request.getParameter( Constants.PARAMETER_ALERT_AFTER_BEFORE ) );
931         }
932 
933         /**
934          * Fills the configuration with the marker
935          */
936         private void fillConfigWithMarkerAlert( )
937         {
938             _config.setMarkerAlert( _request.getParameter( Constants.PARAMETER_MARKER_ALERT ) );
939         }
940 
941         /**
942          * Fills the configuration with the CRM status
943          */
944         private void fillConfigWithCrmStatus( )
945         {
946             String strCrmStatusId = _request.getParameter( Constants.PARAMETER_CRM_STATUS_ID );
947             int nCrmStatusId = ( ( StringUtils.equals( strCrmStatusId, "1" ) ) || ( StringUtils.equals( strCrmStatusId, "0" ) ) )
948                     ? Integer.parseInt( strCrmStatusId )
949                     : 1;
950 
951             _config.setCrmStatusId( nCrmStatusId );
952         }
953 
954         /**
955          * Fills the configuration with the current alert configuration
956          *
957          * @param alertConfig
958          *            the current alert configuration
959          */
960         private void fillConfigWithCurrentAlertConfig( IAlertConfig alertConfig )
961         {
962             int nCurrentTabIndex = ServiceConfigTaskForm.getTabIndex( alertConfig.getName( ) );
963             _config.setSetOnglet( nCurrentTabIndex );
964         }
965 
966         /**
967          * Saves the alert configurations
968          */
969         private void saveAlertConfigs( )
970         {
971             for ( IAlertConfig alertConfig : filterActiveAlertConfigs( ) )
972             {
973                 alertConfig.addConfig( );
974             }
975         }
976 
977         /**
978          * Saves the configuration of the task
979          */
980         private void saveConfig( )
981         {
982             boolean bCreate = false;
983 
984             if ( _config.getIdTask( ) == 0 )
985             {
986                 _config.setIdTask( _task.getId( ) );
987                 bCreate = true;
988             }
989 
990             if ( bCreate )
991             {
992                 _taskAlertGruConfigService.create( _config );
993             }
994             else
995             {
996                 _taskAlertGruConfigService.update( _config );
997             }
998         }
999     }
1000 
1001 }