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.web.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.portlet.NewsLetterSubscriptionPortlet;
39 import fr.paris.lutece.plugins.newsletter.business.portlet.NewsLetterSubscriptionPortletHome;
40 import fr.paris.lutece.plugins.newsletter.util.NewsLetterConstants;
41 import fr.paris.lutece.portal.business.portlet.PortletHome;
42 import fr.paris.lutece.portal.service.message.AdminMessage;
43 import fr.paris.lutece.portal.service.message.AdminMessageService;
44 import fr.paris.lutece.portal.service.message.SiteMessage;
45 import fr.paris.lutece.portal.service.message.SiteMessageException;
46 import fr.paris.lutece.portal.service.message.SiteMessageService;
47 import fr.paris.lutece.portal.service.plugin.Plugin;
48 import fr.paris.lutece.portal.service.plugin.PluginService;
49 import fr.paris.lutece.portal.service.template.AppTemplateService;
50 import fr.paris.lutece.portal.service.workgroup.AdminWorkgroupService;
51 import fr.paris.lutece.portal.web.constants.Messages;
52 import fr.paris.lutece.portal.web.constants.Parameters;
53 import fr.paris.lutece.portal.web.portlet.PortletJspBean;
54 import fr.paris.lutece.util.html.HtmlTemplate;
55
56 import java.util.Collection;
57 import java.util.Enumeration;
58 import java.util.HashMap;
59 import java.util.HashSet;
60 import java.util.Set;
61
62 import javax.servlet.http.HttpServletRequest;
63
64 import org.apache.commons.lang3.StringUtils;
65
66
67
68
69 public class NewsLetterSubscriptionPortletJspBean extends PortletJspBean
70 {
71
72
73
74 private static final long serialVersionUID = -2321192141005152468L;
75
76
77
78
79 private static final String PROPERTIES_PREFIX = "portlet.newsletter_subscription";
80
81
82
83
84 private static final String PREFIX_CHECKBOX_NAME = "cbx_snd_";
85
86
87 private static final String BOOKMARK_PAGE_ID = "@page_id@";
88 private static final String BOOKMARK_PORTLET_ID = "@portlet_id@";
89
90
91 private static final String MARK_NEWSLETTER_LIST = "subscribed_newsletter_list";
92 private static final String MARK_SELECTED_NEWSLETTER_LIST = "selected_newsletter_list";
93 private static final String MARK_NEWSLETTER_SUBCRIPTION_LIST = "newsletter_subscription_list";
94
95
96
97
98
99
100
101
102 public String getCreate( HttpServletRequest request )
103 {
104 String strPageId = request.getParameter( PARAMETER_PAGE_ID );
105 String strPortletTypeId = request.getParameter( PARAMETER_PORTLET_TYPE_ID );
106 Plugin plugin = PluginService.getPlugin( NewsLetterConstants.PLUGIN_NAME );
107
108 Collection<NewsLetter> colNewsLetter = NewsLetterHome.findAll( plugin );
109 colNewsLetter = AdminWorkgroupService.getAuthorizedCollection( colNewsLetter, getUser( ) );
110 Set<Integer> selectedNewsletterList = new HashSet<Integer>( );
111 HashMap<String, Object> model = new HashMap<String, Object>( );
112 model.put( MARK_NEWSLETTER_LIST, colNewsLetter );
113 model.put( MARK_SELECTED_NEWSLETTER_LIST, selectedNewsletterList );
114 HtmlTemplate templateNewsletterList = AppTemplateService.getTemplate(NewsLetterConstants.TEMPLATE_NEWSLETTER_SUBSCRIPTION_LIST, this.getLocale(), model);
115 model.put( MARK_NEWSLETTER_SUBCRIPTION_LIST, templateNewsletterList.getHtml( ) );
116 HtmlTemplate templateCreate = getCreateTemplate( strPageId, strPortletTypeId , model );
117 return templateCreate.getHtml( );
118 }
119
120
121
122
123
124
125
126
127 public String doCreate( HttpServletRequest request )
128 {
129 NewsLetterSubscriptionPortlet/portlet/NewsLetterSubscriptionPortlet.html#NewsLetterSubscriptionPortlet">NewsLetterSubscriptionPortlet portlet = new NewsLetterSubscriptionPortlet( );
130
131
132 String strIdPage = request.getParameter( PARAMETER_PAGE_ID );
133 int nIdPage = Integer.parseInt( strIdPage );
134
135 String strStyleId = request.getParameter( Parameters.STYLE );
136
137 if ( StringUtils.isEmpty( strStyleId ) )
138 {
139 return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
140 }
141
142 setPortletCommonData( request, portlet );
143
144
145 String strName = portlet.getName( );
146
147 if ( StringUtils.isBlank( strName ) )
148 {
149 return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
150 }
151
152 portlet.setPageId( nIdPage );
153
154
155 NewsLetterSubscriptionPortletHome.getInstance( ).create( portlet );
156
157
158 modifySubscriptions( request, portlet );
159
160 return getPageUrl( nIdPage );
161 }
162
163
164
165
166
167
168
169
170 public String getModify( HttpServletRequest request )
171 {
172
173 String strPortletId = request.getParameter( PARAMETER_PORTLET_ID );
174 int nPortletId = Integer.parseInt( strPortletId );
175 NewsLetterSubscriptionPortletparis/lutece/plugins/newsletter/business/portlet/NewsLetterSubscriptionPortlet.html#NewsLetterSubscriptionPortlet">NewsLetterSubscriptionPortlet portlet = (NewsLetterSubscriptionPortlet) PortletHome.findByPrimaryKey( nPortletId );
176
177 String strIdPage = request.getParameter( PARAMETER_PAGE_ID );
178
179 HashMap<String, Object> model = new HashMap<String, Object>( );
180 model.put( BOOKMARK_PORTLET_ID, strPortletId );
181 model.put( BOOKMARK_PAGE_ID, strIdPage );
182
183 Plugin plugin = PluginService.getPlugin( portlet.getPluginName( ) );
184
185 Collection<NewsLetter> colNewsLetter = NewsLetterHome.findAll( plugin );
186 colNewsLetter = AdminWorkgroupService.getAuthorizedCollection( colNewsLetter, getUser( ) );
187
188 Set<Integer> selectedNewsletterList = NewsLetterSubscriptionPortletHome.findSelectedNewsletters( nPortletId );
189 model.put( MARK_NEWSLETTER_LIST, colNewsLetter );
190 model.put( MARK_SELECTED_NEWSLETTER_LIST, selectedNewsletterList );
191
192 HtmlTemplate template = getModifyTemplate( portlet, model );
193
194 return template.getHtml( );
195 }
196
197
198
199
200
201
202
203
204 public String doModify( HttpServletRequest request )
205 {
206
207 String strPortletId = request.getParameter( PARAMETER_PORTLET_ID );
208 int nPortletId = Integer.parseInt( strPortletId );
209 NewsLetterSubscriptionPortletparis/lutece/plugins/newsletter/business/portlet/NewsLetterSubscriptionPortlet.html#NewsLetterSubscriptionPortlet">NewsLetterSubscriptionPortlet portlet = (NewsLetterSubscriptionPortlet) PortletHome.findByPrimaryKey( nPortletId );
210
211
212 String strStyleId = request.getParameter( Parameters.STYLE );
213
214 if ( StringUtils.isEmpty( strStyleId ) )
215 {
216 return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
217 }
218
219 setPortletCommonData( request, portlet );
220
221
222 String strName = portlet.getName( );
223
224 if ( StringUtils.isBlank( strName ) )
225 {
226 return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
227 }
228
229
230 portlet.update( );
231
232
233 modifySubscriptions( request, portlet );
234
235
236 return getPageUrl( portlet.getPageId( ) );
237 }
238
239
240
241
242
243
244 public String getPropertiesPrefix( )
245 {
246 return PROPERTIES_PREFIX;
247 }
248
249
250
251
252
253
254
255
256
257 private static void modifySubscriptions( HttpServletRequest request, NewsLetterSubscriptionPortlet portlet )
258 {
259
260 Set<Integer> checkedSubscriptions = new HashSet<Integer>( );
261
262
263 @SuppressWarnings( "unchecked" )
264 Enumeration<String> enumParameterNames = request.getParameterNames( );
265
266 while ( enumParameterNames.hasMoreElements( ) )
267 {
268 String strParameterName = enumParameterNames.nextElement( );
269
270
271 if ( strParameterName.startsWith( PREFIX_CHECKBOX_NAME ) )
272 {
273
274 String strSubscriptionId = strParameterName.substring( PREFIX_CHECKBOX_NAME.length( ) );
275
276
277 checkedSubscriptions.add( new Integer( strSubscriptionId ) );
278 }
279 }
280
281
282
283 Set<Integer> previousSubscriptions = NewsLetterSubscriptionPortletHome.findSelectedNewsletters( portlet.getId( ) );
284
285
286 for ( Integer newSubscription : checkedSubscriptions )
287 {
288 if ( !previousSubscriptions.contains( newSubscription ) )
289 {
290 NewsLetterSubscriptionPortletHome.insertSubscription( portlet.getId( ), newSubscription.intValue( ) );
291 }
292 }
293
294
295 for ( Integer oldSubscription : previousSubscriptions )
296 {
297 if ( !checkedSubscriptions.contains( oldSubscription ) )
298 {
299 NewsLetterSubscriptionPortletHome.removeNewsletter( portlet.getId( ), oldSubscription.intValue( ) );
300 }
301 }
302 }
303
304
305
306
307
308
309
310
311
312
313 public static String doRegister( HttpServletRequest request ) throws SiteMessageException
314 {
315 SiteMessageService.setMessage( request, NewsLetterConstants.PROPERTY_SUBSCRIPTION_OK_ALERT_MESSAGE, null,
316 NewsLetterConstants.PROPERTY_SUBSCRIPTION_OK_TITLE_MESSAGE, null, null, SiteMessage.TYPE_CONFIRMATION );
317
318 return StringUtils.EMPTY;
319 }
320 }