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.business.portlet;
35
36 import fr.paris.lutece.plugins.newsletter.business.NewsLetter;
37 import fr.paris.lutece.plugins.newsletter.business.NewsLetterHome;
38 import fr.paris.lutece.plugins.newsletter.business.NewsLetterProperties;
39 import fr.paris.lutece.plugins.newsletter.business.NewsletterPropertiesHome;
40 import fr.paris.lutece.plugins.newsletter.util.NewsletterUtils;
41 import fr.paris.lutece.portal.business.portlet.Portlet;
42 import fr.paris.lutece.portal.service.captcha.CaptchaSecurityService;
43 import fr.paris.lutece.portal.service.i18n.I18nService;
44 import fr.paris.lutece.portal.service.plugin.Plugin;
45 import fr.paris.lutece.portal.service.plugin.PluginService;
46 import fr.paris.lutece.util.date.DateUtil;
47 import fr.paris.lutece.util.xml.XmlUtil;
48 import org.apache.commons.lang3.StringUtils;
49
50 import javax.servlet.http.HttpServletRequest;
51 import java.util.*;
52
53 import static fr.paris.lutece.portal.service.admin.AdminUserService.getLocale;
54
55
56
57
58 public class NewsLetterSubscriptionPortlet extends Portlet
59 {
60
61 private static final String TAG_CDATA_BEGIN = "<![CDATA[";
62 private static final String TAG_CDATA_END = "]]>";
63 private static final String TAG_NEWSLETTER_SUBSCRIPTION_LIST = "newsletter-subscription-list";
64 private static final String TAG_NEWSLETTER_SUBSCRIPTION = "newsletter-subscription";
65 private static final String TAG_NEWSLETTER_SUBSCRIPTION_ID = "newsletter-subscription-id";
66 private static final String TAG_NEWSLETTER_SUBSCRIPTION_EMAIL = "newsletter-subscription-email";
67 private static final String TAG_NEWSLETTER_SUBSCRIPTION_DATE = "newsletter-subscription-date";
68 private static final String TAG_NEWSLETTER_SUBSCRIPTION_DESC = "newsletter-subscription-subject";
69 private static final String TAG_NEWSLETTER_SUBSCRIPTION_BUTTON = "newsletter-subscription-button";
70 private static final String TAG_NEWSLETTER_EMAIL_ERROR = "newsletter-email-error";
71 private static final String TAG_NEWSLETTER_NO_CHOICE_ERROR = "subscription-nochoice-error";
72 private static final String TAG_NEWSLETTER_CAPTCHA = "newsletter-subscription-captcha";
73 private static final String TAG_NEWSLETTER_TOS = "newsletter-subscription-tos";
74 private static final String TAG_NEWSLETTER_TOS_CONTENT = "newsletter-subscription-tos-content";
75 private static final String PROPERTY_LABEL_MAIL = "newsletter.portlet.mail.label";
76 private static final String PROPERTY_LABEL_BUTTON = "newsletter.portlet.button.label";
77 private static final String PROPERTY_ERROR_INVALID_MAIL = "newsletter.message.error.newsletter.invalid.mail";
78 private static final String PROPERTY_ERROR_NO_CHOICE_ERROR = "newsletter.message.error.newsletter.nochoice.error";
79 private static final String PARAMETER_EMAIL_ERROR = "email-error";
80 private static final String PARAMETER_NO_NEWSLETTER_CHOSEN = "nochoice-error";
81 private static final String JCAPTCHA_PLUGIN = "jcaptcha";
82
83
84
85
86 private static final Comparator<NewsLetter> COMPARATOR_DATE_DESC = new Comparator<NewsLetter>( )
87 {
88
89
90
91
92
93
94
95
96
97
98 public int compare( NewsLetter../../../../../../../fr/paris/lutece/plugins/newsletter/business/NewsLetter.html#NewsLetter">NewsLetter obj1, NewsLetter obj2 )
99 {
100 return obj2.getDateLastSending( ).compareTo( obj1.getDateLastSending( ) );
101 }
102 };
103
104
105 private CaptchaSecurityService _captchaService;
106 private Plugin _plugin;
107
108
109
110
111
112
113
114 public void setPluginName( String strPluginName )
115 {
116 super.setPluginName( strPluginName );
117
118
119 this._plugin = PluginService.getPlugin( strPluginName );
120 }
121
122
123
124
125
126
127
128
129 public String getXmlDocument( HttpServletRequest request )
130 {
131 return XmlUtil.getXmlHeader( ) + getXml( request );
132 }
133
134
135
136
137
138
139
140
141 public String getXml( HttpServletRequest request )
142 {
143
144 String strMailError = null;
145 String strNoChoiceError = null;
146
147 if ( request != null )
148 {
149 strMailError = request.getParameter( PARAMETER_EMAIL_ERROR );
150 strNoChoiceError = request.getParameter( PARAMETER_NO_NEWSLETTER_CHOSEN );
151 }
152
153 if ( StringUtils.isBlank( strMailError ) )
154 {
155 strMailError = StringUtils.EMPTY;
156 }
157
158 if ( StringUtils.isBlank( strNoChoiceError ) )
159 {
160 strNoChoiceError = StringUtils.EMPTY;
161 }
162
163 StringBuffer strXml = new StringBuffer( );
164
165 XmlUtil.beginElement( strXml, TAG_NEWSLETTER_SUBSCRIPTION_LIST );
166
167
168
169
170
171
172
173
174 Set<Integer> sendingIds = NewsLetterSubscriptionPortletHome.findSelectedNewsletters( this.getId( ) );
175 Iterator<Integer> iterIds = sendingIds.iterator( );
176
177
178 List<NewsLetter> sendings = new ArrayList<NewsLetter>( );
179
180 while ( iterIds.hasNext( ) )
181 {
182 int sendingId = iterIds.next( ).intValue( );
183
184
185 NewsLetter sending = NewsLetterHome.findByPrimaryKey( sendingId, _plugin );
186
187 sendings.add( sending );
188 }
189
190
191 Collections.sort( sendings, COMPARATOR_DATE_DESC );
192
193
194 Iterator<NewsLetter> iterSendings = sendings.iterator( );
195
196 while ( iterSendings.hasNext( ) )
197 {
198 NewsLetter sending = iterSendings.next( );
199
200
201 XmlUtil.beginElement( strXml, TAG_NEWSLETTER_SUBSCRIPTION );
202 XmlUtil.addElement( strXml, TAG_NEWSLETTER_SUBSCRIPTION_ID, sending.getId( ) );
203 XmlUtil.addElement( strXml, TAG_NEWSLETTER_SUBSCRIPTION_DESC, sending.getName( ) );
204 XmlUtil.addElement( strXml, TAG_NEWSLETTER_SUBSCRIPTION_DATE,
205 DateUtil.getDateString( sending.getDateLastSending( ), NewsletterUtils.getLocale( request ) ) );
206 XmlUtil.endElement( strXml, TAG_NEWSLETTER_SUBSCRIPTION );
207 }
208
209 XmlUtil.endElement( strXml, TAG_NEWSLETTER_SUBSCRIPTION_LIST );
210
211 boolean bIsCaptchaEnabled = PluginService.isPluginEnable( JCAPTCHA_PLUGIN );
212 NewsLetterProperties properties = NewsletterPropertiesHome.find( _plugin );
213
214 if ( bIsCaptchaEnabled && properties.isCaptchaActive( ) )
215 {
216 _captchaService = new CaptchaSecurityService( );
217 XmlUtil.addElement( strXml, TAG_NEWSLETTER_CAPTCHA, TAG_CDATA_BEGIN + _captchaService.getHtmlCode( ) + TAG_CDATA_END );
218 }
219
220 if ( StringUtils.isNotEmpty( properties.getTOS( ) ) )
221 {
222 XmlUtil.addElement( strXml, TAG_NEWSLETTER_TOS, "true" );
223 XmlUtil.addElementHtml( strXml, TAG_NEWSLETTER_TOS_CONTENT, properties.getTOS( ) );
224 }
225
226 if ( request != null )
227 {
228 if ( StringUtils.isNotEmpty( strMailError ) )
229 {
230 XmlUtil.addElement( strXml, TAG_NEWSLETTER_EMAIL_ERROR, I18nService.getLocalizedString( PROPERTY_ERROR_INVALID_MAIL, getLocale( request ) ) );
231 }
232
233 if ( StringUtils.isNotEmpty( strNoChoiceError ) )
234 {
235 XmlUtil.addElement( strXml, TAG_NEWSLETTER_NO_CHOICE_ERROR,
236 I18nService.getLocalizedString( PROPERTY_ERROR_NO_CHOICE_ERROR, getLocale( request ) ) );
237 }
238
239 XmlUtil.addElement( strXml, TAG_NEWSLETTER_SUBSCRIPTION_BUTTON, I18nService.getLocalizedString( PROPERTY_LABEL_BUTTON, getLocale( request ) ) );
240 XmlUtil.addElement( strXml, TAG_NEWSLETTER_SUBSCRIPTION_EMAIL, I18nService.getLocalizedString( PROPERTY_LABEL_MAIL, getLocale( request ) ) );
241 }
242
243 String str = addPortletTags( strXml );
244
245 return str;
246 }
247
248
249
250
251 public void update( )
252 {
253 NewsLetterSubscriptionPortletHome.getInstance( ).update( this );
254 }
255
256
257
258
259 public void remove( )
260 {
261 NewsLetterSubscriptionPortletHome.getInstance( ).remove( this );
262 }
263 }