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  /*
35   * AnnounceUtils.java
36   *
37   * Created on 23 octobre 2009, 11:31
38   *
39   * To change this template, choose Tools | Template Manager
40   * and open the template in the editor.
41   */
42  package fr.paris.lutece.plugins.blog.utils;
43  
44  import java.text.Normalizer;
45  import java.util.HashMap;
46  import java.util.List;
47  import java.util.Locale;
48  
49  import javax.servlet.http.HttpServletRequest;
50  import javax.servlet.http.HttpServletResponse;
51  
52  import org.apache.commons.lang3.ArrayUtils;
53  import org.apache.commons.lang3.StringUtils;
54  import org.apache.commons.text.StringEscapeUtils;
55  
56  import fr.paris.lutece.plugins.blog.business.BlogSearchFilter;
57  import fr.paris.lutece.plugins.blog.service.BlogPlugin;
58  import fr.paris.lutece.portal.business.user.AdminUser;
59  import fr.paris.lutece.portal.service.plugin.Plugin;
60  import fr.paris.lutece.portal.service.plugin.PluginService;
61  import fr.paris.lutece.portal.service.template.AppTemplateService;
62  import fr.paris.lutece.util.date.DateUtil;
63  import fr.paris.lutece.util.html.HtmlTemplate;
64  
65  /**
66   * Utility class for announce plugin
67   */
68  public final class BlogUtils
69  {
70      // Constants
71      public static final String CONSTANT_WHERE = " WHERE ";
72      public static final String CONSTANT_AND = " AND ";
73      public static final int CONSTANT_ID_NULL = -1;
74      public static final String CONSTANT_TYPE_RESOURCE = "BLOG_DOCUMENT";
75      public static final String CONSTANT_PARAMETER_BLOG = "blog";
76      public static final String CONSTANT_PARAMETER_ID = "id";
77      public static final String CONSTANT_LINK_TARGET_NEW_WINDOW = "_blank";
78  
79      // Templates
80      public static final String TEMPLATE_INSERT_BLOG_LINK = "admin/plugins/blog/insertservice/insert_blog_link.html";
81  
82      /**
83       * Private default constructor
84       */
85      private BlogUtils( )
86      {
87          // Do nothing
88      }
89  
90      /**
91       * Builds a query with filters placed in parameters
92       *
93       * @param strSelect
94       *            the select of the query
95       * @param listStrFilter
96       *            the list of filter to add in the query
97       * @param strOrder
98       *            the order by of the query
99       * @return a query
100      */
101     public static String buildRequetteWithFilter( String strSelect, List<String> listStrFilter, String strOrder )
102     {
103         StringBuilder strBuffer = new StringBuilder( );
104         strBuffer.append( strSelect );
105 
106         int nCount = 0;
107 
108         for ( String strFilter : listStrFilter )
109         {
110             if ( ++nCount == 1 )
111             {
112                 strBuffer.append( CONSTANT_WHERE );
113             }
114 
115             strBuffer.append( strFilter );
116 
117             if ( nCount != listStrFilter.size( ) )
118             {
119                 strBuffer.append( CONSTANT_AND );
120             }
121         }
122 
123         if ( strOrder != null )
124         {
125             strBuffer.append( strOrder );
126         }
127 
128         return strBuffer.toString( );
129     }
130 
131     /**
132      * write the http header in the response
133      *
134      * @param request
135      *            the httpServletRequest
136      * @param response
137      *            the http response
138      * @param strFileName
139      *            the name of the file who must insert in the response
140      *
141      */
142     public static void addHeaderResponse( HttpServletRequest request, HttpServletResponse response, String strFileName )
143     {
144         response.setHeader( "Content-Disposition", "attachment ;filename=\"" + strFileName + "\"" );
145         response.setHeader( "Pragma", "public" );
146         response.setHeader( "Expires", "0" );
147         response.setHeader( "Cache-Control", "must-revalidate,post-check=0,pre-check=0" );
148     }
149 
150     /**
151      * Create a Blog search filter with specific values. The unnecessary parameters can be replaced by null values
152      *
153      * @param searchKeyword
154      *            Keywords to search for in the blog posts
155      * @param searchTagsArray
156      *            Specific tags to search for
157      * @param user
158      *            Filter by the user who created the blog
159      * @param isUnpublished
160      *            Search for blogs currently unpublished. Set to '0' to retrieve all blogs, '1' published, '2' unpublished
161      * @param strDateUpdateBlogAfter
162      *            Filter by blogs updated after a specific date
163      * @param strDateUpdateBlogBefore
164      *            Filter by blogs updated before a specific date
165      * @param userEditor
166      *            Filter by the last user who edited the blog
167      * @param locale
168      *            The Locale to use for the dates' format
169      * @return the BlogSearchFilter objects created with the given parameters
170      */
171     public static BlogSearchFilter buildBlogSearchFilter( String searchKeyword, String [ ] searchTagsArray, AdminUser user, int isUnpublished,
172             String strDateUpdateBlogAfter, String strDateUpdateBlogBefore, String userEditor, Locale locale )
173     {
174         BlogSearchFilteriness/BlogSearchFilter.html#BlogSearchFilter">BlogSearchFilter filter = new BlogSearchFilter( );
175 
176         if ( StringUtils.isNotBlank( searchKeyword ) )
177         {
178             filter.setKeywords( searchKeyword );
179         }
180         if ( !ArrayUtils.isEmpty( searchTagsArray ) )
181         {
182             filter.setTag( searchTagsArray );
183         }
184         if ( user != null )
185         {
186             filter.setUser( user.getAccessCode( ) );
187         }
188         if ( isUnpublished > 0 )
189         {
190             filter.setIsUnpulished( isUnpublished );
191         }
192         if ( StringUtils.isNotBlank( strDateUpdateBlogAfter ) )
193         {
194             filter.setUpdateDateAfter( DateUtil.formatDate( strDateUpdateBlogAfter, locale ) );
195         }
196         if ( StringUtils.isNotBlank( strDateUpdateBlogBefore ) )
197         {
198             filter.setUpdateDateBefor( DateUtil.formatDate( strDateUpdateBlogBefore, locale ) );
199         }
200         if ( StringUtils.isNotBlank( userEditor ) )
201         {
202             filter.setUserEditedBlogVersion( userEditor );
203         }
204         return filter;
205     }
206 
207     /**
208      * Create an HTML link to reach the specified URL
209      *
210      * @param customLinkUrl
211      *            URL of the link
212      * @param customLinkText
213      *            Text to display in the link
214      * @param customLinkTitle
215      *            Title of the link
216      * @param targetedWindow
217      *            Window where the link will be opened ("_self", "_blank", etc.). Opens in new window by default ("_blank")
218      * @return the HTML link to the specified url
219      */
220     public static String buildCustomBlogLink( String customLinkUrl, String customLinkText, String customLinkTitle, String targetedWindow )
221     {
222         HashMap<String, Object> model = new HashMap<>( );
223 
224         model.put( "url", customLinkUrl );
225         model.put( "target", targetedWindow );
226         model.put( "title", StringEscapeUtils.escapeHtml4( customLinkTitle ) );
227         model.put( "text", StringEscapeUtils.escapeHtml4( customLinkText ) );
228 
229         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_INSERT_BLOG_LINK, null, model );
230 
231         return template.getHtml( );
232     }
233 
234     /**
235      * Gets the plugin
236      *
237      * @return the plugin
238      */
239     public static Plugin getPlugin( )
240     {
241         return PluginService.getPlugin( BlogPlugin.PLUGIN_NAME );
242     }
243 
244     public static String removeAccents(String text) {
245         String normalized = Normalizer.normalize(text, Normalizer.Form.NFD);
246         return normalized.replaceAll("[\\p{InCombiningDiacriticalMarks}]+", "");
247     }
248 }