001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import java.util.Properties;
018    
019    import javax.portlet.PortletRequest;
020    
021    import javax.servlet.http.HttpServletRequest;
022    
023    /**
024     * @author Brian Wing Shun Chan
025     */
026    public class PropertiesParamUtil {
027    
028            public static boolean getBoolean(
029                    Properties properties, HttpServletRequest request, String param) {
030    
031                    return getBoolean(
032                            properties, request, param, GetterUtil.DEFAULT_BOOLEAN);
033            }
034    
035            public static boolean getBoolean(
036                    Properties properties, HttpServletRequest request, String param,
037                    boolean defaultValue) {
038    
039                    String propertiesValue = properties.getProperty(param, null);
040    
041                    boolean getterUtilValue = GetterUtil.getBoolean(
042                            propertiesValue, defaultValue);
043    
044                    return ParamUtil.get(request, param, getterUtilValue);
045            }
046    
047            public static boolean getBoolean(
048                    Properties properties, PortletRequest portletRequest, String param) {
049    
050                    return getBoolean(
051                            properties, portletRequest, param, GetterUtil.DEFAULT_BOOLEAN);
052            }
053    
054            public static boolean getBoolean(
055                    Properties properties, PortletRequest portletRequest, String param,
056                    boolean defaultValue) {
057    
058                    String propertiesValue = properties.getProperty(param, null);
059    
060                    boolean getterUtilValue = GetterUtil.getBoolean(
061                            propertiesValue, defaultValue);
062    
063                    return ParamUtil.get(portletRequest, param, getterUtilValue);
064            }
065    
066            public static boolean getBoolean(
067                    UnicodeProperties properties, HttpServletRequest request,
068                    String param) {
069    
070                    return getBoolean(
071                            properties, request, param, GetterUtil.DEFAULT_BOOLEAN);
072            }
073    
074            public static boolean getBoolean(
075                    UnicodeProperties properties, HttpServletRequest request, String param,
076                    boolean defaultValue) {
077    
078                    String propertiesValue = properties.getProperty(param, null);
079    
080                    boolean getterUtilValue = GetterUtil.getBoolean(
081                            propertiesValue, defaultValue);
082    
083                    return ParamUtil.get(request, param, getterUtilValue);
084            }
085    
086            public static boolean getBoolean(
087                    UnicodeProperties properties, PortletRequest portletRequest,
088                    String param) {
089    
090                    return getBoolean(
091                            properties, portletRequest, param, GetterUtil.DEFAULT_BOOLEAN);
092            }
093    
094            public static boolean getBoolean(
095                    UnicodeProperties properties, PortletRequest portletRequest,
096                    String param, boolean defaultValue) {
097    
098                    String propertiesValue = properties.getProperty(param, null);
099    
100                    boolean getterUtilValue = GetterUtil.getBoolean(
101                            propertiesValue, defaultValue);
102    
103                    return ParamUtil.get(portletRequest, param, getterUtilValue);
104            }
105    
106            public static double getDouble(
107                    Properties properties, HttpServletRequest request, String param) {
108    
109                    return getDouble(properties, request, param, GetterUtil.DEFAULT_DOUBLE);
110            }
111    
112            public static double getDouble(
113                    Properties properties, HttpServletRequest request, String param,
114                    double defaultValue) {
115    
116                    String propertiesValue = properties.getProperty(param, null);
117    
118                    double getterUtilValue = GetterUtil.getDouble(
119                            propertiesValue, defaultValue);
120    
121                    return ParamUtil.get(request, param, getterUtilValue);
122            }
123    
124            public static double getDouble(
125                    Properties properties, PortletRequest portletRequest, String param) {
126    
127                    return getDouble(
128                            properties, portletRequest, param, GetterUtil.DEFAULT_DOUBLE);
129            }
130    
131            public static double getDouble(
132                    Properties properties, PortletRequest portletRequest, String param,
133                    double defaultValue) {
134    
135                    String propertiesValue = properties.getProperty(param, null);
136    
137                    double getterUtilValue = GetterUtil.getDouble(
138                            propertiesValue, defaultValue);
139    
140                    return ParamUtil.get(portletRequest, param, getterUtilValue);
141            }
142    
143            public static double getDouble(
144                    UnicodeProperties properties, HttpServletRequest request,
145                    String param) {
146    
147                    return getDouble(properties, request, param, GetterUtil.DEFAULT_DOUBLE);
148            }
149    
150            public static double getDouble(
151                    UnicodeProperties properties, HttpServletRequest request, String param,
152                    double defaultValue) {
153    
154                    String propertiesValue = properties.getProperty(param, null);
155    
156                    double getterUtilValue = GetterUtil.getDouble(
157                            propertiesValue, defaultValue);
158    
159                    return ParamUtil.get(request, param, getterUtilValue);
160            }
161    
162            public static double getDouble(
163                    UnicodeProperties properties, PortletRequest portletRequest,
164                    String param) {
165    
166                    return getDouble(
167                            properties, portletRequest, param, GetterUtil.DEFAULT_DOUBLE);
168            }
169    
170            public static double getDouble(
171                    UnicodeProperties properties, PortletRequest portletRequest,
172                    String param, double defaultValue) {
173    
174                    String propertiesValue = properties.getProperty(param, null);
175    
176                    double getterUtilValue = GetterUtil.getDouble(
177                            propertiesValue, defaultValue);
178    
179                    return ParamUtil.get(portletRequest, param, getterUtilValue);
180            }
181    
182            public static int getInteger(
183                    Properties properties, HttpServletRequest request, String param) {
184    
185                    return getInteger(
186                            properties, request, param, GetterUtil.DEFAULT_INTEGER);
187            }
188    
189            public static int getInteger(
190                    Properties properties, HttpServletRequest request, String param,
191                    int defaultValue) {
192    
193                    String propertiesValue = properties.getProperty(param, null);
194    
195                    int getterUtilValue = GetterUtil.getInteger(
196                            propertiesValue, defaultValue);
197    
198                    return ParamUtil.get(request, param, getterUtilValue);
199            }
200    
201            public static int getInteger(
202                    Properties properties, PortletRequest portletRequest, String param) {
203    
204                    return getInteger(
205                            properties, portletRequest, param, GetterUtil.DEFAULT_INTEGER);
206            }
207    
208            public static int getInteger(
209                    Properties properties, PortletRequest portletRequest, String param,
210                    int defaultValue) {
211    
212                    String propertiesValue = properties.getProperty(param, null);
213    
214                    int getterUtilValue = GetterUtil.getInteger(
215                            propertiesValue, defaultValue);
216    
217                    return ParamUtil.get(portletRequest, param, getterUtilValue);
218            }
219    
220            public static int getInteger(
221                    UnicodeProperties properties, HttpServletRequest request,
222                    String param) {
223    
224                    return getInteger(
225                            properties, request, param, GetterUtil.DEFAULT_INTEGER);
226            }
227    
228            public static int getInteger(
229                    UnicodeProperties properties, HttpServletRequest request, String param,
230                    int defaultValue) {
231    
232                    String propertiesValue = properties.getProperty(param, null);
233    
234                    int getterUtilValue = GetterUtil.getInteger(
235                            propertiesValue, defaultValue);
236    
237                    return ParamUtil.get(request, param, getterUtilValue);
238            }
239    
240            public static int getInteger(
241                    UnicodeProperties properties, PortletRequest portletRequest,
242                    String param) {
243    
244                    return getInteger(
245                            properties, portletRequest, param, GetterUtil.DEFAULT_INTEGER);
246            }
247    
248            public static int getInteger(
249                    UnicodeProperties properties, PortletRequest portletRequest,
250                    String param, int defaultValue) {
251    
252                    String propertiesValue = properties.getProperty(param, null);
253    
254                    int getterUtilValue = GetterUtil.getInteger(
255                            propertiesValue, defaultValue);
256    
257                    return ParamUtil.get(portletRequest, param, getterUtilValue);
258            }
259    
260            public static long getLong(
261                    Properties properties, HttpServletRequest request, String param) {
262    
263                    return getLong(properties, request, param, GetterUtil.DEFAULT_LONG);
264            }
265    
266            public static long getLong(
267                    Properties properties, HttpServletRequest request, String param,
268                    long defaultValue) {
269    
270                    String propertiesValue = properties.getProperty(param, null);
271    
272                    long getterUtilValue = GetterUtil.getLong(
273                            propertiesValue, defaultValue);
274    
275                    return ParamUtil.get(request, param, getterUtilValue);
276            }
277    
278            public static long getLong(
279                    Properties properties, PortletRequest portletRequest, String param) {
280    
281                    return getLong(
282                            properties, portletRequest, param, GetterUtil.DEFAULT_LONG);
283            }
284    
285            public static long getLong(
286                    Properties properties, PortletRequest portletRequest, String param,
287                    long defaultValue) {
288    
289                    String propertiesValue = properties.getProperty(param, null);
290    
291                    long getterUtilValue = GetterUtil.getLong(
292                            propertiesValue, defaultValue);
293    
294                    return ParamUtil.get(portletRequest, param, getterUtilValue);
295            }
296    
297            public static long getLong(
298                    UnicodeProperties properties, HttpServletRequest request,
299                    String param) {
300    
301                    return getLong(properties, request, param, GetterUtil.DEFAULT_LONG);
302            }
303    
304            public static long getLong(
305                    UnicodeProperties properties, HttpServletRequest request, String param,
306                    long defaultValue) {
307    
308                    String propertiesValue = properties.getProperty(param, null);
309    
310                    long getterUtilValue = GetterUtil.getLong(
311                            propertiesValue, defaultValue);
312    
313                    return ParamUtil.get(request, param, getterUtilValue);
314            }
315    
316            public static long getLong(
317                    UnicodeProperties properties, PortletRequest portletRequest,
318                    String param) {
319    
320                    return getLong(
321                            properties, portletRequest, param, GetterUtil.DEFAULT_LONG);
322            }
323    
324            public static long getLong(
325                    UnicodeProperties properties, PortletRequest portletRequest,
326                    String param, long defaultValue) {
327    
328                    String propertiesValue = properties.getProperty(param, null);
329    
330                    long getterUtilValue = GetterUtil.getLong(
331                            propertiesValue, defaultValue);
332    
333                    return ParamUtil.get(portletRequest, param, getterUtilValue);
334            }
335    
336            public static UnicodeProperties getProperties(
337                    PortletRequest portletRequest, String prefix) {
338    
339                    UnicodeProperties properties = new UnicodeProperties(true);
340    
341                    for (String param : portletRequest.getParameterMap().keySet()) {
342                            if (param.startsWith(prefix) && !param.endsWith("--Checkbox")) {
343                                    String key = param.substring(
344                                            prefix.length(), param.length() - 2);
345                                    String value = portletRequest.getParameter(param);
346    
347                                    properties.setProperty(key, value);
348                            }
349                    }
350    
351                    return properties;
352            }
353    
354            public static String getString(
355                    Properties properties, HttpServletRequest request, String param) {
356    
357                    return getString(properties, request, param, GetterUtil.DEFAULT_STRING);
358            }
359    
360            public static String getString(
361                    Properties properties, HttpServletRequest request, String param,
362                    String defaultValue) {
363    
364                    String propertiesValue = properties.getProperty(param, null);
365    
366                    String getterUtilValue = GetterUtil.getString(
367                            propertiesValue, defaultValue);
368    
369                    return ParamUtil.get(request, param, getterUtilValue);
370            }
371    
372            public static String getString(
373                    Properties properties, PortletRequest portletRequest, String param) {
374    
375                    return getString(
376                            properties, portletRequest, param, GetterUtil.DEFAULT_STRING);
377            }
378    
379            public static String getString(
380                    Properties properties, PortletRequest portletRequest, String param,
381                    String defaultValue) {
382    
383                    String propertiesValue = properties.getProperty(param, null);
384    
385                    String getterUtilValue = GetterUtil.getString(
386                            propertiesValue, defaultValue);
387    
388                    return ParamUtil.get(portletRequest, param, getterUtilValue);
389            }
390    
391            public static String getString(
392                    UnicodeProperties properties, HttpServletRequest request,
393                    String param) {
394    
395                    return getString(properties, request, param, GetterUtil.DEFAULT_STRING);
396            }
397    
398            public static String getString(
399                    UnicodeProperties properties, HttpServletRequest request, String param,
400                    String defaultValue) {
401    
402                    String propertiesValue = properties.getProperty(param, null);
403    
404                    String getterUtilValue = GetterUtil.getString(
405                            propertiesValue, defaultValue);
406    
407                    return ParamUtil.get(request, param, getterUtilValue);
408            }
409    
410            public static String getString(
411                    UnicodeProperties properties, PortletRequest portletRequest,
412                    String param) {
413    
414                    return getString(
415                            properties, portletRequest, param, GetterUtil.DEFAULT_STRING);
416            }
417    
418            public static String getString(
419                    UnicodeProperties properties, PortletRequest portletRequest,
420                    String param, String defaultValue) {
421    
422                    String propertiesValue = properties.getProperty(param, null);
423    
424                    String getterUtilValue = GetterUtil.getString(
425                            propertiesValue, defaultValue);
426    
427                    return ParamUtil.get(portletRequest, param, getterUtilValue);
428            }
429    
430    }