View Javadoc
1   /*
2    * Copyright (c) 2002-2021, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.announce.web;
35  
36  import fr.paris.lutece.plugins.announce.business.Category;
37  import fr.paris.lutece.plugins.announce.business.CategoryHome;
38  import fr.paris.lutece.plugins.announce.service.EntryService;
39  import fr.paris.lutece.plugins.announce.service.EntryTypeService;
40  import fr.paris.lutece.plugins.genericattributes.business.Entry;
41  import fr.paris.lutece.plugins.genericattributes.business.EntryFilter;
42  import fr.paris.lutece.plugins.genericattributes.business.EntryHome;
43  import fr.paris.lutece.plugins.genericattributes.business.EntryType;
44  import fr.paris.lutece.plugins.genericattributes.business.EntryTypeHome;
45  import fr.paris.lutece.plugins.genericattributes.business.Field;
46  import fr.paris.lutece.plugins.genericattributes.business.FieldHome;
47  import fr.paris.lutece.plugins.genericattributes.service.entrytype.EntryTypeServiceManager;
48  import fr.paris.lutece.plugins.genericattributes.service.entrytype.IEntryTypeService;
49  import fr.paris.lutece.portal.service.admin.AdminUserService;
50  import fr.paris.lutece.portal.service.i18n.I18nService;
51  import fr.paris.lutece.portal.service.message.AdminMessage;
52  import fr.paris.lutece.portal.service.message.AdminMessageService;
53  import fr.paris.lutece.portal.service.plugin.Plugin;
54  import fr.paris.lutece.portal.service.util.AppPathService;
55  import fr.paris.lutece.portal.util.mvc.admin.MVCAdminJspBean;
56  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
57  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
58  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
59  import fr.paris.lutece.portal.util.mvc.utils.MVCUtils;
60  import fr.paris.lutece.util.ReferenceList;
61  import fr.paris.lutece.util.url.UrlItem;
62  
63  import org.apache.commons.lang3.StringUtils;
64  
65  import java.util.ArrayList;
66  import java.util.HashMap;
67  import java.util.List;
68  import java.util.Map;
69  
70  import javax.servlet.http.HttpServletRequest;
71  
72  /**
73   * JspBean to manage category entries
74   */
75  @Controller( controllerJsp = "ManageCategoryEntries.jsp", controllerPath = "jsp/admin/plugins/announce/", right = AnnounceUserJspBean.RIGHT_MANAGE_ANNOUNCE )
76  public class CategoryEntryJspBean extends MVCAdminJspBean
77  {
78      private static final long serialVersionUID = -4951787792196104967L;
79  
80      // Parameters
81      private static final String PARAMETER_ID_ENTRY_TYPE = "id_type";
82      private static final String PARAMETER_ID_CATEGORY = "id_category";
83      private static final String PARAMETER_ID_FIELD = "id_field";
84      private static final String PARAMETER_ID_ENTRY = "id_entry";
85      private static final String PARAMETER_CANCEL = "cancel";
86      private static final String PARAMETER_APPLY = "apply";
87      private static final String PARAMETER_ORDER_ID = "order_id_";
88      private static final String PARAMETER_ADD_TO_GROUP = "add_to_group";
89      private static final String PARAMETER_ID_ENTRY_GROUP = "id_entry_group";
90      private static final String PARAMETER_ENTRY_ID_MOVE = "entry_id_move";
91      private static final String PARAMETER_ID_EXPRESSION = "id_expression";
92  
93      // Urls
94      private static final String JSP_URL_MANAGE_CATEGORY_ENTRIES = "jsp/admin/plugins/announce/ManageCategoryEntries.jsp";
95  
96      // Messages
97      private static final String MESSAGE_CONFIRM_REMOVE_ENTRY = "announce.message.confirmRemoveEntry";
98      private static final String MESSAGE_CANT_REMOVE_ENTRY = "announce.message.cantRemoveEntry";
99      private static final String PROPERTY_CREATE_ENTRY_TITLE = "announce.createEntry.titleQuestion";
100     private static final String PROPERTY_MODIFY_QUESTION_TITLE = "announce.modifyEntry.titleQuestion";
101     private static final String PROPERTY_COPY_ENTRY_TITLE = "announce.copyEntry.title";
102 
103     // Views
104     private static final String VIEW_GET_CREATE_ENTRY = "getCreateEntry";
105     private static final String VIEW_GET_MODIFY_ENTRY = "getModifyEntry";
106     private static final String VIEW_CONFIRM_REMOVE_ENTRY = "confirmRemoveEntry";
107 
108     // Actions
109     private static final String ACTION_DO_CREATE_ENTRY = "doCreateEntry";
110     private static final String ACTION_DO_MODIFY_ENTRY = "doModifyEntry";
111     private static final String ACTION_DO_REMOVE_ENTRY = "doRemoveEntry";
112     private static final String ACTION_DO_COPY_ENTRY = "doCopyEntry";
113     private static final String ACTION_DO_CHANGE_ORDER_ENTRY = "doChangeOrderEntry";
114     private static final String ACTION_DO_MOVE_OUT_ENTRY = "doMoveOutEntry";
115     private static final String ACTION_DO_MOVE_UP_ENTRY_CONDITIONAL = "doMoveUpEntryConditional";
116     private static final String ACTION_DO_MOVE_DOWN_ENTRY_CONDITIONAL = "doMoveDownEntryConditional";
117     private static final String ACTION_DO_REMOVE_REGULAR_EXPRESSION = "doRemoveRegularExpression";
118     private static final String ACTION_DO_INSERT_REGULAR_EXPRESSION = "doInsertRegularExpression";
119 
120     // Marks
121     private static final String MARK_WEBAPP_URL = "webapp_url";
122     private static final String MARK_LOCALE = "locale";
123     private static final String MARK_REGULAR_EXPRESSION_LIST_REF_LIST = "regular_expression_list";
124     private static final String MARK_ENTRY = "entry";
125     private static final String MARK_LIST = "list";
126     private static final String MARK_CATEGORY = "category";
127     private static final String MARK_ENTRY_TYPE_SERVICE = "entryTypeService";
128 
129     // Local variables
130     private EntryService _entryService = EntryService.getService( );
131 
132     /**
133      * Get the HTML code to create an entry
134      * 
135      * @param request
136      *            The request
137      * @return The HTML code to display or the next URL to redirect to
138      */
139     @View( value = VIEW_GET_CREATE_ENTRY )
140     public String getCreateEntry( HttpServletRequest request )
141     {
142         String strIdCategory = request.getParameter( PARAMETER_ID_CATEGORY );
143 
144         if ( StringUtils.isEmpty( strIdCategory ) || !StringUtils.isNumeric( strIdCategory ) )
145         {
146             return redirect( request, CategoryJspBean.getUrlManageCategories( request ) );
147         }
148 
149         String strIdType = request.getParameter( PARAMETER_ID_ENTRY_TYPE );
150 
151         if ( StringUtils.isEmpty( strIdType ) || !StringUtils.isNumeric( strIdType ) )
152         {
153             return redirect( request, CategoryJspBean.getUrlModifyCategory( request, Integer.parseInt( strIdCategory ) ) );
154         }
155 
156         int nIdCategory = Integer.parseInt( strIdCategory );
157         int nIdType = Integer.parseInt( strIdType );
158 
159         Entry entry = new Entry( );
160         entry.setEntryType( EntryTypeHome.findByPrimaryKey( nIdType ) );
161 
162         String strIdField = request.getParameter( PARAMETER_ID_FIELD );
163         int nIdField = -1;
164 
165         if ( StringUtils.isNotEmpty( strIdField ) && StringUtils.isNumeric( strIdField ) )
166         {
167             nIdField = Integer.parseInt( strIdField );
168 
169             Field field = new Field( );
170             field.setIdField( nIdField );
171             entry.setFieldDepend( field );
172         }
173 
174         entry.setIdResource( nIdCategory );
175         entry.setResourceType( Category.RESOURCE_TYPE );
176 
177         Category category = CategoryHome.findByPrimaryKey( nIdCategory );
178 
179         // Default Values
180         Map<String, Object> model = new HashMap<>( );
181         model.put( MARK_ENTRY, entry );
182         model.put( MARK_CATEGORY, category );
183         model.put( MARK_WEBAPP_URL, AppPathService.getBaseUrl( request ) );
184         model.put( MARK_LOCALE, AdminUserService.getLocale( request ).getLanguage( ) );
185         model.put( MARK_ENTRY_TYPE_SERVICE, EntryTypeServiceManager.getEntryTypeService( entry ) );
186 
187         String strTemplate = EntryTypeServiceManager.getEntryTypeService( entry ).getTemplateCreate( entry, false );
188 
189         if ( strTemplate == null )
190         {
191             return doCreateEntry( request );
192         }
193 
194         return getPage( PROPERTY_CREATE_ENTRY_TITLE, strTemplate, model );
195     }
196 
197     /**
198      * Do create an entry
199      * 
200      * @param request
201      *            the request
202      * @return The HTML code to display or the next URL to redirect to
203      */
204     @Action( ACTION_DO_CREATE_ENTRY )
205     public String doCreateEntry( HttpServletRequest request )
206     {
207         String strIdCategory = request.getParameter( PARAMETER_ID_CATEGORY );
208 
209         if ( StringUtils.isEmpty( strIdCategory ) || !StringUtils.isNumeric( strIdCategory ) )
210         {
211             return redirect( request, CategoryJspBean.getUrlManageCategories( request ) );
212         }
213 
214         String strIdType = request.getParameter( PARAMETER_ID_ENTRY_TYPE );
215 
216         int nIdCategory = Integer.parseInt( strIdCategory );
217         Field fieldDepend = null;
218 
219         if ( ( request.getParameter( PARAMETER_CANCEL ) == null ) && StringUtils.isNotEmpty( strIdType ) && StringUtils.isNumeric( strIdType ) )
220         {
221             int nIdType = Integer.parseInt( strIdType );
222             EntryType entryType = new EntryType( );
223             entryType.setIdType( nIdType );
224 
225             Entry entry = new Entry( );
226             entry.setEntryType( EntryTypeService.getInstance( ).getEntryType( nIdType ) );
227 
228             String strIdField = request.getParameter( PARAMETER_ID_FIELD );
229             int nIdField = -1;
230 
231             if ( StringUtils.isNotEmpty( strIdField ) && StringUtils.isNumeric( strIdField ) )
232             {
233                 nIdField = Integer.parseInt( strIdField );
234 
235                 fieldDepend = new Field( );
236                 fieldDepend.setIdField( nIdField );
237                 entry.setFieldDepend( fieldDepend );
238             }
239 
240             String strError = EntryTypeServiceManager.getEntryTypeService( entry ).getRequestData( entry, request, getLocale( ) );
241 
242             if ( strError != null )
243             {
244                 return redirect( request, strError );
245             }
246 
247             entry.setIdResource( nIdCategory );
248             entry.setResourceType( Category.RESOURCE_TYPE );
249             entry.setIdEntry( EntryHome.create( entry ) );
250 
251             if ( entry.getFields( ) != null )
252             {
253                 for ( Field field : entry.getFields( ) )
254                 {
255                     field.setParentEntry( entry );
256                     FieldHome.create( field );
257                 }
258             }
259 
260             if ( request.getParameter( PARAMETER_APPLY ) != null )
261             {
262                 return redirect( request, VIEW_GET_MODIFY_ENTRY, PARAMETER_ID_ENTRY, entry.getIdEntry( ) );
263             }
264         }
265 
266         if ( fieldDepend != null )
267         {
268             return redirect( request, CategoryFieldJspBean.getUrlModifyField( request, fieldDepend.getIdField( ) ) );
269         }
270 
271         return redirect( request, CategoryJspBean.getUrlModifyCategory( request, nIdCategory ) );
272     }
273 
274     /**
275      * Gets the entry modification page
276      * 
277      * @param request
278      *            The HTTP request
279      * @return The entry modification page
280      */
281     @View( VIEW_GET_MODIFY_ENTRY )
282     public String getModifyEntry( HttpServletRequest request )
283     {
284         Plugin plugin = getPlugin( );
285         String strIdEntry = request.getParameter( PARAMETER_ID_ENTRY );
286 
287         if ( StringUtils.isNotEmpty( strIdEntry ) && StringUtils.isNumeric( strIdEntry ) )
288         {
289             int nIdEntry = Integer.parseInt( strIdEntry );
290 
291             if ( nIdEntry <= 0 )
292             {
293                 return redirect( request, CategoryJspBean.getUrlManageCategories( request ) );
294             }
295 
296             Entry entry = EntryHome.findByPrimaryKey( nIdEntry );
297 
298             List<Field> listField = new ArrayList<>( entry.getFields( ).size( ) );
299 
300             for ( Field field : entry.getFields( ) )
301             {
302                 field = FieldHome.findByPrimaryKey( field.getIdField( ) );
303                 listField.add( field );
304             }
305 
306             entry.setFields( listField );
307 
308             IEntryTypeService entryTypeService = EntryTypeServiceManager.getEntryTypeService( entry );
309 
310             Map<String, Object> model = new HashMap<>( );
311             model.put( MARK_ENTRY, entry );
312             model.put( MARK_CATEGORY, CategoryHome.findByPrimaryKey( entry.getIdResource( ) ) );
313 
314             UrlItem urlItem = new UrlItem( AppPathService.getBaseUrl( request ) + getViewUrl( VIEW_GET_MODIFY_ENTRY ) );
315             urlItem.addParameter( PARAMETER_ID_ENTRY, strIdEntry );
316 
317             model.put( MARK_LIST, entry.getFields( ) );
318 
319             ReferenceList refListRegularExpression = entryTypeService.getReferenceListRegularExpression( entry, plugin );
320 
321             if ( refListRegularExpression != null )
322             {
323                 model.put( MARK_REGULAR_EXPRESSION_LIST_REF_LIST, refListRegularExpression );
324             }
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 
330             return getPage( PROPERTY_MODIFY_QUESTION_TITLE, entryTypeService.getTemplateModify( entry, false ), model );
331         }
332 
333         return redirect( request, CategoryJspBean.getUrlManageCategories( request ) );
334     }
335 
336     /**
337      * Perform the entry modification
338      * 
339      * @param request
340      *            The HTTP request
341      * @return The URL to go after performing the action
342      */
343     @Action( ACTION_DO_MODIFY_ENTRY )
344     public String doModifyEntry( HttpServletRequest request )
345     {
346         String strIdEntry = request.getParameter( PARAMETER_ID_ENTRY );
347 
348         if ( StringUtils.isNotEmpty( strIdEntry ) && StringUtils.isNumeric( strIdEntry ) )
349         {
350             int nIdEntry = Integer.parseInt( strIdEntry );
351 
352             if ( nIdEntry <= 0 )
353             {
354                 return redirect( request, CategoryJspBean.getUrlManageCategories( request ) );
355             }
356 
357             Entry entry = EntryHome.findByPrimaryKey( nIdEntry );
358 
359             if ( request.getParameter( PARAMETER_CANCEL ) == null )
360             {
361                 String strError = EntryTypeServiceManager.getEntryTypeService( entry ).getRequestData( entry, request, getLocale( ) );
362 
363                 if ( strError != null )
364                 {
365                     return strError;
366                 }
367 
368                 EntryHome.update( entry );
369 
370                 if ( entry.getFields( ) != null )
371                 {
372                     for ( Field field : entry.getFields( ) )
373                     {
374                         // Check if the field already exists in the database
375                         Field fieldStored = FieldHome.findByPrimaryKey( field.getIdField( ) );
376 
377                         if ( fieldStored != null )
378                         {
379                             // If it exists, update
380                             FieldHome.update( field );
381                         }
382                         else
383                         {
384                             // If it does not exist, create
385                             FieldHome.create( field );
386                         }
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 
396             String strUrl;
397 
398             if ( entry.getFieldDepend( ) != null )
399             {
400                 strUrl = CategoryFieldJspBean.getUrlModifyField( request, entry.getFieldDepend( ).getIdField( ) );
401             }
402             else
403             {
404                 strUrl = CategoryJspBean.getUrlModifyCategory( request, entry.getIdResource( ) );
405             }
406 
407             return redirect( request, strUrl );
408         }
409 
410         return redirect( request, CategoryJspBean.getUrlManageCategories( request ) );
411     }
412 
413     /**
414      * Gets the confirmation page of delete entry
415      * 
416      * @param request
417      *            The HTTP request
418      * @return the confirmation page of delete entry
419      */
420     @View( VIEW_CONFIRM_REMOVE_ENTRY )
421     public String getConfirmRemoveEntry( HttpServletRequest request )
422     {
423         String strIdEntry = request.getParameter( PARAMETER_ID_ENTRY );
424         UrlItem url = new UrlItem( getActionUrl( ACTION_DO_REMOVE_ENTRY ) );
425         url.addParameter( PARAMETER_ID_ENTRY, strIdEntry );
426 
427         return redirect( request, AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_ENTRY, url.getUrl( ), AdminMessage.TYPE_CONFIRMATION ) );
428     }
429 
430     /**
431      * Perform the entry removal
432      * 
433      * @param request
434      *            The HTTP request
435      * @return The URL to go after performing the action
436      */
437     @Action( ACTION_DO_REMOVE_ENTRY )
438     public String doRemoveEntry( HttpServletRequest request )
439     {
440         String strIdEntry = request.getParameter( PARAMETER_ID_ENTRY );
441 
442         if ( StringUtils.isNotEmpty( strIdEntry ) && StringUtils.isNumeric( strIdEntry ) )
443         {
444             int nIdEntry = Integer.parseInt( strIdEntry );
445 
446             if ( nIdEntry <= 0 )
447             {
448                 return redirect( request, CategoryJspBean.getUrlManageCategories( request ) );
449             }
450 
451             Entry entry = EntryHome.findByPrimaryKey( nIdEntry );
452 
453             List<String> listErrors = new ArrayList<>( );
454 
455             if ( !_entryService.checkForRemoval( strIdEntry, listErrors, getLocale( ) ) )
456             {
457                 String strCause = AdminMessageService.getFormattedList( listErrors, getLocale( ) );
458                 Object [ ] args = {
459                         strCause
460                 };
461 
462                 return AdminMessageService.getMessageUrl( request, MESSAGE_CANT_REMOVE_ENTRY, args, AdminMessage.TYPE_STOP );
463             }
464 
465             // Update order
466             List<Entry> listEntry;
467             EntryFilter filter = new EntryFilter( );
468             filter.setIdResource( entry.getIdResource( ) );
469             filter.setResourceType( Category.RESOURCE_TYPE );
470             listEntry = EntryHome.getEntryList( filter );
471 
472             if ( entry.getFieldDepend( ) == null )
473             {
474                 _entryService.moveDownEntryOrder( listEntry.size( ), entry );
475             }
476             else
477             {
478                 // conditional questions
479                 EntryHome.decrementOrderByOne( entry.getPosition( ), entry.getFieldDepend( ).getIdField( ), entry.getIdResource( ), entry.getResourceType( ) );
480             }
481 
482             // Remove entry
483             EntryHome.remove( nIdEntry );
484 
485             if ( entry.getFieldDepend( ) != null )
486             {
487                 return redirect( request, CategoryFieldJspBean.getUrlModifyField( request, entry.getFieldDepend( ).getIdField( ) ) );
488             }
489 
490             return redirect( request, CategoryJspBean.getUrlModifyCategory( request, entry.getIdResource( ) ) );
491         }
492 
493         return redirect( request, CategoryJspBean.getUrlManageCategories( request ) );
494     }
495 
496     /**
497      * Do move up an conditional entry of a field
498      * 
499      * @param request
500      *            The request
501      * @return The next URL to redirect to
502      */
503     @Action( ACTION_DO_MOVE_UP_ENTRY_CONDITIONAL )
504     public String doMoveUpEntryConditional( HttpServletRequest request )
505     {
506         return doMoveEntryConditional( request, true );
507     }
508 
509     /**
510      * Do move down an conditional entry of a field
511      * 
512      * @param request
513      *            The request
514      * @return The next URL to redirect to
515      */
516     @Action( ACTION_DO_MOVE_DOWN_ENTRY_CONDITIONAL )
517     public String doMoveDownEntryConditional( HttpServletRequest request )
518     {
519         return doMoveEntryConditional( request, false );
520     }
521 
522     /**
523      * Do move up or down an conditional entry of a field
524      * 
525      * @param request
526      *            The request
527      * @param bMoveUp
528      *            True to move the entry up, false to move it down
529      * @return The next URL to redirect to
530      */
531     private String doMoveEntryConditional( HttpServletRequest request, boolean bMoveUp )
532     {
533         String strIdEntry = request.getParameter( PARAMETER_ID_ENTRY );
534 
535         if ( StringUtils.isNotEmpty( strIdEntry ) && StringUtils.isNumeric( strIdEntry ) )
536         {
537             int nIdEntry = Integer.parseInt( strIdEntry );
538             Entry entry = EntryHome.findByPrimaryKey( nIdEntry );
539             int nNewPosition = bMoveUp ? ( entry.getPosition( ) - 1 ) : ( entry.getPosition( ) + 1 );
540 
541             if ( nNewPosition > 0 )
542             {
543                 Entry entryToMove = EntryHome.findByOrderAndIdFieldAndIdResource( nNewPosition, entry.getFieldDepend( ).getIdField( ), entry.getIdResource( ),
544                         entry.getResourceType( ) );
545 
546                 if ( entryToMove != null )
547                 {
548                     entryToMove.setPosition( entry.getPosition( ) );
549                     EntryHome.update( entryToMove );
550                     entry.setPosition( nNewPosition );
551                     EntryHome.update( entry );
552                 }
553             }
554 
555             return redirect( request, CategoryFieldJspBean.getUrlModifyField( request, entry.getFieldDepend( ).getIdField( ) ) );
556         }
557 
558         return redirect( request, CategoryJspBean.getUrlManageCategories( request ) );
559     }
560 
561     /**
562      * Copy the entry whose key is specified in the HTTP request
563      * 
564      * @param request
565      *            The HTTP request
566      * @return The URL to go after performing the action
567      */
568     @Action( ACTION_DO_COPY_ENTRY )
569     public String doCopyEntry( HttpServletRequest request )
570     {
571         String strIdEntry = request.getParameter( PARAMETER_ID_ENTRY );
572 
573         if ( StringUtils.isNotEmpty( strIdEntry ) && StringUtils.isNumeric( strIdEntry ) )
574         {
575             int nIdEntry = Integer.parseInt( strIdEntry );
576 
577             if ( nIdEntry == -1 )
578             {
579                 return redirect( request, CategoryJspBean.getUrlManageCategories( request ) );
580             }
581 
582             Entry entry = EntryHome.findByPrimaryKey( nIdEntry );
583 
584             Object [ ] tabEntryTileCopy = {
585                     entry.getTitle( )
586             };
587             String strTitleCopyEntry = I18nService.getLocalizedString( PROPERTY_COPY_ENTRY_TITLE, tabEntryTileCopy, getLocale( ) );
588 
589             if ( strTitleCopyEntry != null )
590             {
591                 entry.setTitle( strTitleCopyEntry );
592             }
593 
594             EntryHome.copy( entry );
595 
596             // If the entry has a parent
597             if ( entry.getParent( ) != null )
598             {
599                 // We reload the entry to get the copy and not he original entry
600                 // The id of the entry is the id of the copy. It has been set by the create method of EntryDAO
601                 entry = EntryHome.findByPrimaryKey( entry.getIdEntry( ) );
602 
603                 Entry entryParent = EntryHome.findByPrimaryKey( entry.getParent( ).getIdEntry( ) );
604                 _entryService.moveUpEntryOrder( entryParent.getPosition( ) + entryParent.getChildren( ).size( ), entry );
605             }
606 
607             if ( entry.getFieldDepend( ) != null )
608             {
609                 return redirect( request, CategoryFieldJspBean.getUrlModifyField( request, entry.getFieldDepend( ).getIdField( ) ) );
610             }
611 
612             return redirect( request, CategoryJspBean.getUrlModifyCategory( request, entry.getIdResource( ) ) );
613         }
614 
615         return redirect( request, CategoryJspBean.getUrlManageCategories( request ) );
616     }
617 
618     /**
619      * Change the attribute's order (move up or move down in the list)
620      * 
621      * @param request
622      *            the request
623      * @return The newt URL to redirect to
624      */
625     @Action( ACTION_DO_CHANGE_ORDER_ENTRY )
626     public String doChangeOrderEntry( HttpServletRequest request )
627     {
628         // gets the entry which needs to be changed (order)
629         String strIdCategory = request.getParameter( PARAMETER_ID_CATEGORY );
630         int nIdCategory = Integer.parseInt( strIdCategory );
631 
632         // If the parameter move.x has been set, then we have to add entries to a group
633         if ( StringUtils.isNotEmpty( request.getParameter( PARAMETER_ADD_TO_GROUP ) ) )
634         {
635             String strIdEntryGroup = request.getParameter( PARAMETER_ID_ENTRY_GROUP );
636 
637             if ( StringUtils.isNotEmpty( strIdEntryGroup ) && StringUtils.isNumeric( strIdEntryGroup ) )
638             {
639                 int nIdEntryGroup = Integer.parseInt( strIdEntryGroup );
640                 Entry entryParent = EntryHome.findByPrimaryKey( nIdEntryGroup );
641                 String [ ] strArrayIdEntries = request.getParameterValues( PARAMETER_ENTRY_ID_MOVE );
642 
643                 if ( ( strArrayIdEntries != null ) && ( strArrayIdEntries.length > 0 ) )
644                 {
645                     for ( String strIdEntry : strArrayIdEntries )
646                     {
647                         if ( StringUtils.isNotEmpty( strIdEntry ) && StringUtils.isNumeric( strIdEntry ) )
648                         {
649                             int nIdEntry = Integer.parseInt( strIdEntry );
650                             _entryService.moveEntryIntoGroup( EntryHome.findByPrimaryKey( nIdEntry ), entryParent );
651                         }
652                     }
653                 }
654             }
655         }
656         else
657         {
658             Integer nEntryId = Integer.parseInt( request.getParameter( PARAMETER_ID_ENTRY ) );
659             Integer nOrderToSet = Integer.parseInt( request.getParameter( PARAMETER_ORDER_ID + request.getParameter( PARAMETER_ID_ENTRY ) ) );
660 
661             Entry entryToChangeOrder = EntryHome.findByPrimaryKey( nEntryId );
662             int nActualOrder = entryToChangeOrder.getPosition( );
663 
664             // does nothing if the order to set is equal to the actual order
665             if ( nOrderToSet != nActualOrder )
666             {
667                 // entry goes up in the list
668                 if ( nOrderToSet < entryToChangeOrder.getPosition( ) )
669                 {
670                     _entryService.moveUpEntryOrder( nOrderToSet, entryToChangeOrder );
671                 }
672 
673                 // entry goes down in the list
674                 else
675                 {
676                     _entryService.moveDownEntryOrder( nOrderToSet, entryToChangeOrder );
677                 }
678             }
679         }
680 
681         return redirect( request, CategoryJspBean.getUrlModifyCategory( request, nIdCategory ) );
682     }
683 
684     /**
685      * Remove an entry from a group
686      * 
687      * @param request
688      *            The request
689      * @return The newt URL to redirect to
690      */
691     @Action( ACTION_DO_MOVE_OUT_ENTRY )
692     public String doMoveOutEntry( HttpServletRequest request )
693     {
694         String strIdEntry = request.getParameter( PARAMETER_ID_ENTRY );
695 
696         if ( StringUtils.isNotEmpty( strIdEntry ) && StringUtils.isNumeric( strIdEntry ) )
697         {
698             int nIdEntry = Integer.parseInt( strIdEntry );
699             Entry entry = EntryHome.findByPrimaryKey( nIdEntry );
700 
701             if ( entry.getParent( ) != null )
702             {
703                 _entryService.moveOutEntryFromGroup( entry );
704             }
705 
706             return redirect( request, CategoryJspBean.getUrlModifyCategory( request, entry.getIdResource( ) ) );
707         }
708 
709         return redirect( request, CategoryJspBean.getUrlManageCategories( request ) );
710     }
711 
712     /**
713      * Delete the association between a field and and regular expression
714      * 
715      * @param request
716      *            the HTTP Request
717      * @return The URL to go after performing the action
718      */
719     @Action( ACTION_DO_REMOVE_REGULAR_EXPRESSION )
720     public String doRemoveRegularExpression( HttpServletRequest request )
721     {
722         String strIdExpression = request.getParameter( PARAMETER_ID_EXPRESSION );
723         String strIdField = request.getParameter( PARAMETER_ID_FIELD );
724 
725         if ( StringUtils.isNotEmpty( strIdExpression ) && StringUtils.isNotEmpty( strIdField ) && StringUtils.isNumeric( strIdExpression )
726                 && StringUtils.isNumeric( strIdField ) )
727         {
728             int nIdField = Integer.parseInt( strIdField );
729             int nIdExpression = Integer.parseInt( strIdExpression );
730             FieldHome.removeVerifyBy( nIdField, nIdExpression );
731 
732             Field field = FieldHome.findByPrimaryKey( nIdField );
733 
734             return redirect( request, VIEW_GET_MODIFY_ENTRY, PARAMETER_ID_ENTRY, field.getParentEntry( ).getIdEntry( ) );
735         }
736 
737         return redirect( request, CategoryJspBean.getUrlManageCategories( request ) );
738     }
739 
740     /**
741      * Insert an association between a field and a regular expression
742      * 
743      * @param request
744      *            the HTTP Request
745      * @return The URL to go after performing the action
746      */
747     @Action( ACTION_DO_INSERT_REGULAR_EXPRESSION )
748     public String doInsertRegularExpression( HttpServletRequest request )
749     {
750         String strIdExpression = request.getParameter( PARAMETER_ID_EXPRESSION );
751         String strIdField = request.getParameter( PARAMETER_ID_FIELD );
752 
753         if ( StringUtils.isNotEmpty( strIdExpression ) && StringUtils.isNotEmpty( strIdField ) && StringUtils.isNumeric( strIdExpression )
754                 && StringUtils.isNumeric( strIdField ) )
755         {
756             int nIdField = Integer.parseInt( strIdField );
757             int nIdExpression = Integer.parseInt( strIdExpression );
758 
759             FieldHome.createVerifyBy( nIdField, nIdExpression );
760 
761             Field field = FieldHome.findByPrimaryKey( nIdField );
762 
763             return redirect( request, VIEW_GET_MODIFY_ENTRY, PARAMETER_ID_ENTRY, field.getParentEntry( ).getIdEntry( ) );
764         }
765 
766         return redirect( request, CategoryJspBean.getUrlManageCategories( request ) );
767     }
768 
769     /**
770      * Get the URL to modify an entry
771      * 
772      * @param request
773      *            The request
774      * @param nIdEntry
775      *            The id of the entry
776      * @return The URL to modify the given entry
777      */
778     public static String getURLModifyEntry( HttpServletRequest request, int nIdEntry )
779     {
780         UrlItem urlItem = new UrlItem( AppPathService.getBaseUrl( request ) + JSP_URL_MANAGE_CATEGORY_ENTRIES );
781         urlItem.addParameter( MVCUtils.PARAMETER_VIEW, VIEW_GET_MODIFY_ENTRY );
782         urlItem.addParameter( PARAMETER_ID_ENTRY, nIdEntry );
783 
784         return urlItem.getUrl( );
785     }
786 }