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.workflow.utils;
35
36 import java.sql.Timestamp;
37 import java.util.ArrayList;
38 import java.util.Arrays;
39 import java.util.Calendar;
40 import java.util.Collection;
41 import java.util.List;
42 import java.util.Locale;
43
44 import javax.servlet.http.HttpServletResponse;
45
46 import fr.paris.lutece.plugins.workflow.service.WorkflowPlugin;
47 import fr.paris.lutece.plugins.workflowcore.business.IReferenceItem;
48 import fr.paris.lutece.portal.service.i18n.I18nService;
49 import fr.paris.lutece.portal.service.plugin.Plugin;
50 import fr.paris.lutece.portal.service.plugin.PluginService;
51 import fr.paris.lutece.portal.service.util.AppLogService;
52 import fr.paris.lutece.util.ReferenceList;
53
54
55
56
57
58
59 public final class WorkflowUtils
60 {
61
62 public static final String CONSTANT_WHERE = " WHERE ";
63 public static final String CONSTANT_AND = " AND ";
64 public static final int CONSTANT_ID_NULL = -1;
65 public static final String EMPTY_STRING = "";
66 public static final String PROPERTY_SELECT_EMPTY_CHOICE = "workflow.select_empty_choice";
67
68
69 public static final String CONSTANT_ACTION_MODIFY_WORKFLOW = "modify_workflow";
70
71
72 public static final String MESSAGE_ERROR_INVALID_SECURITY_TOKEN = "workflow.message.error.invalidSecurityToken";
73
74
75 private static final String REGEX_ID = "^[\\d]+$";
76
77
78
79
80
81 private WorkflowUtils( )
82 {
83 }
84
85
86
87
88
89
90 public static Timestamp getCurrentTimestamp( )
91 {
92 return new Timestamp( Calendar.getInstance( ).getTimeInMillis( ) );
93 }
94
95
96
97
98
99
100
101
102
103
104 public static void addHeaderResponse( HttpServletResponse response, String strFileName )
105 {
106 response.setHeader( "Content-Disposition", "attachment ;filename=\"" + strFileName + "\"" );
107 response.setHeader( "Pragma", "public" );
108 response.setHeader( "Expires", "0" );
109 response.setHeader( "Cache-Control", "must-revalidate,post-check=0,pre-check=0" );
110 }
111
112
113
114
115
116
117
118
119 public static int convertStringToInt( String strParameter )
120 {
121 int nIdParameter = -1;
122
123 try
124 {
125 if ( ( strParameter != null ) && strParameter.matches( REGEX_ID ) )
126 {
127 nIdParameter = Integer.parseInt( strParameter );
128 }
129 }
130 catch( NumberFormatException ne )
131 {
132 AppLogService.error( ne );
133 }
134
135 return nIdParameter;
136 }
137
138
139
140
141
142
143
144
145 public static List<String> convertStringArrayToList( String[] strParameter )
146 {
147 List<String> lstStringParameter = new ArrayList<>( );
148
149 if ( ( strParameter != null ) )
150 {
151 lstStringParameter = Arrays.asList( strParameter );
152 }
153
154 return lstStringParameter;
155 }
156
157
158
159
160
161
162
163
164 public static String trim( String strParameter )
165 {
166 if ( strParameter != null )
167 {
168 return strParameter.trim( );
169 }
170
171 return strParameter;
172 }
173
174
175
176
177
178
179
180
181
182
183
184
185 public static String buildRequestWithFilter( String strSelect, List<String> listStrFilter, String strOrder )
186 {
187 StringBuilder strBuilder = new StringBuilder( );
188 strBuilder.append( strSelect );
189
190 int nCount = 0;
191
192 for ( String strFilter : listStrFilter )
193 {
194 if ( ++nCount == 1 )
195 {
196 strBuilder.append( CONSTANT_WHERE );
197 }
198
199 strBuilder.append( strFilter );
200
201 if ( nCount != listStrFilter.size( ) )
202 {
203 strBuilder.append( CONSTANT_AND );
204 }
205 }
206
207 if ( strOrder != null )
208 {
209 strBuilder.append( strOrder );
210 }
211
212 return strBuilder.toString( );
213 }
214
215
216
217
218
219
220
221
222
223
224
225
226 public static ReferenceList getRefList( Collection listReferenceItem, boolean bWitdthEmptyChoice, Locale locale )
227 {
228 return getRefList( listReferenceItem, bWitdthEmptyChoice, I18nService.getLocalizedString( PROPERTY_SELECT_EMPTY_CHOICE, locale ) );
229 }
230
231
232
233
234
235
236
237
238
239
240
241
242 public static ReferenceList getRefList( Collection listReferenceItem, boolean bWitdthEmptyChoice, String strLabelEmptyChoice )
243 {
244 ReferenceList refList = new ReferenceList( );
245
246 if ( bWitdthEmptyChoice )
247 {
248 refList.addItem( WorkflowUtils.CONSTANT_ID_NULL, strLabelEmptyChoice );
249 }
250
251 for ( Object item : listReferenceItem )
252 {
253 refList.addItem( ( (IReferenceItem) item ).getId( ), ( (IReferenceItem) item ).getName( ) );
254 }
255
256 return refList;
257 }
258
259
260
261
262
263
264 public static Plugin getPlugin( )
265 {
266 return PluginService.getPlugin( WorkflowPlugin.PLUGIN_NAME );
267 }
268
269
270
271
272
273
274
275
276 public static Integer [ ] convertStringToInt( String [ ] listToConvert )
277 {
278 if ( ( listToConvert != null ) && ( listToConvert.length > 0 ) )
279 {
280 int nIndex = 0;
281 Integer [ ] listConverted = new Integer [ listToConvert.length];
282
283 for ( String strToConvert : listToConvert )
284 {
285 listConverted [nIndex++] = convertStringToInt( strToConvert );
286 }
287
288 return listConverted;
289 }
290
291 return new Integer [ 0];
292 }
293 }