View Javadoc
1   /*
2    * Copyright (c) 2002-2016, 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  
35  package fr.paris.lutece.plugins.myportal.modules.myfavorites.web;
36  
37  import java.util.Map;
38  
39  import javax.servlet.http.HttpServletRequest;
40  
41  import org.apache.commons.lang.StringUtils;
42  import org.apache.commons.lang3.math.NumberUtils;
43  
44  import fr.paris.lutece.plugins.myportal.modules.myfavorites.business.MyFavorites;
45  import fr.paris.lutece.plugins.myportal.modules.myfavorites.business.MyFavoritesHome;
46  import fr.paris.lutece.plugins.myportal.modules.myfavorites.services.MyFavoritesService;
47  import fr.paris.lutece.plugins.myportal.service.IconService;
48  import fr.paris.lutece.plugins.myportal.service.WidgetContentService;
49  import fr.paris.lutece.portal.service.i18n.I18nService;
50  import fr.paris.lutece.portal.service.message.SiteMessage;
51  import fr.paris.lutece.portal.service.message.SiteMessageException;
52  import fr.paris.lutece.portal.service.message.SiteMessageService;
53  import fr.paris.lutece.portal.service.page.PageNotFoundException;
54  import fr.paris.lutece.portal.service.security.LuteceUser;
55  import fr.paris.lutece.portal.service.security.SecurityService;
56  import fr.paris.lutece.portal.service.security.UserNotSignedException;
57  import fr.paris.lutece.portal.service.spring.SpringContextService;
58  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
59  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
60  import fr.paris.lutece.portal.util.mvc.xpage.MVCApplication;
61  import fr.paris.lutece.portal.util.mvc.xpage.annotations.Controller;
62  import fr.paris.lutece.portal.web.constants.Messages;
63  import fr.paris.lutece.portal.web.xpages.XPage;
64  import fr.paris.lutece.util.ReferenceList;
65  import fr.paris.lutece.util.url.UrlItem;
66  
67  /**
68   * This class provides the user interface to manage MyFavorites xpages ( manage, create, modify, remove )
69   */
70  @Controller( xpageName = "myfavorites", pageTitleI18nKey = "module.myportal.myfavorites.xpage.myfavorites.pageTitle", pagePathI18nKey = "module.myportal.myfavorites.xpage.myfavorites.pagePathLabel" )
71  public class MyFavoritesXPage extends MVCApplication
72  {
73      /**
74       * Generated serial ID
75       */
76      private static final long serialVersionUID = -2075209499748771950L;
77  
78      // Templates
79      private static final String TEMPLATE_MANAGE_MYFAVORITESS = "/skin/plugins/myportal/modules/myfavorites/manage_myfavoritess.html";
80      private static final String TEMPLATE_CREATE_MYFAVORITES = "/skin/plugins/myportal/modules/myfavorites/create_myfavorites.html";
81      private static final String TEMPLATE_MODIFY_MYFAVORITES = "/skin/plugins/myportal/modules/myfavorites/modify_myfavorites.html";
82  
83      // JSP
84      private static final String JSP_PAGE_REMOVE_FAVORITE = "jsp/site/plugins/myportal/modules/myfavorites/DoRemoveMyFavorite.jsp";
85      // Parameters
86      private static final String PARAMETER_ID_MYFAVORITES = "id";
87      private static final String PARAMETER_FAVORITES_URL_RETURN = "myfavorites_url_return";
88      private static final String PARAMETER_ID_WIDGET = "id_widget";
89      private static final String PARAMETER_MYPORTAL_URL_RETURN = "myportal_url_return";
90      private static final String PARAMETER_BACK = "back";
91  
92      // Markers
93      private static final String MARK_MYFAVORITES_LIST = "myfavorites_list";
94      private static final String MARK_MYFAVORITES = "myfavorites";
95  
96      private static final String MARK_MYFAVORITES_URL_RETURN = "myfavorites_url_return";
97      private static final String MARK_MYPORTAL_URL_RETURN = "myportal_url_return";
98      private static final String MARK_ID_WIDGET = "id_widget";
99      private static final String MARK_ICONS_LIST = "icons_list";
100     private static final String MARK_FAVORITES_ORDER_LIST = "favorites_order_list";
101     private static final String MARK_USER_CONNECTED = "user_connected";
102 
103     // Message
104     private static final String MESSAGE_CONFIRM_REMOVE_MYFAVORITES = "module.myportal.myfavorites.message.confirmRemoveMyFavorites";
105 
106     // Views
107     private static final String VIEW_MANAGE_MYFAVORITESS = "manageMyFavoritess";
108     private static final String VIEW_CREATE_MYFAVORITES = "createMyFavorites";
109     private static final String VIEW_MODIFY_MYFAVORITES = "modifyMyFavorites";
110 
111     // Actions
112     private static final String ACTION_CREATE_MYFAVORITES = "createMyFavorites";
113     private static final String ACTION_MODIFY_MYFAVORITES = "modifyMyFavorites";
114     private static final String ACTION_REMOVE_MYFAVORITES = "removeMyFavorites";
115     private static final String ACTION_CONFIRM_REMOVE_MYFAVORITES = "confirmRemoveMyFavorites";
116 
117     // Infos
118     private static final String INFO_MYFAVORITES_CREATED = "module.myportal.myfavorites.info.myfavorites.created";
119     private static final String INFO_MYFAVORITES_UPDATED = "module.myportal.myfavorites.info.myfavorites.updated";
120     private static final String INFO_MYFAVORITES_REMOVED = "module.myportal.myfavorites.info.myfavorites.removed";
121 
122     // Errors
123     private static final String ERROR_USER_NOT_CONNECTED = "module.myportal.myfavorites.error.user.notConnected";
124 
125     // Session variable to store working values
126     private MyFavorites _myfavorites;
127     private final MyFavoritesService _myFavoritesService = SpringContextService.getBean( MyFavoritesService.BEAN_NAME );
128     private final WidgetContentService _widgetContentService = SpringContextService.getBean( WidgetContentService.BEAN_NAME );
129 
130     @View( value = VIEW_MANAGE_MYFAVORITESS, defaultView = true )
131     public XPage getManageMyFavoritess( HttpServletRequest request )
132     {
133         _myfavorites = null;
134         Map<String, Object> model = getModel( );
135         model.put( MARK_MYFAVORITES_LIST, MyFavoritesHome.getMyFavoritessList( ) );
136 
137         return getXPage( TEMPLATE_MANAGE_MYFAVORITESS, request.getLocale( ), model );
138     }
139 
140     /**
141      * Returns the form to create a myfavorites
142      *
143      * @param request
144      *            The Http request
145      * @return the html code of the myfavorites form
146      * @throws SiteMessageException
147      * @throws UserNotSignedException
148      */
149     @View( VIEW_CREATE_MYFAVORITES )
150     public XPage getCreateMyFavorites( HttpServletRequest request ) throws SiteMessageException
151     {
152         Map<String, Object> model = getModel( );
153 
154         LuteceUser user = null;
155         try
156         {
157             user = getUser( request );
158             model.put( MARK_USER_CONNECTED, Boolean.TRUE );
159         }
160         catch( UserNotSignedException exception )
161         {
162             addError( I18nService.getLocalizedString( ERROR_USER_NOT_CONNECTED, request.getLocale( ) ) );
163             model.put( MARK_USER_CONNECTED, Boolean.FALSE );
164         }
165 
166         _myfavorites = ( _myfavorites != null ) ? _myfavorites : new MyFavorites( );
167 
168         String strIdWidget = request.getParameter( PARAMETER_ID_WIDGET );
169         String strMyPortalMyFavoritesUrlReturn = request.getParameter( MARK_MYFAVORITES_URL_RETURN );
170         String strMyPortalUrlReturn = request.getParameter( PARAMETER_MYPORTAL_URL_RETURN );
171 
172         if ( StringUtils.isBlank( strMyPortalMyFavoritesUrlReturn ) )
173         {
174             strMyPortalMyFavoritesUrlReturn = StringUtils.EMPTY;
175         }
176 
177         if ( StringUtils.isBlank( strMyPortalUrlReturn ) )
178         {
179             strMyPortalUrlReturn = StringUtils.EMPTY;
180         }
181 
182         if ( StringUtils.isNotBlank( strIdWidget ) && StringUtils.isNumeric( strIdWidget ) )
183         {
184             model.put( MARK_ICONS_LIST, IconService.getInstance( ).getIconFO( ) );
185             model.put( PARAMETER_FAVORITES_URL_RETURN, strMyPortalMyFavoritesUrlReturn );
186             model.put( MARK_MYPORTAL_URL_RETURN, strMyPortalUrlReturn );
187             model.put( MARK_ID_WIDGET, strIdWidget );
188 
189             String strIdUser = ( user != null ) ? user.getName( ) : StringUtils.EMPTY;
190             ReferenceList listOrderUserFavorites = _myFavoritesService.getUserOrderFavoritesListForCreation( strIdUser );
191             model.put( MARK_FAVORITES_ORDER_LIST, listOrderUserFavorites );
192         }
193 
194         model.put( MARK_MYFAVORITES, _myfavorites );
195         fillCommons( model );
196 
197         return getXPage( TEMPLATE_CREATE_MYFAVORITES, request.getLocale( ), model );
198     }
199 
200     /**
201      * Process the data capture form of a new myfavorites
202      *
203      * @param request
204      *            The Http Request
205      * @return The Jsp URL of the process result
206      * @throws SiteMessageException
207      * @throws UserNotSignedException
208      */
209     @Action( ACTION_CREATE_MYFAVORITES )
210     public String doCreateMyFavorites( HttpServletRequest request ) throws SiteMessageException, UserNotSignedException
211     {
212         // Manage the case where the user cancel his action
213         String strUrlReturn = request.getParameter( PARAMETER_FAVORITES_URL_RETURN );
214         if ( request.getParameter( PARAMETER_BACK ) != null && StringUtils.isNotEmpty( strUrlReturn ) )
215         {
216             return strUrlReturn;
217         }
218 
219         LuteceUser user = getUser( request );
220 
221         // Populate MyFavorites
222         _myfavorites = ( _myfavorites != null ) ? _myfavorites : new MyFavorites( );
223         populate( _myfavorites, request );
224         _myfavorites.setIdUser( user.getName( ) );
225 
226         // Check constraints
227         if ( !validateBean( _myfavorites, getLocale( request ) ) )
228         {
229             SiteMessageService.setMessage( request, Messages.MANDATORY_FIELDS, SiteMessage.TYPE_ERROR );
230         }
231 
232         String strIdWidget = request.getParameter( PARAMETER_ID_WIDGET );
233         if ( StringUtils.isNotBlank( strIdWidget ) && StringUtils.isNumeric( strIdWidget ) )
234         {
235             int nIdWidget = Integer.parseInt( strIdWidget );
236             _widgetContentService.removeCache( nIdWidget, user );
237             String strUserName = ( user != null ) ? user.getName( ) : StringUtils.EMPTY;
238             _myFavoritesService.manageMyFavoritesCreation( strUserName, _myfavorites );
239             addInfo( INFO_MYFAVORITES_CREATED, getLocale( request ) );
240         }
241         else
242         {
243             SiteMessageService.setMessage( request, Messages.MANDATORY_FIELDS, SiteMessage.TYPE_ERROR );
244         }
245 
246         return strUrlReturn;
247     }
248 
249     /**
250      * Manages the removal form of a myfavorites whose identifier is in the http request
251      *
252      * @param request
253      *            The Http request
254      * @return the html code to confirm
255      * @throws fr.paris.lutece.portal.service.message.SiteMessageException
256      * @throws UserNotSignedException
257      */
258     @Action( ACTION_CONFIRM_REMOVE_MYFAVORITES )
259     public XPage getConfirmRemoveMyFavorites( HttpServletRequest request ) throws SiteMessageException, UserNotSignedException
260     {
261         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_MYFAVORITES ) );
262         String strMyPortalMyFavoritesUrlReturn = request.getParameter( MARK_MYFAVORITES_URL_RETURN );
263         String strIdWidget = request.getParameter( PARAMETER_ID_WIDGET );
264 
265         UrlItem url = new UrlItem( JSP_PAGE_REMOVE_FAVORITE );
266         url.addParameter( PARAMETER_ID_MYFAVORITES, nId );
267         url.addParameter( PARAMETER_FAVORITES_URL_RETURN, strMyPortalMyFavoritesUrlReturn );
268         url.addParameter( MARK_ID_WIDGET, strIdWidget );
269 
270         SiteMessageService.setMessage( request, MESSAGE_CONFIRM_REMOVE_MYFAVORITES, SiteMessage.TYPE_CONFIRMATION, url.getUrl( ) );
271         return null;
272     }
273 
274     /**
275      * Handles the removal form of a myfavorites
276      *
277      * @param request
278      *            The Http request
279      * @return the jsp URL to display the form to manage myfavoritess
280      * @throws UserNotSignedException
281      */
282     @Action( ACTION_REMOVE_MYFAVORITES )
283     public String doRemoveMyFavorites( HttpServletRequest request ) throws UserNotSignedException
284     {
285         LuteceUser user = getUser( request );
286         String strUrlReturn = request.getParameter( PARAMETER_FAVORITES_URL_RETURN );
287 
288         String strIdWidget = request.getParameter( PARAMETER_ID_WIDGET );
289         int nIdWidget = Integer.parseInt( strIdWidget );
290         _widgetContentService.removeCache( nIdWidget, user );
291 
292         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_MYFAVORITES ) );
293         String strIdUser = ( user != null ) ? user.getName( ) : StringUtils.EMPTY;
294         _myFavoritesService.manageMyFavoritesRemoving( nId, strIdUser );
295 
296         addInfo( INFO_MYFAVORITES_REMOVED, getLocale( request ) );
297 
298         return strUrlReturn;
299     }
300 
301     /**
302      * Returns the form to update info about a myfavorites
303      *
304      * @param request
305      *            The Http request
306      * @return The HTML form to update info
307      * @throws UserNotSignedException
308      */
309     @View( VIEW_MODIFY_MYFAVORITES )
310     public XPage getModifyMyFavorites( HttpServletRequest request ) throws UserNotSignedException
311     {
312         LuteceUser user = getUser( request );
313 
314         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_MYFAVORITES ) );
315 
316         if ( _myfavorites == null || ( _myfavorites.getId( ) != nId ) )
317         {
318             _myfavorites = MyFavoritesHome.findByPrimaryKey( nId );
319         }
320 
321         Map<String, Object> model = getModel( );
322 
323         String strIdWidget = request.getParameter( PARAMETER_ID_WIDGET );
324         String strMyPortalMyFavoritesUrlReturn = request.getParameter( MARK_MYFAVORITES_URL_RETURN );
325         String strMyPortalUrlReturn = request.getParameter( PARAMETER_MYPORTAL_URL_RETURN );
326 
327         if ( StringUtils.isBlank( strMyPortalMyFavoritesUrlReturn ) )
328         {
329             strMyPortalMyFavoritesUrlReturn = StringUtils.EMPTY;
330         }
331 
332         if ( StringUtils.isBlank( strMyPortalUrlReturn ) )
333         {
334             strMyPortalUrlReturn = StringUtils.EMPTY;
335         }
336 
337         if ( StringUtils.isNotBlank( strIdWidget ) && StringUtils.isNumeric( strIdWidget ) )
338         {
339             model.put( MARK_ICONS_LIST, IconService.getInstance( ).getIconFO( ) );
340             model.put( PARAMETER_FAVORITES_URL_RETURN, strMyPortalMyFavoritesUrlReturn );
341             model.put( MARK_MYPORTAL_URL_RETURN, strMyPortalUrlReturn );
342             model.put( MARK_ID_WIDGET, strIdWidget );
343         }
344 
345         model.put( MARK_MYFAVORITES, _myfavorites );
346 
347         String strIdUser = ( user != null ) ? user.getName( ) : StringUtils.EMPTY;
348         ReferenceList listOrderUserFavorites = _myFavoritesService.getUserOrderFavorites( strIdUser );
349         model.put( MARK_FAVORITES_ORDER_LIST, listOrderUserFavorites );
350 
351         return getXPage( TEMPLATE_MODIFY_MYFAVORITES, request.getLocale( ), model );
352     }
353 
354     /**
355      * Process the change form of a myfavorites
356      *
357      * @param request
358      *            The Http request
359      * @return The Jsp URL of the process result
360      * @throws UserNotSignedException
361      * @throws SiteMessageException
362      */
363     @Action( ACTION_MODIFY_MYFAVORITES )
364     public String doModifyMyFavorites( HttpServletRequest request ) throws UserNotSignedException, SiteMessageException
365     {
366         // Manage the case where the user cancel his action
367         String strUrlReturn = request.getParameter( PARAMETER_FAVORITES_URL_RETURN );
368         if ( request.getParameter( PARAMETER_BACK ) != null && StringUtils.isNotEmpty( strUrlReturn ) )
369         {
370             return strUrlReturn;
371         }
372 
373         LuteceUser user = getUser( request );
374 
375         int nIdMyFavorites = NumberUtils.toInt( request.getParameter( PARAMETER_ID_MYFAVORITES ), NumberUtils.INTEGER_ZERO );
376         MyFavorites myFavorites = MyFavoritesHome.findByPrimaryKey( nIdMyFavorites );
377 
378         int nOrderOrigin = NumberUtils.INTEGER_ZERO;
379         if ( myFavorites != null )
380         {
381             nOrderOrigin = myFavorites.getOrder( );
382         }
383 
384         _myfavorites = ( _myfavorites != null ) ? _myfavorites : new MyFavorites( );
385         populate( _myfavorites, request );
386         _myfavorites.setIdUser( user.getName( ) );
387 
388         // Check constraints
389         if ( !validateBean( _myfavorites, getLocale( request ) ) )
390         {
391             SiteMessageService.setMessage( request, Messages.MANDATORY_FIELDS, SiteMessage.TYPE_ERROR );
392         }
393 
394         String strIdWidget = request.getParameter( PARAMETER_ID_WIDGET );
395         int nIdWidget = Integer.parseInt( strIdWidget );
396         _widgetContentService.removeCache( nIdWidget, user );
397         String strIdUser = ( user != null ) ? user.getName( ) : StringUtils.EMPTY;
398         _myFavoritesService.manageModifyMyFavorites( _myfavorites, strIdUser, nOrderOrigin );
399         addInfo( INFO_MYFAVORITES_UPDATED, getLocale( request ) );
400 
401         return strUrlReturn;
402     }
403 
404     /**
405      * Gets the user from the request
406      * 
407      * @param request
408      *            The HTTP user
409      * @return The Lutece User
410      * @throws UserNotSignedException
411      *             exception if user not connected
412      */
413     public LuteceUser getUser( HttpServletRequest request ) throws UserNotSignedException
414     {
415         if ( SecurityService.isAuthenticationEnable( ) )
416         {
417             LuteceUser user = SecurityService.getInstance( ).getRemoteUser( request );
418 
419             if ( user == null )
420             {
421                 throw new UserNotSignedException( );
422             }
423 
424             return user;
425         }
426         else
427         {
428             throw new PageNotFoundException( );
429         }
430     }
431 }