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.announce.web;
35  
36  import fr.paris.lutece.plugins.announce.business.AnnounceSearchFilter;
37  import fr.paris.lutece.plugins.announce.business.AnnounceSearchFilterHome;
38  import fr.paris.lutece.plugins.announce.service.AnnounceSubscriptionProvider;
39  import fr.paris.lutece.portal.service.message.SiteMessage;
40  import fr.paris.lutece.portal.service.message.SiteMessageException;
41  import fr.paris.lutece.portal.service.message.SiteMessageService;
42  import fr.paris.lutece.portal.service.security.LuteceUser;
43  import fr.paris.lutece.portal.service.security.SecurityService;
44  import fr.paris.lutece.portal.service.security.UserNotSignedException;
45  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
46  import fr.paris.lutece.portal.util.mvc.xpage.MVCApplication;
47  import fr.paris.lutece.portal.util.mvc.xpage.annotations.Controller;
48  import fr.paris.lutece.portal.web.xpages.XPage;
49  
50  import org.apache.commons.lang3.StringUtils;
51  
52  import javax.servlet.http.HttpServletRequest;
53  
54  /**
55   * Announce subscribe Application
56   */
57  @Controller( xpageName = "announce-subscribe", pageTitleI18nKey = "module.announce.subscribe.announceSubscribeApp.defaultTitle", pagePathI18nKey = "module.announce.subscribe.announceSubscribeApp.defaultPath" )
58  public class AnnounceSubscribeApp extends MVCApplication
59  {
60      private static final long serialVersionUID = -3612994086181738393L;
61      private static final String ACTION_DO_CREATE_SUBSCRIPTION_FILTER = "doCreateSubscriptionFilter";
62      private static final String ACTION_DO_CREATE_USER_SUBSCRIPTION = "doCreateUserSubscription";
63      private static final String ACTION_DO_CREATE_CATEGORY_SUBSCRIPTION = "doCreateCategorySubscription";
64      private static final String PARAMETER_USER_NAME = "username";
65      private static final String PARAMETER_ID_CATEGORY = "id_category";
66      private static final String PARAMETER_REFERER = "referer";
67      private static final String MESSAGE_CATEGORY_ALREADY_SUBSCRIBED = "announce.error.subscribe.category_already_subscribed";
68      private static final String MESSAGE_USER_ALREADY_SUBSCRIBED = "announce.error.subscribe.user_already_subscribed";
69  
70      /**
71       * Do create a subscription filter and redirect the user to the search page
72       * 
73       * @param request
74       *            The request
75       * @return The XPage
76       * @throws UserNotSignedException
77       *             If the user has not signed in
78       */
79      @Action( ACTION_DO_CREATE_SUBSCRIPTION_FILTER )
80      public XPage doCreateSubscriptionFilter( HttpServletRequest request ) throws UserNotSignedException
81      {
82          LuteceUser user = SecurityService.getInstance( ).getRegisteredUser( request );
83  
84          if ( user == null )
85          {
86              throw new UserNotSignedException( );
87          }
88  
89          AnnounceSearchFilter filter = AnnounceApp.getAnnounceFilterFromRequest( request );
90          AnnounceSearchFilterHome.create( filter );
91  
92          AnnounceSubscriptionProvider.getService( ).createSubscriptionToFilter( user, filter.getIdFilter( ) );
93  
94          return redirect( request, AnnounceApp.getUrlSearchAnnounce( request ) );
95      }
96  
97      /**
98       * Do subscribe to a user
99       * 
100      * @param request
101      *            The request
102      * @return The XPage
103      * @throws UserNotSignedException
104      *             If the user has not signed in
105      * @throws SiteMessageException
106      *             If a site message needs to be displayed
107      */
108     @Action( ACTION_DO_CREATE_USER_SUBSCRIPTION )
109     public XPage doSubscribeToUser( HttpServletRequest request ) throws UserNotSignedException, SiteMessageException
110     {
111         LuteceUser user = SecurityService.getInstance( ).getRegisteredUser( request );
112 
113         if ( user == null )
114         {
115             throw new UserNotSignedException( );
116         }
117 
118         String strUserName = request.getParameter( PARAMETER_USER_NAME );
119 
120         if ( AnnounceSubscriptionProvider.getService( ).hasSubscribedToUser( user, strUserName ) )
121         {
122             SiteMessageService.setMessage( request, MESSAGE_USER_ALREADY_SUBSCRIBED, SiteMessage.TYPE_STOP );
123         }
124 
125         AnnounceSubscriptionProvider.getService( ).createSubscriptionToUser( user, strUserName );
126 
127         String strReferer = request.getHeader( PARAMETER_REFERER );
128 
129         if ( StringUtils.isNotEmpty( strReferer ) )
130         {
131             return redirect( request, strReferer );
132         }
133 
134         return redirect( request, AnnounceApp.getUrlSearchAnnounce( request ) );
135     }
136 
137     /**
138      * Do subscribe to a category
139      * 
140      * @param request
141      *            The request
142      * @return The XPage
143      * @throws UserNotSignedException
144      *             If the user has not signed in
145      * @throws SiteMessageException
146      *             If a site message needs to be displayed
147      */
148     @Action( ACTION_DO_CREATE_CATEGORY_SUBSCRIPTION )
149     public XPage doSubscribeToCategory( HttpServletRequest request ) throws UserNotSignedException, SiteMessageException
150     {
151         LuteceUser user = SecurityService.getInstance( ).getRegisteredUser( request );
152 
153         if ( user == null )
154         {
155             throw new UserNotSignedException( );
156         }
157 
158         String strIdCategory = request.getParameter( PARAMETER_ID_CATEGORY );
159 
160         if ( StringUtils.isNotEmpty( strIdCategory ) && StringUtils.isNumeric( strIdCategory ) )
161         {
162             int nIdCategory = Integer.parseInt( strIdCategory );
163 
164             if ( AnnounceSubscriptionProvider.getService( ).hasSubscribedToCategory( user, nIdCategory ) )
165             {
166                 SiteMessageService.setMessage( request, MESSAGE_CATEGORY_ALREADY_SUBSCRIBED, SiteMessage.TYPE_STOP );
167             }
168 
169             AnnounceSubscriptionProvider.getService( ).createSubscriptionToCategory( user, nIdCategory );
170         }
171 
172         String strReferer = request.getHeader( PARAMETER_REFERER );
173 
174         if ( StringUtils.isNotEmpty( strReferer ) )
175         {
176             return redirect( request, strReferer );
177         }
178 
179         return redirect( request, AnnounceApp.getUrlSearchAnnounce( request ) );
180     }
181 }