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