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.subscribe.service;
35
36 import fr.paris.lutece.plugins.subscribe.business.Subscription;
37 import fr.paris.lutece.plugins.subscribe.business.SubscriptionFilter;
38 import fr.paris.lutece.portal.service.security.LuteceUser;
39 import fr.paris.lutece.portal.service.security.LuteceUserService;
40 import fr.paris.lutece.portal.service.spring.SpringContextService;
41
42 import java.util.Collection;
43 import java.util.HashSet;
44 import java.util.List;
45 import java.util.Set;
46
47 import org.apache.commons.lang3.StringUtils;
48
49 import fr.paris.lutece.plugins.subscribe.business.ISubscriptionDAO;
50
51 /**
52 * Service to manage subscriptions
53 */
54 public final class SubscriptionService
55 {
56 private static SubscriptionServiceice/SubscriptionService.html#SubscriptionService">SubscriptionService _instance = new SubscriptionService( );
57 private ISubscriptionDAO _dao = SpringContextService.getBean( "subscribe.subscriptionDAO" );
58
59 /**
60 * Private constructor
61 */
62 private SubscriptionService( )
63 {
64 // Do nothing
65 }
66
67 /**
68 * Get the instance of the subscription service
69 *
70 * @return The instance of the subscription service
71 */
72 public static SubscriptionService getInstance( )
73 {
74 return _instance;
75 }
76
77 /**
78 * Create a new subscription for the given user
79 *
80 * @param subscription
81 * The subscription to create
82 * @param user
83 * The user to associate the subscription to
84 */
85 public void createSubscription( Subscription subscription, LuteceUser user )
86 {
87 createSubscription( subscription, user.getName( ) );
88 }
89
90 /**
91 * Create a new subscription for the given user Key
92 *
93 * @param subscription
94 * The subscription to create
95 * @param strLuteceUserName
96 * The user key to associate the subscription to
97 */
98 public void createSubscription( Subscription subscription, String strLuteceUserName )
99 {
100 subscription.setUserId( strLuteceUserName );
101 createSubscription( subscription );
102 }
103
104 /**
105 * Create a new subscription. The subscriber id of the subscription must have been filled
106 *
107 * @param subscription
108 * The subscription to create
109 */
110 public void createSubscription( Subscription subscription )
111 {
112 _dao.insert( subscription, SubscribePlugin.getPlugin( ) );
113 }
114
115 /**
116 * Get a subscription from its id
117 *
118 * @param nIdSubscription
119 * The id of the subscription
120 * @return The subscription, or null if no subscription has the given id
121 */
122 public Subscription findBySubscriptionId( int nIdSubscription )
123 {
124 return _dao.load( nIdSubscription, SubscribePlugin.getPlugin( ) );
125 }
126
127 /**
128 * Find subscriptions that match a given filter
129 *
130 * @param filter
131 * The filter
132 * @return The list of subscriptions that match the given filter
133 */
134 public List<Subscription> findByFilter( SubscriptionFilter filter )
135 {
136 return _dao.findByFilter( filter );
137 }
138
139 /**
140 * Remove a subscription from its id
141 *
142 * @param nIdSubscription
143 * The id of the subscription to remove
144 * @param bNotifySubscriptionProvider
145 * True to notify the provider of the subscription that it has been removed, false otherwise
146 */
147 public void removeSubscription( int nIdSubscription, boolean bNotifySubscriptionProvider )
148 {
149 if ( bNotifySubscriptionProvider )
150 {
151 removeSubscription( findBySubscriptionId( nIdSubscription ), bNotifySubscriptionProvider );
152 }
153 else
154 {
155 _dao.delete( nIdSubscription, SubscribePlugin.getPlugin( ) );
156 }
157 }
158
159 /**
160 * Remove a subscription
161 *
162 * @param subscription
163 * The subscription to remove
164 * @param bNotifySubscriptionProvider
165 * True to notify the provider of the subscription that it has been removed, false otherwise
166 */
167 public void removeSubscription( Subscription subscription, boolean bNotifySubscriptionProvider )
168 {
169 if ( bNotifySubscriptionProvider )
170 {
171 List<ISubscriptionProviderService> listProviders = SpringContextService.getBeansOfType( ISubscriptionProviderService.class );
172 for ( ISubscriptionProviderService provider : listProviders )
173 {
174 if ( StringUtils.equals( subscription.getSubscriptionProvider( ), provider.getProviderName( ) ) )
175 {
176 provider.notifySubscriptionRemoval( subscription );
177 }
178 }
179 }
180 _dao.delete( subscription.getIdSubscription( ), SubscribePlugin.getPlugin( ) );
181 }
182
183 /**
184 * Get a lutece user associated to a subscription
185 *
186 * @param subscription
187 * The subscription
188 * @return The lutece user, or null if no lutece user was found
189 */
190 public LuteceUser getLuteceUserFromSubscription( Subscription subscription )
191 {
192 return LuteceUserService.getLuteceUserFromName( subscription.getUserId( ) );
193 }
194
195 /**
196 * Get the collection of users that subscribed to a given resource with the given key
197 *
198 * @param strSubscriptionProvider
199 * The subscription provider of subscribers to get
200 * @param strSubscriptionKey
201 * The subscription key of subscribers to get
202 * @param strIdSubscribedResource
203 * The id of the subscribed resource
204 * @return The collection of users that subscribed to the given resource with the given key. Returns an empty collection if no users was found
205 */
206 public Collection<LuteceUser> getSubscriberList( String strSubscriptionProvider, String strSubscriptionKey, String strIdSubscribedResource )
207 {
208 SubscriptionFilterbusiness/SubscriptionFilter.html#SubscriptionFilter">SubscriptionFilter filter = new SubscriptionFilter( );
209 filter.setSubscriptionProvider( strSubscriptionProvider );
210 filter.setSubscriptionKey( strSubscriptionKey );
211 filter.setIdSubscribedResource( strIdSubscribedResource );
212 List<Subscription> listSubscription = findByFilter( filter );
213 Set<LuteceUser> usersFound = new HashSet<>( );
214 for ( Subscription subscription : listSubscription )
215 {
216 LuteceUser user = LuteceUserService.getLuteceUserFromName( subscription.getUserId( ) );
217 if ( user != null )
218 {
219 usersFound.add( user );
220 }
221 }
222 return usersFound;
223 }
224
225 /**
226 * Get a provider service from its name
227 *
228 * @param strProvider
229 * The name of the provider service to get
230 * @return The provider service, or null if no provider service has the given name
231 */
232 public ISubscriptionProviderService getProviderService( String strProvider )
233 {
234 List<ISubscriptionProviderService> listProviderServices = SpringContextService.getBeansOfType( ISubscriptionProviderService.class );
235 for ( ISubscriptionProviderService provider : listProviderServices )
236 {
237 if ( StringUtils.equals( strProvider, provider.getProviderName( ) ) )
238 {
239 return provider;
240 }
241 }
242 return null;
243 }
244
245 /**
246 * setSubscription
247 * @param subscription
248 */
249 public void setSubscription( Subscription subscription )
250 {
251 _dao.store( subscription, SubscribePlugin.getPlugin( ) );
252 }
253 }