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.modules.blog.web;
35
36 import fr.paris.lutece.plugins.blog.business.Blog;
37 import fr.paris.lutece.plugins.blog.business.BlogFilter;
38 import fr.paris.lutece.plugins.blog.service.BlogService;
39 import fr.paris.lutece.plugins.blog.service.PublishingService;
40 import fr.paris.lutece.plugins.newsletter.business.NewsLetterTemplateHome;
41 import fr.paris.lutece.plugins.newsletter.modules.blog.service.NewsletterBlogService;
42 import fr.paris.lutece.plugins.newsletter.modules.blog.service.NewsletterBlogTopicService;
43 import fr.paris.lutece.plugins.newsletter.modules.blog.util.NewsletterBlogUtils;
44 import fr.paris.lutece.plugins.newsletter.service.NewsletterPlugin;
45 import fr.paris.lutece.plugins.newsletter.service.NewsletterService;
46 import fr.paris.lutece.plugins.newsletter.util.NewsLetterConstants;
47 import fr.paris.lutece.plugins.newsletter.util.NewsletterUtils;
48 import fr.paris.lutece.portal.service.admin.AdminUserService;
49 import fr.paris.lutece.portal.service.i18n.I18nService;
50 import fr.paris.lutece.portal.service.message.AdminMessage;
51 import fr.paris.lutece.portal.service.message.AdminMessageService;
52 import fr.paris.lutece.portal.service.plugin.Plugin;
53 import fr.paris.lutece.portal.service.plugin.PluginService;
54 import fr.paris.lutece.portal.service.template.AppTemplateService;
55 import fr.paris.lutece.portal.service.util.AppPathService;
56 import fr.paris.lutece.portal.web.insert.InsertServiceJspBean;
57 import fr.paris.lutece.portal.web.insert.InsertServiceSelectionBean;
58 import fr.paris.lutece.util.ReferenceItem;
59 import fr.paris.lutece.util.ReferenceList;
60 import fr.paris.lutece.util.date.DateUtil;
61 import fr.paris.lutece.util.html.HtmlTemplate;
62
63 import java.sql.Timestamp;
64 import java.util.ArrayList;
65 import java.util.Collection;
66 import java.util.HashMap;
67 import java.util.List;
68 import java.util.Locale;
69 import java.util.Map;
70 import java.util.stream.Stream;
71
72 import javax.servlet.http.HttpServletRequest;
73
74 import org.apache.commons.lang3.StringEscapeUtils;
75 import org.apache.commons.lang3.StringUtils;
76 import org.apache.commons.lang3.ArrayUtils;
77
78
79
80
81 public class NewsletterDocumentServiceJspBean extends InsertServiceJspBean implements InsertServiceSelectionBean
82 {
83
84
85
86 public static final String RIGHT_NEWSLETTER_MANAGEMENT = "NEWSLETTER_MANAGEMENT";
87
88
89
90
91 private static final long serialVersionUID = -4095074358460689539L;
92
93
94 private static final String TEMPLATE_SELECT_DOCUMENTS = "admin/plugins/newsletter/modules/blog/select_blogs.html";
95 private static final String TEMPLATE_INSERT_DOCEMENTS = "admin/plugins/newsletter/modules/blog/insert_blogs.html";
96
97
98 private static final String BOOKMARK_START_PUBLISHED_DATE = "start_published_date";
99 private static final String MARK_DOCUMENT_LIST = "document_list";
100 private static final String MARK_COMBO_DOCUMENT_LIST = "documents_lists_list";
101 private static final String MARK_INPUT = "input";
102 private static final String MARK_TEMPLATES_LIST = "documents_templates_list";
103 private static final String MARK_DOCUMENTS_LIST = "documents_list";
104
105
106 private static final String PARAMETER_DOCUMENT_LIST_ID = "document_list_id";
107 private static final String PARAMETER_TEMPLATE_ID = "template_id";
108 private static final String PARAMETER_DOCUMENTS_LIST = "documents_list";
109 private static final String PARAMETER_PUBLISHED_DATE = "published_date";
110 private static final String PARAMETER_INPUT = "input";
111
112
113 private static final String LABEL_FRAGMENT_COMBO_ALL_DOCUMENT_LIST_ITEM = "module.newsletter.blog.documents.selection.lists.all";
114 private static final String MESSAGE_NO_DOCUMENT_TEMPLATE = "module.newsletter.blog.message.noDocumentTemplate";
115 private static final String MESSAGE_NO_DOCUMENT_CHOSEN = "module.newsletter.blog.message.noDocumentChosen";
116
117 private static final String CONSTANT_STRING_ZERO = "0";
118 private NewsletterService _newsletterService = NewsletterService.getService( );
119
120
121
122
123
124
125
126
127 @Override
128 public String getInsertServiceSelectorUI( HttpServletRequest request )
129 {
130 Plugin pluginNewsletter = PluginService.getPlugin( NewsletterPlugin.PLUGIN_NAME );
131
132 Locale locale = AdminUserService.getLocale( request );
133
134
135 String strDocumentListId = request.getParameter( PARAMETER_DOCUMENT_LIST_ID );
136 strDocumentListId = ( strDocumentListId != null ) ? strDocumentListId : CONSTANT_STRING_ZERO;
137
138 int nDocumentTagId = Integer.parseInt( strDocumentListId );
139
140
141 String strTemplateId = request.getParameter( PARAMETER_TEMPLATE_ID );
142 strTemplateId = ( strTemplateId != null ) ? strTemplateId : CONSTANT_STRING_ZERO;
143
144 String strPublishedDate = request.getParameter( PARAMETER_PUBLISHED_DATE );
145 strPublishedDate = ( strPublishedDate != null ) ? strPublishedDate : StringUtils.EMPTY;
146
147 Timestamp publishedDate = DateUtil.formatTimestamp( strPublishedDate, AdminUserService.getLocale( request ) );
148 Map<String, Object> model = new HashMap<String, Object>( );
149
150
151
152 ReferenceList listDocumentPortlets = NewsletterBlogService.getInstance( ).getPortletBlogList( );
153 ReferenceItem refItem = new ReferenceItem( );
154 refItem.setCode( CONSTANT_STRING_ZERO );
155 refItem.setName( I18nService.getLocalizedString( LABEL_FRAGMENT_COMBO_ALL_DOCUMENT_LIST_ITEM, locale ) );
156 listDocumentPortlets.add( 0, refItem );
157 model.put( MARK_COMBO_DOCUMENT_LIST, listDocumentPortlets );
158
159
160 model.put( BOOKMARK_START_PUBLISHED_DATE, strPublishedDate );
161
162
163 BlogFilter documentFilter = new BlogFilter( );
164 if ( nDocumentTagId != 0 )
165 {
166 documentFilter.setPortletId( nDocumentTagId );
167 }
168 Collection<Blog> list = PublishingService.getInstance( ).getPublishedBlogsSinceDate( publishedDate, publishedDate, documentFilter, locale );
169 model.put( MARK_DOCUMENT_LIST, list );
170
171 ReferenceList templateList = NewsLetterTemplateHome.getTemplatesListByType( NewsletterBlogTopicService.NEWSLETTER_DOCUMENT_TOPIC_TYPE,
172 pluginNewsletter );
173 model.put( MARK_TEMPLATES_LIST, templateList );
174
175
176 String strWebappUrl = AppPathService.getBaseUrl( request );
177 model.put( NewsLetterConstants.WEBAPP_PATH_FOR_LINKSERVICE, strWebappUrl );
178 model.put( MARK_INPUT, request.getParameter( PARAMETER_INPUT ) );
179 model.put( PARAMETER_DOCUMENT_LIST_ID, strDocumentListId );
180
181 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_SELECT_DOCUMENTS, locale, model );
182
183 return template.getHtml( );
184 }
185
186
187
188
189
190
191
192
193 public String doSearchDocuments( HttpServletRequest request )
194 {
195 return getInsertServiceSelectorUI( request );
196 }
197
198
199
200
201
202
203
204
205 public String doInsert( HttpServletRequest request )
206 {
207 Plugin pluginNewsletter = PluginService.getPlugin( NewsletterPlugin.PLUGIN_NAME );
208
209 String strBaseUrl = AppPathService.getBaseUrl( request );
210 String strInput = request.getParameter( PARAMETER_INPUT );
211 Map<String, Object> model = new HashMap<String, Object>( );
212 String strTemplateId = request.getParameter( PARAMETER_TEMPLATE_ID );
213 strTemplateId = ( strTemplateId != null ) ? strTemplateId : CONSTANT_STRING_ZERO;
214
215 if ( StringUtils.isEmpty( strTemplateId ) || !StringUtils.isNumeric( strTemplateId ) || StringUtils.equals( strTemplateId, CONSTANT_STRING_ZERO ) )
216 {
217 return AdminMessageService.getMessageUrl( request, MESSAGE_NO_DOCUMENT_TEMPLATE, AdminMessage.TYPE_STOP );
218 }
219
220 int nTemplateId = Integer.parseInt( strTemplateId );
221 String [ ] strDocumentsIdsList = request.getParameterValues( PARAMETER_DOCUMENTS_LIST );
222 int [ ] documentsIdList = Stream.of( strDocumentsIdsList ).mapToInt( Integer::parseInt ).toArray( );
223
224 if ( ( strDocumentsIdsList == null ) )
225 {
226 return AdminMessageService.getMessageUrl( request, MESSAGE_NO_DOCUMENT_CHOSEN, AdminMessage.TYPE_STOP );
227 }
228
229 Locale locale = AdminUserService.getLocale( request );
230
231
232 String strPathDocumentTemplate = NewsletterUtils.getHtmlTemplatePath( nTemplateId, pluginNewsletter );
233 String templateFileKey = NewsLetterTemplateHome.findByPrimaryKey( nTemplateId, pluginNewsletter ).getFileKey( );
234
235 BlogFilter documentFilter = new BlogFilter( );
236 documentFilter.setIds( ArrayUtils.toObject( documentsIdList ) );
237 List<Blog> blogList = BlogService.getInstance( ).findByFilter( documentFilter );
238
239 String strDocumentHtmlCode = StringUtils.EMPTY;
240 if ( templateFileKey != null && StringUtils.isNumeric( templateFileKey ) )
241 {
242 strDocumentHtmlCode = NewsletterBlogService.getInstance( ).fillTemplateWithDocumentInfos( templateFileKey, blogList, locale, strBaseUrl,
243 AdminUserService.getAdminUser( request ) );
244 }
245 else
246 {
247 strDocumentHtmlCode = NewsletterBlogService.getInstance( ).fillTemplateWithDocumentInfos( strPathDocumentTemplate, blogList, locale, strBaseUrl,
248 AdminUserService.getAdminUser( request ) );
249 }
250
251 model.put( MARK_DOCUMENTS_LIST, strDocumentHtmlCode );
252
253 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_INSERT_DOCEMENTS, locale, model );
254 template.substitute( NewsLetterConstants.WEBAPP_PATH_FOR_LINKSERVICE, strBaseUrl );
255
256 String strContent = template.getHtml( );
257
258
259
260
261
262
263
264
265 return insertUrl( request, strInput, StringEscapeUtils.escapeEcmaScript( strContent ) );
266 }
267 }