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.FreeHtmlTopic;
37  import fr.paris.lutece.plugins.newsletter.business.topic.FreeHtmlTopicHome;
38  import fr.paris.lutece.plugins.newsletter.business.topic.NewsletterTopic;
39  import fr.paris.lutece.plugins.newsletter.service.NewsletterPlugin;
40  import fr.paris.lutece.plugins.newsletter.util.NewsletterUtils;
41  import fr.paris.lutece.portal.business.user.AdminUser;
42  import fr.paris.lutece.portal.service.i18n.I18nService;
43  import fr.paris.lutece.portal.service.plugin.Plugin;
44  import fr.paris.lutece.portal.service.plugin.PluginService;
45  import fr.paris.lutece.portal.service.template.AppTemplateService;
46  import fr.paris.lutece.util.html.HtmlTemplate;
47  
48  import java.util.HashMap;
49  import java.util.Locale;
50  import java.util.Map;
51  
52  import org.apache.commons.lang3.StringUtils;
53  
54  /**
55   * Service to manage topics with free html.
56   */
57  public class FreeHtmlTopicService implements INewsletterTopicService
58  {
59  
60      /**
61       * Code of the topic type
62       */
63      public static final String NEWSLETTER_FREE_HTML_TOPIC_TYPE_CODE = "FREE_HTML";
64  
65      private static final String NEWSLETTER_FREE_HTML_TOPIC_TYPE_NAME = "newsletter.topic.freeHtmlTopicType";
66  
67      // MARKS
68      private static final String MARK_HTML_TOPIC = "htmlTopic";
69      private static final String MARK_WEBAPP_URL = "webapp_url";
70      private static final String MARK_LOCALE = "locale";
71  
72      // PARAMETERS
73      private static final String PARAMETER_CONTENT = "html_content";
74  
75      // TEMPLATES
76      private static final String TEMPLATE_MODIFY_FREE_HTML_CONFIGURATION = "admin/plugins/newsletter/free_html/modify_config_free_html.html";
77  
78      private Plugin _plugin;
79  
80      /**
81       * {@inheritDoc}
82       */
83      @Override
84      public String getNewsletterTopicTypeCode( )
85      {
86          return NEWSLETTER_FREE_HTML_TOPIC_TYPE_CODE;
87      }
88  
89      /**
90       * {@inheritDoc}
91       */
92      @Override
93      public String getNewsletterTopicTypeName( Locale locale )
94      {
95          return I18nService.getLocalizedString( NEWSLETTER_FREE_HTML_TOPIC_TYPE_NAME, locale );
96      }
97  
98      /**
99       * {@inheritDoc}
100      * 
101      * @return This method always returns <i>true</i>.
102      */
103     public boolean hasConfiguration( )
104     {
105         return true;
106     }
107 
108     /**
109      * {@inheritDoc}
110      */
111     @Override
112     public String getConfigurationPage( NewsletterTopic newsletterTopic, String strBaseUrl, AdminUser user, Locale locale )
113     {
114         FreeHtmlTopic htmlTopic = FreeHtmlTopicHome.findByPrimaryKey( newsletterTopic.getId( ), getPlugin( ) );
115         Map<String, Object> model = new HashMap<String, Object>( );
116 
117         model.put( MARK_HTML_TOPIC, htmlTopic );
118         model.put( MARK_WEBAPP_URL, strBaseUrl );
119         model.put( MARK_LOCALE, locale );
120 
121         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_FREE_HTML_CONFIGURATION, locale, model );
122 
123         return template.getHtml( );
124     }
125 
126     /**
127      * {@inheritDoc}
128      */
129     @Override
130     public void saveConfiguration( Map<String, String [ ]> mapParameters, NewsletterTopic newsletterTopic, AdminUser user, Locale locale )
131     {
132         String strContent = NewsletterUtils.getStringFromStringArray( mapParameters.get( PARAMETER_CONTENT ) );
133         if ( StringUtils.isNotEmpty( strContent ) )
134         {
135             FreeHtmlTopic topic = FreeHtmlTopicHome.findByPrimaryKey( newsletterTopic.getId( ), getPlugin( ) );
136             topic.setHtmlContent( strContent );
137             FreeHtmlTopicHome.updateFreeHtmlTopic( topic, getPlugin( ) );
138         }
139     }
140 
141     /**
142      * {@inheritDoc}
143      */
144     @Override
145     public void createNewsletterTopic( NewsletterTopic newsletterTopic, AdminUser user, Locale locale )
146     {
147         FreeHtmlTopicr/business/topic/FreeHtmlTopic.html#FreeHtmlTopic">FreeHtmlTopic freeHtmlTopic = new FreeHtmlTopic( );
148         freeHtmlTopic.setId( newsletterTopic.getId( ) );
149         freeHtmlTopic.setHtmlContent( StringUtils.EMPTY );
150         FreeHtmlTopicHome.insertFreeHtmlTopic( freeHtmlTopic, getPlugin( ) );
151     }
152 
153     /**
154      * {@inheritDoc}
155      */
156     @Override
157     public void removeNewsletterTopic( int nNewsletterTopicId )
158     {
159         FreeHtmlTopicHome.removeFreeHtmlTopic( nNewsletterTopicId, getPlugin( ) );
160     }
161 
162     /**
163      * {@inheritDoc}
164      */
165     @Override
166     public String getHtmlContent( NewsletterTopic newsletterTopic, AdminUser user, Locale locale )
167     {
168         FreeHtmlTopic freeHtmlTopic = FreeHtmlTopicHome.findByPrimaryKey( newsletterTopic.getId( ), getPlugin( ) );
169         return freeHtmlTopic.getHtmlContent( );
170     }
171 
172     /**
173      * Get the instance of the newsletter plugin
174      * 
175      * @return the newsletter plugin
176      */
177     private Plugin getPlugin( )
178     {
179         if ( _plugin == null )
180         {
181             _plugin = PluginService.getPlugin( NewsletterPlugin.PLUGIN_NAME );
182         }
183         return _plugin;
184     }
185 
186     @Override
187     public void copyNewsletterTopic( int oldTopicId, NewsletterTopic newsletterTopic, AdminUser user, Locale locale )
188     {
189         FreeHtmlTopic oldFreeHtmlTopic = FreeHtmlTopicHome.findByPrimaryKey( oldTopicId, getPlugin( ) );
190 
191         FreeHtmlTopicr/business/topic/FreeHtmlTopic.html#FreeHtmlTopic">FreeHtmlTopic freeHtmlTopic = new FreeHtmlTopic( );
192         freeHtmlTopic.setId( newsletterTopic.getId( ) );
193         if ( oldFreeHtmlTopic != null )
194         {
195             freeHtmlTopic.setHtmlContent( oldFreeHtmlTopic.getHtmlContent( ) );
196         }
197         else
198         {
199             freeHtmlTopic.setHtmlContent( StringUtils.EMPTY );
200         }
201         FreeHtmlTopicHome.insertFreeHtmlTopic( freeHtmlTopic, getPlugin( ) );
202     }
203 }