001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.bean;
016    
017    import com.liferay.portal.kernel.bean.BeanProperties;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    
022    import java.util.Enumeration;
023    
024    import javax.servlet.http.HttpServletRequest;
025    
026    import jodd.bean.BeanTool;
027    import jodd.bean.BeanUtil;
028    
029    import jodd.util.ReflectUtil;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     */
034    public class BeanPropertiesImpl implements BeanProperties {
035    
036            public void copyProperties(Object source, Object target) {
037                    try {
038                            BeanTool.copyProperties(source, target);
039                    }
040                    catch (Exception e) {
041                            _log.error(e, e);
042                    }
043            }
044    
045            public void copyProperties(
046                    Object source, Object target, Class<?> editable) {
047    
048                    try {
049                            BeanTool.copyProperties(source, target, editable);
050                    }
051                    catch (Exception e) {
052                            _log.error(e, e);
053                    }
054            }
055    
056            public void copyProperties(
057                    Object source, Object target, String[] ignoreProperties) {
058    
059                    try {
060                            BeanTool.copyProperties(source, target, ignoreProperties, false);
061                    }
062                    catch (Exception e) {
063                            _log.error(e, e);
064                    }
065            }
066    
067            public boolean getBoolean(Object bean, String param) {
068                    return getBoolean(bean, param, GetterUtil.DEFAULT_BOOLEAN);
069            }
070    
071            public boolean getBoolean(Object bean, String param, boolean defaultValue) {
072                    Boolean beanValue = null;
073    
074                    if (bean != null) {
075                            try {
076                                    Object value = BeanUtil.getProperty(bean, param);
077    
078                                    beanValue = ReflectUtil.castType(value, Boolean.class);
079                            }
080                            catch (Exception e) {
081                                    _log.error(e, e);
082                            }
083                    }
084    
085                    if (beanValue == null) {
086                            return defaultValue;
087                    }
088                    else {
089                            return beanValue.booleanValue();
090                    }
091            }
092    
093            public boolean getBooleanSilent(Object bean, String param) {
094                    return getBooleanSilent(bean, param, GetterUtil.DEFAULT_BOOLEAN);
095            }
096    
097            public boolean getBooleanSilent(
098                    Object bean, String param, boolean defaultValue) {
099    
100                    Boolean beanValue = null;
101    
102                    if (bean != null) {
103                            try {
104                                    Object value = BeanUtil.getProperty(bean, param);
105    
106                                    beanValue = ReflectUtil.castType(value, Boolean.class);
107                            }
108                            catch (Exception e) {
109                            }
110                    }
111    
112                    if (beanValue == null) {
113                            return defaultValue;
114                    }
115                    else {
116                            return beanValue.booleanValue();
117                    }
118            }
119    
120            public byte getByte(Object bean, String param) {
121                    return getByte(bean, param, GetterUtil.DEFAULT_BYTE);
122            }
123    
124            public byte getByte(Object bean, String param, byte defaultValue) {
125                    Byte beanValue = null;
126    
127                    if (bean != null) {
128                            try {
129                                    Object value = BeanUtil.getProperty(bean, param);
130    
131                                    beanValue = ReflectUtil.castType(value, Byte.class);
132                            }
133                            catch (Exception e) {
134                                    _log.error(e, e);
135                            }
136                    }
137    
138                    if (beanValue == null) {
139                            return defaultValue;
140                    }
141                    else {
142                            return beanValue.byteValue();
143                    }
144            }
145    
146            public byte getByteSilent(Object bean, String param) {
147                    return getByteSilent(bean, param, GetterUtil.DEFAULT_BYTE);
148            }
149    
150            public byte getByteSilent(Object bean, String param, byte defaultValue) {
151                    Byte beanValue = null;
152    
153                    if (bean != null) {
154                            try {
155                                    Object value = BeanUtil.getProperty(bean, param);
156    
157                                    beanValue = ReflectUtil.castType(value, Byte.class);
158                            }
159                            catch (Exception e) {
160                            }
161                    }
162    
163                    if (beanValue == null) {
164                            return defaultValue;
165                    }
166                    else {
167                            return beanValue.byteValue();
168                    }
169            }
170    
171            public double getDouble(Object bean, String param) {
172                    return getDouble(bean, param, GetterUtil.DEFAULT_DOUBLE);
173            }
174    
175            public double getDouble(Object bean, String param, double defaultValue) {
176                    Double beanValue = null;
177    
178                    if (bean != null) {
179                            try {
180                                    Object value = BeanUtil.getProperty(bean, param);
181    
182                                    beanValue = ReflectUtil.castType(value, Double.class);
183                            }
184                            catch (Exception e) {
185                                    _log.error(e, e);
186                            }
187                    }
188    
189                    if (beanValue == null) {
190                            return defaultValue;
191                    }
192                    else {
193                            return beanValue.doubleValue();
194                    }
195            }
196    
197            public double getDoubleSilent(Object bean, String param) {
198                    return getDoubleSilent(bean, param, GetterUtil.DEFAULT_DOUBLE);
199            }
200    
201            public double getDoubleSilent(
202                    Object bean, String param, double defaultValue) {
203    
204                    Double beanValue = null;
205    
206                    if (bean != null) {
207                            try {
208                                    Object value = BeanUtil.getProperty(bean, param);
209    
210                                    beanValue = ReflectUtil.castType(value, Double.class);
211                            }
212                            catch (Exception e) {
213                            }
214                    }
215    
216                    if (beanValue == null) {
217                            return defaultValue;
218                    }
219                    else {
220                            return beanValue.doubleValue();
221                    }
222            }
223    
224            public float getFloat(Object bean, String param) {
225                    return getFloat(bean, param, GetterUtil.DEFAULT_FLOAT);
226            }
227    
228            public float getFloat(Object bean, String param, float defaultValue) {
229                    Float beanValue = null;
230    
231                    if (bean != null) {
232                            try {
233                                    Object value = BeanUtil.getProperty(bean, param);
234    
235                                    beanValue = ReflectUtil.castType(value, Float.class);
236                            }
237                            catch (Exception e) {
238                                    _log.error(e, e);
239                            }
240                    }
241    
242                    if (beanValue == null) {
243                            return defaultValue;
244                    }
245                    else {
246                            return beanValue.floatValue();
247                    }
248            }
249    
250            public float getFloatSilent(Object bean, String param) {
251                    return getFloatSilent(bean, param, GetterUtil.DEFAULT_FLOAT);
252            }
253    
254            public float getFloatSilent(Object bean, String param, float defaultValue) {
255                    Float beanValue = null;
256    
257                    if (bean != null) {
258                            try {
259                                    Object value = BeanUtil.getProperty(bean, param);
260    
261                                    beanValue = ReflectUtil.castType(value, Float.class);
262                            }
263                            catch (Exception e) {
264                            }
265                    }
266    
267                    if (beanValue == null) {
268                            return defaultValue;
269                    }
270                    else {
271                            return beanValue.floatValue();
272                    }
273            }
274    
275            public int getInteger(Object bean, String param) {
276                    return getInteger(bean, param, GetterUtil.DEFAULT_INTEGER);
277            }
278    
279            public int getInteger(Object bean, String param, int defaultValue) {
280                    Integer beanValue = null;
281    
282                    if (bean != null) {
283                            try {
284                                    Object value = BeanUtil.getProperty(bean, param);
285    
286                                    beanValue = ReflectUtil.castType(value, Integer.class);
287                            }
288                            catch (Exception e) {
289                                    _log.error(e, e);
290                            }
291                    }
292    
293                    if (beanValue == null) {
294                            return defaultValue;
295                    }
296                    else {
297                            return beanValue.intValue();
298                    }
299            }
300    
301            public int getIntegerSilent(Object bean, String param) {
302                    return getIntegerSilent(bean, param, GetterUtil.DEFAULT_INTEGER);
303            }
304    
305            public int getIntegerSilent(Object bean, String param, int defaultValue) {
306                    Integer beanValue = null;
307    
308                    if (bean != null) {
309                            try {
310                                    Object value = BeanUtil.getProperty(bean, param);
311    
312                                    beanValue = ReflectUtil.castType(value, Integer.class);
313                            }
314                            catch (Exception e) {
315                            }
316                    }
317    
318                    if (beanValue == null) {
319                            return defaultValue;
320                    }
321                    else {
322                            return beanValue.intValue();
323                    }
324            }
325    
326            public long getLong(Object bean, String param) {
327                    return getLong(bean, param, GetterUtil.DEFAULT_LONG);
328            }
329    
330            public long getLong(Object bean, String param, long defaultValue) {
331                    Long beanValue = null;
332    
333                    if (bean != null) {
334                            try {
335                                    Object value = BeanUtil.getProperty(bean, param);
336    
337                                    beanValue = ReflectUtil.castType(value, Long.class);
338                            }
339                            catch (Exception e) {
340                                    _log.error(e, e);
341                            }
342                    }
343    
344                    if (beanValue == null) {
345                            return defaultValue;
346                    }
347                    else {
348                            return beanValue.longValue();
349                    }
350            }
351    
352            public long getLongSilent(Object bean, String param) {
353                    return getLongSilent(bean, param, GetterUtil.DEFAULT_LONG);
354            }
355    
356            public long getLongSilent(Object bean, String param, long defaultValue) {
357                    Long beanValue = null;
358    
359                    if (bean != null) {
360                            try {
361                                    Object value = BeanUtil.getProperty(bean, param);
362    
363                                    beanValue = ReflectUtil.castType(value, Long.class);
364                            }
365                            catch (Exception e) {
366                            }
367                    }
368    
369                    if (beanValue == null) {
370                            return defaultValue;
371                    }
372                    else {
373                            return beanValue.longValue();
374                    }
375            }
376    
377            public Object getObject(Object bean, String param) {
378                    return getObject(bean, param, null);
379            }
380    
381            public Object getObject(Object bean, String param, Object defaultValue) {
382                    Object beanValue = null;
383    
384                    if (bean != null) {
385                            try {
386                                    beanValue = BeanUtil.getProperty(bean, param);
387                            }
388                            catch (Exception e) {
389                                    _log.error(e, e);
390                            }
391                    }
392    
393                    if (beanValue == null) {
394                            return defaultValue;
395                    }
396                    else {
397                            return beanValue;
398                    }
399            }
400    
401            public Object getObjectSilent(Object bean, String param) {
402                    return getObjectSilent(bean, param, null);
403            }
404    
405            public Object getObjectSilent(
406                    Object bean, String param, Object defaultValue) {
407    
408                    Object beanValue = null;
409    
410                    if (bean != null) {
411                            try {
412                                    beanValue = BeanUtil.getProperty(bean, param);
413                            }
414                            catch (Exception e) {
415                            }
416                    }
417    
418                    if (beanValue == null) {
419                            return defaultValue;
420                    }
421                    else {
422                            return beanValue;
423                    }
424            }
425    
426            public short getShort(Object bean, String param) {
427                    return getShort(bean, param, GetterUtil.DEFAULT_SHORT);
428            }
429    
430            public short getShort(Object bean, String param, short defaultValue) {
431                    Short beanValue = null;
432    
433                    if (bean != null) {
434                            try {
435                                    Object value = BeanUtil.getProperty(bean, param);
436    
437                                    beanValue = ReflectUtil.castType(value, Short.class);
438                            }
439                            catch (Exception e) {
440                                    _log.error(e, e);
441                            }
442                    }
443    
444                    if (beanValue == null) {
445                            return defaultValue;
446                    }
447                    else {
448                            return beanValue.shortValue();
449                    }
450            }
451    
452            public short getShortSilent(Object bean, String param) {
453                    return getShortSilent(bean, param, GetterUtil.DEFAULT_SHORT);
454            }
455    
456            public short getShortSilent(Object bean, String param, short defaultValue) {
457                    Short beanValue = null;
458    
459                    if (bean != null) {
460                            try {
461                                    Object value = BeanUtil.getProperty(bean, param);
462    
463                                    beanValue = ReflectUtil.castType(value, Short.class);
464                            }
465                            catch (Exception e) {
466                            }
467                    }
468    
469                    if (beanValue == null) {
470                            return defaultValue;
471                    }
472                    else {
473                            return beanValue.shortValue();
474                    }
475            }
476    
477            public String getString(Object bean, String param) {
478                    return getString(bean, param, GetterUtil.DEFAULT_STRING);
479            }
480    
481            public String getString(Object bean, String param, String defaultValue) {
482                    String beanValue = null;
483    
484                    if (bean != null) {
485                            try {
486                                    Object value = BeanUtil.getProperty(bean, param);
487    
488                                    beanValue = ReflectUtil.castType(value, String.class);
489                            }
490                            catch (Exception e) {
491                                    _log.error(e, e);
492                            }
493                    }
494    
495                    if (beanValue == null) {
496                            return defaultValue;
497                    }
498                    else {
499                            return beanValue;
500                    }
501            }
502    
503            public String getStringSilent(Object bean, String param) {
504                    return getStringSilent(bean, param, GetterUtil.DEFAULT_STRING);
505            }
506    
507            public String getStringSilent(
508                    Object bean, String param, String defaultValue) {
509    
510                    String beanValue = null;
511    
512                    if (bean != null) {
513                            try {
514                                    Object value = BeanUtil.getProperty(bean, param);
515    
516                                    beanValue = ReflectUtil.castType(value, String.class);
517                            }
518                            catch (Exception e) {
519                            }
520                    }
521    
522                    if (beanValue == null) {
523                            return defaultValue;
524                    }
525                    else {
526                            return beanValue;
527                    }
528            }
529    
530            public void setProperties(Object bean, HttpServletRequest request) {
531                    Enumeration<String> enu = request.getParameterNames();
532    
533                    while (enu.hasMoreElements()) {
534                            String name = enu.nextElement();
535    
536                            String value = request.getParameter(name);
537    
538                            BeanUtil.setPropertyForcedSilent(bean, name, value);
539                    }
540            }
541    
542            public void setProperty(Object bean, String param, Object value) {
543                    try {
544                            BeanUtil.setProperty(bean, param, value);
545                    }
546                    catch (Exception e) {
547                            _log.error(e, e);
548                    }
549            }
550    
551            private static Log _log = LogFactoryUtil.getLog(BeanPropertiesImpl.class);
552    
553    }