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.Collection;
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.Widget;
46  import fr.paris.lutece.plugins.myportal.business.WidgetStatusEnum;
47  import fr.paris.lutece.plugins.myportal.business.icon.IconHome;
48  import fr.paris.lutece.plugins.myportal.service.CategoryService;
49  import fr.paris.lutece.plugins.myportal.service.StyleService;
50  import fr.paris.lutece.plugins.myportal.service.WidgetService;
51  import fr.paris.lutece.plugins.myportal.service.handler.WidgetHandlerService;
52  import fr.paris.lutece.portal.service.message.AdminMessage;
53  import fr.paris.lutece.portal.service.message.AdminMessageService;
54  import fr.paris.lutece.portal.service.spring.SpringContextService;
55  import fr.paris.lutece.portal.service.template.AppTemplateService;
56  import fr.paris.lutece.portal.service.util.AppPropertiesService;
57  import fr.paris.lutece.portal.web.admin.PluginAdminPageJspBean;
58  import fr.paris.lutece.portal.web.constants.Messages;
59  import fr.paris.lutece.portal.web.util.LocalizedPaginator;
60  import fr.paris.lutece.util.html.HtmlTemplate;
61  import fr.paris.lutece.util.html.Paginator;
62  import fr.paris.lutece.util.url.UrlItem;
63  
64  /**
65   * This class provides the user interface to manage Category, Widget, features ( manage, create, modify, remove )
66   */
67  public class WidgetJspBean extends PluginAdminPageJspBean
68  {
69      // Right
70      public static final String RIGHT_MANAGE_MYPORTAL_WIDGET = "MYPORTAL_WIDGET_MANAGEMENT";
71      private static final String BEAN_MYPORTAL_WIDGETSERVICE = "myportal.widgetService";
72  
73      // Parameters
74      private static final String PARAMETER_ID_WIDGET = "id_widget";
75      private static final String PARAMETER_WIDGET_NAME = "widget_name";
76      private static final String PARAMETER_WIDGET_DESCRIPTION = "widget_description";
77      private static final String PARAMETER_ID_CATEGORY = "id_category";
78      private static final String PARAMETER_ID_ICON = "id_icon";
79      private static final String PARAMETER_ID_STYLE = "id_style";
80      private static final String PARAMETER_WIDGET_TYPE = "widget_type";
81      private static final String PARAMETER_WIDGET_ICON = "widget_icon";
82      private static final String PARAMETER_WIDGET_CONFIG_DATA = "widget_config_data";
83      private static final String PARAMETER_PAGE_INDEX = "page_index";
84      private static final String PARAMETER_WIDGET_STATUS = "widget_status";
85      private static final String PARAMETER_UPDATE_FILE = "update_file";
86      private static final String PARAMETER_IS_ESSENTIAL = "is_essential";
87      private static final String PARAMETER_IS_NEW = "is_new";
88  
89      // templates
90      private static final String TEMPLATE_MANAGE_WIDGETS = "/admin/plugins/myportal/manage_widgets.html";
91      private static final String TEMPLATE_CREATE_WIDGET = "/admin/plugins/myportal/create_widget.html";
92      private static final String TEMPLATE_MODIFY_WIDGET = "/admin/plugins/myportal/modify_widget.html";
93  
94      // Properties
95      private static final String PROPERTY_PAGE_TITLE_MANAGE_WIDGETS = "myportal.manage_widgets.pageTitle";
96      private static final String PROPERTY_PAGE_TITLE_MODIFY_WIDGET = "myportal.modify_widget.pageTitle";
97      private static final String PROPERTY_PAGE_TITLE_CREATE_WIDGET = "myportal.create_widget.pageTitle";
98      private static final String PROPERTY_DEFAULT_LIST_WIDGET_PER_PAGE = "myportal.listWidgets.itemsPerPage";
99      private static final String PROPERTY_ACCEPTED_ICON_FORMATS = "myportal.acceptedIconFormats";
100 
101     // Markers
102     private static final String MARK_WIDGET_LIST = "widget_list";
103     private static final String MARK_WIDGET = "widget";
104     private static final String MARK_WIDGET_TYPES_LIST = "widget_types_list";
105     private static final String MARK_CATEGORIES_LIST = "categories_list";
106     private static final String MARK_ICONS_LIST = "icons_list";
107     private static final String MARK_STYLES_LIST = "widget_styles_list";
108     private static final String MARK_PAGINATOR = "paginator";
109     private static final String MARK_NB_ITEMS_PER_PAGE = "nb_items_per_page";
110     private static final String MARK_STATUS_DRAFT = "status_draft";
111     private static final String MARK_STATUS_MANDATORY = "status_mandatory";
112     private static final String MARK_STATUS_PUBLIC = "status_public";
113 
114     // Jsp Definition
115     private static final String JSP_URL_DO_REMOVE_WIDGET = "jsp/admin/plugins/myportal/DoRemoveWidget.jsp";
116     private static final String JSP_URL_MANAGE_WIDGETS = "jsp/admin/plugins/myportal/ManageWidgets.jsp";
117     private static final String JSP_REDIRECT_TO_MANAGE_WIDGETS = "ManageWidgets.jsp";
118 
119     // Messages
120     private static final String MESSAGE_CONFIRM_REMOVE_WIDGET = "myportal.message.confirmRemoveWidget";
121     private static final String MESSAGE_ERROR = "myportal.message.error";
122     private static final String MESSAGE_ERROR_ICON_FORMAT_NOT_CORRECT = "myportal.message.errorIconFormatNotCorrect";
123 
124     // Variables
125     private int _nDefaultItemsPerPage;
126     private String _strCurrentPageIndex;
127     private int _nItemsPerPage;
128     private WidgetService _widgetService = SpringContextService.getBean( BEAN_MYPORTAL_WIDGETSERVICE );
129 
130     private CategoryService _categoryService = SpringContextService.getBean( CategoryService.BEAN_NAME );
131 
132     /**
133      * Returns the list of widget
134      *
135      * @param request
136      *            The Http request
137      * @return the widgets list
138      */
139     public String getManageWidgets( HttpServletRequest request )
140     {
141         setPageTitleProperty( PROPERTY_PAGE_TITLE_MANAGE_WIDGETS );
142 
143         _strCurrentPageIndex = Paginator.getPageIndex( request, Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
144         _nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_DEFAULT_LIST_WIDGET_PER_PAGE, 50 );
145         _nItemsPerPage = Paginator.getItemsPerPage( request, Paginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage, _nDefaultItemsPerPage );
146 
147         UrlItem url = new UrlItem( JSP_URL_MANAGE_WIDGETS );
148         String strUrl = url.getUrl( );
149         Collection<Widget> listWidgets = _widgetService.getWidgetsList( );
150         LocalizedPaginator paginator = new LocalizedPaginator( (List<Widget>) listWidgets, _nItemsPerPage, strUrl, PARAMETER_PAGE_INDEX, _strCurrentPageIndex,
151                 getLocale( ) );
152 
153         Map<String, Object> model = new HashMap<String, Object>( );
154 
155         model.put( MARK_NB_ITEMS_PER_PAGE, Integer.toString( _nItemsPerPage ) );
156         model.put( MARK_PAGINATOR, paginator );
157         model.put( MARK_WIDGET_LIST, paginator.getPageItems( ) );
158 
159         HtmlTemplate templateList = AppTemplateService.getTemplate( TEMPLATE_MANAGE_WIDGETS, getLocale( ), model );
160 
161         return getAdminPage( templateList.getHtml( ) );
162     }
163 
164     /**
165      * Returns the form to create a widget
166      *
167      * @param request
168      *            The Http request
169      * @return the html code of the widget form
170      */
171     public String getCreateWidget( HttpServletRequest request )
172     {
173         setPageTitleProperty( PROPERTY_PAGE_TITLE_CREATE_WIDGET );
174 
175         Map<String, Object> model = new HashMap<String, Object>( );
176         model.put( MARK_CATEGORIES_LIST, _categoryService.getCategories( ) );
177         model.put( MARK_ICONS_LIST, IconHome.getListIcons( getPlugin( ) ) );
178         model.put( MARK_WIDGET_TYPES_LIST, WidgetHandlerService.instance( ).getHandlers( ) );
179         model.put( MARK_STYLES_LIST, StyleService.getInstance( ).getWidgetStyles( ) );
180         model.put( MARK_STATUS_DRAFT, WidgetStatusEnum.DRAFT.getId( ) );
181         model.put( MARK_STATUS_MANDATORY, WidgetStatusEnum.MANDATORY.getId( ) );
182         model.put( MARK_STATUS_PUBLIC, WidgetStatusEnum.PUBLIC.getId( ) );
183 
184         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_WIDGET, getLocale( ), model );
185 
186         return getAdminPage( template.getHtml( ) );
187     }
188 
189     /**
190      * Process the data capture form of a new widget
191      *
192      * @param request
193      *            The Http Request
194      * @return The Jsp URL of the process result
195      */
196     public String doCreateWidget( HttpServletRequest request )
197     {
198         String strUrl = StringUtils.EMPTY;
199         String strWidgetName = request.getParameter( PARAMETER_WIDGET_NAME );
200         String strWidgetDescription = request.getParameter( PARAMETER_WIDGET_DESCRIPTION );
201         String strWidgetType = request.getParameter( PARAMETER_WIDGET_TYPE );
202         String strIdCategory = request.getParameter( PARAMETER_ID_CATEGORY );
203         String strIdIcon = request.getParameter( PARAMETER_ID_ICON );
204         String strIdStyle = request.getParameter( PARAMETER_ID_STYLE );
205         String strWidgetStatus = request.getParameter( PARAMETER_WIDGET_STATUS );
206 
207         if ( StringUtils.isNotBlank( strWidgetName ) && StringUtils.isNotBlank( strWidgetDescription ) && StringUtils.isNotBlank( strIdCategory )
208                 && StringUtils.isNumeric( strIdCategory ) && StringUtils.isNotBlank( strIdIcon ) && StringUtils.isNumeric( strIdIcon )
209                 && StringUtils.isNotBlank( strIdStyle ) && StringUtils.isNumeric( strIdStyle ) && StringUtils.isNotBlank( strWidgetStatus )
210                 && StringUtils.isNumeric( strWidgetStatus ) && StringUtils.isNotBlank( strWidgetType ) )
211         {
212             int nIdCategory = Integer.parseInt( strIdCategory );
213             int nIdIcon = Integer.parseInt( strIdIcon );
214             int nIdStyle = Integer.parseInt( strIdStyle );
215             int nWidgetStatus = Integer.parseInt( strWidgetStatus );
216             String strConfigData = request.getParameter( PARAMETER_WIDGET_CONFIG_DATA );
217             boolean bIsEssential = StringUtils.isNotBlank( request.getParameter( PARAMETER_IS_ESSENTIAL ) );
218             boolean bIsNew = StringUtils.isNotBlank( request.getParameter( PARAMETER_IS_NEW ) );
219 
220             Widgets/myportal/business/Widget.html#Widget">Widget widget = new Widget( );
221             widget.setName( strWidgetName );
222             widget.setDescription( strWidgetDescription );
223             widget.setIdCategory( nIdCategory );
224             widget.setIdIcon( nIdIcon );
225             widget.setIdStyle( nIdStyle );
226             widget.setWidgetType( strWidgetType );
227             widget.setConfigData( strConfigData );
228             widget.setStatus( nWidgetStatus );
229             widget.setIsEssential( bIsEssential );
230             widget.setIsNew( bIsNew );
231 
232             _widgetService.createWidget( widget );
233 
234             strUrl = JSP_REDIRECT_TO_MANAGE_WIDGETS;
235         }
236         else
237         {
238             strUrl = AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
239         }
240 
241         return strUrl;
242     }
243 
244     /**
245      * Manages the removal form of a widget whose identifier is in the http request
246      *
247      * @param request
248      *            The Http request
249      * @return the html code to confirm
250      */
251     public String getConfirmRemoveWidget( HttpServletRequest request )
252     {
253         String strUrl = StringUtils.EMPTY;
254         String strWidgetId = request.getParameter( PARAMETER_ID_WIDGET );
255 
256         if ( StringUtils.isNotBlank( strWidgetId ) && StringUtils.isNumeric( strWidgetId ) )
257         {
258             int nId = Integer.parseInt( strWidgetId );
259             UrlItem url = new UrlItem( JSP_URL_DO_REMOVE_WIDGET );
260             url.addParameter( PARAMETER_ID_WIDGET, nId );
261 
262             strUrl = AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_WIDGET, url.getUrl( ), AdminMessage.TYPE_CONFIRMATION );
263         }
264         else
265         {
266             strUrl = AdminMessageService.getMessageUrl( request, MESSAGE_ERROR, AdminMessage.TYPE_STOP );
267         }
268 
269         return strUrl;
270     }
271 
272     /**
273      * Handles the removal form of a widget
274      *
275      * @param request
276      *            The Http request
277      * @return the jsp URL to display the form to manage widgets
278      */
279     public String doRemoveWidget( HttpServletRequest request )
280     {
281         String strUrl = StringUtils.EMPTY;
282         String strWidgetId = request.getParameter( PARAMETER_ID_WIDGET );
283 
284         if ( StringUtils.isNotBlank( strWidgetId ) && StringUtils.isNumeric( strWidgetId ) )
285         {
286             int nWidgetId = Integer.parseInt( strWidgetId );
287             _widgetService.removeWidget( nWidgetId );
288 
289             strUrl = JSP_REDIRECT_TO_MANAGE_WIDGETS;
290         }
291         else
292         {
293             strUrl = AdminMessageService.getMessageUrl( request, MESSAGE_ERROR, AdminMessage.TYPE_STOP );
294         }
295 
296         return strUrl;
297     }
298 
299     /**
300      * Returns the form to update info about a widget
301      *
302      * @param request
303      *            The Http request
304      * @return The HTML form to update info
305      */
306     public String getModifyWidget( HttpServletRequest request )
307     {
308         setPageTitleProperty( PROPERTY_PAGE_TITLE_MODIFY_WIDGET );
309 
310         String strUrl = StringUtils.EMPTY;
311         String strWidgetId = request.getParameter( PARAMETER_ID_WIDGET );
312 
313         if ( StringUtils.isNotBlank( strWidgetId ) && StringUtils.isNumeric( strWidgetId ) )
314         {
315             int nWidgetId = Integer.parseInt( strWidgetId );
316             Widget widget = _widgetService.getWidget( nWidgetId );
317 
318             if ( widget != null )
319             {
320                 Map<String, Object> model = new HashMap<String, Object>( );
321                 model.put( MARK_WIDGET, widget );
322                 model.put( MARK_CATEGORIES_LIST, _categoryService.getCategories( ) );
323                 model.put( MARK_ICONS_LIST, IconHome.getListIcons( getPlugin( ) ) );
324                 model.put( MARK_WIDGET_TYPES_LIST, WidgetHandlerService.instance( ).getHandlers( ) );
325                 model.put( MARK_STYLES_LIST, StyleService.getInstance( ).getWidgetStyles( ) );
326                 model.put( MARK_STATUS_DRAFT, WidgetStatusEnum.DRAFT.getId( ) );
327                 model.put( MARK_STATUS_MANDATORY, WidgetStatusEnum.MANDATORY.getId( ) );
328                 model.put( MARK_STATUS_PUBLIC, WidgetStatusEnum.PUBLIC.getId( ) );
329 
330                 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_WIDGET, getLocale( ), model );
331 
332                 strUrl = getAdminPage( template.getHtml( ) );
333             }
334             else
335             {
336                 strUrl = AdminMessageService.getMessageUrl( request, MESSAGE_ERROR, AdminMessage.TYPE_STOP );
337             }
338         }
339         else
340         {
341             strUrl = AdminMessageService.getMessageUrl( request, MESSAGE_ERROR, AdminMessage.TYPE_STOP );
342         }
343 
344         return strUrl;
345     }
346 
347     /**
348      * Process the change form of a widget
349      *
350      * @param request
351      *            The Http request
352      * @return The Jsp URL of the process result
353      */
354     public String doModifyWidget( HttpServletRequest request )
355     {
356         String strUrl = StringUtils.EMPTY;
357         String strWidgetId = request.getParameter( PARAMETER_ID_WIDGET );
358         String strWidgetName = request.getParameter( PARAMETER_WIDGET_NAME );
359         String strWidgetDescription = request.getParameter( PARAMETER_WIDGET_DESCRIPTION );
360         String strWidgetType = request.getParameter( PARAMETER_WIDGET_TYPE );
361         String strIdCategory = request.getParameter( PARAMETER_ID_CATEGORY );
362         String strIdIcon = request.getParameter( PARAMETER_ID_ICON );
363         String strIdStyle = request.getParameter( PARAMETER_ID_STYLE );
364         String strWidgetStatus = request.getParameter( PARAMETER_WIDGET_STATUS );
365 
366         if ( StringUtils.isNotBlank( strWidgetName ) && StringUtils.isNotBlank( strWidgetDescription ) && StringUtils.isNotBlank( strIdCategory )
367                 && StringUtils.isNumeric( strIdCategory ) && StringUtils.isNotBlank( strIdIcon ) && StringUtils.isNumeric( strIdIcon )
368                 && StringUtils.isNotBlank( strIdStyle ) && StringUtils.isNumeric( strIdStyle ) && StringUtils.isNotBlank( strWidgetStatus )
369                 && StringUtils.isNumeric( strWidgetStatus ) && StringUtils.isNotBlank( strWidgetId ) && StringUtils.isNumeric( strWidgetId )
370                 && StringUtils.isNotBlank( strWidgetType ) )
371         {
372             int nWidgetId = Integer.parseInt( strWidgetId );
373             Widget widget = _widgetService.getWidget( nWidgetId );
374 
375             if ( widget != null )
376             {
377                 int nIdCategory = Integer.parseInt( strIdCategory );
378                 int nIdIcon = Integer.parseInt( strIdIcon );
379                 int nIdStyle = Integer.parseInt( strIdStyle );
380                 int nWidgetStatus = Integer.parseInt( strWidgetStatus );
381                 String strConfigData = request.getParameter( PARAMETER_WIDGET_CONFIG_DATA );
382                 boolean bUpdateIcon = StringUtils.isNotBlank( request.getParameter( PARAMETER_UPDATE_FILE ) );
383                 boolean bIsEssential = StringUtils.isNotBlank( request.getParameter( PARAMETER_IS_ESSENTIAL ) );
384                 boolean bIsNew = StringUtils.isNotBlank( request.getParameter( PARAMETER_IS_NEW ) );
385 
386                 widget.setName( strWidgetName );
387                 widget.setDescription( strWidgetDescription );
388                 widget.setIdCategory( nIdCategory );
389                 widget.setIdIcon( nIdIcon );
390                 widget.setIdStyle( nIdStyle );
391                 widget.setWidgetType( strWidgetType );
392                 widget.setConfigData( strConfigData );
393                 widget.setStatus( nWidgetStatus );
394                 widget.setIsEssential( bIsEssential );
395                 widget.setIsNew( bIsNew );
396                 _widgetService.updateWidget( widget );
397                 strUrl = JSP_REDIRECT_TO_MANAGE_WIDGETS;
398             }
399             else
400             {
401                 strUrl = AdminMessageService.getMessageUrl( request, MESSAGE_ERROR, AdminMessage.TYPE_STOP );
402             }
403         }
404         else
405         {
406             strUrl = AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
407         }
408 
409         return strUrl;
410     }
411 }