1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.bean;
21  
22  import com.liferay.portal.kernel.bean.BeanProperties;
23  import com.liferay.portal.kernel.log.Log;
24  import com.liferay.portal.kernel.log.LogFactoryUtil;
25  import com.liferay.portal.kernel.util.GetterUtil;
26  
27  import org.apache.commons.beanutils.PropertyUtils;
28  
29  /**
30   * <a href="BeanPropertiesImpl.java.html"><b><i>View Source</i></b></a>
31   *
32   * @author Brian Wing Shun Chan
33   *
34   */
35  public class BeanPropertiesImpl implements BeanProperties {
36  
37      public boolean getBoolean(Object bean, String param) {
38          return getBoolean(bean, param, GetterUtil.DEFAULT_BOOLEAN);
39      }
40  
41      public boolean getBoolean(Object bean, String param, boolean defaultValue) {
42          Boolean beanValue = null;
43  
44          if (bean != null) {
45              try {
46                  beanValue = (Boolean)PropertyUtils.getProperty(bean, param);
47              }
48              catch (Exception e) {
49                  _log.error(e);
50              }
51          }
52  
53          if (beanValue == null) {
54              return defaultValue;
55          }
56          else {
57              return beanValue.booleanValue();
58          }
59      }
60  
61      public double getDouble(Object bean, String param) {
62          return getDouble(bean, param, GetterUtil.DEFAULT_DOUBLE);
63      }
64  
65      public double getDouble(Object bean, String param, double defaultValue) {
66          Double beanValue = null;
67  
68          if (bean != null) {
69              try {
70                  beanValue = (Double)PropertyUtils.getProperty(bean, param);
71              }
72              catch (Exception e) {
73                  _log.error(e);
74              }
75          }
76  
77          if (beanValue == null) {
78              return defaultValue;
79          }
80          else {
81              return beanValue.doubleValue();
82          }
83      }
84  
85      public int getInteger(Object bean, String param) {
86          return getInteger(bean, param, GetterUtil.DEFAULT_INTEGER);
87      }
88  
89      public int getInteger(Object bean, String param, int defaultValue) {
90          Integer beanValue = null;
91  
92          if (bean != null) {
93              try {
94                  beanValue = (Integer)PropertyUtils.getProperty(bean, param);
95              }
96              catch (Exception e) {
97                  _log.error(e);
98              }
99          }
100 
101         if (beanValue == null) {
102             return defaultValue;
103         }
104         else {
105             return beanValue.intValue();
106         }
107     }
108 
109     public long getLong(Object bean, String param) {
110         return getLong(bean, param, GetterUtil.DEFAULT_LONG);
111     }
112 
113     public long getLong(Object bean, String param, long defaultValue) {
114         Long beanValue = null;
115 
116         if (bean != null) {
117             try {
118                 beanValue = (Long)PropertyUtils.getProperty(bean, param);
119             }
120             catch (Exception e) {
121                 _log.error(e);
122             }
123         }
124 
125         if (beanValue == null) {
126             return defaultValue;
127         }
128         else {
129             return beanValue.longValue();
130         }
131     }
132 
133     public Object getObject(Object bean, String param) {
134         return getObject(bean, param, null);
135     }
136 
137     public Object getObject(Object bean, String param, Object defaultValue) {
138         Object beanValue = null;
139 
140         if (bean != null) {
141             try {
142                 beanValue = PropertyUtils.getProperty(bean, param);
143             }
144             catch (Exception e) {
145                 _log.error(e);
146             }
147         }
148 
149         if (beanValue == null) {
150             return defaultValue;
151         }
152         else {
153             return beanValue;
154         }
155     }
156 
157     public String getString(Object bean, String param) {
158         return getString(bean, param, GetterUtil.DEFAULT_STRING);
159     }
160 
161     public String getString(Object bean, String param, String defaultValue) {
162         String beanValue = null;
163 
164         if (bean != null) {
165             try {
166                 beanValue = (String)PropertyUtils.getProperty(bean, param);
167             }
168             catch (Exception e) {
169                 _log.error(e);
170             }
171         }
172 
173         if (beanValue == null) {
174             return defaultValue;
175         }
176         else {
177             return beanValue;
178         }
179     }
180 
181     public void setProperty(Object bean, String param, Object value) {
182         try {
183             PropertyUtils.setProperty(bean, param, value);
184         }
185         catch (Exception e) {
186             _log.error(e);
187         }
188     }
189 
190     private static Log _log = LogFactoryUtil.getLog(BeanPropertiesImpl.class);
191 
192 }