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  import org.springframework.beans.BeanUtils;
33  
34  /**
35   * <a href="BeanPropertiesImpl.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   */
39  public class BeanPropertiesImpl implements BeanProperties {
40  
41      public void copyProperties(Object source, Object target) {
42          BeanUtils.copyProperties(source, target);
43      }
44  
45      public void copyProperties(
46          Object source, Object target, Class<?> editable) {
47  
48          BeanUtils.copyProperties(source, target, editable);
49      }
50  
51      public void copyProperties(
52          Object source, Object target, String[] ignoreProperties) {
53  
54          BeanUtils.copyProperties(source, target, ignoreProperties);
55      }
56  
57      public boolean getBoolean(Object bean, String param) {
58          return getBoolean(bean, param, GetterUtil.DEFAULT_BOOLEAN);
59      }
60  
61      public boolean getBoolean(Object bean, String param, boolean defaultValue) {
62          Boolean beanValue = null;
63  
64          if (bean != null) {
65              try {
66                  beanValue = (Boolean)PropertyUtils.getProperty(bean, param);
67              }
68              catch (NoSuchMethodException nsme) {
69                  if (_log.isWarnEnabled()) {
70                      _log.warn(nsme.getMessage());
71                  }
72              }
73              catch (Exception e) {
74                  _log.error(e);
75              }
76          }
77  
78          if (beanValue == null) {
79              return defaultValue;
80          }
81          else {
82              return beanValue.booleanValue();
83          }
84      }
85  
86      public double getDouble(Object bean, String param) {
87          return getDouble(bean, param, GetterUtil.DEFAULT_DOUBLE);
88      }
89  
90      public double getDouble(Object bean, String param, double defaultValue) {
91          Double beanValue = null;
92  
93          if (bean != null) {
94              try {
95                  beanValue = (Double)PropertyUtils.getProperty(bean, param);
96              }
97              catch (NoSuchMethodException nsme) {
98                  if (_log.isWarnEnabled()) {
99                      _log.warn(nsme.getMessage());
100                 }
101             }
102             catch (Exception e) {
103                 _log.error(e);
104             }
105         }
106 
107         if (beanValue == null) {
108             return defaultValue;
109         }
110         else {
111             return beanValue.doubleValue();
112         }
113     }
114 
115     public int getInteger(Object bean, String param) {
116         return getInteger(bean, param, GetterUtil.DEFAULT_INTEGER);
117     }
118 
119     public int getInteger(Object bean, String param, int defaultValue) {
120         Integer beanValue = null;
121 
122         if (bean != null) {
123             try {
124                 beanValue = (Integer)PropertyUtils.getProperty(bean, param);
125             }
126             catch (NoSuchMethodException nsme) {
127                 if (_log.isWarnEnabled()) {
128                     _log.warn(nsme.getMessage());
129                 }
130             }
131             catch (Exception e) {
132                 _log.error(e);
133             }
134         }
135 
136         if (beanValue == null) {
137             return defaultValue;
138         }
139         else {
140             return beanValue.intValue();
141         }
142     }
143 
144     public long getLong(Object bean, String param) {
145         return getLong(bean, param, GetterUtil.DEFAULT_LONG);
146     }
147 
148     public long getLong(Object bean, String param, long defaultValue) {
149         Long beanValue = null;
150 
151         if (bean != null) {
152             try {
153                 beanValue = (Long)PropertyUtils.getProperty(bean, param);
154             }
155             catch (NoSuchMethodException nsme) {
156                 if (_log.isWarnEnabled()) {
157                     _log.warn(nsme.getMessage());
158                 }
159             }
160             catch (Exception e) {
161                 _log.error(e);
162             }
163         }
164 
165         if (beanValue == null) {
166             return defaultValue;
167         }
168         else {
169             return beanValue.longValue();
170         }
171     }
172 
173     public Object getObject(Object bean, String param) {
174         return getObject(bean, param, null);
175     }
176 
177     public Object getObject(Object bean, String param, Object defaultValue) {
178         Object beanValue = null;
179 
180         if (bean != null) {
181             try {
182                 beanValue = PropertyUtils.getProperty(bean, param);
183             }
184             catch (NoSuchMethodException nsme) {
185                 if (_log.isWarnEnabled()) {
186                     _log.warn(nsme.getMessage());
187                 }
188             }
189             catch (Exception e) {
190                 _log.error(e);
191             }
192         }
193 
194         if (beanValue == null) {
195             return defaultValue;
196         }
197         else {
198             return beanValue;
199         }
200     }
201 
202     public String getString(Object bean, String param) {
203         return getString(bean, param, GetterUtil.DEFAULT_STRING);
204     }
205 
206     public String getString(Object bean, String param, String defaultValue) {
207         String beanValue = null;
208 
209         if (bean != null) {
210             try {
211                 beanValue = (String)PropertyUtils.getProperty(bean, param);
212             }
213             catch (NoSuchMethodException nsme) {
214                 if (_log.isWarnEnabled()) {
215                     _log.warn(nsme.getMessage());
216                 }
217             }
218             catch (Exception e) {
219                 _log.error(e);
220             }
221         }
222 
223         if (beanValue == null) {
224             return defaultValue;
225         }
226         else {
227             return beanValue;
228         }
229     }
230 
231     public void setProperty(Object bean, String param, Object value) {
232         try {
233             PropertyUtils.setProperty(bean, param, value);
234         }
235         catch (NoSuchMethodException nsme) {
236             if (_log.isWarnEnabled()) {
237                 _log.warn(nsme.getMessage());
238             }
239         }
240         catch (Exception e) {
241             _log.error(e);
242         }
243     }
244 
245     private static Log _log = LogFactoryUtil.getLog(BeanPropertiesImpl.class);
246 
247 }