View Javadoc
1   /*
2    * Copyright (c) 2002-2014, 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.myportal.web;
35  
36  import java.util.ArrayList;
37  import java.util.HashMap;
38  import java.util.List;
39  import java.util.Map;
40  
41  import javax.servlet.http.HttpServletRequest;
42  
43  import org.apache.commons.lang.StringUtils;
44  
45  import fr.paris.lutece.plugins.myportal.business.Category;
46  import fr.paris.lutece.plugins.myportal.business.Widget;
47  import fr.paris.lutece.plugins.myportal.business.page.TabConfig;
48  import fr.paris.lutece.plugins.myportal.service.CategoryService;
49  import fr.paris.lutece.plugins.myportal.service.DefaultPageBuilderService;
50  import fr.paris.lutece.plugins.myportal.service.MyPortalPageService;
51  import fr.paris.lutece.plugins.myportal.service.MyPortalPlugin;
52  import fr.paris.lutece.plugins.myportal.service.WidgetService;
53  import fr.paris.lutece.plugins.myportal.util.auth.MyPortalUser;
54  import fr.paris.lutece.portal.service.i18n.I18nService;
55  import fr.paris.lutece.portal.service.message.SiteMessageException;
56  import fr.paris.lutece.portal.service.plugin.Plugin;
57  import fr.paris.lutece.portal.service.security.LuteceUser;
58  import fr.paris.lutece.portal.service.security.SecurityService;
59  import fr.paris.lutece.portal.service.security.UserNotSignedException;
60  import fr.paris.lutece.portal.service.spring.SpringContextService;
61  import fr.paris.lutece.portal.service.template.AppTemplateService;
62  import fr.paris.lutece.portal.service.util.AppLogService;
63  import fr.paris.lutece.portal.service.util.AppPathService;
64  import fr.paris.lutece.portal.service.util.AppPropertiesService;
65  import fr.paris.lutece.portal.web.util.LocalizedPaginator;
66  import fr.paris.lutece.portal.web.xpages.XPage;
67  import fr.paris.lutece.portal.web.xpages.XPageApplication;
68  import fr.paris.lutece.util.html.HtmlTemplate;
69  import fr.paris.lutece.util.html.Paginator;
70  import fr.paris.lutece.util.url.UrlItem;
71  
72  /**
73   * This class provides a simple implementation of an XPage
74   */
75  public class MyPortalApp implements XPageApplication
76  {
77      // CONSTANTS
78      private static final String LINE = "-";
79      private static final String BEAN_MYPORTAL_WIDGETSERVICE = "myportal.widgetService";
80  
81      // TEMPLATES
82      private static final String TEMPLATE_MYPORTAL_PAGE = "skin/plugins/myportal/myportal.html";
83      private static final String TEMPLATE_BROWSE_CATEGORIES = "skin/plugins/myportal/browse_categories.html";
84      private static final String TEMPLATE_BROWSE_CATEGORIES_WIDGETS = "skin/plugins/myportal/browse_categories_widgets.html";
85      private static final String TEMPLATE_MYPORTAL_NAVIGATION = "skin/plugins/myportal/myportal_navigation.html";
86      private static final String TEMPLATE_MYPORTAL_ADD_TAB = "skin/plugins/myportal/myportal_add_tab.html";
87      private static final String TEMPLATE_MYPORTAL_EDIT_TAB = "skin/plugins/myportal/myportal_edit_tab.html";
88      private static final String TEMPLATE_MYPORTAL_EDIT_WIDGET = "skin/plugins/myportal/myportal_edit_widget.html";
89      private static final String TEMPLATE_BROWSE_ESSENTIAL_WIDGETS = "skin/plugins/myportal/browse_essential_widgets.html";
90      private static final String TEMPLATE_BROWSE_NEW_WIDGETS = "skin/plugins/myportal/browse_new_widgets.html";
91      private static final String TEMPLATE_SEARCH_WIDGETS = "skin/plugins/myportal/search_widgets.html";
92  
93      // PARAMETERS
94      private static final String PARAMETER_ID_WIDGET = "id_widget";
95      private static final String PARAMETER_TAB_NAME = "tab_name";
96      private static final String PARAMETER_WIDGET = "widget";
97      private static final String PARAMETER_COLUMN = "column";
98      private static final String PARAMETER_PORTAL_STATE = "portalState";
99      private static final String PARAMETER_CATEGORY_ID_CATEGORY = "category_id_category";
100     private static final String PARAMETER_ACTION = "action";
101     private static final String PARAMETER_SEARCH_WIDGETS_NAME = "search_widgets_name";
102     private static final String PARAMETER_TAB_INDEX = "tab_index";
103     private static final String PARAMETER_PAGE = "page";
104 
105     // MARKS
106     private static final String MARK_WIDGETS = "widgets";
107     private static final String MARK_WIDGETS_LIST = "widgets_list";
108     private static final String MARK_LIST_TAB = "tabs_list";
109     private static final String MARK_BASE_URL = "base_url";
110     private static final String MARK_TAB_NAME = "tab_name";
111     private static final String MARK_CATEGORIES_LIST = "categories_list";
112     private static final String MARK_CATEGORY_ID_CATEGORY = "category_id_category";
113     private static final String MARK_PAGINATOR = "paginator";
114     private static final String MARK_NB_ITEMS_PER_PAGE = "nb_items_per_page";
115     private static final String MARK_WIDGETS_LIST_HTML = "widgets_list_html";
116     private static final String MARK_MYPORTAL_NAVIGATION_CONTENT = "myportal_navigation_content";
117     private static final String MARK_ACTION = "action";
118     private static final String MARK_PAGINATOR_URL_FOR_JS = "paginator_url_for_js";
119     private static final String MARK_SEARCH_WIDGETS_NAME = "search_widgets_name";
120     private static final String MARK_TAB_INDEX = "tab_index";
121     private static final String MARK_USER_WIDGET_IDS = "user_widget_ids";
122     private static final String MARK_WIDGET = "widget";
123     private static final String MARK_NB_COLUMNS = "nb_columns";
124 
125     // PROPERTIES
126     private static final String PROPERTY_PAGE_PATH = "myportal.myportal.pagePathLabel";
127     private static final String PROPERTY_PAGE_TITLE = "myportal.myportal.pageTitle";
128     private static final String PROPERTY_DEFAULT_LIST_WIDGET_PER_PAGE_IN_FO = "myportal.listWidgets.itemsPerPageInFO";
129     private static final String PROPERTY_URL_RETURN = "myportal.urlReturn";
130     private static final String PROPERTY_DEFAULT_COLUMN = "myportal.defaultColumn";
131 
132     // ACTIONS
133     private static final String ACTION_BROWSE_CATEGORIES = "browse_categories";
134     private static final String ACTION_BROWSE_ESSENTIAL_WIDGETS = "browse_essential_widgets";
135     private static final String ACTION_BROWSE_NEW_WIDGETS = "browse_new_widgets";
136     private static final String ACTION_SEARCH_WIDGETS = "search_widgets";
137     private static final String ACTION_ADD_TAB = "add_tab";
138     private static final String ACTION_EDIT_TAB = "edit_tab";
139     private static final String ACTION_EDIT_WIDGET = "edit_widget";
140 
141     // JSP
142     private static final String JSP_URL_BROWSE_CATEGORIES_WIDGETS = "jsp/site/plugins/myportal/BrowseCategoriesWidgets.jsp";
143     private static final String JSP_URL_BROWSE_ESSENTIAL_WIDGETS = "jsp/site/plugins/myportal/BrowseEssentialWidgets.jsp";
144     private static final String JSP_URL_BROWSE_NEW_WIDGETS = "jsp/site/plugins/myportal/BrowseNewWidgets.jsp";
145     private static final String JSP_URL_SEARCH_WIDGETS = "jsp/site/plugins/myportal/SearchWidgets.jsp";
146 
147     // private fields
148     private MyPortalPageService _pageService = MyPortalPageService.getInstance( );
149     private WidgetService _widgetService = SpringContextService.getBean( BEAN_MYPORTAL_WIDGETSERVICE );
150 
151     private CategoryService _categoryService = SpringContextService.getBean( CategoryService.BEAN_NAME );
152     private DefaultPageBuilderService _defaultPageBuilderService = SpringContextService.getBean( DefaultPageBuilderService.BEAN_NAME );
153 
154     /**
155      * Returns the content of the page myportal.
156      * 
157      * @param request
158      *            The http request
159      * @param nMode
160      *            The current mode
161      * @param plugin
162      *            The plugin object
163      * @return the {@link XPage}
164      * @throws fr.paris.lutece.portal.service.message.SiteMessageException
165      *             Message displayed if an exception occurs
166      */
167     public XPage getPage( HttpServletRequest request, int nMode, Plugin plugin ) throws SiteMessageException
168     {
169         XPage page = new XPage( );
170 
171         page.setTitle( I18nService.getLocalizedString( PROPERTY_PAGE_TITLE, request.getLocale( ) ) );
172         page.setPathLabel( I18nService.getLocalizedString( PROPERTY_PAGE_PATH, request.getLocale( ) ) );
173 
174         String strAction = request.getParameter( PARAMETER_ACTION );
175 
176         if ( StringUtils.isNotBlank( strAction ) )
177         {
178             if ( ACTION_ADD_TAB.equals( strAction ) )
179             {
180                 page.setContent( getMyPortalAddTab( request ) );
181             }
182             else
183                 if ( ACTION_EDIT_TAB.equals( strAction ) )
184                 {
185                     page.setContent( getMyPortalEditTab( request ) );
186                 }
187                 else
188                     if ( ACTION_EDIT_WIDGET.equals( strAction ) )
189                     {
190                         page.setContent( getMyPortalEditWidget( request ) );
191                     }
192                     else
193                     {
194                         page.setContent( getMyPortalNavigation( request ) );
195                     }
196         }
197         else
198         {
199             String strWidgets = _pageService.getUserPage( getUser( request ), request );
200             Map<String, Object> model = new HashMap<String, Object>( );
201             model.put( MARK_WIDGETS, strWidgets );
202 
203             HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MYPORTAL_PAGE, request.getLocale( ), model );
204             page.setContent( template.getHtml( ) );
205         }
206 
207         return page;
208     }
209 
210     /**
211      * Get the content of the page MyPortalNavigation
212      * 
213      * @param request
214      *            {@link HttpServletRequest}
215      * @return the html code
216      */
217     public String getMyPortalNavigation( HttpServletRequest request )
218     {
219         String strHtml = StringUtils.EMPTY;
220         String strAction = request.getParameter( PARAMETER_ACTION );
221 
222         if ( StringUtils.isNotBlank( strAction ) )
223         {
224             String strNavigationContentHtml = StringUtils.EMPTY;
225             String strName = request.getParameter( PARAMETER_SEARCH_WIDGETS_NAME );
226             String strTabIndex = request.getParameter( PARAMETER_TAB_INDEX );
227 
228             if ( strName == null )
229             {
230                 strName = StringUtils.EMPTY;
231             }
232 
233             Map<String, Object> model = new HashMap<String, Object>( );
234 
235             if ( ACTION_BROWSE_CATEGORIES.equals( strAction ) )
236             {
237                 strNavigationContentHtml = getBrowseCategories( request );
238             }
239             else
240                 if ( ACTION_BROWSE_ESSENTIAL_WIDGETS.equals( strAction ) )
241                 {
242                     strNavigationContentHtml = getBrowseEssentialWidgets( request );
243                 }
244                 else
245                     if ( ACTION_BROWSE_NEW_WIDGETS.equals( strAction ) )
246                     {
247                         strNavigationContentHtml = getBrowseNewWidgets( request );
248                     }
249                     else
250                         if ( ACTION_SEARCH_WIDGETS.equals( strAction ) )
251                         {
252                             strNavigationContentHtml = getSearchWidgets( request );
253                         }
254 
255             String strBaseUrl = AppPathService.getBaseUrl( request );
256             model.put( MARK_BASE_URL, strBaseUrl );
257             model.put( MARK_ACTION, strAction );
258             model.put( MARK_MYPORTAL_NAVIGATION_CONTENT, strNavigationContentHtml );
259             model.put( MARK_SEARCH_WIDGETS_NAME, strName );
260             model.put( MARK_TAB_INDEX, strTabIndex );
261 
262             HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MYPORTAL_NAVIGATION, request.getLocale( ), model );
263             strHtml = template.getHtml( );
264         }
265 
266         return strHtml;
267     }
268 
269     /**
270      * Get Browse categories popup
271      * 
272      * @param request
273      *            The HTTP request
274      * @return The page
275      */
276     public String getBrowseCategories( HttpServletRequest request )
277     {
278         String strCategoryId = request.getParameter( PARAMETER_CATEGORY_ID_CATEGORY );
279 
280         if ( StringUtils.isBlank( strCategoryId ) )
281         {
282             Category category = _categoryService.findFirstCategory( );
283             strCategoryId = Integer.toString( category.getIdCategory( ) );
284         }
285 
286         String strBaseUrl = AppPathService.getBaseUrl( request );
287         String strWidgetsListHtml = getBrowseCategoriesWidgets( request );
288         String strTabIndex = request.getParameter( PARAMETER_TAB_INDEX );
289 
290         Map<String, Object> model = new HashMap<String, Object>( );
291         model.put( MARK_BASE_URL, strBaseUrl );
292         model.put( MARK_CATEGORIES_LIST, _categoryService.getCategoriesList( ) );
293         model.put( MARK_CATEGORY_ID_CATEGORY, strCategoryId );
294         model.put( MARK_WIDGETS_LIST_HTML, strWidgetsListHtml );
295         model.put( MARK_TAB_INDEX, strTabIndex );
296 
297         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_BROWSE_CATEGORIES, request.getLocale( ), model );
298 
299         return template.getHtml( );
300     }
301 
302     /**
303      * Get Add Widget Content
304      * 
305      * @param request
306      *            The HTTP request
307      * @return The page
308      */
309     public String getBrowseCategoriesWidgets( HttpServletRequest request )
310     {
311         String strHtml = StringUtils.EMPTY;
312         String strCategoryId = request.getParameter( PARAMETER_CATEGORY_ID_CATEGORY );
313         String strTabIndex = request.getParameter( PARAMETER_TAB_INDEX );
314         List<Widget> listWidgets;
315         Category category = null;
316 
317         if ( StringUtils.isNotBlank( strCategoryId ) && StringUtils.isNumeric( strCategoryId ) )
318         {
319             int nCategoryId = Integer.parseInt( strCategoryId );
320             category = _categoryService.findByPrimaryKey( nCategoryId );
321         }
322         else
323         {
324             category = _categoryService.findFirstCategory( );
325             strCategoryId = Integer.toString( category.getIdCategory( ) );
326         }
327 
328         if ( category != null )
329         {
330             listWidgets = _widgetService.getWidgetsByCategoryId( category.getIdCategory( ) );
331         }
332         else
333         {
334             listWidgets = new ArrayList<Widget>( );
335         }
336 
337         List<TabConfig> listTabs = _pageService.getTabList( getUser( request ) );
338 
339         UrlItem url = new UrlItem( AppPathService.getPortalUrl( ) );
340         url.addParameter( PARAMETER_PAGE, MyPortalPlugin.PLUGIN_NAME );
341         url.addParameter( PARAMETER_ACTION, ACTION_BROWSE_CATEGORIES );
342         url.addParameter( PARAMETER_CATEGORY_ID_CATEGORY, strCategoryId );
343         url.addParameter( PARAMETER_TAB_INDEX, strTabIndex );
344 
345         UrlItem urlForJs = new UrlItem( JSP_URL_BROWSE_CATEGORIES_WIDGETS );
346         urlForJs.addParameter( PARAMETER_CATEGORY_ID_CATEGORY, strCategoryId );
347         urlForJs.addParameter( PARAMETER_TAB_INDEX, strTabIndex );
348 
349         // Paginator
350         String strCurrentPageIndex = Paginator.getPageIndex( request, Paginator.PARAMETER_PAGE_INDEX, "1" );
351         int nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_DEFAULT_LIST_WIDGET_PER_PAGE_IN_FO, 50 );
352         LocalizedPaginator paginator = new LocalizedPaginator( listWidgets, nDefaultItemsPerPage, url.getUrl( ), Paginator.PARAMETER_PAGE_INDEX,
353                 strCurrentPageIndex, request.getLocale( ) );
354         String strBaseUrl = AppPathService.getBaseUrl( request );
355 
356         Map<String, Object> model = new HashMap<String, Object>( );
357         model.put( MARK_BASE_URL, strBaseUrl );
358         model.put( MARK_WIDGETS_LIST, paginator.getPageItems( ) );
359         model.put( MARK_NB_ITEMS_PER_PAGE, Integer.toString( nDefaultItemsPerPage ) );
360         model.put( MARK_PAGINATOR, paginator );
361         model.put( MARK_LIST_TAB, listTabs );
362         model.put( MARK_PAGINATOR_URL_FOR_JS, urlForJs.getUrl( ) );
363         model.put( MARK_TAB_INDEX, strTabIndex );
364         model.put( MARK_USER_WIDGET_IDS, _widgetService.getUserWidgetIds( getUser( request ) ) );
365 
366         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_BROWSE_CATEGORIES_WIDGETS, request.getLocale( ), model );
367 
368         strHtml = template.getHtml( );
369 
370         return strHtml;
371     }
372 
373     /**
374      * Process add content - add a widget to the page
375      * 
376      * @param request
377      *            The HTTP request
378      * @return the forward url
379      */
380     public String doAddWidget( HttpServletRequest request )
381     {
382         String strTabIndex = request.getParameter( PARAMETER_TAB_INDEX );
383         String strIdWidget = request.getParameter( PARAMETER_ID_WIDGET );
384 
385         if ( StringUtils.isNotBlank( strIdWidget ) && StringUtils.isNumeric( strIdWidget ) && StringUtils.isNotBlank( strTabIndex )
386                 && StringUtils.isNumeric( strTabIndex ) )
387         {
388             int nIdWidget = Integer.parseInt( strIdWidget );
389             int nColumn = AppPropertiesService.getPropertyInt( PROPERTY_DEFAULT_COLUMN, 1 );
390             int nTabIndex = Integer.parseInt( strTabIndex );
391 
392             _pageService.addWidget( getUser( request ), nIdWidget, nTabIndex, nColumn );
393         }
394 
395         return AppPropertiesService.getProperty( PROPERTY_URL_RETURN );
396     }
397 
398     /**
399      * Edit a widget
400      * 
401      * @param request
402      *            The HTTP request
403      * @return the forward url
404      */
405     public String doEditWidget( HttpServletRequest request )
406     {
407         String strIdTab = request.getParameter( PARAMETER_TAB_INDEX );
408         String strIdWidget = request.getParameter( PARAMETER_ID_WIDGET );
409         String strColumn = request.getParameter( PARAMETER_COLUMN );
410 
411         if ( StringUtils.isNotBlank( strIdTab ) && StringUtils.isNumeric( strIdTab ) && StringUtils.isNotBlank( strIdWidget )
412                 && StringUtils.isNumeric( strIdWidget ) && StringUtils.isNotBlank( strColumn ) && StringUtils.isNumeric( strColumn ) )
413         {
414             int nIdTab = Integer.parseInt( strIdTab );
415             int nIdWidget = Integer.parseInt( strIdWidget );
416             int nColumn = Integer.parseInt( strColumn );
417             _pageService.editWidget( getUser( request ), nIdTab, nIdWidget, nColumn );
418         }
419 
420         return AppPropertiesService.getProperty( PROPERTY_URL_RETURN );
421     }
422 
423     /**
424      * Process delete - remove a widget from the page
425      * 
426      * @param request
427      *            The HTTP request
428      * @return the forward url
429      */
430     public String doRemoveWidget( HttpServletRequest request )
431     {
432         String strWidget = request.getParameter( PARAMETER_WIDGET );
433         String strIdWidget = request.getParameter( PARAMETER_ID_WIDGET );
434 
435         if ( StringUtils.isNotBlank( strWidget ) )
436         {
437             String strWidgetCssId = PARAMETER_WIDGET + LINE;
438             int nIdWidget = Integer.parseInt( strWidget.substring( strWidgetCssId.length( ) ) );
439 
440             _pageService.removeWidget( getUser( request ), nIdWidget );
441         }
442         else
443             if ( StringUtils.isNotBlank( strIdWidget ) && StringUtils.isNumeric( strIdWidget ) )
444             {
445                 int nIdWidget = Integer.parseInt( strIdWidget );
446 
447                 _pageService.removeWidget( getUser( request ), nIdWidget );
448             }
449 
450         return AppPropertiesService.getProperty( PROPERTY_URL_RETURN );
451     }
452 
453     /**
454      * Process save portal state
455      * 
456      * @param request
457      *            The HTTP request
458      * @return the forward url
459      */
460     public String doSavePortalState( HttpServletRequest request )
461     {
462         String strJson = request.getParameter( PARAMETER_PORTAL_STATE );
463 
464         _pageService.setPageConfigUser( getUser( request ), strJson );
465 
466         return "portal state saved!"; // todo : use properties conf to permit url rewriting
467     }
468 
469     /**
470      * Get the essential widgets list html
471      * 
472      * @param request
473      *            {@link HttpServletRequest}
474      * @return the html code
475      */
476     public String getBrowseEssentialWidgets( HttpServletRequest request )
477     {
478         List<Widget> listWidgets = _widgetService.getEssentialWidgets( );
479         List<TabConfig> listTabs = _pageService.getTabList( getUser( request ) );
480         String strBaseUrl = AppPathService.getBaseUrl( request );
481         String strTabIndex = request.getParameter( PARAMETER_TAB_INDEX );
482 
483         UrlItem url = new UrlItem( AppPathService.getPortalUrl( ) );
484         url.addParameter( PARAMETER_PAGE, MyPortalPlugin.PLUGIN_NAME );
485         url.addParameter( PARAMETER_ACTION, ACTION_BROWSE_ESSENTIAL_WIDGETS );
486         url.addParameter( PARAMETER_TAB_INDEX, strTabIndex );
487 
488         UrlItem urlForJs = new UrlItem( JSP_URL_BROWSE_ESSENTIAL_WIDGETS );
489         urlForJs.addParameter( PARAMETER_TAB_INDEX, strTabIndex );
490 
491         // Paginator
492         String strCurrentPageIndex = Paginator.getPageIndex( request, Paginator.PARAMETER_PAGE_INDEX, "1" );
493         int nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_DEFAULT_LIST_WIDGET_PER_PAGE_IN_FO, 50 );
494         LocalizedPaginator paginator = new LocalizedPaginator( listWidgets, nDefaultItemsPerPage, url.getUrl( ), Paginator.PARAMETER_PAGE_INDEX,
495                 strCurrentPageIndex, request.getLocale( ) );
496 
497         Map<String, Object> model = new HashMap<String, Object>( );
498         model.put( MARK_BASE_URL, strBaseUrl );
499         model.put( MARK_WIDGETS_LIST, paginator.getPageItems( ) );
500         model.put( MARK_NB_ITEMS_PER_PAGE, Integer.toString( nDefaultItemsPerPage ) );
501         model.put( MARK_PAGINATOR, paginator );
502         model.put( MARK_LIST_TAB, listTabs );
503         model.put( MARK_PAGINATOR_URL_FOR_JS, urlForJs.getUrl( ) );
504         model.put( MARK_TAB_INDEX, strTabIndex );
505         model.put( MARK_USER_WIDGET_IDS, _widgetService.getUserWidgetIds( getUser( request ) ) );
506 
507         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_BROWSE_ESSENTIAL_WIDGETS, request.getLocale( ), model );
508 
509         return template.getHtml( );
510     }
511 
512     /**
513      * Get the new widgets list html
514      * 
515      * @param request
516      *            {@link HttpServletRequest}
517      * @return the html code
518      */
519     public String getBrowseNewWidgets( HttpServletRequest request )
520     {
521         List<Widget> listWidgets = _widgetService.getNewWidgets( );
522         List<TabConfig> listTabs = _pageService.getTabList( getUser( request ) );
523         String strBaseUrl = AppPathService.getBaseUrl( request );
524         String strTabIndex = request.getParameter( PARAMETER_TAB_INDEX );
525 
526         UrlItem url = new UrlItem( AppPathService.getPortalUrl( ) );
527         url.addParameter( PARAMETER_PAGE, MyPortalPlugin.PLUGIN_NAME );
528         url.addParameter( PARAMETER_ACTION, ACTION_BROWSE_NEW_WIDGETS );
529         url.addParameter( PARAMETER_TAB_INDEX, strTabIndex );
530 
531         UrlItem urlForJs = new UrlItem( JSP_URL_BROWSE_NEW_WIDGETS );
532         urlForJs.addParameter( PARAMETER_TAB_INDEX, strTabIndex );
533 
534         // Paginator
535         String strCurrentPageIndex = Paginator.getPageIndex( request, Paginator.PARAMETER_PAGE_INDEX, "1" );
536         int nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_DEFAULT_LIST_WIDGET_PER_PAGE_IN_FO, 50 );
537         LocalizedPaginator paginator = new LocalizedPaginator( listWidgets, nDefaultItemsPerPage, url.getUrl( ), Paginator.PARAMETER_PAGE_INDEX,
538                 strCurrentPageIndex, request.getLocale( ) );
539 
540         Map<String, Object> model = new HashMap<String, Object>( );
541         model.put( MARK_BASE_URL, strBaseUrl );
542         model.put( MARK_WIDGETS_LIST, paginator.getPageItems( ) );
543         model.put( MARK_NB_ITEMS_PER_PAGE, Integer.toString( nDefaultItemsPerPage ) );
544         model.put( MARK_PAGINATOR, paginator );
545         model.put( MARK_LIST_TAB, listTabs );
546         model.put( MARK_PAGINATOR_URL_FOR_JS, urlForJs.getUrl( ) );
547         model.put( MARK_TAB_INDEX, strTabIndex );
548         model.put( MARK_USER_WIDGET_IDS, _widgetService.getUserWidgetIds( getUser( request ) ) );
549 
550         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_BROWSE_NEW_WIDGETS, request.getLocale( ), model );
551 
552         return template.getHtml( );
553     }
554 
555     /**
556      * Get the new widgets list html
557      * 
558      * @param request
559      *            {@link HttpServletRequest}
560      * @return the html code
561      */
562     public String getSearchWidgets( HttpServletRequest request )
563     {
564         String strName = request.getParameter( PARAMETER_SEARCH_WIDGETS_NAME );
565         String strTabIndex = request.getParameter( PARAMETER_TAB_INDEX );
566 
567         if ( strName == null )
568         {
569             strName = StringUtils.EMPTY;
570         }
571 
572         List<Widget> listWidgets = _widgetService.getWidgetsByName( strName );
573         List<TabConfig> listTabs = _pageService.getTabList( getUser( request ) );
574         String strBaseUrl = AppPathService.getBaseUrl( request );
575 
576         UrlItem url = new UrlItem( AppPathService.getPortalUrl( ) );
577         url.addParameter( PARAMETER_PAGE, MyPortalPlugin.PLUGIN_NAME );
578         url.addParameter( PARAMETER_ACTION, ACTION_SEARCH_WIDGETS );
579         url.addParameter( PARAMETER_SEARCH_WIDGETS_NAME, strName );
580         url.addParameter( PARAMETER_TAB_INDEX, strTabIndex );
581 
582         UrlItem urlForJs = new UrlItem( JSP_URL_SEARCH_WIDGETS );
583         urlForJs.addParameter( PARAMETER_SEARCH_WIDGETS_NAME, strName );
584         urlForJs.addParameter( PARAMETER_TAB_INDEX, strTabIndex );
585 
586         // Paginator
587         String strCurrentPageIndex = Paginator.getPageIndex( request, Paginator.PARAMETER_PAGE_INDEX, "1" );
588         int nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_DEFAULT_LIST_WIDGET_PER_PAGE_IN_FO, 50 );
589         LocalizedPaginator paginator = new LocalizedPaginator( listWidgets, nDefaultItemsPerPage, url.getUrl( ), Paginator.PARAMETER_PAGE_INDEX,
590                 strCurrentPageIndex, request.getLocale( ) );
591 
592         Map<String, Object> model = new HashMap<String, Object>( );
593         model.put( MARK_BASE_URL, strBaseUrl );
594         model.put( MARK_WIDGETS_LIST, paginator.getPageItems( ) );
595         model.put( MARK_NB_ITEMS_PER_PAGE, Integer.toString( nDefaultItemsPerPage ) );
596         model.put( MARK_PAGINATOR, paginator );
597         model.put( MARK_LIST_TAB, listTabs );
598         model.put( MARK_PAGINATOR_URL_FOR_JS, urlForJs.getUrl( ) );
599         model.put( MARK_TAB_INDEX, strTabIndex );
600         model.put( MARK_USER_WIDGET_IDS, _widgetService.getUserWidgetIds( getUser( request ) ) );
601 
602         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_SEARCH_WIDGETS, request.getLocale( ), model );
603 
604         return template.getHtml( );
605     }
606 
607     /**
608      * Gets the user from the request
609      * 
610      * @param request
611      *            The HTTP user
612      * @return The Lutece User
613      */
614     private LuteceUser getUser( HttpServletRequest request )
615     {
616         LuteceUser user = null;
617 
618         if ( SecurityService.isAuthenticationEnable( ) )
619         {
620             try
621             {
622                 user = SecurityService.getInstance( ).getRemoteUser( request );
623             }
624             catch ( UserNotSignedException ue )
625             {
626                 AppLogService.debug( ue.getMessage( ) );
627             }
628         }
629 
630         if ( user == null )
631         {
632             user = new MyPortalUser( "Anonymous" );
633         }
634 
635         return user;
636     }
637 
638     /**
639      * Get the content of the page getMyPortalAddTab
640      * 
641      * @param request
642      *            {@link HttpServletRequest}
643      * @return the html code
644      */
645     public String getMyPortalAddTab( HttpServletRequest request )
646     {
647         Map<String, Object> model = new HashMap<String, Object>( );
648 
649         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MYPORTAL_ADD_TAB, request.getLocale( ), model );
650 
651         return template.getHtml( );
652     }
653 
654     /**
655      * Process add tab
656      * 
657      * @param request
658      *            The HTTP request
659      * @return the forward url
660      */
661     public String doAddTab( HttpServletRequest request )
662     {
663         String strTabName = request.getParameter( PARAMETER_TAB_NAME );
664         _pageService.addTab( getUser( request ), strTabName );
665 
666         return AppPropertiesService.getProperty( PROPERTY_URL_RETURN );
667     }
668 
669     /**
670      * Get the content of editing a tab
671      * 
672      * @param request
673      *            {@link HttpServletRequest}
674      * @return the html code
675      */
676     public String getMyPortalEditTab( HttpServletRequest request )
677     {
678         String strHtml = StringUtils.EMPTY;
679         String strTabId = request.getParameter( PARAMETER_TAB_INDEX );
680 
681         if ( StringUtils.isNotBlank( strTabId ) && StringUtils.isNumeric( strTabId ) )
682         {
683             int nIdTab = Integer.parseInt( strTabId );
684 
685             List<TabConfig> listTabs = _pageService.getTabList( getUser( request ) );
686             TabConfig tabConfig = listTabs.get( nIdTab - 1 );
687 
688             Map<String, Object> model = new HashMap<String, Object>( );
689 
690             model.put( MARK_TAB_INDEX, nIdTab );
691             model.put( MARK_TAB_NAME, tabConfig.getName( ) );
692 
693             HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MYPORTAL_EDIT_TAB, request.getLocale( ), model );
694 
695             strHtml = template.getHtml( );
696         }
697 
698         return strHtml;
699     }
700 
701     /**
702      * Edit a tab
703      * 
704      * @param request
705      *            The HTTP request
706      * @return the forward url
707      */
708     public String doEditTab( HttpServletRequest request )
709     {
710         String strTabNewName = request.getParameter( PARAMETER_TAB_NAME );
711         String strTabIndex = request.getParameter( PARAMETER_TAB_INDEX );
712 
713         if ( StringUtils.isNotBlank( strTabIndex ) && StringUtils.isNumeric( strTabIndex ) )
714         {
715             int nIdTab = Integer.parseInt( strTabIndex );
716 
717             _pageService.editTab( getUser( request ), strTabNewName, nIdTab );
718         }
719 
720         return AppPropertiesService.getProperty( PROPERTY_URL_RETURN );
721     }
722 
723     /**
724      * Delete a tab
725      * 
726      * @param request
727      *            The HTTP request
728      * @return the forward url
729      */
730     public String doDelTab( HttpServletRequest request )
731     {
732         String strTabIndex = request.getParameter( PARAMETER_TAB_INDEX );
733 
734         if ( StringUtils.isNotBlank( strTabIndex ) && StringUtils.isNumeric( strTabIndex ) )
735         {
736             int nIdTab = Integer.parseInt( strTabIndex );
737 
738             // The first tab cannot be deleted
739             if ( nIdTab != 1 )
740             {
741                 _pageService.delTab( getUser( request ), nIdTab );
742             }
743         }
744 
745         return AppPropertiesService.getProperty( PROPERTY_URL_RETURN );
746     }
747 
748     /**
749      * Get the content of editing a widget
750      * 
751      * @param request
752      *            {@link HttpServletRequest}
753      * @return the html code
754      */
755     public String getMyPortalEditWidget( HttpServletRequest request )
756     {
757         String strHtml = StringUtils.EMPTY;
758         String strIdTab = request.getParameter( PARAMETER_TAB_INDEX );
759         String strIdWidget = request.getParameter( PARAMETER_ID_WIDGET );
760 
761         if ( StringUtils.isNotBlank( strIdTab ) && StringUtils.isNumeric( strIdTab ) && StringUtils.isNotBlank( strIdWidget )
762                 && StringUtils.isNumeric( strIdWidget ) )
763         {
764             int nIdTab = Integer.parseInt( strIdTab );
765             int nIdWidget = Integer.parseInt( strIdWidget );
766             int nNbColumns = _defaultPageBuilderService.getColumnCount( );
767 
768             Widget widget = _widgetService.getWidget( nIdWidget );
769 
770             if ( widget != null )
771             {
772                 Map<String, Object> model = new HashMap<String, Object>( );
773 
774                 model.put( MARK_TAB_INDEX, nIdTab );
775                 model.put( MARK_WIDGET, widget );
776                 model.put( MARK_NB_COLUMNS, nNbColumns );
777 
778                 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MYPORTAL_EDIT_WIDGET, request.getLocale( ), model );
779 
780                 strHtml = template.getHtml( );
781             }
782         }
783 
784         return strHtml;
785     }
786 
787     /**
788      * Get tabs of my portal used by MyPortal.jsp
789      * 
790      * @param request
791      *            The HTTP request
792      * @return tabs
793      */
794     public String getTabs( HttpServletRequest request )
795     {
796         return _pageService.getUserPage( getUser( request ), request );
797     }
798 }