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 (NoSuchMethodException nsme) {
52                  if (_log.isWarnEnabled()) {
53                      _log.warn(nsme.getMessage());
54                  }
55              }
56              catch (Exception e) {
57                  _log.error(e);
58              }
59          }
60  
61          if (beanValue == null) {
62              return defaultValue;
63          }
64          else {
65              return beanValue.booleanValue();
66          }
67      }
68  
69      public double getDouble(Object bean, String param) {
70          return getDouble(bean, param, GetterUtil.DEFAULT_DOUBLE);
71      }
72  
73      public double getDouble(Object bean, String param, double defaultValue) {
74          Double beanValue = null;
75  
76          if (bean != null) {
77              try {
78                  beanValue = (Double)PropertyUtils.getProperty(bean, param);
79              }
80              catch (NoSuchMethodException nsme) {
81                  if (_log.isWarnEnabled()) {
82                      _log.warn(nsme.getMessage());
83                  }
84              }
85              catch (Exception e) {
86                  _log.error(e);
87              }
88          }
89  
90          if (beanValue == null) {
91              return defaultValue;
92          }
93          else {
94              return beanValue.doubleValue();
95          }
96      }
97  
98      public int getInteger(Object bean, String param) {
99          return getInteger(bean, param, GetterUtil.DEFAULT_INTEGER);
100     }
101 
102     public int getInteger(Object bean, String param, int defaultValue) {
103         Integer beanValue = null;
104 
105         if (bean != null) {
106             try {
107                 beanValue = (Integer)PropertyUtils.getProperty(bean, param);
108             }
109             catch (NoSuchMethodException nsme) {
110                 if (_log.isWarnEnabled()) {
111                     _log.warn(nsme.getMessage());
112                 }
113             }
114             catch (Exception e) {
115                 _log.error(e);
116             }
117         }
118 
119         if (beanValue == null) {
120             return defaultValue;
121         }
122         else {
123             return beanValue.intValue();
124         }
125     }
126 
127     public long getLong(Object bean, String param) {
128         return getLong(bean, param, GetterUtil.DEFAULT_LONG);
129     }
130 
131     public long getLong(Object bean, String param, long defaultValue) {
132         Long beanValue = null;
133 
134         if (bean != null) {
135             try {
136                 beanValue = (Long)PropertyUtils.getProperty(bean, param);
137             }
138             catch (NoSuchMethodException nsme) {
139                 if (_log.isWarnEnabled()) {
140                     _log.warn(nsme.getMessage());
141                 }
142             }
143             catch (Exception e) {
144                 _log.error(e);
145             }
146         }
147 
148         if (beanValue == null) {
149             return defaultValue;
150         }
151         else {
152             return beanValue.longValue();
153         }
154     }
155 
156     public Object getObject(Object bean, String param) {
157         return getObject(bean, param, null);
158     }
159 
160     public Object getObject(Object bean, String param, Object defaultValue) {
161         Object beanValue = null;
162 
163         if (bean != null) {
164             try {
165                 beanValue = PropertyUtils.getProperty(bean, param);
166             }
167             catch (NoSuchMethodException nsme) {
168                 if (_log.isWarnEnabled()) {
169                     _log.warn(nsme.getMessage());
170                 }
171             }
172             catch (Exception e) {
173                 _log.error(e);
174             }
175         }
176 
177         if (beanValue == null) {
178             return defaultValue;
179         }
180         else {
181             return beanValue;
182         }
183     }
184 
185     public String getString(Object bean, String param) {
186         return getString(bean, param, GetterUtil.DEFAULT_STRING);
187     }
188 
189     public String getString(Object bean, String param, String defaultValue) {
190         String beanValue = null;
191 
192         if (bean != null) {
193             try {
194                 beanValue = (String)PropertyUtils.getProperty(bean, param);
195             }
196             catch (NoSuchMethodException nsme) {
197                 if (_log.isWarnEnabled()) {
198                     _log.warn(nsme.getMessage());
199                 }
200             }
201             catch (Exception e) {
202                 _log.error(e);
203             }
204         }
205 
206         if (beanValue == null) {
207             return defaultValue;
208         }
209         else {
210             return beanValue;
211         }
212     }
213 
214     public void setProperty(Object bean, String param, Object value) {
215         try {
216             PropertyUtils.setProperty(bean, param, value);
217         }
218         catch (NoSuchMethodException nsme) {
219             if (_log.isWarnEnabled()) {
220                 _log.warn(nsme.getMessage());
221             }
222         }
223         catch (Exception e) {
224             _log.error(e);
225         }
226     }
227 
228     private static Log _log = LogFactoryUtil.getLog(BeanPropertiesImpl.class);
229 
230 }