View Javadoc
1   /*
2    * Copyright (c) 2002-2021, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
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   * This class is responsible for the insertion of document lists in the newsletter.
80   */
81  public class NewsletterDocumentServiceJspBean extends InsertServiceJspBean implements InsertServiceSelectionBean
82  {
83      /**
84       * The newsletter right needed to manage
85       */
86      public static final String RIGHT_NEWSLETTER_MANAGEMENT = "NEWSLETTER_MANAGEMENT";
87  
88      /**
89       * Serial version UID
90       */
91      private static final long serialVersionUID = -4095074358460689539L;
92  
93      // templates
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      // bookmarks
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     // parameters
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     // property
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      * Inserts Html code by the insert service
122      * 
123      * @param request
124      *            The Http request
125      * @return The string representation of the category
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         // get the document list from request
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         // get template from request
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         // Criteria
151         // Combo of available document list portlets
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         // re-display the published date field
160         model.put( BOOKMARK_START_PUBLISHED_DATE, strPublishedDate );
161 
162         // Document list
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         // Replace portal path for editor and document display
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      * Search for a list of documents Actually does the same as getHtmlSelectorUI
188      * 
189      * @param request
190      *            the http request
191      * @return the found documents
192      */
193     public String doSearchDocuments( HttpServletRequest request )
194     {
195         return getInsertServiceSelectorUI( request );
196     }
197 
198     /**
199      * Insert the selected documents as a piece of html code into the html editor
200      * 
201      * @param request
202      *            the http request
203      * @return the html code to display (this code uses a javascript to close the selection window and insert the document list into the editor)
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         // retrieves the html template in order to use it to display the list of documents
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         // We check if we need to unsecure files of the document to include them as links in the content
259         /*
260          * if ( _newsletterService.useUnsecuredImages( ) ) { String strUnsecuredFolder = _newsletterService.getUnsecuredImagefolder( ); String
261          * strUnsecuredFolderPath = _newsletterService.getUnsecuredFolderPath( ); strContent = NewsletterBlogUtils.rewriteImgUrls( strContent,
262          * AppPathService.getBaseUrl( ), _newsletterService.getUnsecuredWebappUrl( ), strUnsecuredFolderPath, strUnsecuredFolder ); }
263          */
264 
265         return insertUrl( request, strInput, StringEscapeUtils.escapeEcmaScript( strContent ) );
266     }
267 }