View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de 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.htmldocs.web;
35  
36  import fr.paris.lutece.plugins.htmldocs.business.HtmlDoc;
37  import fr.paris.lutece.plugins.htmldocs.business.HtmlDocFilter;
38  import fr.paris.lutece.plugins.htmldocs.service.HtmlDocService;
39  import fr.paris.lutece.plugins.htmldocs.service.PublishingService;
40  import fr.paris.lutece.plugins.newsletter.business.NewsLetterTemplateHome;
41  import fr.paris.lutece.plugins.newsletter.modules.htmldocs.service.NewsletterHtmlDocService;
42  import fr.paris.lutece.plugins.newsletter.modules.htmldocs.service.NewsletterHtmlDocTopicService;
43  import fr.paris.lutece.plugins.newsletter.modules.htmldocs.util.NewsletterHtmlDocUtils;
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.Locale;
68  import java.util.Map;
69  
70  import javax.servlet.http.HttpServletRequest;
71  
72  import org.apache.commons.lang.StringEscapeUtils;
73  import org.apache.commons.lang.StringUtils;
74  
75  /**
76   * This class is responsible for the insertion of document lists in the newsletter.
77   */
78  public class NewsletterDocumentServiceJspBean extends InsertServiceJspBean implements InsertServiceSelectionBean
79  {
80      /**
81       * The newsletter right needed to manage
82       */
83      public static final String RIGHT_NEWSLETTER_MANAGEMENT = "NEWSLETTER_MANAGEMENT";
84  
85      /**
86       * Serial version UID
87       */
88      private static final long serialVersionUID = -4095074358460689539L;
89  
90      // templates
91      private static final String TEMPLATE_SELECT_DOCUMENTS = "admin/plugins/newsletter/modules/htmldocs/select_htmldocs.html";
92      private static final String TEMPLATE_INSERT_DOCEMENTS = "admin/plugins/newsletter/modules/htmldocs/insert_htmldocs.html";
93  
94      // bookmarks
95      private static final String BOOKMARK_START_PUBLISHED_DATE = "start_published_date";
96      private static final String MARK_DOCUMENT_LIST = "document_list";
97      private static final String MARK_COMBO_DOCUMENT_LIST = "documents_lists_list";
98      private static final String MARK_INPUT = "input";
99      private static final String MARK_TEMPLATES_LIST = "documents_templates_list";
100     private static final String MARK_DOCUMENTS_LIST = "documents_list";
101 
102     // parameters
103     private static final String PARAMETER_DOCUMENT_LIST_ID = "document_list_id";
104     private static final String PARAMETER_TEMPLATE_ID = "template_id";
105     private static final String PARAMETER_DOCUMENTS_LIST = "documents_list";
106     private static final String PARAMETER_PUBLISHED_DATE = "published_date";
107     private static final String PARAMETER_INPUT = "input";
108 
109     // property
110     private static final String LABEL_FRAGMENT_COMBO_ALL_DOCUMENT_LIST_ITEM = "module.newsletter.htmldocs.documents.selection.lists.all";
111     private static final String MESSAGE_NO_DOCUMENT_TEMPLATE = "module.newsletter.htmldocs.message.noDocumentTemplate";
112     private static final String MESSAGE_NO_DOCUMENT_CHOSEN = "module.newsletter.htmldocs.message.noDocumentChosen";
113 
114     private static final String CONSTANT_STRING_ZERO = "0";
115     private NewsletterService _newsletterService = NewsletterService.getService( );
116 
117     /**
118      * Inserts Html code by the insert service
119      * 
120      * @param request
121      *            The Http request
122      * @return The string representation of the category
123      */
124     @Override
125     public String getInsertServiceSelectorUI( HttpServletRequest request )
126     {
127         Plugin pluginNewsletter = PluginService.getPlugin( NewsletterPlugin.PLUGIN_NAME );
128 
129         Locale locale = AdminUserService.getLocale( request );
130 
131         // get the document list from request
132         String strDocumentListId = request.getParameter( PARAMETER_DOCUMENT_LIST_ID );
133         strDocumentListId = ( strDocumentListId != null ) ? strDocumentListId : CONSTANT_STRING_ZERO;
134 
135         int nDocumentTagId = Integer.parseInt( strDocumentListId );
136 
137         // get template from request
138         String strTemplateId = request.getParameter( PARAMETER_TEMPLATE_ID );
139         strTemplateId = ( strTemplateId != null ) ? strTemplateId : CONSTANT_STRING_ZERO;
140 
141         String strPublishedDate = request.getParameter( PARAMETER_PUBLISHED_DATE );
142         strPublishedDate = ( strPublishedDate != null ) ? strPublishedDate : StringUtils.EMPTY;
143 
144         Timestamp publishedDate = DateUtil.formatTimestamp( strPublishedDate, AdminUserService.getLocale( request ) );
145         Map<String, Object> model = new HashMap<String, Object>( );
146 
147         // Criteria
148         // Combo of available document list portlets
149         ReferenceList listDocumentPortlets = NewsletterHtmlDocService.getInstance().getPortletHtmlDocList();
150         ReferenceItem refItem = new ReferenceItem( );
151         refItem.setCode( CONSTANT_STRING_ZERO );
152         refItem.setName( I18nService.getLocalizedString( LABEL_FRAGMENT_COMBO_ALL_DOCUMENT_LIST_ITEM, locale ) );
153         listDocumentPortlets.add( 0, refItem );
154         model.put( MARK_COMBO_DOCUMENT_LIST, listDocumentPortlets );
155 
156         // re-display the published date field
157         model.put( BOOKMARK_START_PUBLISHED_DATE, strPublishedDate );
158 
159         // Document list
160         HtmlDocFilter documentFilter = new HtmlDocFilter();
161         int tableauEntier[] = {nDocumentTagId};
162         documentFilter.setTagsId(tableauEntier);
163         Collection<HtmlDoc> list = PublishingService.getInstance().getPublishedDocumentsSinceDate(publishedDate, publishedDate, documentFilter, locale);
164         model.put( MARK_DOCUMENT_LIST, list );
165 
166         ReferenceList templateList = NewsLetterTemplateHome.getTemplatesListByType( NewsletterHtmlDocTopicService.NEWSLETTER_DOCUMENT_TOPIC_TYPE,
167                 pluginNewsletter );
168         model.put( MARK_TEMPLATES_LIST, templateList );
169 
170         // Replace portal path for editor and document display
171         String strWebappUrl = AppPathService.getBaseUrl( request );
172         model.put( NewsLetterConstants.WEBAPP_PATH_FOR_LINKSERVICE, strWebappUrl );
173         model.put( MARK_INPUT, request.getParameter( PARAMETER_INPUT ) );
174         model.put( PARAMETER_DOCUMENT_LIST_ID, strDocumentListId );
175 
176         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_SELECT_DOCUMENTS, locale, model );
177 
178         return template.getHtml( );
179     }
180 
181     /**
182      * Search for a list of documents Actually does the same as getHtmlSelectorUI
183      * 
184      * @param request
185      *            the http request
186      * @return the found documents
187      */
188     public String doSearchDocuments( HttpServletRequest request )
189     {
190         return getInsertServiceSelectorUI( request );
191     }
192 
193     /**
194      * Insert the selected documents as a piece of html code into the html editor
195      * 
196      * @param request
197      *            the http request
198      * @return the html code to display (this code uses a javascript to close the selection window and insert the document list into the editor)
199      */
200     public String doInsert( HttpServletRequest request )
201     {
202         Plugin pluginNewsletter = PluginService.getPlugin( NewsletterPlugin.PLUGIN_NAME );
203 
204         String strBaseUrl = AppPathService.getBaseUrl( request );
205         String strInput = request.getParameter( PARAMETER_INPUT );
206         Map<String, Object> model = new HashMap<String, Object>( );
207         String strTemplateId = request.getParameter( PARAMETER_TEMPLATE_ID );
208         strTemplateId = ( strTemplateId != null ) ? strTemplateId : CONSTANT_STRING_ZERO;
209 
210         if ( StringUtils.isEmpty( strTemplateId ) || !StringUtils.isNumeric( strTemplateId ) || StringUtils.equals( strTemplateId, CONSTANT_STRING_ZERO ) )
211         {
212             return AdminMessageService.getMessageUrl( request, MESSAGE_NO_DOCUMENT_TEMPLATE, AdminMessage.TYPE_STOP );
213         }
214 
215         int nTemplateId = Integer.parseInt( strTemplateId );
216         String [ ] strDocumentsIdsList = request.getParameterValues( PARAMETER_DOCUMENTS_LIST );
217 
218         if ( ( strDocumentsIdsList == null ) )
219         {
220             return AdminMessageService.getMessageUrl( request, MESSAGE_NO_DOCUMENT_CHOSEN, AdminMessage.TYPE_STOP );
221         }
222 
223         Locale locale = AdminUserService.getLocale( request );
224         Collection<String> documentsList = new ArrayList<String>( );
225 
226         // retrieves the html template in order to use it to display the list of documents
227         String strPathDocumentTemplate = NewsletterUtils.getHtmlTemplatePath( nTemplateId, pluginNewsletter );
228 
229         for ( int i = 0; i < strDocumentsIdsList.length; i++ )
230         {
231             int nDocumentId = Integer.parseInt( strDocumentsIdsList [i] );
232             HtmlDoc document = HtmlDocService.getInstance( ).findByPrimaryKeyWithoutBinaries( nDocumentId );
233             String strDocumentHtmlCode = NewsletterHtmlDocService.getInstance( ).fillTemplateWithDocumentInfos( strPathDocumentTemplate, document, locale,
234                     strBaseUrl, AdminUserService.getAdminUser( request ) );
235             documentsList.add( strDocumentHtmlCode );
236         }
237 
238         model.put( MARK_DOCUMENTS_LIST, documentsList );
239 
240         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_INSERT_DOCEMENTS, locale, model );
241         template.substitute( NewsLetterConstants.WEBAPP_PATH_FOR_LINKSERVICE, strBaseUrl );
242 
243         String strContent = template.getHtml( );
244 
245         // We check if we need to unsecure files of the document to include them as links in the content
246         if ( _newsletterService.useUnsecuredImages( ) )
247         {
248             String strUnsecuredFolder = _newsletterService.getUnsecuredImagefolder( );
249             String strUnsecuredFolderPath = _newsletterService.getUnsecuredFolderPath( );
250             strContent = NewsletterHtmlDocUtils.rewriteImgUrls( strContent, AppPathService.getBaseUrl( ), _newsletterService.getUnsecuredWebappUrl( ),
251                     strUnsecuredFolderPath, strUnsecuredFolder );
252         }
253 
254         return insertUrl( request, strInput, StringEscapeUtils.escapeJavaScript( strContent ) );
255     }
256 }