View Javadoc

1   /*
2    * Copyright (c) 2002-2014, Mairie de 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.digglike.service.subscription;
35  
36  import fr.paris.lutece.plugins.digglike.business.Category;
37  import fr.paris.lutece.plugins.digglike.business.CategoryHome;
38  import fr.paris.lutece.plugins.digglike.business.Digg;
39  import fr.paris.lutece.plugins.digglike.business.DiggHome;
40  import fr.paris.lutece.plugins.digglike.business.DiggSubmit;
41  import fr.paris.lutece.plugins.digglike.service.DiggSubmitService;
42  import fr.paris.lutece.plugins.digglike.service.DigglikePlugin;
43  import fr.paris.lutece.plugins.subscribe.business.Subscription;
44  import fr.paris.lutece.plugins.subscribe.business.SubscriptionFilter;
45  import fr.paris.lutece.plugins.subscribe.service.ISubscriptionProviderService;
46  import fr.paris.lutece.plugins.subscribe.service.SubscriptionService;
47  import fr.paris.lutece.portal.service.i18n.I18nService;
48  import fr.paris.lutece.portal.service.plugin.PluginService;
49  import fr.paris.lutece.portal.service.security.LuteceUser;
50  import fr.paris.lutece.portal.service.spring.SpringContextService;
51  
52  import org.apache.commons.lang.StringUtils;
53  
54  import java.util.List;
55  import java.util.Locale;
56  
57  
58  /**
59   * Subscription provider for digg and digg submit
60   */
61  public class DigglikeSubscriptionProviderService implements ISubscriptionProviderService
62  {
63      /**
64       * Name of the bean of the DigglikeSubscriptionProviderService
65       */
66      public static final String BEAN_NAME = "digglike.digglikeSubscriptionProviderService";
67      public static final String SUBSCRIPTION_DIGG = "digg";
68      public static final String SUBSCRIPTION_DIGG_CATEGORY = "digg_category";
69      public static final String SUBSCRIPTION_DIGG_SUBMIT = "digg_submit";
70      private static final String SUBSCRIPTION_PROVIDER_NAME = "digglike.digglikeSubscriptionProviderService";
71      private static final String MESSAGE_SUBSCRIBED_DIGG = "digglike.message.subscriptions.subscribedDigg";
72      private static final String MESSAGE_SUBSCRIBED_DIGG_SUBMIT = "digglike.message.subscriptions.subscribedDiggSubmit";
73      private static final String MESSAGE_SUBSCRIBED_DIGG_CATEGORY = "digglike.message.subscriptions.subscribedCategory";
74      private static DigglikeSubscriptionProviderService _instance;
75  
76      /**
77       * Returns the instance of the singleton
78       *
79       * @return The instance of the singleton
80       */
81      public static DigglikeSubscriptionProviderService getService(  )
82      {
83          if ( _instance == null )
84          {
85              synchronized ( DigglikeSubscriptionProviderService.class )
86              {
87                  _instance = SpringContextService.getBean( BEAN_NAME );
88              }
89          }
90  
91          return _instance;
92      }
93  
94      /**
95       * {@inheritDoc}
96       */
97      @Override
98      public String getProviderName(  )
99      {
100         return SUBSCRIPTION_PROVIDER_NAME;
101     }
102 
103     /**
104      * {@inheritDoc}
105      */
106     @Override
107     public String getSubscriptionHtmlDescription( LuteceUser user, String strSubscriptionKey,
108         String strIdSubscribedResource, Locale locale )
109     {
110         int nId = ( ( strIdSubscribedResource != null ) && StringUtils.isNumeric( strIdSubscribedResource ) )
111             ? Integer.parseInt( strIdSubscribedResource ) : 0;
112 
113         if ( nId > 0 )
114         {
115             if ( StringUtils.equals( SUBSCRIPTION_DIGG, strSubscriptionKey ) )
116             {
117                 Digg digg = DiggHome.findByPrimaryKey( nId, PluginService.getPlugin( DigglikePlugin.PLUGIN_NAME ) );
118 
119                 if ( digg != null )
120                 {
121                     Object[] params = { digg.getTitle(  ) };
122 
123                     return I18nService.getLocalizedString( MESSAGE_SUBSCRIBED_DIGG, params, locale );
124                 }
125             }
126             else if ( StringUtils.equals( SUBSCRIPTION_DIGG_SUBMIT, strSubscriptionKey ) )
127             {
128                 DiggSubmit diggSubmit = DiggSubmitService.getService(  )
129                                                          .findByPrimaryKey( nId, false,
130                         PluginService.getPlugin( DigglikePlugin.PLUGIN_NAME ) );
131 
132                 if ( diggSubmit != null )
133                 {
134                     Object[] params = { diggSubmit.getDiggSubmitTitle(  ) };
135 
136                     return I18nService.getLocalizedString( MESSAGE_SUBSCRIBED_DIGG_SUBMIT, params, locale );
137                 }
138             }
139             else if ( StringUtils.equals( SUBSCRIPTION_DIGG_CATEGORY, strSubscriptionKey ) )
140             {
141                 Category category = CategoryHome.findByPrimaryKey( nId,
142                         PluginService.getPlugin( DigglikePlugin.PLUGIN_NAME ) );
143 
144                 if ( category != null )
145                 {
146                     Object[] params = { category.getTitle(  ) };
147 
148                     return I18nService.getLocalizedString( MESSAGE_SUBSCRIBED_DIGG_CATEGORY, params, locale );
149                 }
150             }
151         }
152 
153         return StringUtils.EMPTY;
154     }
155 
156     /**
157      * {@inheritDoc}
158      */
159     @Override
160     public boolean isSubscriptionRemovable( LuteceUser user, String strSubscriptionKey, String strIdSubscribedResource )
161     {
162         // Subscriptions for diggs or digg submits are always removable
163         return true;
164     }
165 
166     /**
167      * {@inheritDoc}
168      */
169     @Override
170     public String getUrlModifySubscription( LuteceUser user, String strSubscriptionKey, String strIdSubscribedResource )
171     {
172         //No subscription modification for digg nor digg submits
173         return null;
174     }
175 
176     /**
177      * {@inheritDoc}
178      */
179     @Override
180     public void notifySubscriptionRemoval( Subscription subscription )
181     {
182         // Do nothing
183     }
184 
185     /**
186      * Remove a subscription of a user to a digg
187      * @param user The user to remove the subscription of
188      * @param nIdDigg The id of the digg
189      */
190     public void removeDiggSubscription( LuteceUser user, int nIdDigg )
191     {
192         removeDiggSubmitSubscription( user, nIdDigg, SUBSCRIPTION_DIGG );
193     }
194 
195     /**
196      * Remove a subscription of a user to a digg category
197      * @param user The user to remove the subscription of
198      * @param nIdCategory The id of the digg category
199      */
200     public void removeDiggCategorySubscription( LuteceUser user, int nIdCategory )
201     {
202         removeDiggSubmitSubscription( user, nIdCategory, SUBSCRIPTION_DIGG_CATEGORY );
203     }
204 
205     /**
206      * Remove a subscription of a user to a digg submit
207      * @param user The user to remove the subscription of
208      * @param nIdDiggSubmit The id of the digg submit
209      */
210     public void removeDiggSubmitSubscription( LuteceUser user, int nIdDiggSubmit )
211     {
212         removeDiggSubmitSubscription( user, nIdDiggSubmit, SUBSCRIPTION_DIGG_SUBMIT );
213     }
214 
215     /**
216      * Remove a subscription to a digg resource
217      * @param user The user to remove the subscription of
218      * @param nId The id of the subscribed resource
219      * @param strSubscriptionKey The subscription key
220      */
221     private void removeDiggSubmitSubscription( LuteceUser user, int nId, String strSubscriptionKey )
222     {
223         SubscriptionFilter filter = new SubscriptionFilter( user.getName(  ), getProviderName(  ), strSubscriptionKey,
224                 Integer.toString( nId ) );
225 
226         List<Subscription> listSubscription = SubscriptionService.getInstance(  ).findByFilter( filter );
227 
228         if ( ( listSubscription != null ) && ( listSubscription.size(  ) > 0 ) )
229         {
230             for ( Subscription subscription : listSubscription )
231             {
232                 SubscriptionService.getInstance(  ).removeSubscription( subscription, false );
233             }
234         }
235     }
236 
237     /**
238      * Create a new subscription to a digg
239      * @param user The user to create a subscription of
240      * @param nIdDigg The id of the digg to subscribe to
241      */
242     public void createDiggSubscription( LuteceUser user, int nIdDigg )
243     {
244         if ( !hasUserSubscribedToResource( user, nIdDigg, SUBSCRIPTION_DIGG ) )
245         {
246             createSubscription( user, nIdDigg, SUBSCRIPTION_DIGG );
247         }
248     }
249 
250     /**
251      * Create a new subscription to a digg
252      * @param user The user to create a subscription of
253      * @param nIdDigg The id of the digg to subscribe to
254      */
255     public void createDiggCategorySubscription( LuteceUser user, int nIdDigg )
256     {
257         if ( !hasUserSubscribedToResource( user, nIdDigg, SUBSCRIPTION_DIGG_CATEGORY ) )
258         {
259             createSubscription( user, nIdDigg, SUBSCRIPTION_DIGG_CATEGORY );
260         }
261     }
262 
263     /**
264      * Create a new subscription to a digg submit
265      * @param user The user to create a subscription of
266      * @param nIdDiggSubmit The id of the digg submit to subscribe to
267      */
268     public void createDiggSubmitSubscription( LuteceUser user, int nIdDiggSubmit )
269     {
270         if ( !hasUserSubscribedToResource( user, nIdDiggSubmit, SUBSCRIPTION_DIGG_SUBMIT ) )
271         {
272             createSubscription( user, nIdDiggSubmit, SUBSCRIPTION_DIGG_SUBMIT );
273         }
274     }
275 
276     private void createSubscription( LuteceUser user, int nId, String strSubscriptionKey )
277     {
278         Subscription subscription = new Subscription(  );
279         subscription.setIdSubscribedResource( Integer.toString( nId ) );
280         subscription.setUserId( user.getName(  ) );
281         subscription.setSubscriptionKey( strSubscriptionKey );
282         subscription.setSubscriptionProvider( getProviderName(  ) );
283         SubscriptionService.getInstance(  ).createSubscription( subscription );
284     }
285 
286     /**
287      * Check if a user has subscribed to a digg category
288      * @param user The user
289      * @param nIdDiggCategory The id of the digg category to check the
290      *            subscription to
291      * @return True if the user has subscribed to the given digg category, false
292      *         otherwise
293      */
294     public boolean hasUserSubscribedToDiggCategory( LuteceUser user, int nIdDiggCategory )
295     {
296         return hasUserSubscribedToResource( user, nIdDiggCategory, SUBSCRIPTION_DIGG_CATEGORY );
297     }
298 
299     /**
300      * Check if a user has subscribed to a digg submit
301      * @param user The user
302      * @param nIdDiggSubmit The id of the digg submit to check the subscription
303      *            to
304      * @return True if the user has subscribed to the given digg submit, false
305      *         otherwise
306      */
307     public boolean hasUserSubscribedToDiggSubmit( LuteceUser user, int nIdDiggSubmit )
308     {
309         return hasUserSubscribedToResource( user, nIdDiggSubmit, SUBSCRIPTION_DIGG_SUBMIT );
310     }
311 
312     /**
313      * Check if a user has subscribed to a digg
314      * @param user The user
315      * @param nIdDigg The id of the digg to check the subscription to
316      * @return True if the user has subscribed to the given digg, false
317      *         otherwise
318      */
319     public boolean hasUserSubscribedToDigg( LuteceUser user, int nIdDigg )
320     {
321         return hasUserSubscribedToResource( user, nIdDigg, SUBSCRIPTION_DIGG );
322     }
323 
324     /**
325      * Check if a user has subscribed to a resource
326      * @param user The user
327      * @param nId The id of the subscribed resource
328      * @param strSubscriptionKey The subscription key
329      * @return True if the user has subscribed to the resource, false otherwise
330      */
331     private boolean hasUserSubscribedToResource( LuteceUser user, int nId, String strSubscriptionKey )
332     {
333         SubscriptionFilter filter = new SubscriptionFilter( user.getName(  ), getProviderName(  ), strSubscriptionKey,
334                 Integer.toString( nId ) );
335         List<Subscription> listSubscription = SubscriptionService.getInstance(  ).findByFilter( filter );
336 
337         if ( ( listSubscription != null ) && ( listSubscription.size(  ) > 0 ) )
338         {
339             return true;
340         }
341 
342         return false;
343     }
344 
345     @Override
346     public String getSubscriptionHtmlDescriptionBis( LuteceUser user, String strSubscriptionKey,
347         String strIdSubscribedResource, Locale locale, String userSub )
348     {
349         // TODO is there a difference between getSubscriptionHtmlDescription and getSubscriptionHtmlDescriptionBis ?
350         return getSubscriptionHtmlDescriptionBis( user, strSubscriptionKey, strIdSubscribedResource, locale, userSub );
351     }
352 }