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.service;
35  
36  import fr.paris.lutece.plugins.broadcastproxy.business.Feed;
37  import fr.paris.lutece.plugins.broadcastproxy.business.IBroadcastProvider;
38  import fr.paris.lutece.plugins.broadcastproxy.business.Subscription;
39  import fr.paris.lutece.portal.service.init.LuteceInitException;
40  import fr.paris.lutece.portal.service.spring.SpringContextService;
41  import fr.paris.lutece.portal.service.util.AppLogService;
42  
43  import java.util.List;
44  import java.util.Map;
45  
46  public class BroadcastService
47  {
48  
49      private static final String BEAN_BROADCAST_PROVIDER = "broadcastproxy.provider";
50  
51      private static IBroadcastProvider _broadcastProvider;
52      private static BroadcastService _instance;
53  
54      /**
55       * Private constructor
56       */
57      private BroadcastService( )
58      {
59      }
60  
61      /**
62       * get provider name
63       * 
64       * @return the name of the provider
65       */
66      public String getName( )
67      {
68          return _broadcastProvider.getName( );
69      }
70  
71      /**
72       * Get the unique instance of the Security Service
73       *
74       * @return The instance
75       */
76      public static synchronized BroadcastService getInstance( )
77      {
78          if ( _instance == null )
79          {
80              _instance = new BroadcastService( );
81              _instance.init( );
82          }
83  
84          return _instance;
85      }
86  
87      /**
88       * Initialize service
89       *
90       * @throws LuteceInitException
91       *             If error while initialization
92       */
93      private synchronized void init( )
94      {
95          if ( _broadcastProvider == null )
96          {
97              _broadcastProvider = (IBroadcastProvider) SpringContextService.getBean( BEAN_BROADCAST_PROVIDER );
98              AppLogService.info( "BroadcastProvider loaded : " + _broadcastProvider.getName( ) );
99          }
100     }
101 
102     /**
103      * get user subscriptions returns the user subscription list as a JSON string like :
104      * 
105      * {"subscriptions":[{"name":"EXAMPLE_ONE","active":"0"},{"name":"EXAMPLE_TWO","active":"1","data":["data1","date2","data3"]}]}
106      * 
107      * @param userId
108      * @param typeSubscription
109      * @return a JSON String
110      */
111     public String getUserSubscriptionsAsJson( String userId ) throws Exception
112     {
113         return _broadcastProvider.getUserSubscriptionsAsJson( userId );
114     }
115 
116     /**
117      * returns the user subscriptions list as list of Subscriptions by groups (themes) in HashMap with Group name as key
118      * 
119      * @param userId
120      * @param typeSubscription
121      * @return a JSON String
122      */
123     public Map<String, Map<String, List<Subscription>>> getUserSubscriptionsAsList( String userId ) throws Exception
124     {
125         return _broadcastProvider.getUserSubscriptionsAsList( userId );
126     }
127 
128     /**
129      * update user subscriptions to the specified subscription list
130      * 
131      * @param subscriptionsList
132      * @return true if success
133      * @throws Exception
134      */
135     public boolean updateSubscribtions( String userId, List<Subscription> subscriptionsList ) throws Exception
136     {
137         return _broadcastProvider.updateSubscribtions( userId, subscriptionsList );
138     }
139 
140     /**
141      * update user subscriptions to the specified subscription list
142      * 
143      * @param subscriptionsList
144      * @return true if success
145      * @throws Exception
146      */
147     public boolean updateSubscribtions( String userId, String jsonSubscriptions ) throws Exception
148     {
149         return _broadcastProvider.updateSubscribtions( userId, jsonSubscriptions );
150     }
151 
152     /**
153      * Subscribe
154      * 
155      * @param userId
156      * @param subscriptionId
157      * @param typeSubscription
158      * @return true if success
159      * @throws Exception
160      */
161     public boolean subscribe( String userId, String subscriptionId, String typeSubscription ) throws Exception
162     {
163         return _broadcastProvider.subscribe( userId, subscriptionId, typeSubscription );
164     }
165 
166     /**
167      * unsubscribe
168      * 
169      * @param userId
170      * @param subscriptionId
171      * @param typeSubscription
172      * @return true if success
173      * @throws Exception
174      */
175     public boolean unsubscribe( String userId, String subscriptionId, String typeSubscription ) throws Exception
176     {
177         return _broadcastProvider.unsubscribe( userId, subscriptionId, typeSubscription );
178     }
179 
180     /**
181      * updates a Subscription bean
182      * 
183      * @param sub
184      * @return the map
185      * @throws java.lang.Exception
186      */
187     public boolean update( Subscription sub ) throws Exception
188     {
189         return _broadcastProvider.update( sub );
190     }
191 
192     public List<String> getSubscriptionViewOrder( )
193     {
194         return _broadcastProvider.getSubscriptionViewOrder( );
195     }
196 
197     /**
198      * get the list of available feeds
199      * 
200      * @return the list
201      */
202     public List<Feed> getFeeds( )
203     {
204         return _broadcastProvider.getFeeds( );
205     }
206 
207 }