1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
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
65 private static final String TEMPLATE_TEST_BROADCASTPROXY = "/admin/plugins/broadcastproxy/managebroadcastproxy.html";
66
67
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
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
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
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
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
95 ReferenceList _subscriptionTypes = null;
96 List<Feed> _subscriptionFeeds = null;
97 String _currentFeedType = null;
98
99
100
101
102
103
104
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
129
130
131 List<Subscription> list = null;
132 model.put( MARK_SUBSCRIPTION_LIST, list );
133
134
135
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
157
158
159
160
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
184 for ( Feed feed : _subscriptionFeeds )
185 {
186 if ( feed.getType( ).equals( _currentFeedType ) )
187 {
188
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
204 Enumeration enum1 = request.getParameterNames( );
205 while ( enum1.hasMoreElements( ) )
206 {
207 Object obj = enum1.nextElement( );
208 String fieldName = (String) obj;
209
210
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
222 if ( fieldName.startsWith( "DATA_" + _currentFeedType + "_" ) )
223 {
224 String feedIdAndData = fieldName.substring( _currentFeedType.length( ) + 6 );
225 String feedId = feedIdAndData.substring( 0, feedIdAndData.indexOf( "_" ) );
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
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
265
266
267
268
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
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
324
325
326
327
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
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
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 }