1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.bean;
16  
17  import com.liferay.portal.kernel.bean.BeanProperties;
18  import com.liferay.portal.kernel.log.Log;
19  import com.liferay.portal.kernel.log.LogFactoryUtil;
20  import com.liferay.portal.kernel.util.GetterUtil;
21  
22  import org.apache.commons.beanutils.PropertyUtils;
23  
24  import org.springframework.beans.BeanUtils;
25  
26  /**
27   * <a href="BeanPropertiesImpl.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Brian Wing Shun Chan
30   */
31  public class BeanPropertiesImpl implements BeanProperties {
32  
33      public void copyProperties(Object source, Object target) {
34          BeanUtils.copyProperties(source, target);
35      }
36  
37      public void copyProperties(
38          Object source, Object target, Class<?> editable) {
39  
40          BeanUtils.copyProperties(source, target, editable);
41      }
42  
43      public void copyProperties(
44          Object source, Object target, String[] ignoreProperties) {
45  
46          BeanUtils.copyProperties(source, target, ignoreProperties);
47      }
48  
49      public boolean getBoolean(Object bean, String param) {
50          return getBoolean(bean, param, GetterUtil.DEFAULT_BOOLEAN);
51      }
52  
53      public boolean getBoolean(Object bean, String param, boolean defaultValue) {
54          Boolean beanValue = null;
55  
56          if (bean != null) {
57              try {
58                  beanValue = (Boolean)PropertyUtils.getProperty(bean, param);
59              }
60              catch (NoSuchMethodException nsme) {
61                  if (_log.isWarnEnabled()) {
62                      _log.warn(nsme.getMessage());
63                  }
64              }
65              catch (Exception e) {
66                  _log.error(e);
67              }
68          }
69  
70          if (beanValue == null) {
71              return defaultValue;
72          }
73          else {
74              return beanValue.booleanValue();
75          }
76      }
77  
78      public double getDouble(Object bean, String param) {
79          return getDouble(bean, param, GetterUtil.DEFAULT_DOUBLE);
80      }
81  
82      public double getDouble(Object bean, String param, double defaultValue) {
83          Double beanValue = null;
84  
85          if (bean != null) {
86              try {
87                  beanValue = (Double)PropertyUtils.getProperty(bean, param);
88              }
89              catch (NoSuchMethodException nsme) {
90                  if (_log.isWarnEnabled()) {
91                      _log.warn(nsme.getMessage());
92                  }
93              }
94              catch (Exception e) {
95                  _log.error(e);
96              }
97          }
98  
99          if (beanValue == null) {
100             return defaultValue;
101         }
102         else {
103             return beanValue.doubleValue();
104         }
105     }
106 
107     public int getInteger(Object bean, String param) {
108         return getInteger(bean, param, GetterUtil.DEFAULT_INTEGER);
109     }
110 
111     public int getInteger(Object bean, String param, int defaultValue) {
112         Integer beanValue = null;
113 
114         if (bean != null) {
115             try {
116                 beanValue = (Integer)PropertyUtils.getProperty(bean, param);
117             }
118             catch (NoSuchMethodException nsme) {
119                 if (_log.isWarnEnabled()) {
120                     _log.warn(nsme.getMessage());
121                 }
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.intValue();
133         }
134     }
135 
136     public long getLong(Object bean, String param) {
137         return getLong(bean, param, GetterUtil.DEFAULT_LONG);
138     }
139 
140     public long getLong(Object bean, String param, long defaultValue) {
141         Long beanValue = null;
142 
143         if (bean != null) {
144             try {
145                 beanValue = (Long)PropertyUtils.getProperty(bean, param);
146             }
147             catch (NoSuchMethodException nsme) {
148                 if (_log.isWarnEnabled()) {
149                     _log.warn(nsme.getMessage());
150                 }
151             }
152             catch (Exception e) {
153                 _log.error(e);
154             }
155         }
156 
157         if (beanValue == null) {
158             return defaultValue;
159         }
160         else {
161             return beanValue.longValue();
162         }
163     }
164 
165     public Object getObject(Object bean, String param) {
166         return getObject(bean, param, null);
167     }
168 
169     public Object getObject(Object bean, String param, Object defaultValue) {
170         Object beanValue = null;
171 
172         if (bean != null) {
173             try {
174                 beanValue = PropertyUtils.getProperty(bean, param);
175             }
176             catch (NoSuchMethodException nsme) {
177                 if (_log.isWarnEnabled()) {
178                     _log.warn(nsme.getMessage());
179                 }
180             }
181             catch (Exception e) {
182                 _log.error(e);
183             }
184         }
185 
186         if (beanValue == null) {
187             return defaultValue;
188         }
189         else {
190             return beanValue;
191         }
192     }
193 
194     public String getString(Object bean, String param) {
195         return getString(bean, param, GetterUtil.DEFAULT_STRING);
196     }
197 
198     public String getString(Object bean, String param, String defaultValue) {
199         String beanValue = null;
200 
201         if (bean != null) {
202             try {
203                 beanValue = (String)PropertyUtils.getProperty(bean, param);
204             }
205             catch (NoSuchMethodException nsme) {
206                 if (_log.isWarnEnabled()) {
207                     _log.warn(nsme.getMessage());
208                 }
209             }
210             catch (Exception e) {
211                 _log.error(e);
212             }
213         }
214 
215         if (beanValue == null) {
216             return defaultValue;
217         }
218         else {
219             return beanValue;
220         }
221     }
222 
223     public void setProperty(Object bean, String param, Object value) {
224         try {
225             PropertyUtils.setProperty(bean, param, value);
226         }
227         catch (NoSuchMethodException nsme) {
228             if (_log.isWarnEnabled()) {
229                 _log.warn(nsme.getMessage());
230             }
231         }
232         catch (Exception e) {
233             _log.error(e);
234         }
235     }
236 
237     private static Log _log = LogFactoryUtil.getLog(BeanPropertiesImpl.class);
238 
239 }