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.ServletRequest;
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, ServletRequest req, String param) {
44  
45          return getBoolean(bean, req, param, GetterUtil.DEFAULT_BOOLEAN);
46      }
47  
48      public static boolean getBoolean(
49          Object bean, ServletRequest req, String param, boolean defaultValue) {
50  
51          Boolean beanValue = null;
52  
53          if (bean != null) {
54              try {
55                  beanValue =
56                      (Boolean)PropertyUtils.getSimpleProperty(bean, param);
57              }
58              catch (Exception e) {
59                  _log.error(param + " " + e.getMessage());
60              }
61          }
62  
63          if (beanValue == null) {
64              return ParamUtil.get(req, param, defaultValue);
65          }
66          else {
67              return ParamUtil.get(req, param, beanValue.booleanValue());
68          }
69      }
70  
71      public static double getDouble(
72          Object bean, ServletRequest req, String param) {
73  
74          return getDouble(bean, req, param, GetterUtil.DEFAULT_DOUBLE);
75      }
76  
77      public static double getDouble(
78          Object bean, ServletRequest req, String param, double defaultValue) {
79  
80          Double beanValue = null;
81  
82          if (bean != null) {
83              try {
84                  beanValue =
85                      (Double)PropertyUtils.getSimpleProperty(bean, param);
86              }
87              catch (Exception e) {
88                  _log.error(param + " " + e.getMessage());
89              }
90          }
91  
92          if (beanValue == null) {
93              return ParamUtil.get(req, param, defaultValue);
94          }
95          else {
96              return ParamUtil.get(req, param, beanValue.doubleValue());
97          }
98      }
99  
100     public static int getInteger(
101         Object bean, ServletRequest req, String param) {
102 
103         return getInteger(bean, req, param, GetterUtil.DEFAULT_INTEGER);
104     }
105 
106     public static int getInteger(
107         Object bean, ServletRequest req, String param, int defaultValue) {
108 
109         Integer beanValue = null;
110 
111         if (bean != null) {
112             try {
113                 beanValue =
114                     (Integer)PropertyUtils.getSimpleProperty(bean, param);
115             }
116             catch (Exception e) {
117                 _log.error(param + " " + e.getMessage());
118             }
119         }
120 
121         if (beanValue == null) {
122             return ParamUtil.get(req, param, defaultValue);
123         }
124         else {
125             return ParamUtil.get(req, param, beanValue.intValue());
126         }
127     }
128 
129     public static long getLong(Object bean, ServletRequest req, String param) {
130         return getLong(bean, req, param, GetterUtil.DEFAULT_LONG);
131     }
132 
133     public static long getLong(
134         Object bean, ServletRequest req, String param, long defaultValue) {
135 
136         Long beanValue = null;
137 
138         if (bean != null) {
139             try {
140                 beanValue =
141                     (Long)PropertyUtils.getSimpleProperty(bean, param);
142             }
143             catch (Exception e) {
144                 _log.error(param + " " + e.getMessage());
145             }
146         }
147 
148         if (beanValue == null) {
149             return ParamUtil.get(req, param, defaultValue);
150         }
151         else {
152             return ParamUtil.get(req, param, beanValue.longValue());
153         }
154     }
155 
156     public static String getString(
157         Object bean, ServletRequest req, String param) {
158 
159         return getString(bean, req, param, GetterUtil.DEFAULT_STRING);
160     }
161 
162     public static String getString(
163         Object bean, ServletRequest req, String param, String defaultValue) {
164 
165         String beanValue = null;
166 
167         if (bean != null) {
168             try {
169                 beanValue =
170                     (String)PropertyUtils.getSimpleProperty(bean, param);
171             }
172             catch (Exception e) {
173                 _log.error(param + " " + e.getMessage());
174             }
175         }
176 
177         if (beanValue == null) {
178             return ParamUtil.get(req, param, defaultValue);
179         }
180         else {
181             return ParamUtil.get(req, param, beanValue);
182         }
183     }
184 
185     private static Log _log = LogFactory.getLog(BeanParamUtil.class);
186 
187 }