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 package fr.paris.lutece.plugins.extend.modules.favorite.web;
35
36 import fr.paris.lutece.plugins.extend.business.extender.history.ResourceExtenderHistory;
37 import fr.paris.lutece.plugins.extend.business.extender.history.ResourceExtenderHistoryFilter;
38 import fr.paris.lutece.plugins.extend.modules.favorite.service.FavoriteListenerService;
39 import fr.paris.lutece.plugins.extend.modules.favorite.service.FavoriteService;
40 import fr.paris.lutece.plugins.extend.modules.favorite.service.IFavoriteService;
41 import fr.paris.lutece.plugins.extend.modules.favorite.service.extender.FavoriteResourceExtender;
42 import fr.paris.lutece.plugins.extend.modules.favorite.service.validator.FavoriteValidationManagementService;
43 import fr.paris.lutece.plugins.extend.modules.favorite.util.constants.FavoriteConstants;
44 import fr.paris.lutece.plugins.extend.service.ExtendPlugin;
45 import fr.paris.lutece.plugins.extend.service.extender.history.IResourceExtenderHistoryService;
46 import fr.paris.lutece.plugins.extend.service.extender.history.ResourceExtenderHistoryService;
47 import fr.paris.lutece.portal.service.message.SiteMessage;
48 import fr.paris.lutece.portal.service.message.SiteMessageException;
49 import fr.paris.lutece.portal.service.message.SiteMessageService;
50 import fr.paris.lutece.portal.service.security.LuteceUser;
51 import fr.paris.lutece.portal.service.security.SecurityService;
52 import fr.paris.lutece.portal.service.security.UserNotSignedException;
53 import fr.paris.lutece.portal.service.spring.SpringContextService;
54 import fr.paris.lutece.portal.service.util.AppPathService;
55 import fr.paris.lutece.util.url.UrlItem;
56
57 import org.apache.commons.lang3.StringUtils;
58
59 import java.io.IOException;
60 import java.util.List;
61
62 import javax.inject.Inject;
63 import javax.servlet.http.HttpServletRequest;
64 import javax.servlet.http.HttpServletResponse;
65
66
67
68
69 public class FavoriteJspBean
70 {
71 public static final String URL_JSP_DO_FAVORITE = "jsp/site/plugins/extend/modules/favorite/DoFavorite.jsp";
72
73
74 private static final String CONSTANT_HTTP = "http";
75
76
77 @Inject
78 private IResourceExtenderHistoryService _resourceExtenderHistoryService = SpringContextService.getBean( ResourceExtenderHistoryService.BEAN_SERVICE );;
79 private IFavoriteService _favoriteService = SpringContextService.getBean( FavoriteService.BEAN_SERVICE );
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96 public void doFavorite( HttpServletRequest request, HttpServletResponse response ) throws IOException, SiteMessageException, UserNotSignedException
97 {
98 String strIdExtendableResource = request.getParameter( FavoriteConstants.PARAMETER_ID_EXTENDABLE_RESOURCE );
99 String strExtendableResourceType = request.getParameter( FavoriteConstants.PARAMETER_EXTENDABLE_RESOURCE_TYPE );
100 String strFavoriteValue = request.getParameter( FavoriteConstants.PARAMETER_FAVORITE_VALUE );
101 String strFromUrl = (String) request.getSession( ).getAttribute( ExtendPlugin.PLUGIN_NAME + FavoriteConstants.PARAMETER_FROM_URL );
102
103 if ( StringUtils.isBlank( strIdExtendableResource ) || StringUtils.isBlank( strExtendableResourceType ) || StringUtils.isBlank( strFavoriteValue ) )
104 {
105 SiteMessageService.setMessage( request, FavoriteConstants.MESSAGE_ERROR_GENERIC_MESSAGE, SiteMessage.TYPE_STOP );
106 }
107
108 String strSessionKeyNextUrl = getSessionKeyUrlRedirect( strIdExtendableResource, strExtendableResourceType );
109
110 String strNextUrl = (String) request.getSession( ).getAttribute( strSessionKeyNextUrl );
111
112 if ( StringUtils.isEmpty( strNextUrl ) )
113 {
114 strNextUrl = request.getHeader( FavoriteConstants.PARAMETER_HTTP_REFERER );
115
116 if ( strNextUrl != null )
117 {
118 UrlItem url = new UrlItem( strNextUrl );
119
120 if ( StringUtils.isNotEmpty( strFromUrl ) )
121 {
122 strFromUrl = strFromUrl.replaceAll( "%", "%25" );
123 if ( !url.getUrl( ).contains( FavoriteConstants.PARAMETER_FROM_URL ) )
124 {
125 url.addParameter( FavoriteConstants.PARAMETER_FROM_URL, strFromUrl );
126 }
127 }
128
129 strNextUrl = url.getUrl( );
130 }
131 else
132 {
133 strNextUrl = AppPathService.getPortalUrl( );
134 }
135 }
136 else
137 {
138 request.getSession( ).removeAttribute( strSessionKeyNextUrl );
139 }
140
141 int nFavoriteValue = 0;
142
143 try
144 {
145 nFavoriteValue = Integer.parseInt( strFavoriteValue );
146 }
147 catch( NumberFormatException e )
148 {
149 SiteMessageService.setMessage( request, FavoriteConstants.MESSAGE_ERROR_GENERIC_MESSAGE, SiteMessage.TYPE_STOP );
150 }
151
152 String strErrorUrl = FavoriteValidationManagementService.validateFavorite( request, SecurityService.getInstance( ).getRemoteUser( request ),
153 strIdExtendableResource, strExtendableResourceType, nFavoriteValue );
154
155 if ( StringUtils.isNotEmpty( strErrorUrl ) )
156 {
157 if ( !strErrorUrl.startsWith( CONSTANT_HTTP ) )
158 {
159 strErrorUrl = AppPathService.getBaseUrl( request ) + strErrorUrl;
160 }
161
162 request.getSession( ).setAttribute( strSessionKeyNextUrl, strNextUrl );
163
164 response.sendRedirect( strErrorUrl );
165
166 return;
167 }
168
169 ResourceExtenderHistoryFilter filter = new ResourceExtenderHistoryFilter( );
170
171 filter.setExtenderType( FavoriteResourceExtender.RESOURCE_EXTENDER );
172 filter.setUserGuid( SecurityService.getInstance( ).getRemoteUser( request ).getName( ) );
173 filter.setExtendableResourceType( strExtendableResourceType );
174 filter.setIdExtendableResource( strIdExtendableResource );
175
176 List<ResourceExtenderHistory> listHistories = _resourceExtenderHistoryService.findByFilter( filter );
177
178 if ( listHistories.isEmpty( ) )
179 {
180 if ( FavoriteListenerService.canFavorite( strExtendableResourceType, strIdExtendableResource,
181 SecurityService.getInstance( ).getRemoteUser( request ) ) )
182 {
183 _favoriteService.doFavorite( strIdExtendableResource, strExtendableResourceType, nFavoriteValue, request );
184 }
185 else
186 {
187 SiteMessageService.setMessage( request, FavoriteConstants.MESSAGE_PHASE_IS_CLOSE, SiteMessage.TYPE_STOP );
188 }
189 }
190
191 response.sendRedirect( strNextUrl );
192 }
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207 public void doCancelFavorite( HttpServletRequest request, HttpServletResponse response ) throws IOException, SiteMessageException
208 {
209 String strIdExtendableResource = request.getParameter( FavoriteConstants.PARAMETER_ID_EXTENDABLE_RESOURCE );
210 String strExtendableResourceType = request.getParameter( FavoriteConstants.PARAMETER_EXTENDABLE_RESOURCE_TYPE );
211 String strFromUrl = (String) request.getSession( ).getAttribute( ExtendPlugin.PLUGIN_NAME + FavoriteConstants.PARAMETER_FROM_URL );
212 LuteceUser user = SecurityService.getInstance( ).getRegisteredUser( request );
213
214 if ( StringUtils.isBlank( strIdExtendableResource ) || StringUtils.isBlank( strExtendableResourceType ) )
215 {
216 SiteMessageService.setMessage( request, FavoriteConstants.MESSAGE_ERROR_GENERIC_MESSAGE, SiteMessage.TYPE_STOP );
217 }
218 if ( FavoriteListenerService.canFavorite( strExtendableResourceType, strIdExtendableResource, user ) )
219 {
220 _favoriteService.doCancelFavorite( user, strIdExtendableResource, strExtendableResourceType, request );
221 }
222 else
223 {
224 SiteMessageService.setMessage( request, FavoriteConstants.MESSAGE_PHASE_IS_CLOSE, SiteMessage.TYPE_STOP );
225 }
226 String strReferer = request.getHeader( FavoriteConstants.PARAMETER_HTTP_REFERER );
227
228 if ( strReferer != null )
229 {
230 UrlItem url = new UrlItem( strReferer );
231
232 if ( StringUtils.isNotEmpty( strFromUrl ) )
233 {
234 strFromUrl = strFromUrl.replaceAll( "%", "%25" );
235 if ( !url.getUrl( ).contains( FavoriteConstants.PARAMETER_FROM_URL ) )
236 {
237 url.addParameter( FavoriteConstants.PARAMETER_FROM_URL, strFromUrl );
238 }
239 }
240
241 response.sendRedirect( url.getUrl( ) );
242 }
243 else
244 {
245 response.sendRedirect( AppPathService.getPortalUrl( ) );
246 }
247 }
248
249
250
251
252
253
254
255
256
257
258 public static String getSessionKeyUrlRedirect( String strIdResource, String strResourceType )
259 {
260 return ExtendPlugin.PLUGIN_NAME + FavoriteConstants.PARAMETER_FROM_URL + strResourceType + strIdResource;
261 }
262 }