View Javadoc
1   /*
2    * Copyright (c) 2002-2017, 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.links.web;
35  
36  import fr.paris.lutece.plugins.links.business.Link;
37  import fr.paris.lutece.plugins.links.business.LinkHome;
38  import fr.paris.lutece.plugins.links.business.portlet.LinksPortletHome;
39  import fr.paris.lutece.portal.service.i18n.I18nService;
40  import fr.paris.lutece.portal.service.message.AdminMessage;
41  import fr.paris.lutece.portal.service.message.AdminMessageService;
42  import fr.paris.lutece.portal.service.template.AppTemplateService;
43  import fr.paris.lutece.portal.service.util.AppLogService;
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.web.admin.PluginAdminPageJspBean;
48  import fr.paris.lutece.portal.web.constants.Messages;
49  import fr.paris.lutece.portal.web.upload.MultipartHttpServletRequest;
50  import fr.paris.lutece.util.ReferenceItem;
51  import fr.paris.lutece.util.ReferenceList;
52  import fr.paris.lutece.util.html.HtmlTemplate;
53  import fr.paris.lutece.util.url.UrlItem;
54  
55  import org.apache.commons.fileupload.FileItem;
56  
57  import java.util.Collection;
58  import java.util.HashMap;
59  import java.util.List;
60  
61  import javax.servlet.http.HttpServletRequest;
62  
63  
64  /**
65   * This class provides the user interface to manage links features ( manage, create, modify, remove, change order of
66   * contact )
67   */
68  public class LinksLibraryJspBean extends PluginAdminPageJspBean
69  {
70      ////////////////////////////////////////////////////////////////////////////////////
71      // Constants
72  
73      /**
74       * 
75       */
76      private static final long serialVersionUID = 679961729642614958L;
77      // Jsp
78      private static final String JSP_LINKS_LIBRARY = "LinksLibrary.jsp";
79      private static final String JSP_LINKS_PAGE = "ManageLinksPage.jsp";
80      private static final String JSP_DO_REMOVE_LINK = "jsp/admin/plugins/links/DoRemoveLink.jsp";
81  
82      // Right
83      public static final String RIGHT_MANAGE_LINKS = "LINKS_MANAGEMENT";
84  
85      // Templates
86      private static final String TEMPLATE_LINKS_LIST = "admin/plugins/links/links_list.html";
87      private static final String TEMPLATE_MODIFY_LINK = "admin/plugins/links/modify_link.html";
88      private static final String TEMPLATE_CREATE_LINK = "admin/plugins/links/create_link.html";
89      private static final String TEMPLATE_MANAGE_LINKS_PAGE = "admin/plugins/links/manage_links_page.html";
90  
91      // Property
92      private static final String PROPERTY_PAGE_TITLE_LINKS_LIST = "links.links_list.pageTitle";
93      private static final String PROPERTY_PAGE_TITLE_CREATE_LINK = "links.create_link.pageTitle";
94      private static final String PROPERTY_PAGE_TITLE_MANAGE_LINKS_PAGES = "links.manage_links_page.pageTitle";
95      private static final String PROPERTY_PAGE_TITLE_MODIFY_LINK = "links.modify_link.pageTitle";
96      private static final String PROPERTY_LINKS_IMAGES_PATH = "links.images.path";
97      private static final String PROPERTY_NO_IMAGE_ADMIN = "links.no.image.admin";
98      private static final String PROPERTY_LINKS_URL_DEFAULT_KEY_NAME = "links.url.defaultKeyName";
99      private static final String PROPERTY_LINK_DESCRIPTION_SIZE = "links.description.size";
100     private static final String MESSAGE_CONFIRM_REMOVE_LINK = "links.message.confirmRemoveLink";
101     private static final String PROPERTY_CREATE_LINKS_URL_TITLE = "links.create_link.title";
102     private static final String PROPERTY_MODIFY_LINKS_URL_TITLE = "links.modify_link.title";
103 
104     // Marker
105     private static final String MARK_LINKS_LIST = "links_list";
106     private static final String MARK_ALTERNATE_URLS = "alternate_urls";
107     private static final String MARK_LINK = "link";
108     private static final String MARK_LIST_ALTERNATE_URLS = "alternate_urls_list";
109     private static final String MARK_ORDER_LIST_SELECTED_PORTLETS = "order_list_selected_portlets";
110     private static final String MARK_ORDER_LIST_UNSELECTED_PORTLETS = "order_list_unselected_portlets";
111     private static final String MARK_PORTLETS_LIST = "portlets_list";
112     private static final String MARK_UNSELECTED_PORTLETS_LIST = "unselected_portlets_list";
113     private static final String MARK_LINK_URL_DEFAULT = "url_default";
114     private static final String MARK_LINK_IMAGE_PATH = "images_path";
115     private static final String MARK_LINK_NO_IMAGE = "no_image";
116     private static final String MARK_WORKGROUPS_LIST = "workgroups_list";
117 
118     // Parameters
119     private static final String PARAMETER_LINK_ID = "link_id";
120     private static final String PARAMETER_LINK_NAME = "link_name";
121     private static final String PARAMETER_LINK_URL = "link_url";
122     private static final String PARAMETER_LINK_DESCRIPTION = "link_description";
123     private static final String PARAMETER_LINK_UPDATE_IMAGE = "update_image";
124     private static final String PARAMETER_PORTLET_ORDER = "portlet_order";
125     private static final String PARAMETER_NEW_PORTLET_ORDER = "new_portlet_order";
126     private static final String PARAMETER_PORTLET_ID = "portlet_id";
127     private static final String PARAMETER_SUFFIX_ALTERNATE_URL = "_url";
128     private static final String PARAMETER_SUFFIX_ALTERNATE_TYPE = "_type";
129     private static final String PARAMETER_LINK_IMAGE = "link_image";
130     private static final String PARAMETER_WORKGROUP = "workgroupKey";
131 
132     // Types for optional url input
133     private static final int TYPE_NEW_URL = 1;
134     private static final int TYPE_DEFAULT_URL = 2;
135     private static final int TYPE_NO_URL = 3;
136 
137     /**
138      * Creates a new LinksJspBean object.
139      */
140     public LinksLibraryJspBean(  )
141     {
142     }
143 
144     /**
145      * Returns the links list
146      *
147      * @return the html code for display the links list
148      */
149     public String getLinksList( HttpServletRequest request )
150     {
151         setPageTitleProperty( PROPERTY_PAGE_TITLE_LINKS_LIST );
152 
153         HashMap model = new HashMap(  );
154         String strDefaultUrl = AppPropertiesService.getProperty( PROPERTY_LINKS_URL_DEFAULT_KEY_NAME );
155         String strImagesPath = AppPropertiesService.getProperty( PROPERTY_LINKS_IMAGES_PATH );
156 
157         ReferenceList virtualHosts = new ReferenceList(  );
158         String strDefaultUrlName = "";
159 
160         try
161         {
162             if ( AppPathService.getAvailableVirtualHosts(  ) != null )
163             {
164                 virtualHosts = AppPathService.getAvailableVirtualHosts(  );
165 
166                 for ( ReferenceItem item : virtualHosts )
167                 {
168                     if ( ( strDefaultUrl != null ) && item.getCode(  ).equals( strDefaultUrl ) )
169                     {
170                         strDefaultUrlName = item.getName(  );
171                     }
172                 }
173             }
174         }
175         catch ( NullPointerException e )
176         {
177             AppLogService.error( e.getMessage(  ), e );
178         }
179 
180         //Replace links optional url code by url name
181         Collection<Link> links = LinkHome.getLinksList(  );
182         ReferenceList optUrls = null;
183 
184         for ( Link link : links )
185         {
186             optUrls = link.getOptionalUrls(  );
187 
188             for ( ReferenceItem item : virtualHosts )
189             {
190                 for ( ReferenceItem optUrl : optUrls )
191                 {
192                     if ( optUrl.getCode(  ).equals( item.getCode(  ) ) )
193                     {
194                         optUrl.setCode( item.getName(  ) );
195                     }
196                 }
197             }
198 
199             link.setOptionalUrls( optUrls );
200         }
201 
202         links = (List<Link>) AdminWorkgroupService.getAuthorizedCollection( links, getUser(  ) );
203 
204         model.put( MARK_LINKS_LIST, links );
205         model.put( MARK_LINK_URL_DEFAULT, strDefaultUrlName );
206         model.put( MARK_LINK_IMAGE_PATH, strImagesPath );
207         model.put( MARK_LINK_NO_IMAGE, AppPropertiesService.getProperty( PROPERTY_NO_IMAGE_ADMIN ) );
208 
209         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_LINKS_LIST, getLocale(  ), model );
210 
211         return getAdminPage( template.getHtml(  ) );
212     }
213 
214     /**
215     * Create a new Link
216     *
217     * @param request The Http request
218     * @return The jsp URL which displays the links management page
219     */
220     public String doCreateLink( HttpServletRequest request )
221     {
222         MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request;
223 
224         //get name and url
225         Linkugins/links/business/Link.html#Link">Link link = new Link(  );
226 
227         if ( ( mRequest.getParameter( PARAMETER_LINK_NAME ) == null ) ||
228                 ( mRequest.getParameter( PARAMETER_LINK_NAME ).equals( "" ) ) ||
229                 ( mRequest.getParameter( PARAMETER_LINK_URL ) == null ) ||
230                 ( mRequest.getParameter( PARAMETER_LINK_URL ).equals( "" ) ) )
231         {
232             return AdminMessageService.getMessageUrl( mRequest, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_ERROR );
233         }
234 
235         // control description textarea length (links.properties)
236         if ( mRequest.getParameter( PARAMETER_LINK_DESCRIPTION ).length(  ) > Integer.parseInt( 
237                     AppPropertiesService.getProperty( PROPERTY_LINK_DESCRIPTION_SIZE ) ) )
238         {
239             Object[] messageArgs = { AppPropertiesService.getProperty( PROPERTY_LINK_DESCRIPTION_SIZE ) };
240 
241             return AdminMessageService.getMessageUrl( mRequest, Messages.TEXT_SIZE, messageArgs, AdminMessage.TYPE_ERROR );
242         }
243 
244         ReferenceList listOptionalUrls = getUrlsListFromMultipart( link.getUrl(  ), mRequest );
245 
246         if ( listOptionalUrls == null )
247         {
248             return AdminMessageService.getMessageUrl( mRequest, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_ERROR );
249         }
250 
251         link.setOptionalUrls( listOptionalUrls );
252         link.setName( mRequest.getParameter( PARAMETER_LINK_NAME ) );
253         link.setUrl( mRequest.getParameter( PARAMETER_LINK_URL ) );
254         link.setDescription( mRequest.getParameter( PARAMETER_LINK_DESCRIPTION ) );
255         link.setWorkgroupKey( mRequest.getParameter( PARAMETER_WORKGROUP ) );
256 
257         link.setDate( new java.sql.Date( new java.util.Date(  ).getTime(  ) ) );
258         LinkHome.create( link );
259 
260         FileItem itemPicture = mRequest.getFile( PARAMETER_LINK_IMAGE );
261 
262         byte[] bytes = itemPicture.get(  );
263         String strMimeType = itemPicture.getContentType(  );
264 
265         link.setImageContent( bytes );
266         link.setMimeType( strMimeType );
267 
268         LinkHome.update( link );
269 
270         //getting link object in table
271         return JSP_LINKS_LIBRARY;
272     }
273 
274     /**
275      * Returns the form to remove a link
276      *
277      * @param request The Http request
278      * @return Html form
279      */
280     public String getRemoveLink( HttpServletRequest request )
281     {
282         MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request;
283         UrlItem url = new UrlItem( JSP_DO_REMOVE_LINK );
284         url.addParameter( PARAMETER_LINK_NAME, mRequest.getParameter( PARAMETER_LINK_NAME ) );
285         url.addParameter( PARAMETER_LINK_ID, Integer.parseInt( mRequest.getParameter( PARAMETER_LINK_ID ) ) );
286 
287         return AdminMessageService.getMessageUrl( mRequest, MESSAGE_CONFIRM_REMOVE_LINK, url.getUrl(  ),
288             AdminMessage.TYPE_CONFIRMATION );
289     }
290 
291     /**
292      * Remove a link from the database
293      *
294      * @param request The Http request
295      * @return The jsp URL which displays the management page of the links
296      */
297     public String doRemoveLink( HttpServletRequest request )
298     {
299         int nLinkId = Integer.parseInt( request.getParameter( PARAMETER_LINK_ID ) );
300 
301         // Deleting record in database
302         LinksPortletHome.removeLinkFromPortlets( nLinkId );
303         LinkHome.delete( nLinkId );
304 
305         return JSP_LINKS_LIBRARY;
306     }
307 
308     /**
309      * Returns the form to create a new Link
310      *
311      * @param request The Http request
312      * @return Html form
313      */
314     public String getCreateLink( HttpServletRequest request )
315     {
316         setPageTitleProperty( PROPERTY_PAGE_TITLE_CREATE_LINK );
317 
318         HashMap model = new HashMap(  );
319 
320         ReferenceList virtualHosts = new ReferenceList(  );
321         String strUrlName = "";
322 
323         try
324         {
325             if ( AppPathService.getAvailableVirtualHosts(  ) != null )
326             {
327                 virtualHosts = AppPathService.getAvailableVirtualHosts(  );
328                 strUrlName = I18nService.getLocalizedString( PROPERTY_CREATE_LINKS_URL_TITLE, request.getLocale(  ) );
329 
330                 String strDefaultKey = AppPropertiesService.getProperty( PROPERTY_LINKS_URL_DEFAULT_KEY_NAME );
331 
332                 if ( virtualHosts.isEmpty(  ) )
333                 {
334                     model.put( MARK_ALTERNATE_URLS, "" );
335                 }
336                 else
337                 {
338                     for ( ReferenceItem item : virtualHosts )
339                     {
340                         if ( item.getName(  ) == null )
341                         {
342                             item.setName( "" );
343                         }
344 
345                         if ( strDefaultKey.equals( item.getCode(  ) ) )
346                         {
347                             strUrlName = item.getName(  );
348                             virtualHosts.remove( item );
349                         }
350 
351                         break;
352                     }
353 
354                     model.put( MARK_LIST_ALTERNATE_URLS, virtualHosts );
355                 }
356             }
357         }
358         catch ( NullPointerException e )
359         {
360             AppLogService.error( e.getMessage(  ), e );
361         }
362 
363         model.put( MARK_LINK_URL_DEFAULT, strUrlName );
364         model.put( MARK_WORKGROUPS_LIST, AdminWorkgroupService.getUserWorkgroups( getUser(  ), getLocale(  ) ) );
365 
366         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_LINK, getLocale(  ), model );
367 
368         return getAdminPage( template.getHtml(  ) );
369     }
370 
371     /**
372      * Returns the form to modify an existing link
373      *
374      * @param request The Http request
375      * @return Html form
376      */
377     public String getModifyLink( HttpServletRequest request )
378     {
379         setPageTitleProperty( PROPERTY_PAGE_TITLE_MODIFY_LINK );
380 
381         String strImagesPath = AppPropertiesService.getProperty( PROPERTY_LINKS_IMAGES_PATH );
382         String strUrlName = I18nService.getLocalizedString( PROPERTY_MODIFY_LINKS_URL_TITLE, request.getLocale(  ) );
383         String strDefaultKey = AppPropertiesService.getProperty( PROPERTY_LINKS_URL_DEFAULT_KEY_NAME );
384         String strNoImageName = AppPropertiesService.getProperty( PROPERTY_NO_IMAGE_ADMIN );
385         HashMap model = new HashMap(  );
386         String strId = request.getParameter( PARAMETER_LINK_ID );
387         ReferenceList virtualHosts = new ReferenceList(  );
388 
389         try
390         {
391             if ( AppPathService.getAvailableVirtualHosts(  ) != null )
392             {
393                 virtualHosts = AppPathService.getAvailableVirtualHosts(  );
394 
395                 if ( virtualHosts.isEmpty(  ) )
396                 {
397                     model.put( MARK_ALTERNATE_URLS, "" );
398                 }
399                 else
400                 {
401                     for ( ReferenceItem item : virtualHosts )
402                     {
403                         if ( strDefaultKey.equals( item.getCode(  ) ) )
404                         {
405                             strUrlName = item.getName(  );
406                             virtualHosts.remove( item );
407                         }
408 
409                         break;
410                     }
411                 }
412 
413                 model.put( MARK_LIST_ALTERNATE_URLS, virtualHosts );
414             }
415         }
416         catch ( NullPointerException e )
417         {
418             AppLogService.error( e.getMessage(  ), e );
419         }
420 
421         //getting link object in table    	
422         model.put( MARK_LINK, LinkHome.findByPrimaryKey( Integer.parseInt( strId ) ) );
423         model.put( MARK_LINK_URL_DEFAULT, strUrlName );
424         model.put( MARK_LINK_IMAGE_PATH, strImagesPath );
425         model.put( MARK_LINK_NO_IMAGE, strNoImageName );
426         model.put( MARK_WORKGROUPS_LIST, AdminWorkgroupService.getUserWorkgroups( getUser(  ), getLocale(  ) ) );
427 
428         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_LINK, getLocale(  ), model );
429 
430         return getAdminPage( template.getHtml(  ) );
431     }
432 
433     /**
434      * Process the Link's modification
435      *
436      * @param request The Http request
437      * @return The jsp URL which displays the management page of the links
438      */
439     public String doModifyLink( HttpServletRequest request )
440     {
441         MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request;
442         String strIdLink = mRequest.getParameter( PARAMETER_LINK_ID );
443         int nLinkId = Integer.parseInt( strIdLink );
444 
445         //get name and url
446         String strUpdateImage = mRequest.getParameter( PARAMETER_LINK_UPDATE_IMAGE );
447         Link link = LinkHome.findByPrimaryKey( nLinkId );
448 
449         if ( ( mRequest.getParameter( PARAMETER_LINK_NAME ) == null ) ||
450                 ( mRequest.getParameter( PARAMETER_LINK_NAME ).equals( "" ) ) ||
451                 ( mRequest.getParameter( PARAMETER_LINK_URL ) == null ) ||
452                 ( mRequest.getParameter( PARAMETER_LINK_URL ).equals( "" ) ) )
453         {
454             return AdminMessageService.getMessageUrl( mRequest, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_ERROR );
455         }
456 
457         link.setName( mRequest.getParameter( PARAMETER_LINK_NAME ) );
458         link.setUrl( mRequest.getParameter( PARAMETER_LINK_URL ) );
459 
460         // control description textarea length (links.properties)
461         if ( mRequest.getParameter( PARAMETER_LINK_DESCRIPTION ).length(  ) > Integer.parseInt( 
462                     AppPropertiesService.getProperty( PROPERTY_LINK_DESCRIPTION_SIZE ) ) )
463         {
464             Object[] messageArgs = { AppPropertiesService.getProperty( PROPERTY_LINK_DESCRIPTION_SIZE ) };
465 
466             return AdminMessageService.getMessageUrl( mRequest, Messages.TEXT_SIZE, messageArgs, AdminMessage.TYPE_ERROR );
467         }
468 
469         link.setDescription( mRequest.getParameter( PARAMETER_LINK_DESCRIPTION ) );
470 
471         ReferenceList listOptionalUrls = getUrlsListFromMultipart( link.getUrl(  ), mRequest );
472 
473         if ( listOptionalUrls == null )
474         {
475             return AdminMessageService.getMessageUrl( mRequest, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_ERROR );
476         }
477 
478         link.setOptionalUrls( listOptionalUrls );
479 
480         if ( ( strUpdateImage != null ) && ( strUpdateImage.equals( "on" ) ) )
481         {
482             FileItem itemPicture = mRequest.getFile( PARAMETER_LINK_IMAGE );
483 
484             // Checking object's existence
485             if ( ( itemPicture == null ) || itemPicture.getName(  ).equals( "" ) )
486             {
487                 link.setImageContent( null );
488                 link.setMimeType( null );
489             }
490             else
491             {
492                 byte[] bytes = itemPicture.get(  );
493                 String strMimeType = itemPicture.getContentType(  );
494 
495                 link.setImageContent( bytes );
496                 link.setMimeType( strMimeType );
497             }
498         }
499 
500         link.setWorkgroupKey( mRequest.getParameter( PARAMETER_WORKGROUP ) );
501 
502         LinkHome.update( link );
503 
504         //getting link object in table
505         return JSP_LINKS_LIBRARY;
506     }
507 
508     /**
509      * Select a link portlet for the Links Page
510      *
511      * @param request The Http request
512      * @return The jsp URL which displays the management of the links page
513      */
514     public String doSelectPortletLinks( HttpServletRequest request )
515     {
516         int nPortletId = Integer.parseInt( request.getParameter( PARAMETER_PORTLET_ID ) );
517         String strOrder = request.getParameter( PARAMETER_PORTLET_ORDER );
518         int nOrder = Integer.parseInt( strOrder );
519         int nMax = LinksPortletHome.getPortletMaxOrder(  );
520 
521         for ( int i = nOrder; i < ( nMax + 1 ); i++ )
522         {
523             int nPortletIdTemp = LinksPortletHome.getPortletIdByOrder( i );
524             LinksPortletHome.updatePortletOrder( i + 1, nPortletIdTemp );
525         }
526 
527         LinksPortletHome.insertPortlet( nPortletId, nOrder );
528 
529         return JSP_LINKS_PAGE;
530     }
531 
532     /**
533      * Modify the order of the links portlets in the links page
534      *
535      * @param request The Http request
536      * @return The jsp URL which displays the management of the links page
537      */
538     public String doModifyPortletsOrder( HttpServletRequest request )
539     {
540         int nPortletId = Integer.parseInt( request.getParameter( PARAMETER_PORTLET_ID ) );
541         int nOrder = Integer.parseInt( request.getParameter( PARAMETER_PORTLET_ORDER ) );
542         int nNewOrder = Integer.parseInt( request.getParameter( PARAMETER_NEW_PORTLET_ORDER ) );
543 
544         if ( ( nOrder - nNewOrder ) > 0 )
545         {
546             for ( int i = nOrder - 1; i >= nNewOrder; i-- )
547             {
548                 int nPortletIdTemp = LinksPortletHome.getPortletIdByOrder( i );
549                 LinksPortletHome.updatePortletOrder( i + 1, nPortletIdTemp );
550             }
551         }
552         else
553         {
554             for ( int i = nOrder + 1; i <= nNewOrder; i++ )
555             {
556                 int nPortletIdTemp = LinksPortletHome.getPortletIdByOrder( i );
557                 LinksPortletHome.updatePortletOrder( i - 1, nPortletIdTemp );
558             }
559         }
560 
561         LinksPortletHome.updatePortletOrder( nNewOrder, nPortletId );
562 
563         return JSP_LINKS_PAGE;
564     }
565 
566     /**
567      * Remove a portlet from the links page
568      *
569      * @param request The Http request
570      * @return The jsp URL which displays the management of the links page
571      */
572     public String doUnselectPortlet( HttpServletRequest request )
573     {
574         int nPortletId = Integer.parseInt( request.getParameter( PARAMETER_PORTLET_ID ) );
575 
576         // Deleting record in database
577         String strOrder = request.getParameter( PARAMETER_PORTLET_ORDER );
578         int nOrder = Integer.parseInt( strOrder );
579         int nMax = LinksPortletHome.getPortletMaxOrder(  );
580         LinksPortletHome.removePortlet( nPortletId );
581 
582         for ( int i = nOrder + 1; i < ( nMax + 1 ); i++ )
583         {
584             int nPortletIdTemp = LinksPortletHome.getPortletIdByOrder( i );
585             LinksPortletHome.updatePortletOrder( i - 1, nPortletIdTemp );
586         }
587 
588         return JSP_LINKS_PAGE;
589     }
590 
591     /**
592      * Creates and returns the management page fot links page
593      *
594      * @return the html code of the page
595      */
596     public String getManageLinksPage( HttpServletRequest request )
597     {
598         setPageTitleProperty( PROPERTY_PAGE_TITLE_MANAGE_LINKS_PAGES );
599 
600         HashMap model = new HashMap(  );
601         ReferenceList unselectedPortletList = LinksPortletHome.findUnselectedPortlets(  );
602         Collection portletsList = LinksPortletHome.getPortletsInLinksPage(  );
603 
604         model.put( MARK_ORDER_LIST_UNSELECTED_PORTLETS, getPortletLinksOrdersList(  ) );
605         model.put( MARK_ORDER_LIST_SELECTED_PORTLETS, getOrdersList(  ) );
606 
607         model.put( MARK_PORTLETS_LIST, portletsList );
608         model.put( MARK_UNSELECTED_PORTLETS_LIST, unselectedPortletList );
609 
610         HtmlTemplate templateList = AppTemplateService.getTemplate( TEMPLATE_MANAGE_LINKS_PAGE, getLocale(  ), model );
611 
612         return getAdminPage( templateList.getHtml(  ) );
613     }
614 
615     ////////////////////////////////////////////////////////////////////////////////////////
616     // Private Implementations
617 
618     /**
619      * Returns the list of the possible order for a new link in a portlet
620      *
621      * @return list
622      */
623     private ReferenceList getPortletLinksOrdersList(  )
624     {
625         int nMax = LinksPortletHome.getPortletMaxOrder(  );
626         ReferenceList list = new ReferenceList(  );
627 
628         for ( int i = 1; i < ( nMax + 2 ); i++ )
629         {
630             list.addItem( i, Integer.toString( i ) );
631         }
632 
633         return list;
634     }
635 
636     /**
637      * Returns the list of the possible order to move a link in a portlet
638      *
639      * @return list
640      */
641     private ReferenceList getOrdersList(  )
642     {
643         int nMax = LinksPortletHome.getPortletMaxOrder(  );
644         ReferenceList list = new ReferenceList(  );
645 
646         for ( int i = 1; i < ( nMax + 1 ); i++ )
647         {
648             list.addItem( i, Integer.toString( i ) );
649         }
650 
651         return list;
652     }
653 
654     /**
655      * get optional urls from form
656      *
657      * @param strDefaultUrl name of the default virtualHostKey
658      * @param multi the form
659      * @return list containing submitted urls
660      */
661     private ReferenceList getUrlsListFromMultipart( String strDefaultUrl, MultipartHttpServletRequest multi )
662     {
663         ReferenceList listUrls = new ReferenceList(  );
664 
665         ReferenceList virtualHosts = new ReferenceList(  );
666 
667         try
668         {
669             if ( AppPathService.getAvailableVirtualHosts(  ) != null )
670             {
671                 virtualHosts = AppPathService.getAvailableVirtualHosts(  );
672 
673                 if ( !virtualHosts.isEmpty(  ) )
674                 {
675                     String strDefaultKey = AppPropertiesService.getProperty( PROPERTY_LINKS_URL_DEFAULT_KEY_NAME );
676 
677                     for ( ReferenceItem item : virtualHosts )
678                     {
679                         String strCode = item.getCode(  );
680 
681                         if ( !strDefaultKey.equals( strCode ) )
682                         {
683                             // checkbox processing
684                             switch ( Integer.parseInt( multi.getParameter( strCode + PARAMETER_SUFFIX_ALTERNATE_TYPE ) ) )
685                             {
686                                 case TYPE_NEW_URL:
687 
688                                     String strUrl = multi.getParameter( strCode + PARAMETER_SUFFIX_ALTERNATE_URL );
689 
690                                     if ( ( strUrl == null ) || "".equals( strUrl ) )
691                                     {
692                                         return null;
693                                     }
694 
695                                     listUrls.addItem( strCode, strUrl );
696 
697                                     break;
698 
699                                 case TYPE_DEFAULT_URL:
700                                     listUrls.addItem( strCode, strDefaultUrl );
701 
702                                     break;
703 
704                                 case TYPE_NO_URL:default:
705                                     break;
706                             }
707                         }
708                     }
709                 }
710             }
711         }
712         catch ( NullPointerException e )
713         {
714             AppLogService.error( e.getMessage(  ), e );
715         }
716 
717         return listUrls;
718     }
719 }