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