1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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 java.util.Enumeration;
23  
24  import javax.servlet.http.HttpServletRequest;
25  
26  import jodd.bean.BeanUtil;
27  
28  import org.apache.commons.beanutils.PropertyUtils;
29  
30  import org.springframework.beans.BeanUtils;
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 void copyProperties(Object source, Object target) {
40          BeanUtils.copyProperties(source, target);
41      }
42  
43      public void copyProperties(
44          Object source, Object target, Class<?> editable) {
45  
46          BeanUtils.copyProperties(source, target, editable);
47      }
48  
49      public void copyProperties(
50          Object source, Object target, String[] ignoreProperties) {
51  
52          BeanUtils.copyProperties(source, target, ignoreProperties);
53      }
54  
55      public boolean getBoolean(Object bean, String param) {
56          return getBoolean(bean, param, GetterUtil.DEFAULT_BOOLEAN);
57      }
58  
59      public boolean getBoolean(Object bean, String param, boolean defaultValue) {
60          Boolean beanValue = null;
61  
62          if (bean != null) {
63              try {
64                  beanValue = (Boolean)PropertyUtils.getProperty(bean, param);
65              }
66              catch (NoSuchMethodException nsme) {
67                  if (_log.isWarnEnabled()) {
68                      _log.warn(nsme.getMessage());
69                  }
70              }
71              catch (Exception e) {
72                  _log.error(e);
73              }
74          }
75  
76          if (beanValue == null) {
77              return defaultValue;
78          }
79          else {
80              return beanValue.booleanValue();
81          }
82      }
83  
84      public double getDouble(Object bean, String param) {
85          return getDouble(bean, param, GetterUtil.DEFAULT_DOUBLE);
86      }
87  
88      public double getDouble(Object bean, String param, double defaultValue) {
89          Double beanValue = null;
90  
91          if (bean != null) {
92              try {
93                  beanValue = (Double)PropertyUtils.getProperty(bean, param);
94              }
95              catch (NoSuchMethodException nsme) {
96                  if (_log.isWarnEnabled()) {
97                      _log.warn(nsme.getMessage());
98                  }
99              }
100             catch (Exception e) {
101                 _log.error(e);
102             }
103         }
104 
105         if (beanValue == null) {
106             return defaultValue;
107         }
108         else {
109             return beanValue.doubleValue();
110         }
111     }
112 
113     public int getInteger(Object bean, String param) {
114         return getInteger(bean, param, GetterUtil.DEFAULT_INTEGER);
115     }
116 
117     public int getInteger(Object bean, String param, int defaultValue) {
118         Integer beanValue = null;
119 
120         if (bean != null) {
121             try {
122                 beanValue = (Integer)PropertyUtils.getProperty(bean, param);
123             }
124             catch (NoSuchMethodException nsme) {
125                 if (_log.isWarnEnabled()) {
126                     _log.warn(nsme.getMessage());
127                 }
128             }
129             catch (Exception e) {
130                 _log.error(e);
131             }
132         }
133 
134         if (beanValue == null) {
135             return defaultValue;
136         }
137         else {
138             return beanValue.intValue();
139         }
140     }
141 
142     public long getLong(Object bean, String param) {
143         return getLong(bean, param, GetterUtil.DEFAULT_LONG);
144     }
145 
146     public long getLong(Object bean, String param, long defaultValue) {
147         Long beanValue = null;
148 
149         if (bean != null) {
150             try {
151                 beanValue = (Long)PropertyUtils.getProperty(bean, param);
152             }
153             catch (NoSuchMethodException nsme) {
154                 if (_log.isWarnEnabled()) {
155                     _log.warn(nsme.getMessage());
156                 }
157             }
158             catch (Exception e) {
159                 _log.error(e);
160             }
161         }
162 
163         if (beanValue == null) {
164             return defaultValue;
165         }
166         else {
167             return beanValue.longValue();
168         }
169     }
170 
171     public Object getObject(Object bean, String param) {
172         return getObject(bean, param, null);
173     }
174 
175     public Object getObject(Object bean, String param, Object defaultValue) {
176         Object beanValue = null;
177 
178         if (bean != null) {
179             try {
180                 beanValue = PropertyUtils.getProperty(bean, param);
181             }
182             catch (NoSuchMethodException nsme) {
183                 if (_log.isWarnEnabled()) {
184                     _log.warn(nsme.getMessage());
185                 }
186             }
187             catch (Exception e) {
188                 _log.error(e);
189             }
190         }
191 
192         if (beanValue == null) {
193             return defaultValue;
194         }
195         else {
196             return beanValue;
197         }
198     }
199 
200     public String getString(Object bean, String param) {
201         return getString(bean, param, GetterUtil.DEFAULT_STRING);
202     }
203 
204     public String getString(Object bean, String param, String defaultValue) {
205         String beanValue = null;
206 
207         if (bean != null) {
208             try {
209                 beanValue = (String)PropertyUtils.getProperty(bean, param);
210             }
211             catch (NoSuchMethodException nsme) {
212                 if (_log.isWarnEnabled()) {
213                     _log.warn(nsme.getMessage());
214                 }
215             }
216             catch (Exception e) {
217                 _log.error(e);
218             }
219         }
220 
221         if (beanValue == null) {
222             return defaultValue;
223         }
224         else {
225             return beanValue;
226         }
227     }
228 
229     public void setProperties(Object bean, HttpServletRequest request) {
230         Enumeration<String> enu = request.getParameterNames();
231 
232         while (enu.hasMoreElements()) {
233             String name = enu.nextElement();
234 
235             String value = request.getParameter(name);
236 
237             BeanUtil.setPropertyForcedSilent(bean, name, value);
238         }
239     }
240 
241     public void setProperty(Object bean, String param, Object value) {
242         try {
243             PropertyUtils.setProperty(bean, param, value);
244         }
245         catch (NoSuchMethodException nsme) {
246             if (_log.isWarnEnabled()) {
247                 _log.warn(nsme.getMessage());
248             }
249         }
250         catch (Exception e) {
251             _log.error(e);
252         }
253     }
254 
255     private static Log _log = LogFactoryUtil.getLog(BeanPropertiesImpl.class);
256 
257 }