View Javadoc
1   /*
2    * Copyright (c) 2002-2020, 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.broadcastproxy.web;
35  
36  import fr.paris.lutece.plugins.broadcastproxy.business.Feed;
37  import fr.paris.lutece.plugins.broadcastproxy.business.Subscription;
38  import fr.paris.lutece.plugins.broadcastproxy.service.BroadcastService;
39  import fr.paris.lutece.portal.service.util.AppLogService;
40  import fr.paris.lutece.portal.util.mvc.admin.MVCAdminJspBean;
41  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
42  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
43  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
44  import fr.paris.lutece.util.ReferenceList;
45  import java.util.ArrayList;
46  import java.util.Enumeration;
47  import java.util.HashMap;
48  import java.util.List;
49  import java.util.Map;
50  import javax.servlet.http.HttpServletRequest;
51  
52  /**
53   * This class provides the user interface to manage Lobby features ( manage, create, modify, remove )
54   */
55  @Controller( controllerJsp = "ManageBroadcastProxy.jsp", controllerPath = "jsp/admin/plugins/broadcastproxy/", right = "BROADCASTPROXY_MANAGEMENT" )
56  public class BroadcastproxyJspBean extends MVCAdminJspBean
57  {
58      /**
59       * 
60       */
61      private static final long serialVersionUID = 1L;
62  
63      // Templates
64      private static final String TEMPLATE_TEST_BROADCASTPROXY = "/admin/plugins/broadcastproxy/managebroadcastproxy.html";
65  
66      // actions & views
67      private static final String VIEW_TEST_BROADCAST = "testBroadCast";
68      private static final String ACTION_UPDATE_USER_SUBSCRIPTIONS = "updateUserSubscriptions";
69      private static final String ACTION_UNSUBSCRIBE = "unsubscribe";
70      private static final String ACTION_SUBSCRIBE = "subscribe";
71  
72      // Parameters
73      private static final String PARAMETER_USER_ID = "user_id";
74      private static final String PARAMETER_SUBSCRIPTION_TYPE = "subscription_type";
75      private static final String PARAMETER_SUBSCRIPTION_ID = "subscription_id";
76  
77      // Properties
78      private static final String PROPERTY_PAGE_TITLE_BROADCASTPROXY = "broadcastproxy.pageTitle";
79  
80      // Markers
81      private static final String MARK_SUBSCRIPTION_JSON = "subscription_json";
82      private static final String MARK_SUBSCRIPTION_LIST = "subscription_list";
83      private static final String MARK_SUBSCRIPTION_TYPE_LIST = "subscription_types";
84      private static final String MARK_SUBSCRIPTION_FEED_LIST = "subscription_feeds";
85      private static final String MARK_BROADCASTPROXY = "broadcastproxy";
86      private static final String MARK_LAST_USER_ID = "last_user_id";
87      private static final String MARK_LAST_SUBSCRIPTION_TYPE_ID = "last_subscription_type_id";
88  
89      // messages
90      private static final String MSG_ERROR_GET_USER_SUBSCRIPTIONS = "Error while trying to get user Subscriptions";
91      private static final String MSG_SUCCESS_UPDATE_USER_SUBSCRIPTIONS = "Update successful";
92  
93      // instance variables
94      ReferenceList _subscriptionTypes = null;
95      List<Feed> _subscriptionFeeds = null;
96      String _currentFeedType = null;
97  
98      /**
99       * Build the Manage View
100      * 
101      * @param request
102      *            The HTTP request
103      * @return The page
104      */
105     @View( value = VIEW_TEST_BROADCAST, defaultView = true )
106     public String getTestBroadCastProxy( HttpServletRequest request )
107     {
108         Map<String, Object> model = getModel( );
109         initSubscriptionFeeds( );
110 
111         if ( request.getParameter( PARAMETER_USER_ID ) != null )
112         {
113             String userId = request.getParameter( PARAMETER_USER_ID );
114             int subscriptionTypeId = -1;
115             try
116             {
117                 subscriptionTypeId = Integer.parseInt( request.getParameter( PARAMETER_SUBSCRIPTION_TYPE ) );
118                 _currentFeedType = _subscriptionTypes.get( subscriptionTypeId ).getName( );
119             }
120             catch( NumberFormatException e )
121             {
122                 addError( "Invalid subscription type" );
123             }
124 
125             try
126             {
127                 // List<Subscription> list = BroadcastService.getInstance( ).getUserSubscriptionsAsList( userId,
128                 // _subscriptionTypes.get( subscriptionTypeId ).getName( ) );
129 
130                 List<Subscription> list = null;
131                 model.put( MARK_SUBSCRIPTION_LIST, list );
132 
133                 String json = BroadcastService.getInstance( ).getUserSubscriptionsAsJson( userId );
134                 model.put( MARK_SUBSCRIPTION_JSON, json );
135 
136                 model.put( MARK_BROADCASTPROXY, BroadcastService.getInstance( ).getName( ) );
137                 model.put( MARK_LAST_USER_ID, userId );
138                 model.put( MARK_LAST_SUBSCRIPTION_TYPE_ID, subscriptionTypeId );
139 
140             }
141             catch( Exception e )
142             {
143                 addError( MSG_ERROR_GET_USER_SUBSCRIPTIONS );
144                 AppLogService.error( e.getMessage( ) );
145             }
146         }
147 
148         model.put( MARK_SUBSCRIPTION_TYPE_LIST, _subscriptionTypes );
149         model.put( MARK_SUBSCRIPTION_FEED_LIST, _subscriptionFeeds );
150 
151         return getPage( PROPERTY_PAGE_TITLE_BROADCASTPROXY, TEMPLATE_TEST_BROADCASTPROXY, model );
152     }
153 
154     /**
155      * Update action
156      * 
157      * @param request
158      *            The HTTP request
159      * @return The page
160      */
161     @Action( value = ACTION_UPDATE_USER_SUBSCRIPTIONS )
162     public String doUpdateUserSubscribtions( HttpServletRequest request )
163     {
164         initSubscriptionFeeds( );
165 
166         List<Subscription> subscriptionsList = new ArrayList<>( );
167 
168         if ( request.getParameter( PARAMETER_USER_ID ) != null )
169         {
170             String userId = request.getParameter( PARAMETER_USER_ID );
171             int subscriptionTypeId = -1;
172             try
173             {
174                 subscriptionTypeId = Integer.parseInt( request.getParameter( PARAMETER_SUBSCRIPTION_TYPE ) );
175                 _currentFeedType = _subscriptionTypes.get( subscriptionTypeId ).getName( );
176             }
177             catch( NumberFormatException e )
178             {
179                 addError( "Invalid subscription type" );
180             }
181 
182             // Init subscription list with all feeds (checkbox unchecked are not present in request)
183             for ( Feed feed : _subscriptionFeeds )
184             {
185                 if ( feed.getType( ).equals( _currentFeedType ) )
186                 {
187                     // init sub
188                     Subscriptionroadcastproxy/business/Subscription.html#Subscription">Subscription sub = new Subscription( );
189                     sub.setId( feed.getId( ) );
190                     sub.setActive( false );
191                     sub.setUserId( userId );
192                     sub.setType( feed.getType( ) );
193                     for ( String data : feed.getData( ).keySet( ) )
194                     {
195                         sub.addDataItem( data, "0" );
196                     }
197 
198                     subscriptionsList.add( sub );
199                 }
200             }
201 
202             // Update states of subscription
203             Enumeration enum1 = request.getParameterNames( );
204             while ( enum1.hasMoreElements( ) )
205             {
206                 Object obj = enum1.nextElement( );
207                 String fieldName = (String) obj;
208 
209                 // set subscription states
210                 if ( fieldName.startsWith( "SUB_" + _currentFeedType + "_" ) )
211                 {
212                     String feedId = fieldName.substring( _currentFeedType.length( ) + 5 );
213                     for ( Subscription sub : subscriptionsList )
214                     {
215                         if ( sub.getId( ).equals( feedId ) )
216                             sub.setActive( true );
217                     }
218                 }
219 
220                 // set subscription data state
221                 if ( fieldName.startsWith( "DATA_" + _currentFeedType + "_" ) )
222                 {
223                     String feedIdAndData = fieldName.substring( _currentFeedType.length( ) + 6 );
224                     String feedId = feedIdAndData.substring( 0, feedIdAndData.indexOf( "_" ) ); // feed id MUST NOT contain underscores
225                     String data = feedIdAndData.substring( feedId.length( ) + 1 );
226 
227                     for ( Subscription sub : subscriptionsList )
228                     {
229                         if ( sub.getId( ).equals( feedId ) )
230                         {
231                             sub.addDataItem( data, "1" );
232                         }
233                     }
234                 }
235             }
236 
237             // update user subscriptions
238             try
239             {
240                 boolean result = BroadcastService.getInstance( ).updateSubscribtions( userId, subscriptionsList );
241 
242                 if ( result )
243                 {
244                     addInfo( MSG_SUCCESS_UPDATE_USER_SUBSCRIPTIONS );
245                 }
246                 else
247                 {
248                     addError( MSG_ERROR_GET_USER_SUBSCRIPTIONS );
249                 }
250 
251             }
252             catch( Exception esub )
253             {
254                 addError( MSG_ERROR_GET_USER_SUBSCRIPTIONS );
255                 AppLogService.error( esub.getMessage( ) );
256             }
257         }
258 
259         return getTestBroadCastProxy( request );
260     }
261 
262     /**
263      * Subscribe
264      * 
265      * @param request
266      *            The HTTP request
267      * @return The page
268      */
269     @Action( value = ACTION_SUBSCRIBE )
270     public String doSubscribe( HttpServletRequest request )
271     {
272         initSubscriptionFeeds( );
273 
274         if ( request.getParameter( PARAMETER_USER_ID ) != null )
275         {
276             String userId = request.getParameter( PARAMETER_USER_ID );
277             int subscriptionTypeId = -1;
278             try
279             {
280                 subscriptionTypeId = Integer.parseInt( request.getParameter( PARAMETER_SUBSCRIPTION_TYPE ) );
281                 _currentFeedType = _subscriptionTypes.get( subscriptionTypeId ).getName( );
282             }
283             catch( NumberFormatException e )
284             {
285                 addError( "Invalid subscription type" );
286             }
287 
288             String subscriptionId = request.getParameter( PARAMETER_SUBSCRIPTION_ID );
289 
290             Subscriptionroadcastproxy/business/Subscription.html#Subscription">Subscription sub = new Subscription( );
291             sub.setId( subscriptionId );
292             sub.setActive( true );
293             sub.setUserId( userId );
294             sub.setType( _subscriptionTypes.get( subscriptionTypeId ).getName( ) );
295 
296             // update user subscription
297             try
298             {
299                 boolean result = BroadcastService.getInstance( ).update( sub );
300 
301                 if ( result )
302                 {
303                     addInfo( MSG_SUCCESS_UPDATE_USER_SUBSCRIPTIONS );
304                 }
305                 else
306                 {
307                     addError( MSG_ERROR_GET_USER_SUBSCRIPTIONS );
308                 }
309 
310             }
311             catch( Exception esub )
312             {
313                 addError( MSG_ERROR_GET_USER_SUBSCRIPTIONS );
314                 AppLogService.error( esub.getMessage( ) );
315             }
316         }
317 
318         return getTestBroadCastProxy( request );
319     }
320 
321     /**
322      * Unsubscribe
323      * 
324      * @param request
325      *            The HTTP request
326      * @return The page
327      */
328     @Action( value = ACTION_UNSUBSCRIBE )
329     public String doUnsubscribe( HttpServletRequest request )
330     {
331         initSubscriptionFeeds( );
332 
333         if ( request.getParameter( PARAMETER_USER_ID ) != null )
334         {
335             String userId = request.getParameter( PARAMETER_USER_ID );
336             int subscriptionTypeId = -1;
337             try
338             {
339                 subscriptionTypeId = Integer.parseInt( request.getParameter( PARAMETER_SUBSCRIPTION_TYPE ) );
340                 _currentFeedType = _subscriptionTypes.get( subscriptionTypeId ).getName( );
341             }
342             catch( NumberFormatException e )
343             {
344                 addError( "Invalid subscription type" );
345             }
346 
347             String subscriptionId = request.getParameter( PARAMETER_SUBSCRIPTION_ID );
348 
349             Subscriptionroadcastproxy/business/Subscription.html#Subscription">Subscription sub = new Subscription( );
350             sub.setId( subscriptionId );
351             sub.setActive( false );
352             sub.setUserId( userId );
353             sub.setType( _subscriptionTypes.get( subscriptionTypeId ).getName( ) );
354 
355             // update user subscriptions
356             try
357             {
358                 boolean result = BroadcastService.getInstance( ).update( sub );
359 
360                 if ( result )
361                 {
362                     addInfo( MSG_SUCCESS_UPDATE_USER_SUBSCRIPTIONS );
363                 }
364                 else
365                 {
366                     addError( MSG_ERROR_GET_USER_SUBSCRIPTIONS );
367                 }
368 
369             }
370             catch( Exception esub )
371             {
372                 addError( MSG_ERROR_GET_USER_SUBSCRIPTIONS );
373                 AppLogService.error( esub.getMessage( ) );
374             }
375         }
376 
377         return getTestBroadCastProxy( request );
378     }
379 
380     /**
381      * init
382      */
383     private void initSubscriptionFeeds( )
384     {
385         if ( _subscriptionFeeds == null )
386         {
387             _subscriptionTypes = new ReferenceList( );
388             _subscriptionFeeds = BroadcastService.getInstance( ).getFeeds( );
389 
390             Map<String, String> mapTypes = new HashMap<>( );
391             for ( Feed feed : _subscriptionFeeds )
392             {
393                 mapTypes.put( feed.getType( ), feed.getType( ) );
394             }
395 
396             int i = 0;
397             for ( String feedType : mapTypes.keySet( ) )
398             {
399                 if ( _currentFeedType == null )
400                     _currentFeedType = feedType;
401 
402                 _subscriptionTypes.addItem( String.valueOf( i++ ), feedType );
403             }
404         }
405     }
406 
407 }