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