View Javadoc
1   /*
2    * Copyright (c) 2002-2018, 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.appointment.web;
35  
36  import java.util.HashMap;
37  import java.util.List;
38  import java.util.Map;
39  
40  import javax.servlet.http.HttpServletRequest;
41  
42  import org.apache.commons.lang.StringUtils;
43  
44  import fr.paris.lutece.plugins.appointment.service.EntryTypeService;
45  import fr.paris.lutece.plugins.genericattributes.business.Entry;
46  import fr.paris.lutece.plugins.genericattributes.business.EntryHome;
47  import fr.paris.lutece.plugins.genericattributes.business.Field;
48  import fr.paris.lutece.plugins.genericattributes.business.FieldHome;
49  import fr.paris.lutece.portal.service.i18n.I18nService;
50  import fr.paris.lutece.portal.service.message.AdminMessage;
51  import fr.paris.lutece.portal.service.message.AdminMessageService;
52  import fr.paris.lutece.portal.service.util.AppPathService;
53  import fr.paris.lutece.portal.util.mvc.admin.MVCAdminJspBean;
54  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
55  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
56  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
57  import fr.paris.lutece.portal.util.mvc.utils.MVCUtils;
58  import fr.paris.lutece.util.string.StringUtil;
59  import fr.paris.lutece.util.url.UrlItem;
60  
61  /**
62   * JspBean to manage appointment form fieldsl
63   * 
64   * @author Laurent Payen
65   *
66   */
67  @Controller( controllerJsp = "ManageAppointmentFormFields.jsp", controllerPath = "jsp/admin/plugins/appointment/", right = AppointmentFormJspBean.RIGHT_MANAGEAPPOINTMENTFORM )
68  public class AppointmentFormFieldJspBean extends MVCAdminJspBean
69  {
70      private static final long serialVersionUID = -1505164256619633838L;
71  
72      // Properties
73      private static final String PROPERTY_CREATE_FIELD_TITLE = "appointment.createField.title";
74      private static final String PROPERTY_MODIFY_FIELD_TITLE = "appointment.modifyField.title";
75  
76      // Urls
77      private static final String JSP_URL_MANAGE_APPOINTMENT_FORM_FIELDS = "jsp/admin/plugins/appointment/ManageAppointmentFormFields.jsp";
78  
79      // Marks
80      private static final String MARK_FIELD = "field";
81      private static final String MARK_ENTRY_LIST = "entry_list";
82      private static final String MARK_ENTRY_TYPE_LIST = "entry_type_list";
83  
84      // Messages
85      private static final String MESSAGE_CONFIRM_REMOVE_FIELD = "appointment.message.confirmRemoveField";
86      private static final String MESSAGE_MANDATORY_FIELD = "portal.util.message.mandatoryField";
87      private static final String MESSAGE_FIELD_VALUE_FIELD = "appointment.message.error.fieldValue";
88  
89      // Views
90      private static final String VIEW_GET_CREATE_FIELD = "getCreateField";
91      private static final String VIEW_GET_MODIFY_FIELD = "getModifyField";
92      private static final String VIEW_GET_MODIFY_FIELD_WITH_CONDITIONAL_QUESTIONS = "getModifyFieldCC";
93      private static final String VIEW_GET_CONFIRM_REMOVE_FIELD = "getConfirmRemoveField";
94  
95      // Actions
96      private static final String ACTION_DO_CREATE_FIELD = "doCreateField";
97      private static final String ACTION_DO_MODIFY_FIELD = "doModifyField";
98      private static final String ACTION_DO_MODIFY_FIELD_WITH_CONDITIONAL_QUESTIONS = "doModifyFieldCC";
99      private static final String ACTION_DO_MOVE_FIELD_UP = "doMoveFieldUp";
100     private static final String ACTION_DO_MOVE_FIELD_DOWN = "doMoveFieldDown";
101     private static final String ACTION_DO_REMOVE_FIELD = "doRemoveField";
102 
103     // Parameters
104     private static final String PARAMETER_ID_ENTRY = "id_entry";
105     private static final String PARAMETER_ID_FIELD = "id_field";
106     private static final String PARAMETER_CANCEL = "cancel";
107     private static final String PARAMETER_APPLY = "apply";
108     private static final String PARAMETER_TITLE = "title";
109     private static final String PARAMETER_VALUE = "value";
110     private static final String PARAMETER_DEFAULT_VALUE = "default_value";
111     private static final String PARAMETER_NO_DISPLAY_TITLE = "no_display_title";
112     private static final String PARAMETER_COMMENT = "comment";
113     private static final String FIELD_TITLE_FIELD = "appointment.labelTitle";
114     private static final String FIELD_VALUE_FIELD = "appointment.value.name";
115 
116     // Templates
117     private static final String TEMPLATE_CREATE_FIELD = "admin/plugins/appointment/create_field.html";
118     private static final String TEMPLATE_MODIFY_FIELD_WITH_CONDITIONAL_QUESTION = "admin/plugins/appointment/modify_field_with_conditional_question.html";
119     private static final String TEMPLATE_MODIFY_FIELD = "admin/plugins/appointment/modify_field.html";
120 
121     /**
122      * Gets the field creation page
123      * 
124      * @param request
125      *            The HTTP request
126      * @return the field creation page
127      */
128     @View( VIEW_GET_CREATE_FIELD )
129     public String getCreateField( HttpServletRequest request )
130     {
131         Entry entry = EntryHome.findByPrimaryKey( Integer.parseInt( request.getParameter( PARAMETER_ID_ENTRY ) ) );
132         Field field = new Field( );
133         field.setParentEntry( entry );
134 
135         Map<String, Object> model = new HashMap<String, Object>( );
136         model.put( MARK_FIELD, field );
137 
138         return getPage( PROPERTY_CREATE_FIELD_TITLE, TEMPLATE_CREATE_FIELD, model );
139     }
140 
141     /**
142      * Get the page to modify a field without displaying its conditional questions
143      * 
144      * @param request
145      *            The request
146      * @return The HTML content to display, or the next URL to redirect to
147      */
148     @View( VIEW_GET_MODIFY_FIELD )
149     public String getModifyField( HttpServletRequest request )
150     {
151         return getModifyField( request, false );
152     }
153 
154     /**
155      * Get the page to modify a field with its conditional questions
156      * 
157      * @param request
158      *            The request
159      * @return The HTML content to display, or the next URL to redirect to
160      */
161     @View( VIEW_GET_MODIFY_FIELD_WITH_CONDITIONAL_QUESTIONS )
162     public String getModifyFieldWithConditionalQuestions( HttpServletRequest request )
163     {
164         return getModifyField( request, true );
165     }
166 
167     /**
168      * Gets the field modification page
169      * 
170      * @param request
171      *            The HTTP request
172      * @param bWithConditionalQuestion
173      *            true if the field is associate to conditionals questions
174      * @return the field modification page
175      */
176     private String getModifyField( HttpServletRequest request, boolean bWithConditionalQuestion )
177     {
178         if ( StringUtils.isEmpty( request.getParameter( PARAMETER_ID_FIELD ) ) || !StringUtils.isNumeric( request.getParameter( PARAMETER_ID_FIELD ) ) )
179         {
180             return redirect( request, AppointmentFormJspBean.getURLManageAppointmentForms( request ) );
181         }
182 
183         int nIdField = Integer.parseInt( request.getParameter( PARAMETER_ID_FIELD ) );
184         Field field = FieldHome.findByPrimaryKey( nIdField );
185         Entry entry = EntryHome.findByPrimaryKey( field.getParentEntry( ).getIdEntry( ) );
186 
187         field.setParentEntry( entry );
188 
189         HashMap<String, Object> model = new HashMap<String, Object>( );
190         model.put( MARK_FIELD, field );
191 
192         String strTemplateName;
193 
194         if ( bWithConditionalQuestion )
195         {
196             model.put( MARK_ENTRY_TYPE_LIST, EntryTypeService.getInstance( ).getEntryTypeReferenceList( ) );
197             model.put( MARK_ENTRY_LIST, field.getConditionalQuestions( ) );
198             strTemplateName = TEMPLATE_MODIFY_FIELD_WITH_CONDITIONAL_QUESTION;
199         }
200         else
201         {
202             strTemplateName = TEMPLATE_MODIFY_FIELD;
203         }
204 
205         return getPage( PROPERTY_MODIFY_FIELD_TITLE, strTemplateName, model );
206     }
207 
208     /**
209      * Perform creation field
210      * 
211      * @param request
212      *            The HTTP request
213      * @return The URL to go after performing the action
214      */
215     @Action( ACTION_DO_CREATE_FIELD )
216     public String doCreateField( HttpServletRequest request )
217     {
218         if ( StringUtils.isEmpty( request.getParameter( PARAMETER_ID_ENTRY ) ) || !StringUtils.isNumeric( request.getParameter( PARAMETER_ID_ENTRY ) ) )
219         {
220             return redirect( request, AppointmentFormJspBean.getURLManageAppointmentForms( request ) );
221         }
222 
223         int nIdEntry = Integer.parseInt( request.getParameter( PARAMETER_ID_ENTRY ) );
224 
225         if ( request.getParameter( PARAMETER_CANCEL ) == null )
226         {
227             Entry entry = new Entry( );
228             entry.setIdEntry( nIdEntry );
229 
230             Field field = new Field( );
231             field.setParentEntry( entry );
232 
233             String strError = getFieldData( request, field );
234 
235             if ( strError != null )
236             {
237                 return redirect( request, strError );
238             }
239 
240             FieldHome.create( field );
241         }
242 
243         return redirect( request, AppointmentFormEntryJspBean.getURLModifyEntry( request, nIdEntry ) );
244     }
245 
246     /**
247      * Perform modification field
248      * 
249      * @param request
250      *            The HTTP request
251      * @return The URL to go after performing the action
252      */
253     @Action( ACTION_DO_MODIFY_FIELD )
254     public String doModifyField( HttpServletRequest request )
255     {
256         return doModifyField( request, false );
257     }
258 
259     /**
260      * Perform modification field
261      * 
262      * @param request
263      *            The HTTP request
264      * @return The URL to go after performing the action
265      */
266     @Action( ACTION_DO_MODIFY_FIELD_WITH_CONDITIONAL_QUESTIONS )
267     public String doModifyFieldWithConditionalQuestions( HttpServletRequest request )
268     {
269         return doModifyField( request, true );
270     }
271 
272     /**
273      * Perform modification field
274      * 
275      * @param request
276      *            The HTTP request
277      * @param bWithConditionalQuestion
278      *            True if the field to modify accepts conditional questions
279      * @return The URL to go after performing the action
280      */
281 
282     // @Action( ACTION_DO_MODIFY_FIELD )
283     private String doModifyField( HttpServletRequest request, boolean bWithConditionalQuestion )
284     {
285         String strIdField = request.getParameter( PARAMETER_ID_FIELD );
286 
287         if ( StringUtils.isEmpty( strIdField ) || !StringUtils.isNumeric( strIdField ) )
288         {
289             return redirect( request, AppointmentFormJspBean.getURLManageAppointmentForms( request ) );
290         }
291 
292         Field field = null;
293         int nIdField = Integer.parseInt( strIdField );
294 
295         field = FieldHome.findByPrimaryKey( nIdField );
296 
297         if ( request.getParameter( PARAMETER_CANCEL ) == null )
298         {
299             String strError = getFieldData( request, field );
300 
301             if ( strError != null )
302             {
303                 return redirect( request, strError );
304             }
305 
306             FieldHome.update( field );
307         }
308 
309         if ( request.getParameter( PARAMETER_APPLY ) == null )
310         {
311             return redirect( request, AppointmentFormEntryJspBean.getURLModifyEntry( request, field.getParentEntry( ).getIdEntry( ) ) );
312         }
313 
314         return redirect( request, bWithConditionalQuestion ? VIEW_GET_MODIFY_FIELD_WITH_CONDITIONAL_QUESTIONS : VIEW_GET_MODIFY_FIELD, PARAMETER_ID_FIELD,
315                 nIdField );
316     }
317 
318     /**
319      * Gets the confirmation page before deleting a field
320      * 
321      * @param request
322      *            The HTTP request
323      * @return the confirmation page before deleting a field
324      */
325     @View( VIEW_GET_CONFIRM_REMOVE_FIELD )
326     public String getConfirmRemoveField( HttpServletRequest request )
327     {
328         String strIdField = request.getParameter( PARAMETER_ID_FIELD );
329 
330         if ( StringUtils.isEmpty( strIdField ) || !StringUtils.isNumeric( strIdField ) )
331         {
332             return redirect( request, AppointmentFormJspBean.getURLManageAppointmentForms( request ) );
333         }
334 
335         UrlItem url = new UrlItem( JSP_URL_MANAGE_APPOINTMENT_FORM_FIELDS );
336         url.addParameter( MVCUtils.PARAMETER_ACTION, ACTION_DO_REMOVE_FIELD );
337         url.addParameter( PARAMETER_ID_FIELD, strIdField );
338 
339         return redirect( request, AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_FIELD, url.getUrl( ), AdminMessage.TYPE_CONFIRMATION ) );
340     }
341 
342     /**
343      * Perform the suppression of a field
344      * 
345      * @param request
346      *            The HTTP request
347      * @return The URL to go after performing the action
348      */
349     @Action( ACTION_DO_REMOVE_FIELD )
350     public String doRemoveField( HttpServletRequest request )
351     {
352         String strIdField = request.getParameter( PARAMETER_ID_FIELD );
353 
354         if ( StringUtils.isEmpty( strIdField ) || !StringUtils.isNumeric( strIdField ) )
355         {
356             return redirect( request, AppointmentFormJspBean.getURLManageAppointmentForms( request ) );
357         }
358 
359         int nIdField = Integer.parseInt( strIdField );
360 
361         if ( nIdField != -1 )
362         {
363             Field field = FieldHome.findByPrimaryKey( nIdField );
364 
365             if ( field != null )
366             {
367                 FieldHome.remove( nIdField );
368 
369                 return redirect( request, AppointmentFormEntryJspBean.getURLModifyEntry( request, field.getParentEntry( ).getIdEntry( ) ) );
370             }
371         }
372 
373         return redirect( request, AppointmentFormJspBean.getURLManageAppointmentForms( request ) );
374     }
375 
376     /**
377      * Move a field up
378      * 
379      * @param request
380      *            The request
381      * @return The next URL to redirect to
382      */
383     @Action( ACTION_DO_MOVE_FIELD_UP )
384     public String doMoveFieldUp( HttpServletRequest request )
385     {
386         return doMoveField( request, true );
387     }
388 
389     /**
390      * Move a field up
391      * 
392      * @param request
393      *            The request
394      * @return The next URL to redirect to
395      */
396     @Action( ACTION_DO_MOVE_FIELD_DOWN )
397     public String doMoveFieldDown( HttpServletRequest request )
398     {
399         return doMoveField( request, false );
400     }
401 
402     /**
403      * Move a field up or down
404      * 
405      * @param request
406      *            The request
407      * @param bMoveUp
408      *            True to move the field up, false to move it down
409      * @return The next URL to redirect to
410      */
411     public String doMoveField( HttpServletRequest request, boolean bMoveUp )
412     {
413         String strIdField = request.getParameter( PARAMETER_ID_FIELD );
414 
415         if ( StringUtils.isEmpty( strIdField ) || !StringUtils.isNumeric( strIdField ) )
416         {
417             return redirect( request, AppointmentFormJspBean.getURLManageAppointmentForms( request ) );
418         }
419 
420         int nIdField = Integer.parseInt( strIdField );
421 
422         List<Field> listField;
423         Field field = FieldHome.findByPrimaryKey( nIdField );
424 
425         listField = FieldHome.getFieldListByIdEntry( field.getParentEntry( ).getIdEntry( ) );
426 
427         int nIndexField = getIndexFieldInFieldList( nIdField, listField );
428 
429         int nNewPosition;
430         Field fieldToInversePosition;
431         fieldToInversePosition = listField.get( bMoveUp ? ( nIndexField - 1 ) : ( nIndexField + 1 ) );
432         nNewPosition = fieldToInversePosition.getPosition( );
433         fieldToInversePosition.setPosition( field.getPosition( ) );
434         field.setPosition( nNewPosition );
435         FieldHome.update( field );
436         FieldHome.update( fieldToInversePosition );
437 
438         return redirect( request, AppointmentFormEntryJspBean.getURLModifyEntry( request, field.getParentEntry( ).getIdEntry( ) ) );
439     }
440 
441     /**
442      * Get the request data and if there is no error insert the data in the field specified in parameter. return null if there is no error or else return the
443      * error page URL
444      * 
445      * @param request
446      *            the request
447      * @param field
448      *            field
449      * @return null if there is no error or else return the error page URL
450      */
451     private String getFieldData( HttpServletRequest request, Field field )
452     {
453         String strTitle = request.getParameter( PARAMETER_TITLE );
454         String strValue = request.getParameter( PARAMETER_VALUE );
455         String strDefaultValue = request.getParameter( PARAMETER_DEFAULT_VALUE );
456         String strNoDisplayTitle = request.getParameter( PARAMETER_NO_DISPLAY_TITLE );
457         String strComment = request.getParameter( PARAMETER_COMMENT );
458 
459         String strFieldError = null;
460 
461         if ( StringUtils.isEmpty( strTitle ) )
462         {
463             strFieldError = FIELD_TITLE_FIELD;
464         }
465         else
466             if ( StringUtils.isEmpty( strValue ) )
467             {
468                 strFieldError = FIELD_VALUE_FIELD;
469             }
470             else
471                 if ( !StringUtil.checkCodeKey( strValue ) )
472                 {
473                     return AdminMessageService.getMessageUrl( request, MESSAGE_FIELD_VALUE_FIELD, AdminMessage.TYPE_STOP );
474                 }
475 
476         if ( strFieldError != null )
477         {
478             Object [ ] tabRequiredFields = {
479                 I18nService.getLocalizedString( strFieldError, getLocale( ) )
480             };
481 
482             return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
483         }
484 
485         field.setTitle( strTitle );
486         field.setValue( strValue );
487         field.setComment( strComment );
488 
489         field.setDefaultValue( strDefaultValue != null );
490         field.setNoDisplayTitle( strNoDisplayTitle != null );
491 
492         return null; // No error
493     }
494 
495     /**
496      * Return the index in the list of the field whose key is specified in parameter
497      * 
498      * @param nIdField
499      *            the key of the field
500      * @param listField
501      *            the list of field
502      * @return the index in the list of the field whose key is specified in parameter
503      */
504     private static int getIndexFieldInFieldList( int nIdField, List<Field> listField )
505     {
506         int nIndex = 0;
507 
508         for ( Field field : listField )
509         {
510             if ( field.getIdField( ) == nIdField )
511             {
512                 return nIndex;
513             }
514 
515             nIndex++;
516         }
517 
518         return nIndex;
519     }
520 
521     /**
522      * Get the URL to modify a field. The field is assumed to allow conditional questions.
523      * 
524      * @param request
525      *            The request
526      * @param nIdField
527      *            The id of the field
528      * @return The URL of the page to modify the field
529      */
530     public static String getUrlModifyField( HttpServletRequest request, int nIdField )
531     {
532         UrlItem urlItem = new UrlItem( AppPathService.getBaseUrl( request ) + JSP_URL_MANAGE_APPOINTMENT_FORM_FIELDS );
533         urlItem.addParameter( MVCUtils.PARAMETER_VIEW, VIEW_GET_MODIFY_FIELD_WITH_CONDITIONAL_QUESTIONS );
534         urlItem.addParameter( PARAMETER_ID_FIELD, nIdField );
535 
536         return urlItem.getUrl( );
537     }
538 }