View Javadoc
1   /*
2    * Copyright (c) 2002-2021, City of 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.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   * FavoriteJspBean
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      // TEMPLATES
74      private static final String CONSTANT_HTTP = "http";
75  
76      // SERVICES
77      @Inject
78      private IResourceExtenderHistoryService _resourceExtenderHistoryService = SpringContextService.getBean( ResourceExtenderHistoryService.BEAN_SERVICE );;
79      private IFavoriteService _favoriteService = SpringContextService.getBean( FavoriteService.BEAN_SERVICE );
80  
81      /**
82       * Update the favorite value an count. This method is called in FO by the favoriteing JSP :
83       * <strong>jsp/site/plugins/extend/modules/favorite/DoFavorite.Jsp</strong>
84       *
85       * @param request
86       *            The HTTP request
87       * @param response
88       *            The HTTP response
89       * @throws IOException
90       *             the io exception
91       * @throws SiteMessageException
92       *             the site message exception
93       * @throws UserNotSignedException
94       *             If the user has not signed in
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      * Cancel the favorite value This method is called in FO by the favoriteing JSP :
196      * <strong>jsp/site/plugins/extend/modules/favorite/DoCancelFavorite.Jsp</strong>
197      * 
198      * @param request
199      *            The HTTP request
200      * @param response
201      *            The HTTP response
202      * @throws IOException
203      *             the io exception
204      * @throws SiteMessageException
205      *             the site message exception
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      * Get the session key of the URL to redirect the user to after he has favorited for the resource
251      * 
252      * @param strIdResource
253      *            The id of the resource
254      * @param strResourceType
255      *            The type of the resource
256      * @return The session key
257      */
258     public static String getSessionKeyUrlRedirect( String strIdResource, String strResourceType )
259     {
260         return ExtendPlugin.PLUGIN_NAME + FavoriteConstants.PARAMETER_FROM_URL + strResourceType + strIdResource;
261     }
262 }