View Javadoc
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.newsletter.service.topic;
35  
36  import fr.paris.lutece.plugins.newsletter.business.topic.NewsletterTopic;
37  import fr.paris.lutece.plugins.newsletter.business.topic.NewsletterTopicHome;
38  import fr.paris.lutece.plugins.newsletter.service.NewsletterPlugin;
39  import fr.paris.lutece.portal.business.user.AdminUser;
40  import fr.paris.lutece.portal.service.plugin.Plugin;
41  import fr.paris.lutece.portal.service.plugin.PluginService;
42  import fr.paris.lutece.portal.service.spring.SpringContextService;
43  import fr.paris.lutece.util.ReferenceItem;
44  import fr.paris.lutece.util.ReferenceList;
45  
46  import java.io.Serializable;
47  import java.util.Locale;
48  import java.util.Map;
49  
50  import org.apache.commons.lang3.StringUtils;
51  
52  /**
53   * Service to manage newsletter content types
54   */
55  public class NewsletterTopicService implements Serializable
56  {
57      /**
58       * Name of the bean of this service
59       */
60      public static final String BEAN_NAME = "newsletter.newsletterTopicService";
61  
62      /**
63       * Serial version UID
64       */
65      private static final long serialVersionUID = -555734991348133022L;
66  
67      /**
68       * Get the service from Spring context
69       * 
70       * @return An instance of the service
71       */
72      public static NewsletterTopicService getService( )
73      {
74          return SpringContextService.getBean( BEAN_NAME );
75      }
76  
77      /**
78       * Get a reference list with every newsletter topic types
79       * 
80       * @param locale
81       *            The locale to get the topic types name in.
82       * @return A reference list containing an item for each newsletter topic type.
83       */
84      public ReferenceList getNewsletterTopicTypeRefList( Locale locale )
85      {
86          ReferenceList refListResult = new ReferenceList( );
87          for ( INewsletterTopicService service : SpringContextService.getBeansOfType( INewsletterTopicService.class ) )
88          {
89              ReferenceItem refItem = new ReferenceItem( );
90              refItem.setCode( service.getNewsletterTopicTypeCode( ) );
91              refItem.setName( service.getNewsletterTopicTypeName( locale ) );
92              refListResult.add( refItem );
93          }
94          return refListResult;
95      }
96  
97      /**
98       * Creates a new newsletter topic
99       * 
100      * @param newsletterTopic
101      *            The newsletter topic to create
102      * @param user
103      *            The current admin user
104      * @param locale
105      *            The current locale
106      */
107     public void createNewsletterTopic( NewsletterTopic newsletterTopic, AdminUser user, Locale locale )
108     {
109         Plugin plugin = PluginService.getPlugin( NewsletterPlugin.PLUGIN_NAME );
110         for ( INewsletterTopicService service : SpringContextService.getBeansOfType( INewsletterTopicService.class ) )
111         {
112             if ( StringUtils.equals( service.getNewsletterTopicTypeCode( ), newsletterTopic.getTopicTypeCode( ) ) )
113             {
114                 newsletterTopic.setTitle( service.getNewsletterTopicTypeName( locale ) );
115                 NewsletterTopicHome.insertNewsletterTopic( newsletterTopic, plugin );
116                 service.createNewsletterTopic( newsletterTopic, user, locale );
117             }
118         }
119     }
120 
121     /**
122      * Removes a newsletter topic.
123      * 
124      * @param newsletterTopic
125      *            The topic to remove
126      * @param user
127      *            The current admin user
128      */
129     public void removeNewsletterTopic( NewsletterTopic newsletterTopic, AdminUser user )
130     {
131         Plugin plugin = PluginService.getPlugin( NewsletterPlugin.PLUGIN_NAME );
132         for ( INewsletterTopicService service : SpringContextService.getBeansOfType( INewsletterTopicService.class ) )
133         {
134             if ( StringUtils.equals( service.getNewsletterTopicTypeCode( ), newsletterTopic.getTopicTypeCode( ) ) )
135             {
136                 service.removeNewsletterTopic( newsletterTopic.getId( ) );
137             }
138         }
139         NewsletterTopicHome.removeNewsletterTopic( newsletterTopic.getId( ), plugin );
140         NewsletterTopicHome.fillBlankInOrder( newsletterTopic.getIdNewsletter( ), newsletterTopic.getOrder( ), newsletterTopic.getSection( ), plugin );
141     }
142 
143     /**
144      * Get the configuration page of a topic
145      * 
146      * @param newsletterTopic
147      *            The topic to get the configuration page of.
148      * @param strBaseUrl
149      *            the base url
150      * @param user
151      *            The current user
152      * @param locale
153      *            The locale to use
154      * @return The HTML content of the configuration page of the topic
155      */
156     public String getConfigurationPage( NewsletterTopic newsletterTopic, String strBaseUrl, AdminUser user, Locale locale )
157     {
158         for ( INewsletterTopicService service : SpringContextService.getBeansOfType( INewsletterTopicService.class ) )
159         {
160             if ( StringUtils.equals( service.getNewsletterTopicTypeCode( ), newsletterTopic.getTopicTypeCode( ) ) )
161             {
162                 return service.getConfigurationPage( newsletterTopic, strBaseUrl, user, locale );
163             }
164         }
165         return null;
166     }
167 
168     /**
169      * Save the configuration of a topic
170      * 
171      * @param mapParameters
172      *            The map of parameters of the the configuration. The map contains request parameter if it is a request context.
173      * @param newsletterTopic
174      *            The topic to save the configuration of
175      * @param user
176      *            The current user, or null if there is no current user
177      * @param locale
178      *            The locale to use
179      */
180     public void saveConfiguration( Map<String, String [ ]> mapParameters, NewsletterTopic newsletterTopic, AdminUser user, Locale locale )
181     {
182         for ( INewsletterTopicService service : SpringContextService.getBeansOfType( INewsletterTopicService.class ) )
183         {
184             if ( StringUtils.equals( service.getNewsletterTopicTypeCode( ), newsletterTopic.getTopicTypeCode( ) ) )
185             {
186                 service.saveConfiguration( mapParameters, newsletterTopic, user, locale );
187             }
188         }
189     }
190 
191     /**
192      * Move a topic up or down in its section
193      * 
194      * @param newsletterTopic
195      *            The topic to move
196      * @param bMoveUp
197      *            True to move the topic up (ie to decrease its order), false to move it down (ie to increase its order)
198      */
199     public void modifyNewsletterTopicOrder( NewsletterTopic newsletterTopic, boolean bMoveUp )
200     {
201         Plugin plugin = PluginService.getPlugin( NewsletterPlugin.PLUGIN_NAME );
202         // If we have the first topic and we try to move it up, or if we have the last topic, and we try to move it down, we don't do anything
203         if ( bMoveUp && newsletterTopic.getOrder( ) <= 1 || !bMoveUp && newsletterTopic.getOrder( ) == NewsletterTopicHome
204                 .getLastOrder( newsletterTopic.getIdNewsletter( ), newsletterTopic.getSection( ), plugin ) )
205         {
206             return;
207         }
208 
209         NewsletterTopicHome.updateNewsletterTopicOrder( newsletterTopic, bMoveUp ? newsletterTopic.getOrder( ) - 1 : newsletterTopic.getOrder( ) + 1, plugin );
210     }
211 
212     /**
213      * Modify the section of a topic of a newsletter. The order of the topic in its new section is the last one.
214      * 
215      * @param newsletterTopic
216      *            The topic to update with the old values of topic and order.
217      * @param nSection
218      *            The new section
219      */
220     public void modifyNewsletterTopicSection( NewsletterTopic newsletterTopic, int nSection )
221     {
222         Plugin plugin = PluginService.getPlugin( NewsletterPlugin.PLUGIN_NAME );
223         if ( newsletterTopic.getSection( ) == nSection )
224         {
225             return;
226         }
227         // We save the old values
228         int nCurrentOrder = newsletterTopic.getOrder( );
229         int nCurrentSection = newsletterTopic.getSection( );
230 
231         // We update the new section and order
232         newsletterTopic.setSection( nSection );
233         newsletterTopic.setOrder( NewsletterTopicHome.getNewOrder( newsletterTopic.getIdNewsletter( ), nSection, plugin ) );
234         NewsletterTopicHome.updateNewsletterTopic( newsletterTopic, plugin );
235 
236         // We update ordered of the old section so that there is no blank
237         NewsletterTopicHome.fillBlankInOrder( newsletterTopic.getIdNewsletter( ), nCurrentOrder, nCurrentSection, plugin );
238     }
239 
240     /**
241      * Get the html content of a newsletter topic
242      * 
243      * @param newsletterTopic
244      *            The topic to get the content of
245      * @param user
246      *            The current user
247      * @param locale
248      *            The locale to display the content in.
249      * @return The html content of the topic
250      */
251     public String getTopicContent( NewsletterTopic newsletterTopic, AdminUser user, Locale locale )
252     {
253         for ( INewsletterTopicService service : SpringContextService.getBeansOfType( INewsletterTopicService.class ) )
254         {
255             if ( StringUtils.equals( service.getNewsletterTopicTypeCode( ), newsletterTopic.getTopicTypeCode( ) ) )
256             {
257                 return service.getHtmlContent( newsletterTopic, user, locale );
258             }
259         }
260         return null;
261     }
262 
263     /**
264      * Get the name of the topic type from a topic type code
265      * 
266      * @param strTopicTypeCode
267      *            The code of the topic type to get the name of
268      * @return The name of the topic type, or an empty string if no topic type is found.
269      */
270     public String getTopicTypeName( String strTopicTypeCode )
271     {
272         for ( INewsletterTopicService service : SpringContextService.getBeansOfType( INewsletterTopicService.class ) )
273         {
274             if ( StringUtils.equals( service.getNewsletterTopicTypeCode( ), strTopicTypeCode ) )
275             {
276                 return service.getNewsletterTopicTypeName( Locale.getDefault( ) );
277             }
278         }
279         return StringUtils.EMPTY;
280     }
281 }