1   /**
2    * Copyright (c) 2000-2009 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.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portal.kernel.util.GetterUtil;
29  
30  import org.apache.commons.beanutils.PropertyUtils;
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 = (Boolean)PropertyUtils.getProperty(bean, param);
50              }
51              catch (Exception e) {
52                  _log.error(e);
53              }
54          }
55  
56          if (beanValue == null) {
57              return defaultValue;
58          }
59          else {
60              return beanValue.booleanValue();
61          }
62      }
63  
64      public double getDouble(Object bean, String param) {
65          return getDouble(bean, param, GetterUtil.DEFAULT_DOUBLE);
66      }
67  
68      public double getDouble(Object bean, String param, double defaultValue) {
69          Double beanValue = null;
70  
71          if (bean != null) {
72              try {
73                  beanValue = (Double)PropertyUtils.getProperty(bean, param);
74              }
75              catch (Exception e) {
76                  _log.error(e);
77              }
78          }
79  
80          if (beanValue == null) {
81              return defaultValue;
82          }
83          else {
84              return beanValue.doubleValue();
85          }
86      }
87  
88      public int getInteger(Object bean, String param) {
89          return getInteger(bean, param, GetterUtil.DEFAULT_INTEGER);
90      }
91  
92      public int getInteger(Object bean, String param, int defaultValue) {
93          Integer beanValue = null;
94  
95          if (bean != null) {
96              try {
97                  beanValue = (Integer)PropertyUtils.getProperty(bean, param);
98              }
99              catch (Exception e) {
100                 _log.error(e);
101             }
102         }
103 
104         if (beanValue == null) {
105             return defaultValue;
106         }
107         else {
108             return beanValue.intValue();
109         }
110     }
111 
112     public long getLong(Object bean, String param) {
113         return getLong(bean, param, GetterUtil.DEFAULT_LONG);
114     }
115 
116     public long getLong(Object bean, String param, long defaultValue) {
117         Long beanValue = null;
118 
119         if (bean != null) {
120             try {
121                 beanValue = (Long)PropertyUtils.getProperty(bean, param);
122             }
123             catch (Exception e) {
124                 _log.error(e);
125             }
126         }
127 
128         if (beanValue == null) {
129             return defaultValue;
130         }
131         else {
132             return beanValue.longValue();
133         }
134     }
135 
136     public Object getObject(Object bean, String param) {
137         return getObject(bean, param, null);
138     }
139 
140     public Object getObject(Object bean, String param, Object defaultValue) {
141         Object beanValue = null;
142 
143         if (bean != null) {
144             try {
145                 beanValue = PropertyUtils.getProperty(bean, param);
146             }
147             catch (Exception e) {
148                 _log.error(e);
149             }
150         }
151 
152         if (beanValue == null) {
153             return defaultValue;
154         }
155         else {
156             return beanValue;
157         }
158     }
159 
160     public String getString(Object bean, String param) {
161         return getString(bean, param, GetterUtil.DEFAULT_STRING);
162     }
163 
164     public String getString(Object bean, String param, String defaultValue) {
165         String beanValue = null;
166 
167         if (bean != null) {
168             try {
169                 beanValue = (String)PropertyUtils.getProperty(bean, param);
170             }
171             catch (Exception e) {
172                 _log.error(e);
173             }
174         }
175 
176         if (beanValue == null) {
177             return defaultValue;
178         }
179         else {
180             return beanValue;
181         }
182     }
183 
184     public void setProperty(Object bean, String param, Object value) {
185         try {
186             PropertyUtils.setProperty(bean, param, value);
187         }
188         catch (Exception e) {
189             _log.error(e);
190         }
191     }
192 
193     private static Log _log = LogFactoryUtil.getLog(BeanPropertiesImpl.class);
194 
195 }