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
35
36
37
38
39
40
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
67
68 public final class BlogUtils
69 {
70
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
80 public static final String TEMPLATE_INSERT_BLOG_LINK = "admin/plugins/blog/insertservice/insert_blog_link.html";
81
82
83
84
85 private BlogUtils( )
86 {
87
88 }
89
90
91
92
93
94
95
96
97
98
99
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
133
134
135
136
137
138
139
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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
209
210
211
212
213
214
215
216
217
218
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
236
237
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 }