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.genericalert.web;
35  
36  import java.util.ArrayList;
37  import java.util.HashMap;
38  import java.util.List;
39  import java.util.Locale;
40  import java.util.Map;
41  
42  import javax.inject.Inject;
43  import javax.inject.Named;
44  import javax.servlet.http.HttpServletRequest;
45  
46  import org.apache.commons.lang3.StringUtils;
47  
48  import fr.paris.lutece.plugins.appointment.service.FormService;
49  import fr.paris.lutece.plugins.appointment.service.entrytype.EntryTypePhone;
50  import fr.paris.lutece.plugins.appointment.web.dto.AppointmentFormDTO;
51  import fr.paris.lutece.plugins.genericalert.business.ReminderAppointment;
52  import fr.paris.lutece.plugins.genericalert.business.TaskNotifyReminderConfig;
53  import fr.paris.lutece.plugins.genericalert.business.TaskNotifyReminderConfigHome;
54  import fr.paris.lutece.plugins.genericalert.service.TaskNotifyReminderConfigService;
55  import fr.paris.lutece.plugins.genericattributes.business.Entry;
56  import fr.paris.lutece.plugins.genericattributes.business.EntryFilter;
57  import fr.paris.lutece.plugins.genericattributes.business.EntryHome;
58  import fr.paris.lutece.plugins.genericattributes.service.entrytype.EntryTypeServiceManager;
59  import fr.paris.lutece.plugins.genericattributes.service.entrytype.IEntryTypeService;
60  import fr.paris.lutece.plugins.workflow.web.task.NoFormTaskComponent;
61  import fr.paris.lutece.plugins.workflowcore.business.state.State;
62  import fr.paris.lutece.plugins.workflowcore.business.state.StateFilter;
63  import fr.paris.lutece.plugins.workflowcore.service.config.ITaskConfigService;
64  import fr.paris.lutece.plugins.workflowcore.service.state.IStateService;
65  import fr.paris.lutece.plugins.workflowcore.service.state.StateService;
66  import fr.paris.lutece.plugins.workflowcore.service.task.ITask;
67  import fr.paris.lutece.portal.service.datastore.DatastoreService;
68  import fr.paris.lutece.portal.service.message.AdminMessage;
69  import fr.paris.lutece.portal.service.message.AdminMessageService;
70  import fr.paris.lutece.portal.service.template.AppTemplateService;
71  import fr.paris.lutece.portal.service.util.AppPathService;
72  import fr.paris.lutece.portal.service.util.AppPropertiesService;
73  import fr.paris.lutece.util.html.HtmlTemplate;
74  import fr.paris.lutece.util.url.UrlItem;
75  
76  /**
77   *
78   * NotificationTaskComponent
79   *
80   */
81  public class NotifyReminderTaskComponent extends NoFormTaskComponent
82  {
83      // TEMPLATES
84      private static final String TEMPLATE_TASK_NOTIFY_REMINDER_CONFIG = "admin/plugins/workflow/modules/notifyreminder/task_notifyreminder_config.html";
85      // Parameters
86      private static final String PARAMETER_ID_FORM = "id_form";
87      private static final String PARAMETER_NB_ALERT = "nbAlerts";
88      private static final String PARAMETER_ID_TASK = "id_task";
89      private static final String PARAMETER_APPLY_NB_ALERTS = "apply_nbAlerts";
90      private static final String PARAMETER_APPLY_ID_FORM = "apply_idForm";
91      // MARKS
92      private static final String MARK_CONFIG = "config";
93      private static final String MARK_WEBAPP_URL = "webapp_url";
94  
95      private static final String MARK_LOCALE = "language";
96      private static final String MARK_LOCALE_TINY = "locale";
97      private static final String MARK_FALSE = "false";
98      private static final String MARK_LIST_FORM = "listForms";
99      private static final String MARK_LIST_PHONE_NUMBERS = "listTel";
100     private static final String MARK_LIST_STATUS_WORKFLOW = "listStates";
101     private static final String MARK_TIME_ALERT = "timeToAlert_";
102     private static final String MARK_EMAIL_NOTIFY = "emailNotify_";
103     private static final String MARK_SMS_NOTIFY = "smsNotify_";
104     private static final String MARK_EMAIL_ALERT_MESSAGE = "email_textMessage_";
105     private static final String MARK_SMS_ALERT_MESSAGE = "sms_textMessage_";
106     private static final String MARK_ALERT_SUBJECT = "alert_subject_";
107     private static final String MARK_NUMBER_PHONE = "tel_";
108     private static final String MARK_EMAIL_CC = "emailCc_";
109     private static final String MARK_STATUS_WORKFLOW = "state_";
110     private static final String MARK_SMS_TEXT_LENGTH = "sms_maxlength";
111     // JSP
112     private static final String JSP_MODIFY_TASK = "jsp/admin/plugins/workflow/ModifyTask.jsp";
113     // Errors
114     private static final String MESSAGE_ERROR_SUBJECT_EMPTY = "genericalert.message.error.subjectIsEmpty";
115     private static final String MESSAGE_ERROR_STATUS_EMPTY = "genericalert.message.error.statusIsEmpty";
116     private static final String MESSAGE_ERROR_ALERT_TIME_NO_VALID = "genericalert.message.error.alerttimeNoValid";
117     private static final String MESSAGE_ERROR_NB_ALERT_NO_VALID = "genericalert.message.error.nbAlertsNoValid";
118     private static final String MESSAGE_ERROR_NOTIFY_TYPE_EMPTY = "genericalert.message.error.notifyTypeEmpty";
119     private static final String MESSAGE_ERROR_EMAIL_TEXT_EMPTY = "genericalert.message.error.notifyEmailTextEmpty";
120     private static final String MESSAGE_ERROR_SMS_TEXT_EMPTY = "genericalert.message.error.notifySmsTextEmpty";
121     private static final String MESSAGE_ERROR_NUMBER_PHONE_EMPTY = "genericalert.message.error.numberPhoneEmpty";
122 
123     // properties
124     private static final String PROPERTY_MAX_LENGTH_SMS_TEXT = "genericalert.maxLength.textSms";
125     // private IStateService _stateService = SpringContextService.getBean(
126     // StateService.BEAN_SERVICE );
127 
128     @Inject
129     @Named( StateService.BEAN_SERVICE )
130     private IStateService _stateService;
131 
132     @Inject
133     @Named( TaskNotifyReminderConfigService.BEAN_SERVICE )
134     private ITaskConfigService _taskNotifyReminderConfigService;
135 
136     @Override
137     public String getDisplayTaskForm( int nIdResource, String strResourceType, HttpServletRequest request, Locale locale, ITask task )
138     {
139         return null;
140     }
141 
142     @Override
143     public String getDisplayConfigForm( HttpServletRequest request, Locale locale, ITask task )
144     {
145 
146         String strIdForm = request.getParameter( PARAMETER_ID_FORM ) == null ? StringUtils.EMPTY : request.getParameter( PARAMETER_ID_FORM );
147         int nIdForm = 0;
148         TaskNotifyReminderConfig config = null;
149         if ( StringUtils.isNotEmpty( strIdForm ) && StringUtils.isNumeric( strIdForm ) )
150         {
151             nIdForm = Integer.parseInt( strIdForm );
152         }
153         if ( StringUtils.isNotEmpty( strIdForm ) )
154         {
155             config = TaskNotifyReminderConfigHome.findByIdForm( task.getId( ), nIdForm );
156         }
157 
158         List<AppointmentFormDTO> listForms = FormService.buildAllActiveAppointmentForm( );
159         List<String> listTel = getListPhoneEntries( nIdForm );
160         List<State> listStates = null;
161 
162         AppointmentFormDTO tmpForm = FormService.buildAppointmentFormLight( nIdForm );
163         if ( tmpForm != null )
164         {
165             StateFilter stateFilter = new StateFilter( );
166             stateFilter.setIdWorkflow( tmpForm.getIdWorkflow( ) );
167             listStates = _stateService.getListStateByFilter( stateFilter );
168         }
169         // initialiser la configuration
170         if ( config == null )
171         {
172             config = new TaskNotifyReminderConfig( );
173             config.setIdTask( task.getId( ) );
174             config.setListReminderAppointment( new ArrayList<ReminderAppointment>( ) );
175         }
176 
177         String strMaxLenght = DatastoreService.getDataValue( PROPERTY_MAX_LENGTH_SMS_TEXT, StringUtils.EMPTY );
178 
179         if ( !DatastoreService.existsKey( PROPERTY_MAX_LENGTH_SMS_TEXT ) )
180         {
181             DatastoreService.setDataValue( PROPERTY_MAX_LENGTH_SMS_TEXT, AppPropertiesService.getProperty( PROPERTY_MAX_LENGTH_SMS_TEXT ) );
182         }
183 
184         Map<String, Object> model = new HashMap<>( );
185         model.put( MARK_CONFIG, config );
186         model.put( MARK_LIST_FORM, listForms );
187         model.put( MARK_LIST_PHONE_NUMBERS, listTel );
188         model.put( MARK_LIST_STATUS_WORKFLOW, listStates );
189         model.put( MARK_WEBAPP_URL, AppPathService.getBaseUrl( request ) );
190         model.put( MARK_LOCALE, locale );
191         model.put( MARK_LOCALE_TINY, locale );
192         model.put( MARK_SMS_TEXT_LENGTH, strMaxLenght );
193 
194         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_TASK_NOTIFY_REMINDER_CONFIG, locale, model );
195 
196         return template.getHtml( );
197     }
198 
199     /**
200      * {@inheritDoc}
201      */
202     @Override
203     public String doSaveConfig( HttpServletRequest request, Locale locale, ITask task )
204     {
205         String strIdForm = request.getParameter( PARAMETER_ID_FORM );
206         String strNbAlerts = request.getParameter( PARAMETER_NB_ALERT );
207         String strApplyNbAlerts = request.getParameter( PARAMETER_APPLY_NB_ALERTS );
208         String strForm = request.getParameter( PARAMETER_APPLY_ID_FORM );
209 
210         List<ReminderAppointment> listAppointment = new ArrayList<>( );
211 
212         Boolean bCreate = false;
213 
214         int nIdForm = Integer.parseInt( strIdForm );
215         UrlItem url = new UrlItem( AppPathService.getBaseUrl( request ) + JSP_MODIFY_TASK );
216         url.addParameter( PARAMETER_ID_TASK, task.getId( ) );
217         url.addParameter( PARAMETER_ID_FORM, nIdForm );
218         int nbAlerts = 0;
219 
220         if ( strNbAlerts != null )
221         {
222             if ( StringUtils.isNotEmpty( strNbAlerts ) && StringUtils.isNumeric( strNbAlerts ) )
223             {
224                 nbAlerts = Integer.parseInt( strNbAlerts );
225             }
226             else
227             {
228                 return AdminMessageService.getMessageUrl( request, MESSAGE_ERROR_NB_ALERT_NO_VALID, AdminMessage.TYPE_STOP );
229             }
230         }
231 
232         TaskNotifyReminderConfig config = TaskNotifyReminderConfigHome.findByIdForm( task.getId( ), nIdForm );
233 
234         if ( config == null )
235         {
236             config = new TaskNotifyReminderConfig( );
237             config.setIdTask( task.getId( ) );
238             config.setIdForm( nIdForm );
239             config.setListReminderAppointment( new ArrayList<ReminderAppointment>( ) );
240             bCreate = true;
241         }
242         else
243         {
244 
245             List<ReminderAppointment> listReminder = config.getListReminderAppointment( );
246 
247             if ( nbAlerts == 0 && strForm == null )
248             {
249                 TaskNotifyReminderConfigHome.removeAppointmentReminder( config.getIdTask( ), nIdForm, nbAlerts, true );
250                 listReminder.clear( );
251                 config.setListReminderAppointment( listReminder );
252             }
253 
254             if ( nbAlerts < listReminder.size( ) && nbAlerts != 0 && strForm == null )
255             {
256                 for ( int i = nbAlerts + 1; i <= listReminder.size( ); i++ )
257                 {
258 
259                     TaskNotifyReminderConfigHome.removeAppointmentReminder( config.getIdTask( ), nIdForm, i, false );
260                     listReminder.remove( i - 1 );
261                 }
262                 config.setListReminderAppointment( listReminder );
263             }
264             if ( nbAlerts != 0 && strApplyNbAlerts == null && strForm == null )
265             {
266                 for ( int i = 1; i <= nbAlerts; i++ )
267                 {
268                     ReminderAppointmentminderAppointment.html#ReminderAppointment">ReminderAppointment reminderAppointment = new ReminderAppointment( );
269                     String strTimeToAlert = request.getParameter( MARK_TIME_ALERT + i );
270                     String strEmailNotify = request.getParameter( MARK_EMAIL_NOTIFY + i ) == null ? MARK_FALSE : request.getParameter( MARK_EMAIL_NOTIFY + i );
271                     String strSmsNotify = request.getParameter( MARK_SMS_NOTIFY + i ) == null ? MARK_FALSE : request.getParameter( MARK_SMS_NOTIFY + i );
272                     String strEmailAlertMessage = request.getParameter( MARK_EMAIL_ALERT_MESSAGE + i ) == null ? StringUtils.EMPTY
273                             : request.getParameter( MARK_EMAIL_ALERT_MESSAGE + i );
274                     String strSmsAlertMessage = request.getParameter( MARK_SMS_ALERT_MESSAGE + i ) == null ? StringUtils.EMPTY
275                             : request.getParameter( MARK_SMS_ALERT_MESSAGE + i );
276                     String strAlertSubject = request.getParameter( MARK_ALERT_SUBJECT + i ) == null ? StringUtils.EMPTY
277                             : request.getParameter( MARK_ALERT_SUBJECT + i );
278                     String strPhoneNumber = request.getParameter( MARK_NUMBER_PHONE + i ) == null ? StringUtils.EMPTY
279                             : request.getParameter( MARK_NUMBER_PHONE + i );
280                     String strEmailCc = request.getParameter( MARK_EMAIL_CC + i ) == null ? StringUtils.EMPTY : request.getParameter( MARK_EMAIL_CC + i );
281                     String strIdSateAfter = request.getParameter( MARK_STATUS_WORKFLOW + i ) == null ? StringUtils.EMPTY
282                             : request.getParameter( MARK_STATUS_WORKFLOW + i );
283 
284                     if ( StringUtils.isEmpty( strTimeToAlert ) || !StringUtils.isNumeric( strTimeToAlert ) )
285                     {
286                         if ( strApplyNbAlerts == null && strForm == null )
287                         {
288                             return AdminMessageService.getMessageUrl( request, MESSAGE_ERROR_ALERT_TIME_NO_VALID, AdminMessage.TYPE_STOP );
289                         }
290                     }
291                     if ( strSmsNotify.equals( MARK_FALSE ) && strEmailNotify.equals( MARK_FALSE ) )
292                     {
293                         if ( strApplyNbAlerts == null && strForm == null )
294                         {
295                             return AdminMessageService.getMessageUrl( request, MESSAGE_ERROR_NOTIFY_TYPE_EMPTY, AdminMessage.TYPE_STOP );
296                         }
297                     }
298                     if ( !strSmsNotify.equals( MARK_FALSE ) && StringUtils.isEmpty( strPhoneNumber ) )
299                     {
300                         if ( strApplyNbAlerts == null && strForm == null )
301                         {
302                             return AdminMessageService.getMessageUrl( request, MESSAGE_ERROR_NUMBER_PHONE_EMPTY, AdminMessage.TYPE_STOP );
303                         }
304                     }
305                     if ( StringUtils.isEmpty( strIdSateAfter ) )
306                     {
307                         if ( strApplyNbAlerts == null && strForm == null )
308                         {
309                             return AdminMessageService.getMessageUrl( request, MESSAGE_ERROR_STATUS_EMPTY, AdminMessage.TYPE_STOP );
310                         }
311                     }
312 
313                     if ( StringUtils.isEmpty( strAlertSubject ) )
314                     {
315                         if ( strApplyNbAlerts == null && strForm == null )
316                         {
317                             return AdminMessageService.getMessageUrl( request, MESSAGE_ERROR_SUBJECT_EMPTY, AdminMessage.TYPE_STOP );
318                         }
319                     }
320 
321                     if ( StringUtils.isEmpty( strEmailAlertMessage ) && Boolean.parseBoolean( strEmailNotify ) )
322                     {
323                         if ( strApplyNbAlerts == null && strForm == null )
324                         {
325                             return AdminMessageService.getMessageUrl( request, MESSAGE_ERROR_EMAIL_TEXT_EMPTY, AdminMessage.TYPE_STOP );
326                         }
327                     }
328                     if ( StringUtils.isEmpty( strSmsAlertMessage ) && Boolean.parseBoolean( strSmsNotify ) )
329                     {
330                         if ( strApplyNbAlerts == null && strForm == null )
331                         {
332                             return AdminMessageService.getMessageUrl( request, MESSAGE_ERROR_SMS_TEXT_EMPTY, AdminMessage.TYPE_STOP );
333                         }
334                     }
335 
336                     else
337                     {
338                         reminderAppointment.setIdTask( config.getIdTask( ) );
339                         reminderAppointment.setIdForm( nIdForm );
340                         reminderAppointment.setRank( i );
341                         reminderAppointment.setTimeToAlert( Integer.parseInt( strTimeToAlert ) );
342                         reminderAppointment.setEmailNotify( Boolean.parseBoolean( strEmailNotify ) );
343                         reminderAppointment.setSmsNotify( Boolean.parseBoolean( strSmsNotify ) );
344                         reminderAppointment.setEmailAlertMessage( strEmailAlertMessage );
345                         reminderAppointment.setSmsAlertMessage( strSmsAlertMessage );
346                         reminderAppointment.setAlertSubject( strAlertSubject );
347                         reminderAppointment.setNumberPhone( strPhoneNumber );
348                         reminderAppointment.setEmailCc( strEmailCc );
349                         reminderAppointment.setIdStateAfter( Integer.parseInt( strIdSateAfter ) );
350                         listAppointment.add( reminderAppointment );
351                     }
352                 }
353             }
354 
355             config.setIdForm( nIdForm );
356         }
357 
358         if ( strForm == null )
359         {
360             if ( listAppointment.size( ) > 0 )
361             {
362                 config.setListReminderAppointment( listAppointment );
363             }
364             config.setNbAlerts( nbAlerts );
365 
366             if ( bCreate )
367             {
368                 _taskNotifyReminderConfigService.create( config );
369             }
370             else
371             {
372                 _taskNotifyReminderConfigService.update( config );
373             }
374         }
375 
376         if ( strApplyNbAlerts != null || strForm != null )
377         {
378             return AppPathService.getBaseUrl( request ) + JSP_MODIFY_TASK + "?" + PARAMETER_ID_TASK + "=" + task.getId( ) + "&" + PARAMETER_ID_FORM + "="
379                     + nIdForm;
380         }
381         return null;
382     }
383 
384     /**
385      * Get list Phone Entrie
386      * 
387      * @param idForm
388      *            the id form
389      * @return list getListPhoneEntries
390      */
391     private List<String> getListPhoneEntries( int idForm )
392     {
393         List<String> listPhoneNumber = new ArrayList<>( );
394 
395         EntryFilter entryFilter = new EntryFilter( );
396         entryFilter.setIdResource( idForm );
397         entryFilter.setResourceType( AppointmentFormDTO.RESOURCE_TYPE );
398         entryFilter.setFieldDependNull( EntryFilter.FILTER_TRUE );
399         List<Entry> listEntries = EntryHome.getEntryList( entryFilter );
400         for ( Entry entry : listEntries )
401         {
402             IEntryTypeService entryTypeService = EntryTypeServiceManager.getEntryTypeService( entry );
403 
404             if ( entryTypeService instanceof EntryTypePhone )
405             {
406                 if ( entry.getTitle( ) != null )
407                 {
408                     listPhoneNumber.add( entry.getTitle( ) );
409                 }
410             }
411         }
412         return listPhoneNumber;
413     }
414 
415     @Override
416     public String getDisplayTaskInformation( int nIdHistory, HttpServletRequest request, Locale locale, ITask task )
417     {
418         return null;
419     }
420 
421     public String getTaskInformationXml( int nIdHistory, HttpServletRequest request, Locale locale, ITask task )
422     {
423         return null;
424     }
425 
426     @Override
427     public String doValidateTask( int nIdResource, String strResourceType, HttpServletRequest request, Locale locale, ITask task )
428     {
429         return null;
430     }
431 
432 }