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.notificationstore.modules.broadcast.web;
35  
36  import fr.paris.lutece.portal.service.message.AdminMessage;
37  import fr.paris.lutece.portal.service.message.AdminMessageService;
38  import fr.paris.lutece.portal.service.security.SecurityTokenService;
39  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
40  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
41  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
42  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
43  import fr.paris.lutece.util.url.UrlItem;
44  
45  import java.util.List;
46  import java.util.Map;
47  import javax.servlet.http.HttpServletRequest;
48  import fr.paris.lutece.plugins.notificationstore.modules.broadcast.business.Subscription;
49  import fr.paris.lutece.plugins.notificationstore.modules.broadcast.business.SubscriptionHome;
50  
51  /**
52   * This class provides the user interface to manage Subscription features ( manage, create, modify, remove )
53   */
54  @Controller( controllerJsp = "ManageSubscriptions.jsp", controllerPath = "jsp/admin/plugins/notificationstore/modules/broadcast/", right = "NOTIFICATIONSTORE_BROADCAST_MANAGEMENT" )
55  public class SubscriptionJspBean extends AbstractManageSubscriptionsJspBean
56  {
57      // Templates
58      private static final String TEMPLATE_MANAGE_SUBSCRIPTIONS = "/admin/plugins/notificationstore/modules/broadcast/manage_subscriptions.html";
59      private static final String TEMPLATE_CREATE_SUBSCRIPTION = "/admin/plugins/notificationstore/modules/broadcast/create_subscription.html";
60      private static final String TEMPLATE_MODIFY_SUBSCRIPTION = "/admin/plugins/notificationstore/modules/broadcast/modify_subscription.html";
61  
62      // Parameters
63      private static final String PARAMETER_ID_SUBSCRIPTION = "id";
64  
65      // Properties for page titles
66      private static final String PROPERTY_PAGE_TITLE_MANAGE_SUBSCRIPTIONS = "module.notificationstore.broadcast.manage_subscriptions.pageTitle";
67      private static final String PROPERTY_PAGE_TITLE_MODIFY_SUBSCRIPTION = "module.notificationstore.broadcast.modify_subscription.pageTitle";
68      private static final String PROPERTY_PAGE_TITLE_CREATE_SUBSCRIPTION = "module.notificationstore.broadcast.create_subscription.pageTitle";
69  
70      // Markers
71      private static final String MARK_SUBSCRIPTION_LIST = "subscription_list";
72      private static final String MARK_SUBSCRIPTION = "subscription";
73  
74      private static final String JSP_MANAGE_SUBSCRIPTIONS = "jsp/admin/plugins/notificationstore/modules/broadcast/ManageSubscriptions.jsp";
75  
76      // Properties
77      private static final String MESSAGE_CONFIRM_REMOVE_SUBSCRIPTION = "module.notificationstore.broadcast.message.confirmRemoveSubscription";
78  
79      // Validations
80      private static final String VALIDATION_ATTRIBUTES_PREFIX = "module.notificationstore.broadcast.model.entity.subscription.attribute.";
81  
82      // Views
83      private static final String VIEW_MANAGE_SUBSCRIPTIONS = "manageSubscriptions";
84      private static final String VIEW_CREATE_SUBSCRIPTION = "createSubscription";
85      private static final String VIEW_MODIFY_SUBSCRIPTION = "modifySubscription";
86  
87      // Actions
88      private static final String ACTION_CREATE_SUBSCRIPTION = "createSubscription";
89      private static final String ACTION_MODIFY_SUBSCRIPTION = "modifySubscription";
90      private static final String ACTION_REMOVE_SUBSCRIPTION = "removeSubscription";
91      private static final String ACTION_CONFIRM_REMOVE_SUBSCRIPTION = "confirmRemoveSubscription";
92  
93      // Infos
94      private static final String INFO_SUBSCRIPTION_CREATED = "module.notificationstore.broadcast.info.subscription.created";
95      private static final String INFO_SUBSCRIPTION_UPDATED = "module.notificationstore.broadcast.info.subscription.updated";
96      private static final String INFO_SUBSCRIPTION_REMOVED = "module.notificationstore.broadcast.info.subscription.removed";
97  
98      // Session variable to store working values
99      private Subscription _subscription;
100 
101     /**
102      * Build the Manage View
103      * 
104      * @param request
105      *            The HTTP request
106      * @return The page
107      */
108     @View( value = VIEW_MANAGE_SUBSCRIPTIONS, defaultView = true )
109     public String getManageSubscriptions( HttpServletRequest request )
110     {
111         _subscription = null;
112         List<Subscription> listSubscriptions = SubscriptionHome.getSubscriptionsList( );
113         Map<String, Object> model = getPaginatedListModel( request, MARK_SUBSCRIPTION_LIST, listSubscriptions, JSP_MANAGE_SUBSCRIPTIONS );
114 
115         return getPage( PROPERTY_PAGE_TITLE_MANAGE_SUBSCRIPTIONS, TEMPLATE_MANAGE_SUBSCRIPTIONS, model );
116     }
117 
118     /**
119      * Returns the form to create a subscription
120      *
121      * @param request
122      *            The Http request
123      * @return the html code of the subscription form
124      */
125     @View( VIEW_CREATE_SUBSCRIPTION )
126     public String getCreateSubscription( HttpServletRequest request )
127     {
128         _subscription = ( _subscription != null ) ? _subscription : new Subscription( );
129 
130         Map<String, Object> model = getModel( );
131         model.put( MARK_SUBSCRIPTION, _subscription );
132         model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, ACTION_CREATE_SUBSCRIPTION ) );
133 
134         return getPage( PROPERTY_PAGE_TITLE_CREATE_SUBSCRIPTION, TEMPLATE_CREATE_SUBSCRIPTION, model );
135     }
136 
137     /**
138      * Process the data capture form of a new subscription
139      *
140      * @param request
141      *            The Http Request
142      * @return The Jsp URL of the process result
143      * @throws AccessDeniedException
144      */
145     @Action( ACTION_CREATE_SUBSCRIPTION )
146     public String doCreateSubscription( HttpServletRequest request ) throws AccessDeniedException
147     {
148         populate( _subscription, request, getLocale( ) );
149 
150         if ( !SecurityTokenService.getInstance( ).validate( request, ACTION_CREATE_SUBSCRIPTION ) )
151         {
152             throw new AccessDeniedException( "Invalid security token" );
153         }
154 
155         // Check constraints
156         if ( !validateBean( _subscription, VALIDATION_ATTRIBUTES_PREFIX ) )
157         {
158             return redirectView( request, VIEW_CREATE_SUBSCRIPTION );
159         }
160 
161         SubscriptionHome.create( _subscription );
162         addInfo( INFO_SUBSCRIPTION_CREATED, getLocale( ) );
163 
164         return redirectView( request, VIEW_MANAGE_SUBSCRIPTIONS );
165     }
166 
167     /**
168      * Manages the removal form of a subscription whose identifier is in the http request
169      *
170      * @param request
171      *            The Http request
172      * @return the html code to confirm
173      */
174     @Action( ACTION_CONFIRM_REMOVE_SUBSCRIPTION )
175     public String getConfirmRemoveSubscription( HttpServletRequest request )
176     {
177         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_SUBSCRIPTION ) );
178         UrlItem url = new UrlItem( getActionUrl( ACTION_REMOVE_SUBSCRIPTION ) );
179         url.addParameter( PARAMETER_ID_SUBSCRIPTION, nId );
180 
181         String strMessageUrl = AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_SUBSCRIPTION, url.getUrl( ), AdminMessage.TYPE_CONFIRMATION );
182 
183         return redirect( request, strMessageUrl );
184     }
185 
186     /**
187      * Handles the removal form of a subscription
188      *
189      * @param request
190      *            The Http request
191      * @return the jsp URL to display the form to manage subscriptions
192      */
193     @Action( ACTION_REMOVE_SUBSCRIPTION )
194     public String doRemoveSubscription( HttpServletRequest request )
195     {
196         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_SUBSCRIPTION ) );
197         SubscriptionHome.remove( nId );
198         addInfo( INFO_SUBSCRIPTION_REMOVED, getLocale( ) );
199 
200         return redirectView( request, VIEW_MANAGE_SUBSCRIPTIONS );
201     }
202 
203     /**
204      * Returns the form to update info about a subscription
205      *
206      * @param request
207      *            The Http request
208      * @return The HTML form to update info
209      */
210     @View( VIEW_MODIFY_SUBSCRIPTION )
211     public String getModifySubscription( HttpServletRequest request )
212     {
213         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_SUBSCRIPTION ) );
214 
215         if ( _subscription == null || ( _subscription.getId( ) != nId ) )
216         {
217             _subscription = SubscriptionHome.findByPrimaryKey( nId );
218         }
219 
220         Map<String, Object> model = getModel( );
221         model.put( MARK_SUBSCRIPTION, _subscription );
222         model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, ACTION_MODIFY_SUBSCRIPTION ) );
223 
224         return getPage( PROPERTY_PAGE_TITLE_MODIFY_SUBSCRIPTION, TEMPLATE_MODIFY_SUBSCRIPTION, model );
225     }
226 
227     /**
228      * Process the change form of a subscription
229      *
230      * @param request
231      *            The Http request
232      * @return The Jsp URL of the process result
233      * @throws AccessDeniedException
234      */
235     @Action( ACTION_MODIFY_SUBSCRIPTION )
236     public String doModifySubscription( HttpServletRequest request ) throws AccessDeniedException
237     {
238         populate( _subscription, request, getLocale( ) );
239 
240         if ( !SecurityTokenService.getInstance( ).validate( request, ACTION_MODIFY_SUBSCRIPTION ) )
241         {
242             throw new AccessDeniedException( "Invalid security token" );
243         }
244 
245         // Check constraints
246         if ( !validateBean( _subscription, VALIDATION_ATTRIBUTES_PREFIX ) )
247         {
248             return redirect( request, VIEW_MODIFY_SUBSCRIPTION, PARAMETER_ID_SUBSCRIPTION, _subscription.getId( ) );
249         }
250 
251         SubscriptionHome.update( _subscription );
252         addInfo( INFO_SUBSCRIPTION_UPDATED, getLocale( ) );
253 
254         return redirectView( request, VIEW_MANAGE_SUBSCRIPTIONS );
255     }
256 }