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.appointment.web;
35  
36  import java.util.ArrayList;
37  import java.util.HashMap;
38  import java.util.List;
39  import java.util.Map;
40  
41  import javax.servlet.http.HttpServletRequest;
42  
43  import org.apache.commons.lang3.StringUtils;
44  
45  import fr.paris.lutece.api.user.User;
46  import fr.paris.lutece.plugins.appointment.service.AppointmentResourceIdService;
47  import fr.paris.lutece.plugins.appointment.service.EntryService;
48  import fr.paris.lutece.plugins.appointment.service.EntryTypeService;
49  import fr.paris.lutece.plugins.appointment.service.FormService;
50  import fr.paris.lutece.plugins.appointment.web.dto.AppointmentFormDTO;
51  import fr.paris.lutece.plugins.genericattributes.business.Entry;
52  import fr.paris.lutece.plugins.genericattributes.business.EntryFilter;
53  import fr.paris.lutece.plugins.genericattributes.business.EntryHome;
54  import fr.paris.lutece.plugins.genericattributes.business.EntryType;
55  import fr.paris.lutece.plugins.genericattributes.business.EntryTypeHome;
56  import fr.paris.lutece.plugins.genericattributes.business.Field;
57  import fr.paris.lutece.plugins.genericattributes.business.FieldHome;
58  import fr.paris.lutece.plugins.genericattributes.business.ResponseFilter;
59  import fr.paris.lutece.plugins.genericattributes.business.ResponseHome;
60  import fr.paris.lutece.plugins.genericattributes.service.entrytype.EntryTypeServiceManager;
61  import fr.paris.lutece.plugins.genericattributes.service.entrytype.IEntryTypeService;
62  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
63  import fr.paris.lutece.portal.service.admin.AdminUserService;
64  import fr.paris.lutece.portal.service.i18n.I18nService;
65  import fr.paris.lutece.portal.service.message.AdminMessage;
66  import fr.paris.lutece.portal.service.message.AdminMessageService;
67  import fr.paris.lutece.portal.service.plugin.Plugin;
68  import fr.paris.lutece.portal.service.plugin.PluginService;
69  import fr.paris.lutece.portal.service.rbac.RBACService;
70  import fr.paris.lutece.portal.service.util.AppPathService;
71  import fr.paris.lutece.portal.service.util.AppPropertiesService;
72  import fr.paris.lutece.portal.util.mvc.admin.MVCAdminJspBean;
73  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
74  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
75  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
76  import fr.paris.lutece.portal.util.mvc.utils.MVCUtils;
77  import fr.paris.lutece.util.ReferenceList;
78  import fr.paris.lutece.util.url.UrlItem;
79  import org.apache.commons.lang3.math.NumberUtils;
80  
81  /**
82   * JspBean to manage appointment form entries
83   * 
84   * @author Laurent Payen
85   *
86   */
87  @Controller( controllerJsp = "ManageAppointmentFormEntries.jsp", controllerPath = "jsp/admin/plugins/appointment/", right = AppointmentFormJspBean.RIGHT_MANAGEAPPOINTMENTFORM )
88  public class AppointmentFormEntryJspBean extends MVCAdminJspBean
89  {
90      private static final long serialVersionUID = -4951787792196104967L;
91  
92      // Parameters
93      private static final String PARAMETER_ID_ENTRY_TYPE = "id_type";
94      private static final String PARAMETER_ID_FORM = "id_form";
95      private static final String PARAMETER_ID_FIELD = "id_field";
96      private static final String PARAMETER_ID_ENTRY = "id_entry";
97      private static final String PARAMETER_CANCEL = "cancel";
98      private static final String PARAMETER_APPLY = "apply";
99      private static final String PARAMETER_ORDER_ID = "order_id_";
100     private static final String PARAMETER_ADD_TO_GROUP = "add_to_group";
101     private static final String PARAMETER_ID_ENTRY_GROUP = "id_entry_group";
102     private static final String PARAMETER_ENTRY_ID_MOVE = "entry_id_move";
103     private static final String PARAMETER_ID_EXPRESSION = "id_expression";
104 
105     // Urls
106     private static final String JSP_URL_MANAGE_APPOINTMENT_FORM_ENTRIES = "jsp/admin/plugins/appointment/ManageAppointmentFormEntries.jsp";
107 
108     // Messages
109     private static final String MESSAGE_CONFIRM_REMOVE_ENTRY = "appointment.message.confirmRemoveEntry";
110     private static final String MESSAGE_CANT_REMOVE_ENTRY = "advert.message.cantRemoveEntry";
111     private static final String MESSAGE_CANT_REMOVE_ENTRY_RESOURCES_ATTACHED = "appointment.message.cantRemoveEntry.resourceAttached";
112     private static final String PROPERTY_CREATE_ENTRY_TITLE = "appointment.createEntry.titleQuestion";
113     private static final String PROPERTY_MODIFY_QUESTION_TITLE = "appointment.modifyEntry.titleQuestion";
114     private static final String PROPERTY_COPY_ENTRY_TITLE = "appointment.copyEntry.title";
115     private static final String PROPERTY_MODULE_APPOINTMENT_RESOURCE_NAME = "appointment.moduleAppointmentResource.name";
116 
117     // Views
118     private static final String VIEW_GET_CREATE_ENTRY = "getCreateEntry";
119     private static final String VIEW_GET_MODIFY_ENTRY = "getModifyEntry";
120     private static final String VIEW_CONFIRM_REMOVE_ENTRY = "confirmRemoveEntry";
121     private static final String VIEW_MODIFY_APPOINTMENTFORM_ENTRIES = "modifyAppointmentFormEntries";
122 
123     // Actions
124     private static final String ACTION_DO_CREATE_ENTRY = "doCreateEntry";
125     private static final String ACTION_DO_MODIFY_ENTRY = "doModifyEntry";
126     private static final String ACTION_DO_REMOVE_ENTRY = "doRemoveEntry";
127     private static final String ACTION_DO_COPY_ENTRY = "doCopyEntry";
128     private static final String ACTION_DO_CHANGE_ORDER_ENTRY = "doChangeOrderEntry";
129     private static final String ACTION_DO_MOVE_OUT_ENTRY = "doMoveOutEntry";
130     private static final String ACTION_DO_MOVE_UP_ENTRY_CONDITIONAL = "doMoveUpEntryConditional";
131     private static final String ACTION_DO_MOVE_DOWN_ENTRY_CONDITIONAL = "doMoveDownEntryConditional";
132     private static final String ACTION_DO_REMOVE_REGULAR_EXPRESSION = "doRemoveRegularExpression";
133     private static final String ACTION_DO_INSERT_REGULAR_EXPRESSION = "doInsertRegularExpression";
134     private static final String ACTION_CHANGE_DISABLED = "doChangeDisabled";
135 
136     // Marks
137     private static final String MARK_WEBAPP_URL = "webapp_url";
138     private static final String MARK_LOCALE = "locale";
139     private static final String MARK_REGULAR_EXPRESSION_LIST_REF_LIST = "regular_expression_list";
140     private static final String MARK_ENTRY = "entry";
141     private static final String MARK_LIST = "list";
142     private static final String MARK_FORM = "form";
143     private static final String MARK_ENTRY_TYPE_SERVICE = "entryTypeService";
144     private static final String MARK_ANONYMIZATION_HELP = "anonymization_help_message";
145     private static final String MARK_APPOINTMENT_FORM = "appointmentform";
146     private static final String MARK_APPOINTMENT_RESOURCE_ENABLED = "isResourceInstalled";
147 
148     private static final String PROPERTY_PAGE_TITLE_APPOINTMENT_FORM_ENTRIES = "appointment.modifyAppointmentFormEntries.pageTitle";
149 
150     private static final String TEMPLATE_MODIFY_APPOINTMENT_FORM = "/admin/plugins/appointment/appointmentform/modify_appointmentform_entries.html";
151 
152     // Local variables
153     private EntryService _entryService = EntryService.getService( );
154 
155     /**
156      * Get the view of the entries of the form
157      * 
158      * @param request
159      *            the request
160      * @return the page
161      * @throws AccessDeniedException
162      */
163     @View( VIEW_MODIFY_APPOINTMENTFORM_ENTRIES )
164     public String getModifyAppointmentFormEntries( HttpServletRequest request ) throws AccessDeniedException
165     {
166         String strIdForm = request.getParameter( PARAMETER_ID_FORM );
167         if ( !RBACService.isAuthorized( AppointmentFormDTO.RESOURCE_TYPE, strIdForm, AppointmentResourceIdService.PERMISSION_MODIFY_FORM,
168                 (User) AdminUserService.getAdminUser( request ) ) )
169         {
170             throw new AccessDeniedException( AppointmentResourceIdService.PERMISSION_MODIFY_FORM );
171         }
172         int nIdForm = Integer.parseInt( request.getParameter( PARAMETER_ID_FORM ) );
173         AppointmentFormDTO appointmentForm = FormService.buildAppointmentFormLight( nIdForm );
174         Map<String, Object> model = getModel( );
175         EntryService.addListEntryToModel( nIdForm, model );
176         model.put( MARK_APPOINTMENT_FORM, appointmentForm );
177         Plugin pluginAppointmentResource = PluginService.getPlugin( AppPropertiesService.getProperty( PROPERTY_MODULE_APPOINTMENT_RESOURCE_NAME ) );
178         model.put( MARK_APPOINTMENT_RESOURCE_ENABLED, ( pluginAppointmentResource != null ) && pluginAppointmentResource.isInstalled( ) );
179         return getPage( PROPERTY_PAGE_TITLE_APPOINTMENT_FORM_ENTRIES, TEMPLATE_MODIFY_APPOINTMENT_FORM, model );
180     }
181 
182     /**
183      * Get the HTML code to create an entry
184      * 
185      * @param request
186      *            The request
187      * @return The HTML code to display or the next URL to redirect to
188      */
189     @View( value = VIEW_GET_CREATE_ENTRY )
190     public String getCreateEntry( HttpServletRequest request )
191     {
192         String strIdForm = request.getParameter( PARAMETER_ID_FORM );
193         if ( StringUtils.isEmpty( strIdForm ) || !StringUtils.isNumeric( strIdForm ) )
194         {
195             return redirect( request, AppointmentFormJspBean.getURLManageAppointmentForms( request ) );
196         }
197         String strIdType = request.getParameter( PARAMETER_ID_ENTRY_TYPE );
198         if ( StringUtils.isEmpty( strIdType ) || !StringUtils.isNumeric( strIdType ) )
199         {
200             return redirect( request, getURLModifyAppointmentForm( request, strIdForm ) );
201         }
202         int nIdForm = Integer.parseInt( strIdForm );
203         int nIdType = Integer.parseInt( strIdType );
204         Entry entry = new Entry( );
205         entry.setEntryType( EntryTypeHome.findByPrimaryKey( nIdType ) );
206         String strIdField = request.getParameter( PARAMETER_ID_FIELD );
207         int nIdField = -1;
208         if ( StringUtils.isNotEmpty( strIdField ) && StringUtils.isNumeric( strIdField ) )
209         {
210             nIdField = Integer.parseInt( strIdField );
211             Field field = new Field( );
212             field.setIdField( nIdField );
213             entry.setFieldDepend( field );
214         }
215         entry.setIdResource( nIdForm );
216         entry.setResourceType( AppointmentFormDTO.RESOURCE_TYPE );
217         AppointmentFormDTO appointmentForm = FormService.buildAppointmentForm( nIdForm, 0 );
218         IEntryTypeService entryTypeService = EntryTypeServiceManager.getEntryTypeService( entry );
219         Map<String, Object> model = new HashMap<>( );
220         model.put( MARK_ENTRY, entry );
221         model.put( MARK_FORM, appointmentForm );
222         model.put( MARK_WEBAPP_URL, AppPathService.getBaseUrl( request ) );
223         model.put( MARK_LOCALE, AdminUserService.getLocale( request ).getLanguage( ) );
224         model.put( MARK_ENTRY_TYPE_SERVICE, entryTypeService );
225         model.put( MARK_ANONYMIZATION_HELP, entryTypeService.getAnonymizationHelpMessage( request.getLocale( ) ) );
226         String strTemplate = EntryTypeServiceManager.getEntryTypeService( entry ).getTemplateCreate( entry, false );
227         if ( strTemplate == null )
228         {
229             return doCreateEntry( request );
230         }
231         return getPage( PROPERTY_CREATE_ENTRY_TITLE, strTemplate, model );
232     }
233 
234     /**
235      * Do create an entry
236      * 
237      * @param request
238      *            the request
239      * @return The HTML code to display or the next URL to redirect to
240      */
241     @Action( ACTION_DO_CREATE_ENTRY )
242     public String doCreateEntry( HttpServletRequest request )
243     {
244         String strIdForm = request.getParameter( PARAMETER_ID_FORM );
245         if ( StringUtils.isEmpty( strIdForm ) || !StringUtils.isNumeric( strIdForm ) )
246         {
247             return redirect( request, AppointmentFormJspBean.getURLManageAppointmentForms( request ) );
248         }
249         String strIdType = request.getParameter( PARAMETER_ID_ENTRY_TYPE );
250         int nIdForm = Integer.parseInt( strIdForm );
251         Field fieldDepend = null;
252         if ( ( request.getParameter( PARAMETER_CANCEL ) == null ) && StringUtils.isNotEmpty( strIdType ) && StringUtils.isNumeric( strIdType ) )
253         {
254             int nIdType = Integer.parseInt( strIdType );
255             EntryType entryType = new EntryType( );
256             entryType.setIdType( nIdType );
257             Entry entry = new Entry( );
258             entry.setEntryType( EntryTypeService.getInstance( ).getEntryType( nIdType ) );
259             String strIdField = request.getParameter( PARAMETER_ID_FIELD );
260             int nIdField = -1;
261             if ( StringUtils.isNotEmpty( strIdField ) && StringUtils.isNumeric( strIdField ) )
262             {
263                 nIdField = Integer.parseInt( strIdField );
264                 fieldDepend = new Field( );
265                 fieldDepend.setIdField( nIdField );
266                 entry.setFieldDepend( fieldDepend );
267             }
268             String strError = EntryTypeServiceManager.getEntryTypeService( entry ).getRequestData( entry, request, getLocale( ) );
269             if ( strError != null )
270             {
271                 return redirect( request, strError );
272             }
273             entry.setIdResource( nIdForm );
274             entry.setResourceType( AppointmentFormDTO.RESOURCE_TYPE );
275             entry.setIdEntry( EntryHome.create( entry ) );
276             if ( entry.getFields( ) != null )
277             {
278                 for ( Field field : entry.getFields( ) )
279                 {
280                     field.setParentEntry( entry );
281                     FieldHome.create( field );
282                 }
283             }
284             if ( request.getParameter( PARAMETER_APPLY ) != null )
285             {
286                 return redirect( request, VIEW_GET_MODIFY_ENTRY, PARAMETER_ID_ENTRY, entry.getIdEntry( ) );
287             }
288         }
289         if ( fieldDepend != null )
290         {
291             return redirect( request, AppointmentFormFieldJspBean.getUrlModifyField( request, fieldDepend.getIdField( ) ) );
292         }
293         return redirect( request, getURLModifyAppointmentForm( request, strIdForm ) );
294     }
295 
296     /**
297      * Gets the entry modification page
298      * 
299      * @param request
300      *            The HTTP request
301      * @return The entry modification page
302      */
303     @View( VIEW_GET_MODIFY_ENTRY )
304     public String getModifyEntry( HttpServletRequest request )
305     {
306         Plugin plugin = getPlugin( );
307         String strIdEntry = request.getParameter( PARAMETER_ID_ENTRY );
308         if ( StringUtils.isNotEmpty( strIdEntry ) && StringUtils.isNumeric( strIdEntry ) )
309         {
310             int nIdEntry = Integer.parseInt( strIdEntry );
311             if ( nIdEntry <= 0 )
312             {
313                 return redirect( request, AppointmentFormJspBean.getURLManageAppointmentForms( request ) );
314             }
315             Entry entry = EntryHome.findByPrimaryKey( nIdEntry );
316             List<Field> listField = new ArrayList<>( entry.getFields( ).size( ) );
317             for ( Field field : entry.getFields( ) )
318             {
319                 field = FieldHome.findByPrimaryKey( field.getIdField( ) );
320                 listField.add( field );
321             }
322             entry.setFields( listField );
323             IEntryTypeService entryTypeService = EntryTypeServiceManager.getEntryTypeService( entry );
324             Map<String, Object> model = new HashMap<>( );
325             model.put( MARK_ENTRY, entry );
326             model.put( MARK_FORM, FormService.buildAppointmentForm( entry.getIdResource( ), 0 ) );
327             UrlItem urlItem = new UrlItem( AppPathService.getBaseUrl( request ) + getViewUrl( VIEW_GET_MODIFY_ENTRY ) );
328             urlItem.addParameter( PARAMETER_ID_ENTRY, strIdEntry );
329             model.put( MARK_LIST, entry.getFields( ) );
330             ReferenceList refListRegularExpression = entryTypeService.getReferenceListRegularExpression( entry, plugin );
331             if ( refListRegularExpression != null )
332             {
333                 model.put( MARK_REGULAR_EXPRESSION_LIST_REF_LIST, refListRegularExpression );
334             }
335             model.put( MARK_WEBAPP_URL, AppPathService.getBaseUrl( request ) );
336             model.put( MARK_LOCALE, AdminUserService.getLocale( request ).getLanguage( ) );
337             model.put( MARK_ENTRY_TYPE_SERVICE, EntryTypeServiceManager.getEntryTypeService( entry ) );
338             model.put( MARK_ANONYMIZATION_HELP, entryTypeService.getAnonymizationHelpMessage( request.getLocale( ) ) );
339             return getPage( PROPERTY_MODIFY_QUESTION_TITLE, entryTypeService.getTemplateModify( entry, false ), model );
340         }
341 
342         return redirect( request, AppointmentFormJspBean.getURLManageAppointmentForms( request ) );
343     }
344 
345     /**
346      * Perform the entry modification
347      * 
348      * @param request
349      *            The HTTP request
350      * @return The URL to go after performing the action
351      */
352     @Action( ACTION_DO_MODIFY_ENTRY )
353     public String doModifyEntry( HttpServletRequest request )
354     {
355         String strIdEntry = request.getParameter( PARAMETER_ID_ENTRY );
356         if ( StringUtils.isNotEmpty( strIdEntry ) && StringUtils.isNumeric( strIdEntry ) )
357         {
358             int nIdEntry = Integer.parseInt( strIdEntry );
359             if ( nIdEntry <= 0 )
360             {
361                 return redirect( request, AppointmentFormJspBean.getURLManageAppointmentForms( request ) );
362             }
363             Entry entry = EntryHome.findByPrimaryKey( nIdEntry );
364             if ( request.getParameter( PARAMETER_CANCEL ) == null )
365             {
366                 String strError = EntryTypeServiceManager.getEntryTypeService( entry ).getRequestData( entry, request, getLocale( ) );
367                 if ( strError != null )
368                 {
369                     return redirect( request, strError );
370                 }
371                 EntryHome.update( entry );
372                 if ( entry.getFields( ) != null )
373                 {
374                     for ( Field field : entry.getFields( ) )
375                     {
376                         // Check if the field already exists in the database
377                         Field fieldStored = FieldHome.findByPrimaryKey( field.getIdField( ) );
378                         if ( fieldStored != null )
379                         {
380                             // If it exists, update
381                             FieldHome.update( field );
382                         }
383                         else
384                         {
385                             // If it does not exist, create
386                             FieldHome.create( field );
387                         }
388                     }
389                 }
390             }
391             if ( request.getParameter( PARAMETER_APPLY ) != null )
392             {
393                 return redirect( request, VIEW_GET_MODIFY_ENTRY, PARAMETER_ID_ENTRY, nIdEntry );
394             }
395             String strUrl;
396             if ( entry.getFieldDepend( ) != null )
397             {
398                 strUrl = AppointmentFormFieldJspBean.getUrlModifyField( request, entry.getFieldDepend( ).getIdField( ) );
399             }
400             else
401             {
402                 strUrl = getURLModifyAppointmentForm( request, Integer.toString( entry.getIdResource( ) ) );
403             }
404             return redirect( request, strUrl );
405         }
406         return redirect( request, AppointmentFormJspBean.getURLManageAppointmentForms( request ) );
407     }
408 
409     /**
410      * Gets the confirmation page of delete entry
411      * 
412      * @param request
413      *            The HTTP request
414      * @return the confirmation page of delete entry
415      */
416     @View( VIEW_CONFIRM_REMOVE_ENTRY )
417     public String getConfirmRemoveEntry( HttpServletRequest request )
418     {
419         String strIdEntry = request.getParameter( PARAMETER_ID_ENTRY );
420         int nIdEntry = Integer.parseInt( strIdEntry );
421         ResponseFilter responsefilter = new ResponseFilter( );
422         responsefilter.setIdEntry( nIdEntry );
423         if ( !ResponseHome.getResponseList( responsefilter ).isEmpty( ) )
424         {
425 
426             return redirect( request, AdminMessageService.getMessageUrl( request, MESSAGE_CANT_REMOVE_ENTRY_RESOURCES_ATTACHED, AdminMessage.TYPE_STOP ) );
427 
428         }
429         UrlItem url = new UrlItem( getActionUrl( ACTION_DO_REMOVE_ENTRY ) );
430         url.addParameter( PARAMETER_ID_ENTRY, strIdEntry );
431         return redirect( request, AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_ENTRY, url.getUrl( ), AdminMessage.TYPE_CONFIRMATION ) );
432     }
433 
434     /**
435      * Perform the entry removal
436      * 
437      * @param request
438      *            The HTTP request
439      * @return The URL to go after performing the action
440      */
441     @Action( ACTION_DO_REMOVE_ENTRY )
442     public String doRemoveEntry( HttpServletRequest request )
443     {
444         String strIdEntry = request.getParameter( PARAMETER_ID_ENTRY );
445         if ( StringUtils.isNotEmpty( strIdEntry ) && StringUtils.isNumeric( strIdEntry ) )
446         {
447             int nIdEntry = Integer.parseInt( strIdEntry );
448             if ( nIdEntry <= 0 )
449             {
450                 return redirect( request, AppointmentFormJspBean.getURLManageAppointmentForms( request ) );
451             }
452             Entry entry = EntryHome.findByPrimaryKey( nIdEntry );
453             List<String> listErrors = new ArrayList<>( );
454             if ( !_entryService.checkForRemoval( strIdEntry, listErrors, getLocale( ) ) )
455             {
456                 String strCause = AdminMessageService.getFormattedList( listErrors, getLocale( ) );
457                 Object [ ] args = {
458                         strCause
459                 };
460                 return AdminMessageService.getMessageUrl( request, MESSAGE_CANT_REMOVE_ENTRY, args, AdminMessage.TYPE_STOP );
461             }
462 
463             // Update order
464             List<Entry> listEntry;
465             EntryFilter filter = new EntryFilter( );
466             filter.setIdResource( entry.getIdResource( ) );
467             filter.setResourceType( AppointmentFormDTO.RESOURCE_TYPE );
468             listEntry = EntryHome.getEntryList( filter );
469             if ( entry.getFieldDepend( ) == null )
470             {
471                 _entryService.moveDownEntryOrder( listEntry.size( ), entry );
472             }
473             else
474             {
475                 // conditional questions
476                 EntryHome.decrementOrderByOne( entry.getPosition( ), entry.getFieldDepend( ).getIdField( ), entry.getIdResource( ), entry.getResourceType( ) );
477             }
478             EntryHome.remove( nIdEntry );
479             if ( entry.getFieldDepend( ) != null )
480             {
481                 return redirect( request, AppointmentFormFieldJspBean.getUrlModifyField( request, entry.getFieldDepend( ).getIdField( ) ) );
482             }
483             return redirect( request, getURLModifyAppointmentForm( request, entry.getIdResource( ) ) );
484         }
485         return redirect( request, AppointmentFormJspBean.getURLManageAppointmentForms( request ) );
486     }
487 
488     /**
489      * Do move up an conditional entry of a field
490      * 
491      * @param request
492      *            The request
493      * @return The next URL to redirect to
494      */
495     @Action( ACTION_DO_MOVE_UP_ENTRY_CONDITIONAL )
496     public String doMoveUpEntryConditional( HttpServletRequest request )
497     {
498         return doMoveEntryConditional( request, true );
499     }
500 
501     /**
502      * Do move down an conditional entry of a field
503      * 
504      * @param request
505      *            The request
506      * @return The next URL to redirect to
507      */
508     @Action( ACTION_DO_MOVE_DOWN_ENTRY_CONDITIONAL )
509     public String doMoveDownEntryConditional( HttpServletRequest request )
510     {
511         return doMoveEntryConditional( request, false );
512     }
513 
514     /**
515      * Do move up or down an conditional entry of a field
516      * 
517      * @param request
518      *            The request
519      * @param bMoveUp
520      *            True to move the entry up, false to move it down
521      * @return The next URL to redirect to
522      */
523     private String doMoveEntryConditional( HttpServletRequest request, boolean bMoveUp )
524     {
525         String strIdEntry = request.getParameter( PARAMETER_ID_ENTRY );
526         if ( StringUtils.isNotEmpty( strIdEntry ) && StringUtils.isNumeric( strIdEntry ) )
527         {
528             int nIdEntry = Integer.parseInt( strIdEntry );
529             Entry entry = EntryHome.findByPrimaryKey( nIdEntry );
530             int nNewPosition = bMoveUp ? ( entry.getPosition( ) - 1 ) : ( entry.getPosition( ) + 1 );
531             if ( nNewPosition > 0 )
532             {
533                 Entry entryToMove = EntryHome.findByOrderAndIdFieldAndIdResource( nNewPosition, entry.getFieldDepend( ).getIdField( ), entry.getIdResource( ),
534                         entry.getResourceType( ) );
535                 if ( entryToMove != null )
536                 {
537                     entryToMove.setPosition( entry.getPosition( ) );
538                     EntryHome.update( entryToMove );
539                     entry.setPosition( nNewPosition );
540                     EntryHome.update( entry );
541                 }
542             }
543             return redirect( request, AppointmentFormFieldJspBean.getUrlModifyField( request, entry.getFieldDepend( ).getIdField( ) ) );
544         }
545 
546         return redirect( request, AppointmentFormJspBean.getURLManageAppointmentForms( request ) );
547     }
548 
549     /**
550      * Copy the entry whose key is specified in the HTTP request
551      * 
552      * @param request
553      *            The HTTP request
554      * @return The URL to go after performing the action
555      */
556     @Action( ACTION_DO_COPY_ENTRY )
557     public String doCopyEntry( HttpServletRequest request )
558     {
559         String strIdEntry = request.getParameter( PARAMETER_ID_ENTRY );
560         if ( StringUtils.isNotEmpty( strIdEntry ) && StringUtils.isNumeric( strIdEntry ) )
561         {
562             int nIdEntry = Integer.parseInt( strIdEntry );
563             if ( nIdEntry == -1 )
564             {
565                 return redirect( request, AppointmentFormJspBean.getURLManageAppointmentForms( request ) );
566             }
567             Entry entry = EntryHome.findByPrimaryKey( nIdEntry );
568             Object [ ] tabEntryTileCopy = {
569                     entry.getTitle( )
570             };
571             String strTitleCopyEntry = I18nService.getLocalizedString( PROPERTY_COPY_ENTRY_TITLE, tabEntryTileCopy, getLocale( ) );
572             if ( strTitleCopyEntry != null )
573             {
574                 entry.setTitle( strTitleCopyEntry );
575             }
576             EntryHome.copy( entry );
577             // If the entry has a parent
578             if ( entry.getParent( ) != null )
579             {
580                 // We reload the entry to get the copy and not he original entry
581                 // The id of the entry is the id of the copy. It has been set by
582                 // the create method of EntryDAO
583                 entry = EntryHome.findByPrimaryKey( entry.getIdEntry( ) );
584                 Entry entryParent = EntryHome.findByPrimaryKey( entry.getParent( ).getIdEntry( ) );
585                 _entryService.moveUpEntryOrder( entryParent.getPosition( ) + entryParent.getChildren( ).size( ), entry );
586             }
587             if ( entry.getFieldDepend( ) != null )
588             {
589                 return redirect( request, AppointmentFormFieldJspBean.getUrlModifyField( request, entry.getFieldDepend( ).getIdField( ) ) );
590             }
591             return redirect( request, getURLModifyAppointmentForm( request, entry.getIdResource( ) ) );
592         }
593         return redirect( request, AppointmentFormJspBean.getURLManageAppointmentForms( request ) );
594     }
595 
596     /**
597      * Change the attribute's order (move up or move down in the list)
598      * 
599      * @param request
600      *            the request
601      * @return The URL of the form management page
602      */
603     @Action( ACTION_DO_CHANGE_ORDER_ENTRY )
604     public String doChangeOrderEntry( HttpServletRequest request )
605     {
606         // gets the entry which needs to be changed (order)
607         String strIdForm = request.getParameter( PARAMETER_ID_FORM );
608         int nIdForm = Integer.parseInt( strIdForm );
609         // If the parameter move.x has been set, then we have to add entries to
610         // a group
611         if ( StringUtils.isNotEmpty( request.getParameter( PARAMETER_ADD_TO_GROUP ) ) )
612         {
613             String strIdEntryGroup = request.getParameter( PARAMETER_ID_ENTRY_GROUP );
614             if ( StringUtils.isNotEmpty( strIdEntryGroup ) && StringUtils.isNumeric( strIdEntryGroup ) )
615             {
616                 int nIdEntryGroup = Integer.parseInt( strIdEntryGroup );
617                 Entry entryParent = EntryHome.findByPrimaryKey( nIdEntryGroup );
618                 String [ ] strArrayIdEntries = request.getParameterValues( PARAMETER_ENTRY_ID_MOVE );
619                 if ( ( strArrayIdEntries != null ) && ( strArrayIdEntries.length > 0 ) )
620                 {
621                     for ( String strIdEntry : strArrayIdEntries )
622                     {
623                         if ( StringUtils.isNotEmpty( strIdEntry ) && StringUtils.isNumeric( strIdEntry ) )
624                         {
625                             int nIdEntry = Integer.parseInt( strIdEntry );
626                             _entryService.moveEntryIntoGroup( EntryHome.findByPrimaryKey( nIdEntry ), entryParent );
627                         }
628                     }
629                 }
630             }
631         }
632         else
633         {
634             Integer nEntryId = Integer.parseInt( request.getParameter( PARAMETER_ID_ENTRY ) );
635             Integer nOrderToSet = Integer.parseInt( request.getParameter( PARAMETER_ORDER_ID + request.getParameter( PARAMETER_ID_ENTRY ) ) );
636             Entry entryToChangeOrder = EntryHome.findByPrimaryKey( nEntryId );
637             int nActualOrder = entryToChangeOrder.getPosition( );
638             // does nothing if the order to set is equal to the actual order
639             if ( nOrderToSet != nActualOrder )
640             {
641                 // entry goes up in the list
642                 if ( nOrderToSet < entryToChangeOrder.getPosition( ) )
643                 {
644                     _entryService.moveUpEntryOrder( nOrderToSet, entryToChangeOrder );
645                 }
646                 // entry goes down in the list
647                 else
648                 {
649                     _entryService.moveDownEntryOrder( nOrderToSet, entryToChangeOrder );
650                 }
651             }
652         }
653         return redirect( request, getURLModifyAppointmentForm( request, nIdForm ) );
654     }
655 
656     /**
657      * Remove an entry from a group
658      * 
659      * @param request
660      *            The request
661      * @return The newt URL to redirect to
662      */
663     @Action( ACTION_DO_MOVE_OUT_ENTRY )
664     public String doMoveOutEntry( HttpServletRequest request )
665     {
666         String strIdEntry = request.getParameter( PARAMETER_ID_ENTRY );
667         if ( StringUtils.isNotEmpty( strIdEntry ) && StringUtils.isNumeric( strIdEntry ) )
668         {
669             int nIdEntry = Integer.parseInt( strIdEntry );
670             Entry entry = EntryHome.findByPrimaryKey( nIdEntry );
671             if ( entry.getParent( ) != null )
672             {
673                 _entryService.moveOutEntryFromGroup( entry );
674             }
675             return redirect( request, getURLModifyAppointmentForm( request, entry.getIdResource( ) ) );
676         }
677         return redirect( request, AppointmentFormJspBean.getURLManageAppointmentForms( request ) );
678     }
679 
680     /**
681      * Delete the association between a field and and regular expression
682      * 
683      * @param request
684      *            the HTTP Request
685      * @return The URL to go after performing the action
686      */
687     @Action( ACTION_DO_REMOVE_REGULAR_EXPRESSION )
688     public String doRemoveRegularExpression( HttpServletRequest request )
689     {
690         String strIdExpression = request.getParameter( PARAMETER_ID_EXPRESSION );
691         String strIdField = request.getParameter( PARAMETER_ID_FIELD );
692         if ( StringUtils.isNotEmpty( strIdExpression ) && StringUtils.isNotEmpty( strIdField ) && StringUtils.isNumeric( strIdExpression )
693                 && StringUtils.isNumeric( strIdField ) )
694         {
695             int nIdField = Integer.parseInt( strIdField );
696             int nIdExpression = Integer.parseInt( strIdExpression );
697             FieldHome.removeVerifyBy( nIdField, nIdExpression );
698             Field field = FieldHome.findByPrimaryKey( nIdField );
699             return redirect( request, VIEW_GET_MODIFY_ENTRY, PARAMETER_ID_ENTRY, field.getParentEntry( ).getIdEntry( ) );
700         }
701         return redirect( request, AppointmentFormJspBean.getURLManageAppointmentForms( request ) );
702     }
703 
704     /**
705      * Insert an association between a field and a regular expression
706      * 
707      * @param request
708      *            the HTTP Request
709      * @return The URL to go after performing the action
710      */
711     @Action( ACTION_DO_INSERT_REGULAR_EXPRESSION )
712     public String doInsertRegularExpression( HttpServletRequest request )
713     {
714         String strIdExpression = request.getParameter( PARAMETER_ID_EXPRESSION );
715         String strIdField = request.getParameter( PARAMETER_ID_FIELD );
716         if ( StringUtils.isNotEmpty( strIdExpression ) && StringUtils.isNotEmpty( strIdField ) && StringUtils.isNumeric( strIdExpression )
717                 && StringUtils.isNumeric( strIdField ) )
718         {
719             int nIdField = Integer.parseInt( strIdField );
720             int nIdExpression = Integer.parseInt( strIdExpression );
721             FieldHome.createVerifyBy( nIdField, nIdExpression );
722             Field field = FieldHome.findByPrimaryKey( nIdField );
723             return redirect( request, VIEW_GET_MODIFY_ENTRY, PARAMETER_ID_ENTRY, field.getParentEntry( ).getIdEntry( ) );
724         }
725         return redirect( request, AppointmentFormJspBean.getURLManageAppointmentForms( request ) );
726     }
727 
728     @Action( ACTION_CHANGE_DISABLED )
729     public String doChangeActive( HttpServletRequest request )
730     {
731         int idEntry = NumberUtils.toInt( request.getParameter( PARAMETER_ID_ENTRY ), -1 );
732         int idForm = NumberUtils.toInt( request.getParameter( PARAMETER_ID_FORM ), -1 );
733 
734         if ( idEntry != -1 )
735         {
736             Entry entry = EntryHome.findByPrimaryKey( idEntry );
737             if ( entry != null )
738             {
739                 Field disabledField = entry.getFieldByCode( IEntryTypeService.FIELD_DISABLED );
740 
741                 if ( disabledField != null )
742                 {
743                     disabledField.setValue( Boolean.toString( !Boolean.parseBoolean( disabledField.getValue( ) ) ) );
744                     FieldHome.update( disabledField );
745                 }
746             }
747         }
748 
749         return redirect( request, VIEW_MODIFY_APPOINTMENTFORM_ENTRIES, PARAMETER_ID_FORM,  idForm );
750     }
751 
752     /**
753      * Get the URL to modify an entry
754      * 
755      * @param request
756      *            The request
757      * @param nIdEntry
758      *            The id of the entry
759      * @return The URL to modify the given entry
760      */
761     public static String getURLModifyEntry( HttpServletRequest request, int nIdEntry )
762     {
763         UrlItem urlItem = new UrlItem( AppPathService.getBaseUrl( request ) + JSP_URL_MANAGE_APPOINTMENT_FORM_ENTRIES );
764         urlItem.addParameter( MVCUtils.PARAMETER_VIEW, VIEW_GET_MODIFY_ENTRY );
765         urlItem.addParameter( PARAMETER_ID_ENTRY, nIdEntry );
766         return urlItem.getUrl( );
767     }
768 
769     /**
770      * Get the URL to modify an appointment form
771      * 
772      * @param request
773      *            The request
774      * @param nIdForm
775      *            The id of the form to modify
776      * @return The URL to modify the given Appointment form
777      */
778     public static String getURLModifyAppointmentForm( HttpServletRequest request, int nIdForm )
779     {
780         return getURLModifyAppointmentForm( request, Integer.toString( nIdForm ) );
781     }
782 
783     /**
784      * Get the URL to modify an appointment form
785      * 
786      * @param request
787      *            The request
788      * @param strIdForm
789      *            The id of the form to modify
790      * @return The URL to modify the given Appointment form
791      */
792     public static String getURLModifyAppointmentForm( HttpServletRequest request, String strIdForm )
793     {
794         UrlItem urlItem = new UrlItem( AppPathService.getBaseUrl( request ) + JSP_URL_MANAGE_APPOINTMENT_FORM_ENTRIES );
795         urlItem.addParameter( MVCUtils.PARAMETER_VIEW, VIEW_MODIFY_APPOINTMENTFORM_ENTRIES );
796         urlItem.addParameter( PARAMETER_ID_FORM, strIdForm );
797         return urlItem.getUrl( );
798     }
799 
800 }