1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class BeanPropertiesImpl implements BeanProperties {
38  
39      public boolean getBoolean(Object bean, String param) {
40          return getBoolean(bean, param, GetterUtil.DEFAULT_BOOLEAN);
41      }
42  
43      public boolean getBoolean(Object bean, String param, boolean defaultValue) {
44          Boolean beanValue = null;
45  
46          if (bean != null) {
47              try {
48                  beanValue = (Boolean)PropertyUtils.getProperty(bean, param);
49              }
50              catch (NoSuchMethodException nsme) {
51                  if (_log.isWarnEnabled()) {
52                      _log.warn(nsme.getMessage());
53                  }
54              }
55              catch (Exception e) {
56                  _log.error(e);
57              }
58          }
59  
60          if (beanValue == null) {
61              return defaultValue;
62          }
63          else {
64              return beanValue.booleanValue();
65          }
66      }
67  
68      public double getDouble(Object bean, String param) {
69          return getDouble(bean, param, GetterUtil.DEFAULT_DOUBLE);
70      }
71  
72      public double getDouble(Object bean, String param, double defaultValue) {
73          Double beanValue = null;
74  
75          if (bean != null) {
76              try {
77                  beanValue = (Double)PropertyUtils.getProperty(bean, param);
78              }
79              catch (NoSuchMethodException nsme) {
80                  if (_log.isWarnEnabled()) {
81                      _log.warn(nsme.getMessage());
82                  }
83              }
84              catch (Exception e) {
85                  _log.error(e);
86              }
87          }
88  
89          if (beanValue == null) {
90              return defaultValue;
91          }
92          else {
93              return beanValue.doubleValue();
94          }
95      }
96  
97      public int getInteger(Object bean, String param) {
98          return getInteger(bean, param, GetterUtil.DEFAULT_INTEGER);
99      }
100 
101     public int getInteger(Object bean, String param, int defaultValue) {
102         Integer beanValue = null;
103 
104         if (bean != null) {
105             try {
106                 beanValue = (Integer)PropertyUtils.getProperty(bean, param);
107             }
108             catch (NoSuchMethodException nsme) {
109                 if (_log.isWarnEnabled()) {
110                     _log.warn(nsme.getMessage());
111                 }
112             }
113             catch (Exception e) {
114                 _log.error(e);
115             }
116         }
117 
118         if (beanValue == null) {
119             return defaultValue;
120         }
121         else {
122             return beanValue.intValue();
123         }
124     }
125 
126     public long getLong(Object bean, String param) {
127         return getLong(bean, param, GetterUtil.DEFAULT_LONG);
128     }
129 
130     public long getLong(Object bean, String param, long defaultValue) {
131         Long beanValue = null;
132 
133         if (bean != null) {
134             try {
135                 beanValue = (Long)PropertyUtils.getProperty(bean, param);
136             }
137             catch (NoSuchMethodException nsme) {
138                 if (_log.isWarnEnabled()) {
139                     _log.warn(nsme.getMessage());
140                 }
141             }
142             catch (Exception e) {
143                 _log.error(e);
144             }
145         }
146 
147         if (beanValue == null) {
148             return defaultValue;
149         }
150         else {
151             return beanValue.longValue();
152         }
153     }
154 
155     public Object getObject(Object bean, String param) {
156         return getObject(bean, param, null);
157     }
158 
159     public Object getObject(Object bean, String param, Object defaultValue) {
160         Object beanValue = null;
161 
162         if (bean != null) {
163             try {
164                 beanValue = PropertyUtils.getProperty(bean, param);
165             }
166             catch (NoSuchMethodException nsme) {
167                 if (_log.isWarnEnabled()) {
168                     _log.warn(nsme.getMessage());
169                 }
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 String getString(Object bean, String param) {
185         return getString(bean, param, GetterUtil.DEFAULT_STRING);
186     }
187 
188     public String getString(Object bean, String param, String defaultValue) {
189         String beanValue = null;
190 
191         if (bean != null) {
192             try {
193                 beanValue = (String)PropertyUtils.getProperty(bean, param);
194             }
195             catch (NoSuchMethodException nsme) {
196                 if (_log.isWarnEnabled()) {
197                     _log.warn(nsme.getMessage());
198                 }
199             }
200             catch (Exception e) {
201                 _log.error(e);
202             }
203         }
204 
205         if (beanValue == null) {
206             return defaultValue;
207         }
208         else {
209             return beanValue;
210         }
211     }
212 
213     public void setProperty(Object bean, String param, Object value) {
214         try {
215             PropertyUtils.setProperty(bean, param, value);
216         }
217         catch (NoSuchMethodException nsme) {
218             if (_log.isWarnEnabled()) {
219                 _log.warn(nsme.getMessage());
220             }
221         }
222         catch (Exception e) {
223             _log.error(e);
224         }
225     }
226 
227     private static Log _log = LogFactoryUtil.getLog(BeanPropertiesImpl.class);
228 
229 }