001    /**
002     * Copyright (c) 2000-2013 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.bean;
016    
017    import com.liferay.portal.kernel.util.GetterUtil;
018    import com.liferay.portal.kernel.util.ParamUtil;
019    
020    import java.util.Locale;
021    
022    import javax.portlet.PortletRequest;
023    
024    import javax.servlet.http.HttpServletRequest;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     */
029    public class BeanParamUtil {
030    
031            public static boolean getBoolean(
032                    Object bean, HttpServletRequest request, String param) {
033    
034                    return getBoolean(bean, request, param, GetterUtil.DEFAULT_BOOLEAN);
035            }
036    
037            public static boolean getBoolean(
038                    Object bean, HttpServletRequest request, String param,
039                    boolean defaultValue) {
040    
041                    defaultValue = BeanPropertiesUtil.getBoolean(bean, param, defaultValue);
042    
043                    return ParamUtil.get(request, param, defaultValue);
044            }
045    
046            public static boolean getBoolean(
047                    Object bean, PortletRequest portletRequest, String param) {
048    
049                    return getBoolean(
050                            bean, portletRequest, param, GetterUtil.DEFAULT_BOOLEAN);
051            }
052    
053            public static boolean getBoolean(
054                    Object bean, PortletRequest portletRequest, String param,
055                    boolean defaultValue) {
056    
057                    defaultValue = BeanPropertiesUtil.getBoolean(bean, param, defaultValue);
058    
059                    return ParamUtil.get(portletRequest, param, defaultValue);
060            }
061    
062            public static boolean getBooleanSilent(
063                    Object bean, HttpServletRequest request, String param) {
064    
065                    return getBooleanSilent(
066                            bean, request, param, GetterUtil.DEFAULT_BOOLEAN);
067            }
068    
069            public static boolean getBooleanSilent(
070                    Object bean, HttpServletRequest request, String param,
071                    boolean defaultValue) {
072    
073                    defaultValue = BeanPropertiesUtil.getBooleanSilent(
074                            bean, param, defaultValue);
075    
076                    return ParamUtil.get(request, param, defaultValue);
077            }
078    
079            public static boolean getBooleanSilent(
080                    Object bean, PortletRequest portletRequest, String param) {
081    
082                    return getBooleanSilent(
083                            bean, portletRequest, param, GetterUtil.DEFAULT_BOOLEAN);
084            }
085    
086            public static boolean getBooleanSilent(
087                    Object bean, PortletRequest portletRequest, String param,
088                    boolean defaultValue) {
089    
090                    defaultValue = BeanPropertiesUtil.getBooleanSilent(
091                            bean, param, defaultValue);
092    
093                    return ParamUtil.get(portletRequest, param, defaultValue);
094            }
095    
096            public static double getDouble(
097                    Object bean, HttpServletRequest request, String param) {
098    
099                    return getDouble(bean, request, param, GetterUtil.DEFAULT_DOUBLE);
100            }
101    
102            public static double getDouble(
103                    Object bean, HttpServletRequest request, String param,
104                    double defaultValue) {
105    
106                    defaultValue = BeanPropertiesUtil.getDouble(bean, param, defaultValue);
107    
108                    return ParamUtil.get(request, param, defaultValue);
109            }
110    
111            public static double getDouble(
112                    Object bean, HttpServletRequest request, String param,
113                    double defaultValue, Locale locale) {
114    
115                    defaultValue = BeanPropertiesUtil.getDouble(bean, param, defaultValue);
116    
117                    return ParamUtil.getDouble(request, param, defaultValue, locale);
118            }
119    
120            public static double getDouble(
121                    Object bean, HttpServletRequest request, String param, Locale locale) {
122    
123                            return getDouble(
124                                    bean, request, param, GetterUtil.DEFAULT_DOUBLE, locale);
125            }
126    
127            public static double getDouble(
128                    Object bean, PortletRequest portletRequest, String param) {
129    
130                    return getDouble(
131                            bean, portletRequest, param, GetterUtil.DEFAULT_DOUBLE);
132            }
133    
134            public static double getDouble(
135                    Object bean, PortletRequest portletRequest, String param,
136                    double defaultValue) {
137    
138                    defaultValue = BeanPropertiesUtil.getDouble(bean, param, defaultValue);
139    
140                    return ParamUtil.get(portletRequest, param, defaultValue);
141            }
142    
143            public static double getDouble(
144                    Object bean, PortletRequest portletRequest, String param,
145                    double defaultValue, Locale locale) {
146    
147                    defaultValue = BeanPropertiesUtil.getDouble(bean, param, defaultValue);
148    
149                    return ParamUtil.getDouble(portletRequest, param, defaultValue, locale);
150            }
151    
152            public static double getDouble(
153                    Object bean, PortletRequest portletRequest, String param,
154                    Locale locale) {
155    
156                    return getDouble(
157                            bean, portletRequest, param, GetterUtil.DEFAULT_DOUBLE, locale);
158            }
159    
160            public static double getDoubleSilent(
161                    Object bean, HttpServletRequest request, String param) {
162    
163                    return getDoubleSilent(bean, request, param, GetterUtil.DEFAULT_DOUBLE);
164            }
165    
166            public static double getDoubleSilent(
167                    Object bean, HttpServletRequest request, String param,
168                    double defaultValue) {
169    
170                    defaultValue = BeanPropertiesUtil.getDoubleSilent(
171                            bean, param, defaultValue);
172    
173                    return ParamUtil.get(request, param, defaultValue);
174            }
175    
176            public static double getDoubleSilent(
177                    Object bean, HttpServletRequest request, String param,
178                    double defaultValue, Locale locale) {
179    
180                    defaultValue = BeanPropertiesUtil.getDoubleSilent(
181                            bean, param, defaultValue);
182    
183                    return ParamUtil.getDouble(request, param, defaultValue, locale);
184            }
185    
186            public static double getDoubleSilent(
187                    Object bean, HttpServletRequest request, String param, Locale locale) {
188    
189                    return getDoubleSilent(
190                            bean, request, param, GetterUtil.DEFAULT_DOUBLE, locale);
191            }
192    
193            public static double getDoubleSilent(
194                    Object bean, PortletRequest portletRequest, String param) {
195    
196                    return getDoubleSilent(
197                            bean, portletRequest, param, GetterUtil.DEFAULT_DOUBLE);
198            }
199    
200            public static double getDoubleSilent(
201                    Object bean, PortletRequest portletRequest, String param,
202                    double defaultValue) {
203    
204                    defaultValue = BeanPropertiesUtil.getDoubleSilent(
205                            bean, param, defaultValue);
206    
207                    return ParamUtil.get(portletRequest, param, defaultValue);
208            }
209    
210            public static double getDoubleSilent(
211                    Object bean, PortletRequest portletRequest, String param,
212                    double defaultValue, Locale locale) {
213    
214                    defaultValue = BeanPropertiesUtil.getDoubleSilent(
215                            bean, param, defaultValue);
216    
217                    return ParamUtil.getDouble(portletRequest, param, defaultValue, locale);
218            }
219    
220            public static double getDoubleSilent(
221                    Object bean, PortletRequest portletRequest, String param,
222                    Locale locale) {
223    
224                    return getDoubleSilent(
225                            bean, portletRequest, param, GetterUtil.DEFAULT_DOUBLE, locale);
226            }
227    
228            public static int getInteger(
229                    Object bean, HttpServletRequest request, String param) {
230    
231                    return getInteger(bean, request, param, GetterUtil.DEFAULT_INTEGER);
232            }
233    
234            public static int getInteger(
235                    Object bean, HttpServletRequest request, String param,
236                    int defaultValue) {
237    
238                    defaultValue = BeanPropertiesUtil.getInteger(bean, param, defaultValue);
239    
240                    return ParamUtil.get(request, param, defaultValue);
241            }
242    
243            public static int getInteger(
244                    Object bean, PortletRequest portletRequest, String param) {
245    
246                    return getInteger(
247                            bean, portletRequest, param, GetterUtil.DEFAULT_INTEGER);
248            }
249    
250            public static int getInteger(
251                    Object bean, PortletRequest portletRequest, String param,
252                    int defaultValue) {
253    
254                    defaultValue = BeanPropertiesUtil.getInteger(bean, param, defaultValue);
255    
256                    return ParamUtil.get(portletRequest, param, defaultValue);
257            }
258    
259            public static int getIntegerSilent(
260                    Object bean, HttpServletRequest request, String param) {
261    
262                    return getIntegerSilent(
263                            bean, request, param, GetterUtil.DEFAULT_INTEGER);
264            }
265    
266            public static int getIntegerSilent(
267                    Object bean, HttpServletRequest request, String param,
268                    int defaultValue) {
269    
270                    defaultValue = BeanPropertiesUtil.getIntegerSilent(
271                            bean, param, defaultValue);
272    
273                    return ParamUtil.get(request, param, defaultValue);
274            }
275    
276            public static int getIntegerSilent(
277                    Object bean, PortletRequest portletRequest, String param) {
278    
279                    return getIntegerSilent(
280                            bean, portletRequest, param, GetterUtil.DEFAULT_INTEGER);
281            }
282    
283            public static int getIntegerSilent(
284                    Object bean, PortletRequest portletRequest, String param,
285                    int defaultValue) {
286    
287                    defaultValue = BeanPropertiesUtil.getIntegerSilent(
288                            bean, param, defaultValue);
289    
290                    return ParamUtil.get(portletRequest, param, defaultValue);
291            }
292    
293            public static long getLong(
294                    Object bean, HttpServletRequest request, String param) {
295    
296                    return getLong(bean, request, param, GetterUtil.DEFAULT_LONG);
297            }
298    
299            public static long getLong(
300                    Object bean, HttpServletRequest request, String param,
301                    long defaultValue) {
302    
303                    defaultValue = BeanPropertiesUtil.getLong(bean, param, defaultValue);
304    
305                    return ParamUtil.get(request, param, defaultValue);
306            }
307    
308            public static long getLong(
309                    Object bean, PortletRequest portletRequest, String param) {
310    
311                    return getLong(bean, portletRequest, param, GetterUtil.DEFAULT_LONG);
312            }
313    
314            public static long getLong(
315                    Object bean, PortletRequest portletRequest, String param,
316                    long defaultValue) {
317    
318                    defaultValue = BeanPropertiesUtil.getLong(bean, param, defaultValue);
319    
320                    return ParamUtil.get(portletRequest, param, defaultValue);
321            }
322    
323            public static long getLongSilent(
324                    Object bean, HttpServletRequest request, String param) {
325    
326                    return getLongSilent(bean, request, param, GetterUtil.DEFAULT_LONG);
327            }
328    
329            public static long getLongSilent(
330                    Object bean, HttpServletRequest request, String param,
331                    long defaultValue) {
332    
333                    defaultValue = BeanPropertiesUtil.getLongSilent(
334                            bean, param, defaultValue);
335    
336                    return ParamUtil.get(request, param, defaultValue);
337            }
338    
339            public static long getLongSilent(
340                    Object bean, PortletRequest portletRequest, String param) {
341    
342                    return getLongSilent(
343                            bean, portletRequest, param, GetterUtil.DEFAULT_LONG);
344            }
345    
346            public static long getLongSilent(
347                    Object bean, PortletRequest portletRequest, String param,
348                    long defaultValue) {
349    
350                    defaultValue = BeanPropertiesUtil.getLongSilent(
351                            bean, param, defaultValue);
352    
353                    return ParamUtil.get(portletRequest, param, defaultValue);
354            }
355    
356            public static String getString(
357                    Object bean, HttpServletRequest request, String param) {
358    
359                    return getString(bean, request, param, GetterUtil.DEFAULT_STRING);
360            }
361    
362            public static String getString(
363                    Object bean, HttpServletRequest request, String param,
364                    String defaultValue) {
365    
366                    defaultValue = BeanPropertiesUtil.getString(bean, param, defaultValue);
367    
368                    return ParamUtil.get(request, param, defaultValue);
369            }
370    
371            public static String getString(
372                    Object bean, PortletRequest portletRequest, String param) {
373    
374                    return getString(
375                            bean, portletRequest, param, GetterUtil.DEFAULT_STRING);
376            }
377    
378            public static String getString(
379                    Object bean, PortletRequest portletRequest, String param,
380                    String defaultValue) {
381    
382                    defaultValue = BeanPropertiesUtil.getString(bean, param, defaultValue);
383    
384                    return ParamUtil.get(portletRequest, param, defaultValue);
385            }
386    
387            public static String getStringSilent(
388                    Object bean, HttpServletRequest request, String param) {
389    
390                    return getStringSilent(bean, request, param, GetterUtil.DEFAULT_STRING);
391            }
392    
393            public static String getStringSilent(
394                    Object bean, HttpServletRequest request, String param,
395                    String defaultValue) {
396    
397                    defaultValue = BeanPropertiesUtil.getStringSilent(
398                            bean, param, defaultValue);
399    
400                    return ParamUtil.get(request, param, defaultValue);
401            }
402    
403            public static String getStringSilent(
404                    Object bean, PortletRequest portletRequest, String param) {
405    
406                    return getStringSilent(
407                            bean, portletRequest, param, GetterUtil.DEFAULT_STRING);
408            }
409    
410            public static String getStringSilent(
411                    Object bean, PortletRequest portletRequest, String param,
412                    String defaultValue) {
413    
414                    defaultValue = BeanPropertiesUtil.getStringSilent(
415                            bean, param, defaultValue);
416    
417                    return ParamUtil.get(portletRequest, param, defaultValue);
418            }
419    
420    }