1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
56
57 public class FreeHtmlTopicService implements INewsletterTopicService
58 {
59
60
61
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
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
73 private static final String PARAMETER_CONTENT = "html_content";
74
75
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
82
83 @Override
84 public String getNewsletterTopicTypeCode( )
85 {
86 return NEWSLETTER_FREE_HTML_TOPIC_TYPE_CODE;
87 }
88
89
90
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
100
101
102
103 public boolean hasConfiguration( )
104 {
105 return true;
106 }
107
108
109
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
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
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
155
156 @Override
157 public void removeNewsletterTopic( int nNewsletterTopicId )
158 {
159 FreeHtmlTopicHome.removeFreeHtmlTopic( nNewsletterTopicId, getPlugin( ) );
160 }
161
162
163
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
174
175
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 }