1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
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
75
76 private static final long serialVersionUID = -2075209499748771950L;
77
78
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
84 private static final String JSP_PAGE_REMOVE_FAVORITE = "jsp/site/plugins/myportal/modules/myfavorites/DoRemoveMyFavorite.jsp";
85
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
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
104 private static final String MESSAGE_CONFIRM_REMOVE_MYFAVORITES = "module.myportal.myfavorites.message.confirmRemoveMyFavorites";
105
106
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
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
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
123 private static final String ERROR_USER_NOT_CONNECTED = "module.myportal.myfavorites.error.user.notConnected";
124
125
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
142
143
144
145
146
147
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
202
203
204
205
206
207
208
209 @Action( ACTION_CREATE_MYFAVORITES )
210 public String doCreateMyFavorites( HttpServletRequest request ) throws SiteMessageException, UserNotSignedException
211 {
212
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
222 _myfavorites = ( _myfavorites != null ) ? _myfavorites : new MyFavorites( );
223 populate( _myfavorites, request );
224 _myfavorites.setIdUser( user.getName( ) );
225
226
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
251
252
253
254
255
256
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
276
277
278
279
280
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
303
304
305
306
307
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
356
357
358
359
360
361
362
363 @Action( ACTION_MODIFY_MYFAVORITES )
364 public String doModifyMyFavorites( HttpServletRequest request ) throws UserNotSignedException, SiteMessageException
365 {
366
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
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
406
407
408
409
410
411
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 }