1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.util;
24  
25  import com.liferay.portal.kernel.util.GetterUtil;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  
28  import javax.servlet.http.HttpServletRequest;
29  
30  import org.apache.commons.beanutils.PropertyUtils;
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  
34  /**
35   * <a href="BeanParamUtil.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   *
39   */
40  public class BeanParamUtil {
41  
42      public static boolean getBoolean(
43          Object bean, HttpServletRequest request, String param) {
44  
45          return getBoolean(bean, request, param, GetterUtil.DEFAULT_BOOLEAN);
46      }
47  
48      public static boolean getBoolean(
49          Object bean, HttpServletRequest request, String param,
50          boolean defaultValue) {
51  
52          Boolean beanValue = null;
53  
54          if (bean != null) {
55              try {
56                  beanValue =
57                      (Boolean)PropertyUtils.getSimpleProperty(bean, param);
58              }
59              catch (Exception e) {
60                  _log.error(param + " " + e.getMessage());
61              }
62          }
63  
64          if (beanValue == null) {
65              return ParamUtil.get(request, param, defaultValue);
66          }
67          else {
68              return ParamUtil.get(request, param, beanValue.booleanValue());
69          }
70      }
71  
72      public static double getDouble(
73          Object bean, HttpServletRequest request, String param) {
74  
75          return getDouble(bean, request, param, GetterUtil.DEFAULT_DOUBLE);
76      }
77  
78      public static double getDouble(
79          Object bean, HttpServletRequest request, String param,
80          double defaultValue) {
81  
82          Double beanValue = null;
83  
84          if (bean != null) {
85              try {
86                  beanValue =
87                      (Double)PropertyUtils.getSimpleProperty(bean, param);
88              }
89              catch (Exception e) {
90                  _log.error(param + " " + e.getMessage());
91              }
92          }
93  
94          if (beanValue == null) {
95              return ParamUtil.get(request, param, defaultValue);
96          }
97          else {
98              return ParamUtil.get(request, param, beanValue.doubleValue());
99          }
100     }
101 
102     public static int getInteger(
103         Object bean, HttpServletRequest request, String param) {
104 
105         return getInteger(bean, request, param, GetterUtil.DEFAULT_INTEGER);
106     }
107 
108     public static int getInteger(
109         Object bean, HttpServletRequest request, String param,
110         int defaultValue) {
111 
112         Integer beanValue = null;
113 
114         if (bean != null) {
115             try {
116                 beanValue =
117                     (Integer)PropertyUtils.getSimpleProperty(bean, param);
118             }
119             catch (Exception e) {
120                 _log.error(param + " " + e.getMessage());
121             }
122         }
123 
124         if (beanValue == null) {
125             return ParamUtil.get(request, param, defaultValue);
126         }
127         else {
128             return ParamUtil.get(request, param, beanValue.intValue());
129         }
130     }
131 
132     public static long getLong(
133         Object bean, HttpServletRequest request, String param) {
134 
135         return getLong(bean, request, param, GetterUtil.DEFAULT_LONG);
136     }
137 
138     public static long getLong(
139         Object bean, HttpServletRequest request, String param,
140         long defaultValue) {
141 
142         Long beanValue = null;
143 
144         if (bean != null) {
145             try {
146                 beanValue =
147                     (Long)PropertyUtils.getSimpleProperty(bean, param);
148             }
149             catch (Exception e) {
150                 _log.error(param + " " + e.getMessage());
151             }
152         }
153 
154         if (beanValue == null) {
155             return ParamUtil.get(request, param, defaultValue);
156         }
157         else {
158             return ParamUtil.get(request, param, beanValue.longValue());
159         }
160     }
161 
162     public static String getString(
163         Object bean, HttpServletRequest request, String param) {
164 
165         return getString(bean, request, param, GetterUtil.DEFAULT_STRING);
166     }
167 
168     public static String getString(
169         Object bean, HttpServletRequest request, String param,
170         String defaultValue) {
171 
172         String beanValue = null;
173 
174         if (bean != null) {
175             try {
176                 beanValue =
177                     (String)PropertyUtils.getSimpleProperty(bean, param);
178             }
179             catch (Exception e) {
180                 _log.error(param + " " + e.getMessage());
181             }
182         }
183 
184         if (beanValue == null) {
185             return ParamUtil.get(request, param, defaultValue);
186         }
187         else {
188             return ParamUtil.get(request, param, beanValue);
189         }
190     }
191 
192     private static Log _log = LogFactory.getLog(BeanParamUtil.class);
193 
194 }