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.contact.web;
35  
36  import fr.paris.lutece.plugins.contact.business.Contact;
37  import fr.paris.lutece.plugins.contact.business.ContactHome;
38  import fr.paris.lutece.plugins.contact.business.ContactList;
39  import fr.paris.lutece.plugins.contact.business.ContactListHome;
40  import fr.paris.lutece.portal.business.role.RoleHome;
41  import fr.paris.lutece.portal.service.message.AdminMessage;
42  import fr.paris.lutece.portal.service.message.AdminMessageService;
43  import fr.paris.lutece.portal.service.template.AppTemplateService;
44  import fr.paris.lutece.portal.service.util.AppPathService;
45  import fr.paris.lutece.portal.service.util.AppPropertiesService;
46  import fr.paris.lutece.portal.service.workgroup.AdminWorkgroupService;
47  import fr.paris.lutece.portal.util.mvc.admin.MVCAdminJspBean;
48  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
49  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
50  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
51  import fr.paris.lutece.portal.web.admin.PluginAdminPageJspBean;
52  import fr.paris.lutece.portal.web.constants.Messages;
53  import fr.paris.lutece.portal.web.util.LocalizedPaginator;
54  import fr.paris.lutece.util.ReferenceItem;
55  import fr.paris.lutece.util.ReferenceList;
56  import fr.paris.lutece.util.html.HtmlTemplate;
57  import fr.paris.lutece.util.html.Paginator;
58  import fr.paris.lutece.util.url.UrlItem;
59  
60  import java.util.Collection;
61  import java.util.HashMap;
62  import java.util.List;
63  import java.util.Map;
64  
65  import javax.servlet.http.HttpServletRequest;
66  
67  /**
68   * This class provides the user interface to manage contact features ( manage, create, modify, remove, change order of contact )
69   */
70  @Controller( controllerJsp = "ManageContactLists.jsp", controllerPath = "jsp/admin/plugins/contact/", right = ContactListJspBean.RIGHT_MANAGE_CONTACT )
71  public class ContactListJspBean extends MVCAdminJspBean
72  {
73      // Right
74      public static final String RIGHT_MANAGE_CONTACT = "CONTACT_MANAGEMENT";
75  
76      // properties for page titles
77      private static final String PROPERTY_PAGE_TITLE_CONTACTS = "contact.manage_contact_lists.pageTitle";
78      private static final String PROPERTY_PAGE_TITLE_CREATE_CONTACTS = "contact.create_contact_list.pageTitle";
79      private static final String PROPERTY_PAGE_TITLE_MANAGE_LIST_ASSIGNATIONS = "contact.contact_list_assignations.pageTitle";
80      private static final String PROPERTY_PAGE_TITLE_MANAGE_CONTACT_ASSIGNATIONS = "contact.contact_assignations.pageTitle";
81      private static final String PROPERTY_PAGE_TITLE_MODIFY = "contact.modify_contact_list.pageTitle";
82  
83      // templates
84      private static final String TEMPLATE_MANAGE_CONTACT_LISTS = "/admin/plugins/contact/manage_contact_lists.html";
85      private static final String TEMPLATE_CREATE_CONTACT = "/admin/plugins/contact/create_contact_list.html";
86      private static final String TEMPLATE_MANAGE_LIST_ASSIGNATIONS = "/admin/plugins/contact/manage_list_assignations.html";
87      private static final String TEMPLATE_MANAGE_CONTACT_ASSIGNATIONS = "/admin/plugins/contact/manage_contact_assignations.html";
88      private static final String TEMPLATE_MODIFY_CONTACT_LIST = "/admin/plugins/contact/modify_contact_list.html";
89  
90      // Markers
91      private static final String MARK_CONTACT_LIST = "contact_list"; // one contactList
92      private static final String MARK_LIST_CONTACTS = "list_contacts";
93      private static final String MARK_LIST_CONTACT_LIST = "list_contact_list";
94      private static final String MARK_CONTACTS_NUMBER = "contacts_number";
95      private static final String MARK_CONTACT_ORDER_LIST = "order_list";
96      private static final String MARK_ASSIGNED_CONTACT_LIST = "assigned_contacts";
97      private static final String MARK_CONTACT = "contact";
98      private static final String MARK_NOT_ASSIGNED_LISTS = "not_assigned_lists";
99      private static final String MARK_ASSIGNED_LISTS = "assigned_lists";
100     private static final String MARK_WORKGROUPS_LIST = "workgroups_list";
101     private static final String MARK_PAGINATOR = "paginator";
102     private static final String MARK_NB_ITEMS_PER_PAGE = "nb_items_per_page";
103     private static final String MARK_ROLES_LIST = "roles_list";
104     private static final String MARK_WEBAPP_URL = "webapp_url";
105     private static final String MARK_LOCALE = "locale";
106 
107     // Parameters
108     private static final String PARAMETER_CONTACT_LIST_LABEL = "contact_list_label";
109     private static final String PARAMETER_CONTACT_LIST_DESCRIPTION = "contact_list_description";
110     private static final String PARAMETER_PAGE_INDEX = "page_index";
111     private static final String PARAMETER_ID_CONTACT_LIST = "id_contact_list";
112     private static final String PARAMETER_ID_CONTACT = "id_contact";
113     private static final String PARAMETER_CONTACT_LIST = "list_contact";
114     private static final String PARAMETER_CONTACTS_ORDER = "contacts_order";
115     private static final String PARAMETER_CONTACT_LIST_ORDER = "contact_list_order";
116     private static final String PARAMETER_WORKGROUP = "workgroup";
117     private static final String PARAMETER_ROLE = "role";
118     private static final String PARAMETER_TOS = "active_tos";
119     private static final String PARAMETER_TOS_MESSAGE = "tos_message";
120     private static final String PARAMETER_CANCEL = "cancel";
121 
122     // Properties
123     private static final String PROPERTY_DEFAULT_CONTACT_LIST_PER_PAGE = "contact.contactList.itemsPerPage";
124 
125     // messages
126     private static final String MESSAGE_CONFIRM_REMOVE_CONTACT_LIST = "contact.message.confirmRemoveContactList";
127 
128     // Views
129     private static final String VIEW_CREATE_CONTACT_LIST = "viewCreateContactList";
130     private static final String VIEW_MODIFY_CONTACT_LIST = "viewModifyContactList";
131     private static final String VIEW_MANAGE_CONTACT_LISTS = "viewManageContactLists";
132     private static final String VIEW_CONFIRM_REMOVE_CONTACT_LIST = "viewConfirmRemoveContactList";
133     private static final String VIEW_MANAGE_CONTACT_LIST_ASSIGNATIONS = "viewManageContactListAssignation";
134     private static final String VIEW_MANAGE_CONTACT_ASSIGNATIONS = "viewManageContactAssignation";
135 
136     // Actions
137     private static final String ACTION_CREATE_CONTACT_LIST = "actionCreateContactList";
138     private static final String ACTION_MODIFY_CONTACT_LIST = "actionModifyContactList";
139     private static final String ACTION_REMOVE_CONTACT_LIST = "actionRemoveContactList";
140     private static final String ACTION_MODIFY_CONTACT_LIST_ORDER = "actionModifyContactListOrder";
141     private static final String ACTION_MODIFY_CONTACT_ORDER = "actionModifyContactOrder";
142     private static final String ACTION_ASSIGN_LISTS_TO_CONTACT = "actionAssignListsToContact";
143     private static final String ACTION_REVOKE_LISTS_FROM_CONTACT = "actionRevokeListsFromContact";
144     private static final String ACTION_ASSIGN_CONTACTS_TO_LIST = "actionAssignContactsToList";
145     private static final String ACTION_REVOKE_CONTACTS_FROM_LIST = "actionRevokeContactsFromList";
146 
147     // Variables
148     private int _nDefaultItemsPerPage;
149     private String _strCurrentPageIndex;
150     private int _nItemsPerPage;
151 
152     /**
153      * returns the template of the contactLists management
154      * 
155      * @param request
156      *            The HttpRequest
157      * @return template of lists management
158      */
159     @View( value = VIEW_MANAGE_CONTACT_LISTS, defaultView = true )
160     public String getManageContactLists( HttpServletRequest request )
161     {
162         _strCurrentPageIndex = Paginator.getPageIndex( request, Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
163         _nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_DEFAULT_CONTACT_LIST_PER_PAGE, 50 );
164         _nItemsPerPage = Paginator.getItemsPerPage( request, Paginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage, _nDefaultItemsPerPage );
165 
166         Collection<ContactList> listContactList = ContactListHome.findAll( getPlugin( ) );
167 
168         listContactList = AdminWorkgroupService.getAuthorizedCollection( listContactList, getUser( ) );
169 
170         LocalizedPaginator paginator = new LocalizedPaginator( (List<ContactList>) listContactList, _nItemsPerPage, getViewUrl( VIEW_MANAGE_CONTACT_LISTS ),
171                 PARAMETER_PAGE_INDEX, _strCurrentPageIndex, getLocale( ) );
172 
173         Map<String, Object> model = new HashMap<String, Object>( );
174 
175         model.put( MARK_NB_ITEMS_PER_PAGE, "" + _nItemsPerPage );
176         model.put( MARK_CONTACT_ORDER_LIST, getContactListOrderList( ) );
177         model.put( MARK_PAGINATOR, paginator );
178         model.put( MARK_LIST_CONTACT_LIST, paginator.getPageItems( ) );
179 
180         return getPage( PROPERTY_PAGE_TITLE_CONTACTS, TEMPLATE_MANAGE_CONTACT_LISTS, model );
181     }
182 
183     /**
184      * returns the form of contactList creation
185      * 
186      * @param request
187      *            The HttpRequest
188      * @return the HTML code of contactList form
189      */
190     @View( VIEW_CREATE_CONTACT_LIST )
191     public String getCreateContactList( HttpServletRequest request )
192     {
193         Map<String, Object> model = new HashMap<String, Object>( );
194         ReferenceList workgroupsList = AdminWorkgroupService.getUserWorkgroups( getUser( ), getLocale( ) );
195         model.put( MARK_WEBAPP_URL, AppPathService.getBaseUrl( request ) );
196         model.put( MARK_LOCALE, getLocale( ).getLanguage( ) );
197         model.put( MARK_WORKGROUPS_LIST, workgroupsList );
198         model.put( MARK_ROLES_LIST, RoleHome.getRolesList( ) );
199 
200         return getPage( PROPERTY_PAGE_TITLE_CREATE_CONTACTS, TEMPLATE_CREATE_CONTACT, model );
201     }
202 
203     /**
204      * Inserts a contactList into Database
205      * 
206      * @param request
207      *            The HttpRequest
208      * @return the lsit of contactLists
209      */
210     @Action( ACTION_CREATE_CONTACT_LIST )
211     public String doCreateContactList( HttpServletRequest request )
212     {
213         ContactListbusiness/ContactList.html#ContactList">ContactList contactList = new ContactList( );
214         String strLabel = request.getParameter( PARAMETER_CONTACT_LIST_LABEL );
215         String strDescription = request.getParameter( PARAMETER_CONTACT_LIST_DESCRIPTION );
216         String strWorkgroup = request.getParameter( PARAMETER_WORKGROUP );
217         String strRole = request.getParameter( PARAMETER_ROLE );
218         boolean bTos = request.getParameter( PARAMETER_TOS ) != null;
219         String strTosMessage = request.getParameter( PARAMETER_TOS_MESSAGE );
220 
221         if ( ( strLabel.equals( "" ) ) || ( strDescription.equals( "" ) ) )
222         {
223             return redirect( request, AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP ) );
224         }
225 
226         contactList.setLabel( strLabel );
227         contactList.setDescription( strDescription );
228         contactList.setWorkgroup( strWorkgroup );
229         contactList.setRole( strRole );
230         contactList.setTos( bTos );
231         contactList.setTosMessage( strTosMessage );
232         ContactListHome.create( contactList, getPlugin( ) );
233 
234         // if the operation occurred well, redirects towards the list of the ContactList
235         return redirectView( request, VIEW_MANAGE_CONTACT_LISTS );
236     }
237 
238     /**
239      * returns the template of modification form
240      * 
241      * @param request
242      *            The HttpRequest
243      * @return the HTML code of modify contactList form
244      */
245     @View( VIEW_MODIFY_CONTACT_LIST )
246     public String getModifyContactList( HttpServletRequest request )
247     {
248         Map<String, Object> model = new HashMap<String, Object>( );
249 
250         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_CONTACT_LIST ) );
251 
252         ReferenceList workgroupsList = AdminWorkgroupService.getUserWorkgroups( getUser( ), getLocale( ) );
253         ContactList contactList = ContactListHome.findByPrimaryKey( nId, getPlugin( ) );
254 
255         if ( contactList == null )
256         {
257             return getManageContactLists( request );
258         }
259 
260         model.put( MARK_WEBAPP_URL, AppPathService.getBaseUrl( request ) );
261         model.put( MARK_LOCALE, getLocale( ).getLanguage( ) );
262         model.put( MARK_WORKGROUPS_LIST, workgroupsList );
263         model.put( MARK_CONTACT_LIST, contactList );
264         model.put( MARK_ROLES_LIST, RoleHome.getRolesList( ) );
265 
266         return getPage( PROPERTY_PAGE_TITLE_MODIFY, TEMPLATE_MODIFY_CONTACT_LIST, model );
267     }
268 
269     /**
270      * updates the contactList in database
271      * 
272      * @param request
273      *            The HttpRequest
274      * @return the contactList list
275      */
276     @Action( ACTION_MODIFY_CONTACT_LIST )
277     public String doModifyContactList( HttpServletRequest request )
278     {
279         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_CONTACT_LIST ) );
280         ContactList contactList = ContactListHome.findByPrimaryKey( nId, getPlugin( ) );
281         contactList.setLabel( request.getParameter( PARAMETER_CONTACT_LIST_LABEL ) );
282         contactList.setDescription( request.getParameter( PARAMETER_CONTACT_LIST_DESCRIPTION ) );
283         contactList.setWorkgroup( request.getParameter( PARAMETER_WORKGROUP ) );
284         contactList.setRole( request.getParameter( PARAMETER_ROLE ) );
285         contactList.setTos( request.getParameter( PARAMETER_TOS ) != null );
286         contactList.setTosMessage( request.getParameter( PARAMETER_TOS_MESSAGE ) );
287 
288         // Mandatory fields
289         if ( request.getParameter( PARAMETER_CONTACT_LIST_LABEL ).equals( "" ) )
290         {
291             return redirect( request, AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP ) );
292         }
293 
294         ContactListHome.update( contactList, getPlugin( ) );
295 
296         return redirectView( request, VIEW_MANAGE_CONTACT_LISTS );
297     }
298 
299     /**
300      * Manages the removal form of a contact list
301      * 
302      * @param request
303      *            The Http request
304      * @return the html code to confirm
305      */
306     @View( VIEW_CONFIRM_REMOVE_CONTACT_LIST )
307     public String getConfirmRemoveContactList( HttpServletRequest request )
308     {
309         UrlItem url = new UrlItem( getActionUrl( ACTION_REMOVE_CONTACT_LIST ) );
310         url.addParameter( PARAMETER_ID_CONTACT_LIST, request.getParameter( PARAMETER_ID_CONTACT_LIST ) );
311 
312         return redirect( request,
313                 AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_CONTACT_LIST, url.getUrl( ), AdminMessage.TYPE_CONFIRMATION ) );
314     }
315 
316     /**
317      * removes a record from database
318      * 
319      * @param request
320      *            The HttpRequest
321      * @return the contactList list
322      */
323     @Action( ACTION_REMOVE_CONTACT_LIST )
324     public String doRemoveContactList( HttpServletRequest request )
325     {
326         int nIdContactList = Integer.parseInt( request.getParameter( PARAMETER_ID_CONTACT_LIST ) );
327         int nOrder = ContactListHome.getContactListOrderById( nIdContactList, getPlugin( ) );
328         int nNewOrder = ContactListHome.getMaxOrderContactList( getPlugin( ) );
329         ContactListHome.unassignContactsForList( nIdContactList, getPlugin( ) );
330         modifyContactListsOrder( nOrder, nNewOrder, nIdContactList );
331         ContactListHome.remove( nIdContactList, getPlugin( ) );
332 
333         // Go to the parent page
334         return redirectView( request, VIEW_MANAGE_CONTACT_LISTS );
335     }
336 
337     /**
338      * returns the template of list assignations
339      * 
340      * @param request
341      *            The HttpRequest
342      * @return the HTML code of list assignations
343      */
344     @View( VIEW_MANAGE_CONTACT_LIST_ASSIGNATIONS )
345     public String getManageListAssignations( HttpServletRequest request )
346     {
347         String strIdContactList = request.getParameter( PARAMETER_ID_CONTACT_LIST );
348         int nIdContactList = Integer.parseInt( strIdContactList );
349         ContactList contactList = ContactListHome.findByPrimaryKey( nIdContactList, getPlugin( ) ); // The contactList object concerned -- One obecjt
350 
351         if ( contactList == null )
352         {
353             return getManageContactLists( request );
354         }
355 
356         Collection<Contact> notAssignedContacts = ContactListHome.getNotAssignedContactsFor( nIdContactList, getPlugin( ) ); // The list of contacts objects --
357                                                                                                                              // One list of saveral objects
358         notAssignedContacts = AdminWorkgroupService.getAuthorizedCollection( notAssignedContacts, getUser( ) );
359 
360         ReferenceList refListNotAssigned = new ReferenceList( );
361 
362         for ( Contact contact : notAssignedContacts )
363         {
364             ReferenceItem item = new ReferenceItem( );
365             item.setCode( Integer.toString( contact.getId( ) ) );
366             item.setName( "[ " + contact.getWorkgroup( ) + " ] " + contact.getName( ) );
367             refListNotAssigned.add( item );
368         }
369 
370         Collection<Contact> assignedContacts = ContactListHome.getAssignedContactsFor( nIdContactList, getPlugin( ) );
371 
372         Map<String, Object> model = new HashMap<String, Object>( );
373         model.put( MARK_CONTACT_LIST, contactList );
374         model.put( MARK_LIST_CONTACTS, refListNotAssigned );
375         model.put( MARK_ASSIGNED_CONTACT_LIST, assignedContacts );
376         model.put( MARK_CONTACTS_NUMBER, ContactListHome.countContactsForList( nIdContactList, getPlugin( ) ) );
377         model.put( MARK_CONTACT_ORDER_LIST, getContactOrderList( nIdContactList ) );
378 
379         return getPage( PROPERTY_PAGE_TITLE_MANAGE_LIST_ASSIGNATIONS, TEMPLATE_MANAGE_LIST_ASSIGNATIONS, model );
380     }
381 
382     /**
383      * assigns a contact to a list in database
384      * 
385      * @param request
386      *            The HttpRequest
387      * @return the template of list assignations
388      */
389     @Action( ACTION_ASSIGN_CONTACTS_TO_LIST )
390     public String doAssignContactsToList( HttpServletRequest request )
391     {
392         String strActionCancel = request.getParameter( PARAMETER_CANCEL );
393 
394         if ( strActionCancel != null )
395         {
396             return redirectView( request, VIEW_MANAGE_CONTACT_LISTS );
397         }
398 
399         // retrieve the selected portlets ids
400         String [ ] arrayContactsIds = request.getParameterValues( PARAMETER_CONTACT_LIST );
401         int nIdContactList = Integer.parseInt( request.getParameter( PARAMETER_ID_CONTACT_LIST ) );
402 
403         if ( ( arrayContactsIds != null ) )
404         {
405             for ( int i = 0; i < arrayContactsIds.length; i++ )
406             {
407                 if ( !ContactListHome.isAssigned( Integer.parseInt( arrayContactsIds [i] ), nIdContactList, getPlugin( ) ) )
408                 {
409                     ContactListHome.assign( Integer.parseInt( arrayContactsIds [i] ), nIdContactList, getPlugin( ) );
410                 }
411             }
412         }
413 
414         return redirect( request, VIEW_MANAGE_CONTACT_LIST_ASSIGNATIONS, PARAMETER_ID_CONTACT_LIST, nIdContactList );
415     }
416 
417     /**
418      * unassigns contact from list
419      * 
420      * @param request
421      *            The HttpRequest
422      * @return the HTML code of list assignations
423      */
424     @Action( ACTION_REVOKE_CONTACTS_FROM_LIST )
425     public String doUnAssignContact( HttpServletRequest request )
426     {
427         int nIdContactList = Integer.parseInt( request.getParameter( PARAMETER_ID_CONTACT_LIST ) );
428         int nIdContact = Integer.parseInt( request.getParameter( PARAMETER_ID_CONTACT ) );
429 
430         int nOrder = ContactHome.getContactOrderById( nIdContact, nIdContactList, getPlugin( ) );
431         int nNewOrder = ContactListHome.getMaxOrderContact( nIdContactList, getPlugin( ) );
432         modifyContactsOrder( nIdContact, nOrder, nNewOrder, nIdContactList );
433         ContactListHome.unAssign( nIdContact, nIdContactList, getPlugin( ) );
434 
435         return redirect( request, VIEW_MANAGE_CONTACT_LIST_ASSIGNATIONS, PARAMETER_ID_CONTACT_LIST, nIdContactList );
436     }
437 
438     /**
439      * unassigns list from a contact
440      * 
441      * @param request
442      *            The HttpRequest
443      * @return the html code of contact assignations
444      */
445     @Action( ACTION_REVOKE_LISTS_FROM_CONTACT )
446     public String doUnAssignList( HttpServletRequest request )
447     {
448         int nIdContactList = Integer.parseInt( request.getParameter( PARAMETER_ID_CONTACT_LIST ) );
449         int nIdContact = Integer.parseInt( request.getParameter( PARAMETER_ID_CONTACT ) );
450         ContactListHome.unAssign( nIdContact, nIdContactList, getPlugin( ) );
451 
452         return redirect( request, VIEW_MANAGE_CONTACT_ASSIGNATIONS, PARAMETER_ID_CONTACT, nIdContact );
453     }
454 
455     /**
456      * gets the assignations page of a contact
457      * 
458      * @param request
459      *            The HttpRequest
460      * @return the HTML code of contact assignations
461      */
462     @View( VIEW_MANAGE_CONTACT_ASSIGNATIONS )
463     public String getManageContactAssignations( HttpServletRequest request )
464     {
465         int nIdContact = Integer.parseInt( request.getParameter( PARAMETER_ID_CONTACT ) );
466         Contact contact = ContactHome.findByPrimaryKey( nIdContact, getPlugin( ) );
467         Collection<ContactList> notAssignedLists = ContactListHome.getNotAssignedListsFor( nIdContact, getPlugin( ) );
468         notAssignedLists = AdminWorkgroupService.getAuthorizedCollection( notAssignedLists, getUser( ) );
469 
470         ReferenceList refListNotAssigned = new ReferenceList( );
471 
472         for ( ContactList contactList : notAssignedLists )
473         {
474             ReferenceItem item = new ReferenceItem( );
475             item.setCode( Integer.toString( contactList.getId( ) ) );
476             item.setName( contactList.getLabel( ) );
477             refListNotAssigned.add( item );
478         }
479 
480         Collection<ContactList> assignedLists = ContactListHome.getAssignedListsFor( nIdContact, getPlugin( ) );
481 
482         Map<String, Object> model = new HashMap<String, Object>( );
483         model.put( MARK_CONTACT, contact );
484         model.put( MARK_NOT_ASSIGNED_LISTS, refListNotAssigned );
485         model.put( MARK_ASSIGNED_LISTS, assignedLists );
486 
487         return getPage( PROPERTY_PAGE_TITLE_MANAGE_CONTACT_ASSIGNATIONS, TEMPLATE_MANAGE_CONTACT_ASSIGNATIONS, model );
488     }
489 
490     /**
491      * assigns lists to one contact
492      * 
493      * @param request
494      *            The HttpRequest
495      * @return the HTML code of contact assignations
496      */
497     @Action( ACTION_ASSIGN_LISTS_TO_CONTACT )
498     public String doAssignListsToContact( HttpServletRequest request )
499     {
500         String strActionCancel = request.getParameter( PARAMETER_CANCEL );
501 
502         if ( strActionCancel != null )
503         {
504             return redirectView( request, VIEW_MANAGE_CONTACT_LISTS );
505         }
506 
507         // retrieve the selected portlets ids
508         String [ ] arrayListsIds = request.getParameterValues( PARAMETER_CONTACT_LIST );
509         int nIdContact = Integer.parseInt( request.getParameter( PARAMETER_ID_CONTACT ) );
510 
511         if ( ( arrayListsIds != null ) )
512         {
513             for ( int i = 0; i < arrayListsIds.length; i++ )
514             {
515                 if ( !ContactListHome.isAssigned( nIdContact, Integer.parseInt( arrayListsIds [i] ), getPlugin( ) ) )
516                 {
517                     ContactListHome.assign( nIdContact, Integer.parseInt( arrayListsIds [i] ), getPlugin( ) );
518                 }
519             }
520         }
521 
522         return redirect( request, VIEW_MANAGE_CONTACT_ASSIGNATIONS, PARAMETER_ID_CONTACT, nIdContact );
523     }
524 
525     /**
526      * Modifies the order in the list of contacts
527      * 
528      * @param request
529      *            The Http request
530      * @return The Jsp URL of the process result
531      */
532     @Action( ACTION_MODIFY_CONTACT_ORDER )
533     public String doModifyContactsOrder( HttpServletRequest request )
534     {
535         int nIdContact = Integer.parseInt( request.getParameter( PARAMETER_ID_CONTACT ) );
536         int nIdContactList = Integer.parseInt( request.getParameter( PARAMETER_ID_CONTACT_LIST ) );
537 
538         int nOrder = ContactHome.getContactOrderById( nIdContact, nIdContactList, getPlugin( ) );
539         int nNewOrder = Integer.parseInt( request.getParameter( PARAMETER_CONTACTS_ORDER ) );
540         modifyContactsOrder( nIdContact, nOrder, nNewOrder, nIdContactList );
541 
542         return redirect( request, VIEW_MANAGE_CONTACT_LIST_ASSIGNATIONS, PARAMETER_ID_CONTACT_LIST, nIdContactList );
543     }
544 
545     /**
546      * Builts a list of sequence numbers
547      * 
548      * @param nIdContactList
549      *            the id of the contactList
550      * @return the list of sequence numbers
551      */
552     private ReferenceList getContactOrderList( int nIdContactList )
553     {
554         int nMax = ContactListHome.getMaxOrderContact( nIdContactList, getPlugin( ) );
555         ReferenceList list = new ReferenceList( );
556 
557         for ( int i = 1; i < ( nMax + 1 ); i++ )
558         {
559             list.addItem( i, Integer.toString( i ) );
560         }
561 
562         return list;
563     }
564 
565     //////////////////////////////////////////////////////////////////////////////////
566     // Private implementation
567 
568     /**
569      * Modify the place in the list for a contact
570      * 
571      * @param nId
572      *            the contact identifier
573      * @param nOrder
574      *            the actual place in the list
575      * @param nNewOrder
576      *            the new place in the list
577      * @param nIdContactList
578      *            the id of the contactList
579      */
580     private void modifyContactsOrder( int nId, int nOrder, int nNewOrder, int nIdContactList )
581     {
582         if ( nNewOrder < nOrder )
583         {
584             for ( int i = nOrder - 1; i > ( nNewOrder - 1 ); i-- )
585             {
586                 int nIdContactOrder = ContactHome.getContactIdByOrder( i, nIdContactList, getPlugin( ) );
587                 ContactHome.updateContactOrder( i + 1, nIdContactOrder, nIdContactList, getPlugin( ) );
588             }
589 
590             ContactHome.updateContactOrder( nNewOrder, nId, nIdContactList, getPlugin( ) );
591         }
592         else
593         {
594             for ( int i = nOrder; i < ( nNewOrder + 1 ); i++ )
595             {
596                 int nIdContactOrder = ContactHome.getContactIdByOrder( i, nIdContactList, getPlugin( ) );
597                 ContactHome.updateContactOrder( i - 1, nIdContactOrder, nIdContactList, getPlugin( ) );
598             }
599 
600             ContactHome.updateContactOrder( nNewOrder, nId, nIdContactList, getPlugin( ) );
601         }
602     }
603 
604     /**
605      * Modifies the order in the list of contactLists
606      * 
607      * @param request
608      *            The Http request
609      * @return The Jsp URL of the process result
610      */
611     @Action( ACTION_MODIFY_CONTACT_LIST_ORDER )
612     public String doModifyContactListsOrder( HttpServletRequest request )
613     {
614         int nIdContactList = Integer.parseInt( request.getParameter( PARAMETER_ID_CONTACT_LIST ) );
615 
616         int nOrder = ContactListHome.getContactListOrderById( nIdContactList, getPlugin( ) );
617         int nNewOrder = Integer.parseInt( request.getParameter( PARAMETER_CONTACT_LIST_ORDER ) );
618         modifyContactListsOrder( nOrder, nNewOrder, nIdContactList );
619 
620         return redirectView( request, VIEW_MANAGE_CONTACT_LISTS );
621     }
622 
623     /**
624      * Builts a list of sequence numbers
625      * 
626      * @return the list of sequence numbers
627      */
628     private ReferenceList getContactListOrderList( )
629     {
630         int nMax = ContactListHome.getMaxOrderContactList( getPlugin( ) );
631         ReferenceList list = new ReferenceList( );
632 
633         for ( int i = 1; i < ( nMax + 1 ); i++ )
634         {
635             list.addItem( i, Integer.toString( i ) );
636         }
637 
638         return list;
639     }
640 
641     //////////////////////////////////////////////////////////////////////////////////
642     // Private implementation
643 
644     /**
645      * Modify the place in the list for a contact
646      * 
647      * @param nId
648      *            the contact identifier
649      * @param nOrder
650      *            the actual place in the list
651      * @param nNewOrder
652      *            the new place in the list
653      * @param nIdContactList
654      *            the id of the contactList
655      */
656     private void modifyContactListsOrder( int nOrder, int nNewOrder, int nIdContactList )
657     {
658         if ( nNewOrder < nOrder )
659         {
660             for ( int i = nOrder - 1; i > ( nNewOrder - 1 ); i-- )
661             {
662                 int nIdContactListOrder = ContactListHome.getContactListIdByOrder( i, getPlugin( ) );
663                 ContactListHome.updateContactListOrder( i + 1, nIdContactListOrder, getPlugin( ) );
664             }
665 
666             ContactListHome.updateContactListOrder( nNewOrder, nIdContactList, getPlugin( ) );
667         }
668         else
669         {
670             for ( int i = nOrder; i < ( nNewOrder + 1 ); i++ )
671             {
672                 int nIdContactListOrder = ContactListHome.getContactListIdByOrder( i, getPlugin( ) );
673                 ContactListHome.updateContactListOrder( i - 1, nIdContactListOrder, getPlugin( ) );
674             }
675 
676             ContactListHome.updateContactListOrder( nNewOrder, nIdContactList, getPlugin( ) );
677         }
678     }
679 }