View Javadoc

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