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.SendingNewsLetter;
37 import fr.paris.lutece.plugins.newsletter.business.SendingNewsLetterHome;
38 import fr.paris.lutece.plugins.newsletter.business.portlet.NewsLetterArchivePortlet;
39 import fr.paris.lutece.plugins.newsletter.business.portlet.NewsLetterArchivePortletHome;
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.plugin.Plugin;
45 import fr.paris.lutece.portal.service.plugin.PluginService;
46 import fr.paris.lutece.portal.service.template.AppTemplateService;
47 import fr.paris.lutece.portal.web.constants.Messages;
48 import fr.paris.lutece.portal.web.constants.Parameters;
49 import fr.paris.lutece.portal.web.portlet.PortletJspBean;
50 import fr.paris.lutece.util.html.HtmlTemplate;
51
52 import java.util.ArrayList;
53 import java.util.Enumeration;
54 import java.util.HashMap;
55 import java.util.HashSet;
56 import java.util.List;
57 import java.util.Set;
58
59 import javax.servlet.http.HttpServletRequest;
60
61
62
63 public class NewsLetterArchivePortletJspBean extends PortletJspBean
64 {
65
66
67
68 private static final long serialVersionUID = -5219786237834856528L;
69
70
71 private static final String PROPERTIES_PREFIX = "portlet.newsletter_archive";
72
73
74 private static final String PREFIX_CHECKBOX_NAME = "cbx_snd_";
75
76
77 private static final String BOOKMARK_PAGE_ID = "@page_id@";
78 private static final String BOOKMARK_PORTLET_ID = "@portlet_id@";
79
80
81 private static final String MARK_SENDING_NEWSLETTER_LIST = "sending_newsletter_list";
82 private static final String MARK_SELECTED_SENDING_LIST = "selected_sendings_list";
83 private static final String MARK_NEWSLETTER_SUBCRIPTION_LIST = "newsletter_subscription_list";
84
85
86
87
88
89
90
91
92
93 public String getCreate( HttpServletRequest request )
94 {
95 String strPageId = request.getParameter( PARAMETER_PAGE_ID );
96 String strPortletTypeId = request.getParameter( PARAMETER_PORTLET_TYPE_ID );
97 Plugin plugin = PluginService.getPlugin( NewsLetterConstants.PLUGIN_NAME );
98 ArrayList<Integer> selectedSendings = new ArrayList<>();
99 List<SendingNewsLetter> sendingNewsletterList = SendingNewsLetterHome.findAllSendings( plugin );
100 HashMap<String, Object> model = new HashMap<String, Object>( );
101 model.put( MARK_SENDING_NEWSLETTER_LIST, sendingNewsletterList );
102 model.put( MARK_SELECTED_SENDING_LIST, selectedSendings );
103 HtmlTemplate templateNewsletterList = AppTemplateService.getTemplate( NewsLetterConstants.TEMPLATE_NEWSLETTER_ARCHIVE_LIST, this.getLocale( ),
104 model );
105 model.put( MARK_NEWSLETTER_SUBCRIPTION_LIST, templateNewsletterList.getHtml( ) );
106 HtmlTemplate templateCreate = getCreateTemplate( strPageId, strPortletTypeId , model );
107 return templateCreate.getHtml( );
108 }
109
110
111
112
113
114
115
116
117 public String doCreate( HttpServletRequest request )
118 {
119 NewsLetterArchivePortletiness/portlet/NewsLetterArchivePortlet.html#NewsLetterArchivePortlet">NewsLetterArchivePortlet portlet = new NewsLetterArchivePortlet( );
120
121
122 String strIdPage = request.getParameter( PARAMETER_PAGE_ID );
123 int nIdPage = Integer.parseInt( strIdPage );
124
125 String strStyleId = request.getParameter( Parameters.STYLE );
126
127 if ( ( strStyleId == null ) || strStyleId.trim( ).equals( "" ) )
128 {
129 return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
130 }
131
132 setPortletCommonData( request, portlet );
133
134
135 String strName = portlet.getName( );
136
137 if ( strName.trim( ).equals( "" ) )
138 {
139 return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
140 }
141
142 portlet.setPageId( nIdPage );
143
144
145 NewsLetterArchivePortletHome.getInstance( ).create( portlet );
146
147 modifySendings( request, portlet );
148
149 return getPageUrl( nIdPage );
150 }
151
152
153
154
155
156
157
158
159 public String getModify( HttpServletRequest request )
160 {
161
162 String strPortletId = request.getParameter( PARAMETER_PORTLET_ID );
163 int nPortletId = Integer.parseInt( strPortletId );
164 NewsLetterArchivePortlet./fr/paris/lutece/plugins/newsletter/business/portlet/NewsLetterArchivePortlet.html#NewsLetterArchivePortlet">NewsLetterArchivePortlet portlet = (NewsLetterArchivePortlet) PortletHome.findByPrimaryKey( nPortletId );
165
166 String strIdPage = request.getParameter( PARAMETER_PAGE_ID );
167
168
169 HashMap<String, Object> model = new HashMap<String, Object>( );
170 model.put( BOOKMARK_PORTLET_ID, strPortletId );
171 model.put( BOOKMARK_PAGE_ID, strIdPage );
172
173
174 Plugin plugin = PluginService.getPlugin( portlet.getPluginName( ) );
175
176 ArrayList<Integer> selectedSendings = NewsLetterArchivePortletHome.findSendingsInPortlet( nPortletId, plugin );
177 List<SendingNewsLetter> sendingNewsletterList = SendingNewsLetterHome.findAllSendings( plugin );
178 model.put( MARK_SENDING_NEWSLETTER_LIST, sendingNewsletterList );
179 model.put( MARK_SELECTED_SENDING_LIST, selectedSendings );
180
181
182 HtmlTemplate template = getModifyTemplate( portlet, model );
183
184 return template.getHtml( );
185 }
186
187
188
189
190
191
192
193
194 public String doModify( HttpServletRequest request )
195 {
196
197 String strPortletId = request.getParameter( PARAMETER_PORTLET_ID );
198 int nPortletId = Integer.parseInt( strPortletId );
199 NewsLetterArchivePortlet./fr/paris/lutece/plugins/newsletter/business/portlet/NewsLetterArchivePortlet.html#NewsLetterArchivePortlet">NewsLetterArchivePortlet portlet = (NewsLetterArchivePortlet) PortletHome.findByPrimaryKey( nPortletId );
200
201
202 String strStyleId = request.getParameter( Parameters.STYLE );
203
204 if ( ( strStyleId == null ) || strStyleId.trim( ).equals( "" ) )
205 {
206 return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
207 }
208
209 setPortletCommonData( request, portlet );
210
211
212 String strName = portlet.getName( );
213
214 if ( strName.trim( ).equals( "" ) )
215 {
216 return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
217 }
218
219
220 portlet.update( );
221
222
223 modifySendings( request, portlet );
224
225
226 return getPageUrl( portlet.getPageId( ) );
227 }
228
229
230
231
232
233
234 public String getPropertiesPrefix( )
235 {
236 return PROPERTIES_PREFIX;
237 }
238
239
240
241
242
243
244
245
246
247 private static void modifySendings( HttpServletRequest request, NewsLetterArchivePortlet portlet )
248 {
249
250 Set<Integer> checkedSendings = new HashSet<Integer>( );
251
252
253 @SuppressWarnings( "unchecked" )
254 Enumeration<String> enumParameterNames = request.getParameterNames( );
255
256 while ( enumParameterNames.hasMoreElements( ) )
257 {
258 String strParameterName = enumParameterNames.nextElement( );
259
260
261 if ( strParameterName.startsWith( PREFIX_CHECKBOX_NAME ) )
262 {
263
264 String strSendingId = strParameterName.substring( PREFIX_CHECKBOX_NAME.length( ) );
265
266
267 checkedSendings.add( new Integer( strSendingId ) );
268 }
269 }
270
271 ArrayList<Integer> previousSendings = NewsLetterArchivePortletHome.findSendingsInPortlet( portlet.getId( ),
272 PluginService.getPlugin( portlet.getPluginName( ) ) );
273
274
275 for ( Integer newSending : checkedSendings )
276 {
277 if ( !previousSendings.contains( newSending ) )
278 {
279 NewsLetterArchivePortletHome.insertSending( portlet.getId( ), newSending.intValue( ), PluginService.getPlugin( portlet.getPluginName( ) ) );
280 }
281 }
282
283
284 for ( Integer oldSending : previousSendings )
285 {
286 if ( !checkedSendings.contains( oldSending ) )
287 {
288 NewsLetterArchivePortletHome.removeSending( portlet.getId( ), oldSending.intValue( ), PluginService.getPlugin( portlet.getPluginName( ) ) );
289 }
290 }
291 }
292 }