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