View Javadoc
1   /*
2    * Copyright (c) 2002-2017, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.myportal.modules.myapps.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.lang3.StringUtils;
44  import org.apache.commons.lang3.math.NumberUtils;
45  
46  import fr.paris.lutece.plugins.myapps.business.MyApps;
47  import fr.paris.lutece.plugins.myapps.business.MyAppsUser;
48  import fr.paris.lutece.plugins.myapps.modules.database.business.MyAppsDatabase;
49  import fr.paris.lutece.plugins.myapps.modules.database.business.MyAppsDatabaseFilter;
50  import fr.paris.lutece.plugins.myapps.modules.database.business.MyAppsDatabaseUser;
51  import fr.paris.lutece.plugins.myapps.modules.database.service.MyAppsDatabasePlugin;
52  import fr.paris.lutece.plugins.myapps.modules.database.service.MyAppsDatabaseService;
53  import fr.paris.lutece.plugins.myapps.modules.database.utils.constants.MyAppsDatabaseConstants;
54  import fr.paris.lutece.plugins.myportal.modules.myapps.services.MyPortalMyAppsService;
55  import fr.paris.lutece.plugins.myportal.service.WidgetContentService;
56  import fr.paris.lutece.portal.service.i18n.I18nService;
57  import fr.paris.lutece.portal.service.message.SiteMessage;
58  import fr.paris.lutece.portal.service.message.SiteMessageException;
59  import fr.paris.lutece.portal.service.message.SiteMessageService;
60  import fr.paris.lutece.portal.service.plugin.Plugin;
61  import fr.paris.lutece.portal.service.plugin.PluginService;
62  import fr.paris.lutece.portal.service.security.LuteceUser;
63  import fr.paris.lutece.portal.service.security.SecurityService;
64  import fr.paris.lutece.portal.service.security.UserNotSignedException;
65  import fr.paris.lutece.portal.service.spring.SpringContextService;
66  import fr.paris.lutece.portal.service.template.AppTemplateService;
67  import fr.paris.lutece.portal.service.util.AppPathService;
68  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
69  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
70  import fr.paris.lutece.portal.util.mvc.xpage.MVCApplication;
71  import fr.paris.lutece.portal.util.mvc.xpage.annotations.Controller;
72  import fr.paris.lutece.portal.web.constants.Messages;
73  import fr.paris.lutece.portal.web.xpages.XPage;
74  import fr.paris.lutece.util.html.HtmlTemplate;
75  import fr.paris.lutece.util.url.UrlItem;
76  
77  /**
78   * 
79   * XPage for the widget MyPortalMyApps
80   *
81   */
82  @Controller( xpageName = "myportal-myapps" )
83  public class MyPortalMyAppsXPage extends MVCApplication
84  {
85  
86      /**
87       * Generated serial ID
88       */
89      private static final long serialVersionUID = 5491988303257261581L;
90  
91      // Templates
92      private static final String TEMPLATE_MANAGE_USER_MYAPPS = "/skin/plugins/myportal/modules/myapps/manage_user_myapps.html";
93      private static final String TEMPLATE_INSERT_USER_MYAPPS = "/skin/plugins/myportal/modules/myapps/insert_user_myapps.html";
94      private static final String TEMPLATE_MODIFY_USER_MYAPPS = "/skin/plugins/myportal/modules/myapps/modify_user_myapps.html";
95  
96      // Views
97      private static final String VIEW_MANAGE_USER_MYAPPS = "manage_user_myapps";
98      private static final String VIEW_INSERT_USER_MYAPPS = "insert_user_myapps";
99      private static final String VIEW_MODIFY_USER_MYAPPS = "modify_user_myapps";
100     private static final String VIEW_CONFIRM_REMOVE_USER_MYAPPS = "confirm_remove_user_myapps";
101 
102     // Actions
103     private static final String ACTION_DO_INSERT = "do_insert_user_myapps";
104     private static final String ACTION_DO_MODIFY_USER_MYAPPS = "do_modify_user_myapps";
105     private static final String ACTION_DO_REMOVE_USER_MYAPPS = "do_remove_user_myapps";
106 
107     // Parameters
108     private static final String PARAMETER_BACK = "back";
109     private static final String PARAMETER_MYPORTAL_URL_RETURN = "myportal_url_return";
110     private static final String PARAMETER_MYAPP_ID = "myapp_id";
111     private static final String PARAMETER_USER_LOGIN = "user_login";
112     private static final String PARAMETER_USER_PASSWORD = "user_password";
113     private static final String PARAMETER_USER_EXTRA_DATA = "user_extra_data";
114     private static final String PARAMETER_MYAPP_CODE_CATEGORY = "myapp_code_category";
115     private static final String PARAMETER_FROM_WIDGET = "from_widget";
116     private static final String PARAMETER_MYAPPS_ORDER = "application_order";
117     private static final String PARAMETER_ID_WIDGET = "id_widget";
118 
119     // Marks
120     private static final String MARK_MYAPP = "myapp";
121     private static final String MARK_MYAPP_USER = "myapp_user";
122     private static final String MARK_MYPORTAL_URL_RETURN = "myportal_url_return";
123     private static final String MARK_ENABLED_MYAPPS_LIST = "enabled_myapps_list";
124     private static final String MARK_DISABLED_MYAPPS_LIST = "disabled_myapps_list";
125     private static final String MARK_MYAPP_CATEGORY = "myapp_category";
126     private static final String MARK_USER_MYAPPS_ORDER_LIST = "myapps_order_list";
127     private static final String MARK_MYAPPS_CURRENT_ORDER = "myapps_current_order";
128 
129     // Messages
130     private static final String MESSAGE_ERROR = "module.myapps.database.message.error";
131     private static final String MESSAGE_APPLICATION_ADDED = "module.myportal.myapps.message.applicationAdded";
132     private static final String MESSAGE_CONFIRM_REMOVE_MYAPPS = "module.myportal.myapps.message.confirmRemoveMyApps";
133     private static final String MESSAGE_MYAPPS_SUCCESS_MODIFY = "module.myportal.myapps.message.modificationMade";
134     private static final String MESSAGE_MYAPPS_SUCCESS_REMOVE = "module.myportal.myapps.message.myAppsRemove";
135 
136     // Properties
137     private static final String MANAGE_USER_MYAPPS_TITLE_PAGE = "module.myportal.myapps.manage_user_myapps.pageTitle";
138     private static final String MANAGE_USER_MYAPPS_PATH_PAGE = "module.myportal.myapps.manage_user_myapps.pagePathLabel";
139     private static final String INSERT_USER_MYAPPS_TITLE_PAGE = "module.myportal.myapps.insert_user_myapps.pageTitle";
140     private static final String INSERT_USER_MYAPPS_PATH_PAGE = "module.myportal.myapps.insert_user_myapps.pagePathLabel";
141     private static final String MODIFY_USER_MYAPPS_TITLE_PAGE = "module.myportal.myapps.modify_user_myapps.pageTitle";
142     private static final String MODIFY_USER_MYAPPS_PATH_PAGE = "module.myportal.myapps.modify_user_myapps.pageTitle";
143 
144     // Constants
145     private static final String SITE_URL = "jsp/site/";
146 
147     // Variables
148     private List<Integer> _listUserApplicationOrder = new ArrayList<>( );
149     private final MyPortalMyAppsService _myPortalMyAppsService = SpringContextService.getBean( MyPortalMyAppsService.BEAN_NAME );
150     private final WidgetContentService _widgetContentService = SpringContextService.getBean( WidgetContentService.BEAN_NAME );
151 
152     /**
153      * The manage page for adding or removing applications of a user
154      * 
155      * @param request
156      *            The HttpServletRequest
157      * @return the XPage of the management of the user applications
158      * @throws UserNotSignedException
159      */
160     @View( value = VIEW_MANAGE_USER_MYAPPS, defaultView = true )
161     public XPage getManageUserMyApps( HttpServletRequest request ) throws UserNotSignedException
162     {
163         // Manage the request back url
164         String strBackParameter = request.getParameter( PARAMETER_BACK );
165         String strBaseUrl = AppPathService.getBaseUrl( request );
166         String strMyPortalUrlReturn = request.getParameter( PARAMETER_MYPORTAL_URL_RETURN );
167         String strIdWidget = request.getParameter( PARAMETER_ID_WIDGET );
168 
169         if ( strBackParameter != null && StringUtils.isNotBlank( strMyPortalUrlReturn ) )
170         {
171             return redirect( request, strBaseUrl + strMyPortalUrlReturn );
172         }
173 
174         LuteceUser user = getUser( request );
175         XPage page = new XPage( );
176         Plugin plugin = PluginService.getPlugin( MyAppsDatabasePlugin.PLUGIN_NAME );
177         String strMyAppCategory = request.getParameter( PARAMETER_MYAPP_CODE_CATEGORY );
178 
179         // Retrieve the list of enabled and disabled applications
180         MyAppsDatabaseFilter filter = new MyAppsDatabaseFilter( );
181         filter.setUserName( user.getName( ) );
182         filter.setCategory( strMyAppCategory );
183         List<MyApps> listEnabledMyApps = _myPortalMyAppsService.getOrderedMyAppsList( user.getName( ) );
184         List<MyApps> listDisabledMyApps = MyAppsDatabaseService.getInstance( ).selectMyAppsList( filter, plugin );
185         listDisabledMyApps.removeAll( listEnabledMyApps );
186 
187         // Generate the model
188         Map<String, Object> model = new HashMap<String, Object>( );
189         fillCommons( model );
190         model.put( MARK_ENABLED_MYAPPS_LIST, listEnabledMyApps );
191         model.put( MARK_DISABLED_MYAPPS_LIST, listDisabledMyApps );
192         model.put( MARK_MYAPP_CATEGORY, strMyAppCategory );
193         model.put( MARK_MYPORTAL_URL_RETURN, strMyPortalUrlReturn );
194         model.put( PARAMETER_ID_WIDGET, strIdWidget );
195 
196         // Populate the template with the model and construct the XPage associated
197         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_USER_MYAPPS, request.getLocale( ), model );
198         page.setContent( template.getHtml( ) );
199         page.setTitle( I18nService.getLocalizedString( MANAGE_USER_MYAPPS_TITLE_PAGE, request.getLocale( ) ) );
200         page.setPathLabel( I18nService.getLocalizedString( MANAGE_USER_MYAPPS_PATH_PAGE, request.getLocale( ) ) );
201 
202         // Return XPage
203         return page;
204     }
205 
206     /**
207      * Return the view which allow a user to add favorites applications
208      * 
209      * @param request
210      *            The httpServletRequest
211      * @return the view which allow a user to add favorites applications
212      * @throws UserNotSignedException
213      * @throws SiteMessageException
214      */
215     @View( VIEW_INSERT_USER_MYAPPS )
216     public XPage getInsertUserMyApps( HttpServletRequest request ) throws UserNotSignedException, SiteMessageException
217     {
218         XPage page = new XPage( );
219         Plugin plugin = PluginService.getPlugin( MyAppsDatabasePlugin.PLUGIN_NAME );
220         String strMyPortalUrlReturn = request.getParameter( PARAMETER_MYPORTAL_URL_RETURN );
221 
222         String strMyAppId = request.getParameter( PARAMETER_MYAPP_ID );
223         String strMyAppCategory = request.getParameter( PARAMETER_MYAPP_CODE_CATEGORY );
224         String strIdWidget = request.getParameter( PARAMETER_ID_WIDGET );
225 
226         if ( StringUtils.isNotBlank( strMyAppId ) && StringUtils.isNumeric( strMyAppId ) )
227         {
228             // Retrieve the MyAppsDatabase associated to the application id
229             int nMyAppId = Integer.parseInt( strMyAppId );
230             MyAppsDatabase myApp = (MyAppsDatabase) MyAppsDatabaseService.getInstance( ).findByPrimaryKey( nMyAppId, plugin );
231 
232             if ( myApp != null )
233             {
234                 // Retrieve the list of user applications
235                 String strUserName = getUser( request ).getName( );
236                 _listUserApplicationOrder = _myPortalMyAppsService.getOrderedMyAppsIdList( strUserName );
237 
238                 // Generate the model
239                 Map<String, Object> model = new HashMap<String, Object>( );
240                 model.put( MARK_MYAPP, myApp );
241                 model.put( MARK_MYAPP_CATEGORY, strMyAppCategory );
242                 model.put( MARK_MYPORTAL_URL_RETURN, strMyPortalUrlReturn );
243                 model.put( MARK_USER_MYAPPS_ORDER_LIST, _myPortalMyAppsService.getUserListOrderForCreation( strUserName ) );
244                 model.put( PARAMETER_ID_WIDGET, strIdWidget );
245 
246                 // Populate the template with the model and construct the XPage
247                 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_INSERT_USER_MYAPPS, request.getLocale( ), model );
248                 page.setContent( template.getHtml( ) );
249                 page.setTitle( I18nService.getLocalizedString( INSERT_USER_MYAPPS_TITLE_PAGE, request.getLocale( ) ) );
250                 page.setPathLabel( I18nService.getLocalizedString( INSERT_USER_MYAPPS_PATH_PAGE, request.getLocale( ) ) );
251 
252                 // Return the XPage
253                 return page;
254             }
255             else
256             {
257                 // The application is null
258                 SiteMessageService.setMessage( request, MESSAGE_ERROR, SiteMessage.TYPE_STOP );
259             }
260         }
261         else
262         {
263             // The application id is incorrect
264             SiteMessageService.setMessage( request, MESSAGE_ERROR, SiteMessage.TYPE_STOP );
265         }
266 
267         // Redirect on the default page
268         return redirectView( request, VIEW_MANAGE_USER_MYAPPS );
269     }
270 
271     /**
272      * Action which allow user to add applications
273      * 
274      * @param request
275      *            The HttpServletRequest
276      * @return redirect to the manage page of application favorites
277      * @throws UserNotSignedException
278      * @throws SiteMessageException
279      */
280     @Action( ACTION_DO_INSERT )
281     public XPage doInsertUserMyApps( HttpServletRequest request ) throws UserNotSignedException, SiteMessageException
282     {
283         String strMyPortalUrlReturn = request.getParameter( PARAMETER_MYPORTAL_URL_RETURN );
284 
285         String strIdWidget = request.getParameter( PARAMETER_ID_WIDGET );
286 
287         Map<String, String> model = new HashMap<String, String>( );
288         model.put( MARK_MYPORTAL_URL_RETURN, strMyPortalUrlReturn );
289         model.put( PARAMETER_ID_WIDGET, strIdWidget );
290 
291         // Manage the return back
292         String strBackParameter = request.getParameter( PARAMETER_BACK );
293         if ( strBackParameter != null )
294         {
295             return redirect( request, VIEW_MANAGE_USER_MYAPPS, model );
296         }
297 
298         // Add the application to the user
299         LuteceUser luteceUser = getUser( request );
300         Plugin plugin = PluginService.getPlugin( MyAppsDatabasePlugin.PLUGIN_NAME );
301         MyAppsDatabaseUser myAppsUser = getMyAppsDatabaseUserInfo( request, luteceUser, plugin );
302 
303         if ( myAppsUser != null )
304         {
305             // remove widget content cache
306             int nIdWidget = Integer.parseInt( strIdWidget );
307             _widgetContentService.removeCache( nIdWidget, luteceUser );
308 
309             // Associate the application to the user
310             MyAppsDatabaseService.getInstance( ).createMyAppUser( myAppsUser, plugin );
311 
312             // Manage new ordering
313             List<Integer> listNewMyAppsOrder = _myPortalMyAppsService.manageMyAppsNewOrderList( _listUserApplicationOrder, myAppsUser );
314             _myPortalMyAppsService.manageMyAppsReordering( listNewMyAppsOrder, luteceUser.getName( ) );
315 
316             // Reset the actual list order
317             _listUserApplicationOrder = listNewMyAppsOrder;
318         }
319 
320         // Redirect to the manage view
321         addInfo( MESSAGE_APPLICATION_ADDED, request.getLocale( ) );
322         return redirect( request, VIEW_MANAGE_USER_MYAPPS, model );
323     }
324 
325     /**
326      * Return the XPage associated to the modification of an application
327      * 
328      * @param request
329      *            The HttpServletRequest
330      * @return the XPage associated to the modification of an application
331      * @throws UserNotSignedException
332      * @throws SiteMessageException
333      */
334     @View( VIEW_MODIFY_USER_MYAPPS )
335     public XPage getModifyUserMyApps( HttpServletRequest request ) throws UserNotSignedException, SiteMessageException
336     {
337         // Retrieve data from the request
338         String strMyAppId = request.getParameter( MyAppsDatabaseConstants.PARAMETER_MYAPP_ID );
339         String strMyAppCategory = request.getParameter( MyAppsDatabaseConstants.PARAMETER_MYAPP_CODE_CATEGORY );
340         String strMyPortalUrlReturn = request.getParameter( PARAMETER_MYPORTAL_URL_RETURN );
341         String strFromWidget = request.getParameter( PARAMETER_FROM_WIDGET );
342         String strIdWidget = request.getParameter( PARAMETER_ID_WIDGET );
343 
344         if ( StringUtils.isNotBlank( strMyAppId ) && StringUtils.isNumeric( strMyAppId ) )
345         {
346             String strUserName = getUser( request ).getName( );
347 
348             // Get the MyAppsUser associated to the application id
349             int nMyAppId = Integer.parseInt( strMyAppId );
350             Plugin plugin = PluginService.getPlugin( MyAppsDatabasePlugin.PLUGIN_NAME );
351             MyApps myApp = MyAppsDatabaseService.getInstance( ).findByPrimaryKey( nMyAppId, plugin );
352             MyAppsUser myAppUser = MyAppsDatabaseService.getInstance( ).getCredential( nMyAppId, strUserName, plugin );
353 
354             if ( ( myApp != null ) && ( myAppUser != null ) )
355             {
356                 // Rebuild the order list
357                 _listUserApplicationOrder = _myPortalMyAppsService.getOrderedMyAppsIdList( strUserName );
358                 int nOrder = _listUserApplicationOrder.indexOf( nMyAppId ) + 1;
359 
360                 // Generate the model
361                 Map<String, Object> model = new HashMap<String, Object>( );
362                 model.put( MARK_MYAPP, myApp );
363                 model.put( MARK_MYAPP_CATEGORY, strMyAppCategory );
364                 model.put( MARK_MYPORTAL_URL_RETURN, strMyPortalUrlReturn );
365                 model.put( MARK_MYAPP_USER, myAppUser );
366                 model.put( PARAMETER_FROM_WIDGET, strFromWidget );
367                 model.put( MARK_MYAPPS_CURRENT_ORDER, nOrder );
368                 model.put( MARK_USER_MYAPPS_ORDER_LIST, _myPortalMyAppsService.getUserListOrder( strUserName ) );
369                 model.put( PARAMETER_ID_WIDGET, strIdWidget );
370 
371                 // Populate the template with the model
372                 XPage page = new XPage( );
373                 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_USER_MYAPPS, request.getLocale( ), model );
374                 page.setContent( template.getHtml( ) );
375                 page.setTitle( I18nService.getLocalizedString( MODIFY_USER_MYAPPS_TITLE_PAGE, request.getLocale( ) ) );
376                 page.setPathLabel( I18nService.getLocalizedString( MODIFY_USER_MYAPPS_PATH_PAGE, request.getLocale( ) ) );
377 
378                 // Return the XPage
379                 return page;
380             }
381             else
382             {
383                 // The MyAppsUser is null
384                 SiteMessageService.setMessage( request, MESSAGE_ERROR, SiteMessage.TYPE_STOP );
385             }
386         }
387         else
388         {
389             // The application id is incorrect
390             SiteMessageService.setMessage( request, MESSAGE_ERROR, SiteMessage.TYPE_STOP );
391         }
392 
393         // Redirect to the default page
394         return redirectView( request, VIEW_MANAGE_USER_MYAPPS );
395     }
396 
397     /**
398      * Modify the application with the data filled by the user
399      * 
400      * @param request
401      *            The httpServletRequest
402      * @return make the modification and return to the manage page
403      * @throws UserNotSignedException
404      * @throws SiteMessageException
405      */
406     @Action( ACTION_DO_MODIFY_USER_MYAPPS )
407     public XPage doModifyUserMyApps( HttpServletRequest request ) throws SiteMessageException, UserNotSignedException
408     {
409         String strMyPortalUrlReturn = request.getParameter( PARAMETER_MYPORTAL_URL_RETURN );
410         String strIdWidget = request.getParameter( PARAMETER_ID_WIDGET );
411 
412         Map<String, String> model = new HashMap<String, String>( );
413         model.put( MARK_MYPORTAL_URL_RETURN, strMyPortalUrlReturn );
414         model.put( PARAMETER_ID_WIDGET, strIdWidget );
415 
416         // Manage the return back
417         String strBackParameter = request.getParameter( PARAMETER_BACK );
418         if ( strBackParameter != null )
419         {
420             String strFromWidget = request.getParameter( PARAMETER_FROM_WIDGET );
421             if ( StringUtils.isNotBlank( strFromWidget ) )
422             {
423                 return redirect( request, AppPathService.getBaseUrl( request ) + strMyPortalUrlReturn );
424             }
425             return redirect( request, VIEW_MANAGE_USER_MYAPPS, model );
426         }
427 
428         // Modify the application from the user data
429         LuteceUser luteceUser = getUser( request );
430         Plugin plugin = PluginService.getPlugin( MyAppsDatabasePlugin.PLUGIN_NAME );
431         MyAppsDatabaseUser myAppsUser = getMyAppsDatabaseUserInfo( request, getUser( request ), plugin );
432 
433         if ( myAppsUser != null )
434         {
435             // remove widget content cache
436             int nIdWidget = Integer.parseInt( strIdWidget );
437             _widgetContentService.removeCache( nIdWidget, luteceUser );
438 
439             // Update the user application
440             MyAppsDatabaseService.getInstance( ).updateMyAppUser( myAppsUser, plugin );
441 
442             // Manage new ordering
443             List<Integer> listNewMyAppsOrder = _myPortalMyAppsService.manageMyAppsNewOrderList( _listUserApplicationOrder, myAppsUser );
444             _myPortalMyAppsService.manageMyAppsReordering( listNewMyAppsOrder, luteceUser.getName( ) );
445 
446             // Reset the actual list order
447             _listUserApplicationOrder = listNewMyAppsOrder;
448 
449             // Manage the redirection if we are coming from the widget
450             String strFromWidget = request.getParameter( PARAMETER_FROM_WIDGET );
451             if ( StringUtils.isNotBlank( strFromWidget ) )
452             {
453                 return redirect( request, AppPathService.getBaseUrl( request ) + strMyPortalUrlReturn );
454             }
455 
456             // Return on the managing page
457             addInfo( MESSAGE_MYAPPS_SUCCESS_MODIFY, request.getLocale( ) );
458             return redirect( request, VIEW_MANAGE_USER_MYAPPS, model );
459         }
460 
461         // Redirect to the default page
462         return redirect( request, VIEW_MANAGE_USER_MYAPPS, model );
463     }
464 
465     /**
466      * Return the XPage of the confirmation of the removing of an application
467      * 
468      * @param request
469      *            The HttpServletRequest
470      * @return the XPage of the confirmation of the removing of an application
471      * @throws SiteMessageException
472      */
473     @View( VIEW_CONFIRM_REMOVE_USER_MYAPPS )
474     public XPage getConfirmRemoveUserMyApps( HttpServletRequest request ) throws SiteMessageException
475     {
476         // Get data from request
477         String strUrlReturn = request.getParameter( PARAMETER_MYPORTAL_URL_RETURN );
478         String strIdMyApp = request.getParameter( PARAMETER_MYAPP_ID );
479         String strFromWidget = request.getParameter( PARAMETER_FROM_WIDGET );
480         String strIdWidget = request.getParameter( PARAMETER_ID_WIDGET );
481 
482         // Construct the return url of the confirmation page
483         UrlItem url = new UrlItem( SITE_URL + getActionUrl( ACTION_DO_REMOVE_USER_MYAPPS ) );
484         url.addParameter( PARAMETER_MYAPP_ID, strIdMyApp );
485         url.addParameter( PARAMETER_MYPORTAL_URL_RETURN, strUrlReturn );
486         url.addParameter( PARAMETER_ID_WIDGET, strIdWidget );
487         if ( StringUtils.isNotBlank( strFromWidget ) )
488         {
489             url.addParameter( PARAMETER_FROM_WIDGET, strFromWidget );
490         }
491 
492         SiteMessageService.setMessage( request, MESSAGE_CONFIRM_REMOVE_MYAPPS, SiteMessage.TYPE_CONFIRMATION, url.getUrl( ), null );
493         SiteMessage siteMessage = SiteMessageService.getMessage( request );
494 
495         // Redirect to the confirmation page
496         return redirect( request, siteMessage.getText( request.getLocale( ) ) );
497     }
498 
499     /**
500      * Remove an application form the favorites applications list of the user
501      * 
502      * @param request
503      *            The HttpServletRequest
504      * @return the XPage of the managing of the applications
505      * @throws UserNotSignedException
506      */
507     @Action( ACTION_DO_REMOVE_USER_MYAPPS )
508     public XPage doRemoveUserMyApps( HttpServletRequest request ) throws UserNotSignedException
509     {
510         String strMyAppId = request.getParameter( PARAMETER_MYAPP_ID );
511         String strMyPortalUrlReturn = request.getParameter( PARAMETER_MYPORTAL_URL_RETURN );
512         String strIdWidget = request.getParameter( PARAMETER_ID_WIDGET );
513 
514         Map<String, String> model = new HashMap<String, String>( );
515         model.put( MARK_MYPORTAL_URL_RETURN, strMyPortalUrlReturn );
516         model.put( PARAMETER_ID_WIDGET, strIdWidget );
517 
518         if ( StringUtils.isNotBlank( strMyAppId ) && StringUtils.isNumeric( strMyAppId ) )
519         {
520 
521             // Retrieve the ordered list of user applications
522             LuteceUser luteceUser = getUser( request );
523             _listUserApplicationOrder = _myPortalMyAppsService.getOrderedMyAppsIdList( luteceUser.getName( ) );
524 
525             // remove widget content cache
526             int nIdWidget = Integer.parseInt( strIdWidget );
527             _widgetContentService.removeCache( nIdWidget, luteceUser );
528 
529             // Remove the user application
530             int nMyAppId = Integer.parseInt( strMyAppId );
531             Plugin plugin = PluginService.getPlugin( MyAppsDatabasePlugin.PLUGIN_NAME );
532             MyAppsDatabaseService.getInstance( ).removeMyAppUser( nMyAppId, luteceUser.getName( ), plugin );
533 
534             // Manage new ordering
535             List<Integer> listNewMyAppsOrder = new ArrayList<>( _listUserApplicationOrder );
536             listNewMyAppsOrder.remove( (Integer) nMyAppId );
537 
538             _myPortalMyAppsService.manageMyAppsReordering( listNewMyAppsOrder, luteceUser.getName( ) );
539 
540             // Reset the actual list order
541             _listUserApplicationOrder = listNewMyAppsOrder;
542 
543             // Manage the redirection if we are coming from the widget
544             String strFromWidget = request.getParameter( PARAMETER_FROM_WIDGET );
545             if ( StringUtils.isNotBlank( strFromWidget ) )
546             {
547                 return redirect( request, AppPathService.getBaseUrl( request ) + strMyPortalUrlReturn );
548             }
549 
550             // Redirect on the managing page
551             addInfo( MESSAGE_MYAPPS_SUCCESS_REMOVE, request.getLocale( ) );
552             return redirect( request, VIEW_MANAGE_USER_MYAPPS, model );
553         }
554 
555         return redirect( request, VIEW_MANAGE_USER_MYAPPS, model );
556     }
557 
558     /**
559      * Get the current user
560      *
561      * @param request
562      *            {@link HttpServletRequest}
563      * @return the current {@link LuteceUser}
564      * @throws UserNotSignedException
565      *             exception if the current user is not connected
566      */
567     private LuteceUser getUser( HttpServletRequest request ) throws UserNotSignedException
568     {
569         LuteceUser user = SecurityService.getInstance( ).getRemoteUser( request );
570 
571         if ( user == null )
572         {
573             throw new UserNotSignedException( );
574         }
575 
576         return user;
577     }
578 
579     /**
580      * Get MyAppsDatabaseUser
581      * 
582      * @param request
583      *            {@link HttpServletRequest}
584      * @param user
585      *            the {@link LuteceUser}
586      * @param plugin
587      *            {@link Plugin}
588      * @return a {@link MyAppsDatabaseUser}
589      * @throws SiteMessageException
590      *             exception if some parameters are not correctly filled
591      */
592     private MyAppsDatabaseUser getMyAppsDatabaseUserInfo( HttpServletRequest request, LuteceUser user, Plugin plugin ) throws SiteMessageException
593     {
594         MyAppsDatabaseUser myAppsUser = null;
595         String strMyAppId = request.getParameter( PARAMETER_MYAPP_ID );
596         String strUserLogin = request.getParameter( PARAMETER_USER_LOGIN );
597         String strPassword = request.getParameter( PARAMETER_USER_PASSWORD );
598         String strExtraData = request.getParameter( PARAMETER_USER_EXTRA_DATA );
599         String strApplicationOrder = request.getParameter( PARAMETER_MYAPPS_ORDER );
600 
601         if ( StringUtils.isNotBlank( strMyAppId ) && StringUtils.isNumeric( strMyAppId ) )
602         {
603             int nMyAppId = Integer.parseInt( strMyAppId );
604             MyAppsDatabase myApp = (MyAppsDatabase) MyAppsDatabaseService.getInstance( ).findByPrimaryKey( nMyAppId, plugin );
605 
606             // Check mandatory fields
607             if ( myApp != null
608                     && !( StringUtils.isNotBlank( myApp.getCode( ) ) && StringUtils.isBlank( strUserLogin ) || StringUtils.isNotBlank( myApp.getPassword( ) )
609                             && StringUtils.isBlank( strPassword ) )
610                     && ( ( StringUtils.isNotBlank( myApp.getData( ) ) && StringUtils.isNotBlank( strExtraData ) ) || ( StringUtils.isBlank( myApp.getData( ) ) || StringUtils
611                             .isBlank( myApp.getDataHeading( ) ) ) ) )
612             {
613                 String strUserName = user.getName( );
614                 myAppsUser = (MyAppsDatabaseUser) MyAppsDatabaseService.getInstance( ).getCredential( nMyAppId, strUserName, plugin );
615 
616                 if ( myAppsUser == null )
617                 {
618                     myAppsUser = new MyAppsDatabaseUser( );
619                 }
620                 myAppsUser.setName( strUserName );
621                 myAppsUser.setIdApplication( nMyAppId );
622                 myAppsUser.setStoredUserName( ( strUserLogin != null ) ? strUserLogin : StringUtils.EMPTY );
623                 myAppsUser.setStoredUserPassword( ( strPassword != null ) ? strPassword : StringUtils.EMPTY );
624                 myAppsUser.setStoredUserData( ( strExtraData != null ) ? strExtraData : StringUtils.EMPTY );
625                 myAppsUser.setApplicationOrder( NumberUtils.toInt( strApplicationOrder, NumberUtils.INTEGER_ONE ) );
626             }
627             else
628             {
629                 SiteMessageService.setMessage( request, Messages.MANDATORY_FIELDS, SiteMessage.TYPE_STOP );
630             }
631         }
632         else
633         {
634             SiteMessageService.setMessage( request, Messages.MANDATORY_FIELDS, SiteMessage.TYPE_STOP );
635         }
636 
637         return myAppsUser;
638     }
639 }