View Javadoc
1   /*
2    * Copyright (c) 2002-2022, 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.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   * class DirectoryUtils
57   *
58   */
59  public final class WorkflowUtils
60  {
61      // Constants
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      // ACTIONS
69      public static final String CONSTANT_ACTION_MODIFY_WORKFLOW = "modify_workflow";
70  
71      // MESSAGES
72      public static final String MESSAGE_ERROR_INVALID_SECURITY_TOKEN = "workflow.message.error.invalidSecurityToken";
73  
74      // property
75      private static final String REGEX_ID = "^[\\d]+$";
76  
77      /**
78       * DirectoryUtils
79       *
80       */
81      private WorkflowUtils( )
82      {
83      }
84  
85      /**
86       * return current Timestamp
87       *
88       * @return return current Timestamp
89       */
90      public static Timestamp getCurrentTimestamp( )
91      {
92          return new Timestamp( Calendar.getInstance( ).getTimeInMillis( ) );
93      }
94  
95      /**
96       * write the http header in the response
97       *
98       * @param response
99       *            the http response
100      * @param strFileName
101      *            the name of the file who must insert in the response
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      * convert a string to int
114      *
115      * @param strParameter
116      *            the string parameter to convert
117      * @return the conversion
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      * convert a string array to list
140      *
141      * @param strParameter
142      *            the string array parameter to convert
143      * @return the conversion
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      * Returns a copy of the string , with leading and trailing whitespace omitted.
159      *
160      * @param strParameter
161      *            the string parameter to convert
162      * @return null if the strParameter is null other return with leading and trailing whitespace omitted.
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      * Builds a query with filters placed in parameters
176      * 
177      * @param strSelect
178      *            the select of the query
179      * @param listStrFilter
180      *            the list of filter to add in the query
181      * @param strOrder
182      *            the order by of the query
183      * @return a query
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      * return a referenceList
217      * 
218      * @param listReferenceItem
219      *            a list of referenc Item
220      * @param bWitdthEmptyChoice
221      *            true if a empty item must be insert in the reference list
222      * @param locale
223      *            the locale
224      * @return referencelist
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      * return a referenceList
233      * 
234      * @param listReferenceItem
235      *            a list of referenc Item
236      * @param bWitdthEmptyChoice
237      *            true if a empty item must be insert in the reference list
238      * @param strLabelEmptyChoice
239      *            the empty choice label
240      * @return referencelist
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      * Get the workflow plugin
261      * 
262      * @return the workflow plugin
263      */
264     public static Plugin getPlugin( )
265     {
266         return PluginService.getPlugin( WorkflowPlugin.PLUGIN_NAME );
267     }
268 
269     /**
270      * Convert an array of Strings into an array of Integers
271      * 
272      * @param listToConvert
273      *            the list to convert
274      * @return an array of Integers
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 }