001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.bean;
016    
017    import com.liferay.portal.kernel.bean.BeanProperties;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.security.pacl.DoPrivileged;
022    import com.liferay.portal.kernel.util.GetterUtil;
023    import com.liferay.portal.kernel.util.ParamUtil;
024    import com.liferay.portal.model.User;
025    import com.liferay.portal.theme.ThemeDisplay;
026    import com.liferay.portal.util.PortalUtil;
027    import com.liferay.portal.util.WebKeys;
028    
029    import java.util.Calendar;
030    import java.util.Date;
031    import java.util.Enumeration;
032    
033    import javax.servlet.http.HttpServletRequest;
034    
035    import jodd.bean.BeanCopy;
036    import jodd.bean.BeanUtil;
037    
038    import jodd.typeconverter.Convert;
039    
040    /**
041     * @author Brian Wing Shun Chan
042     */
043    @DoPrivileged
044    public class BeanPropertiesImpl implements BeanProperties {
045    
046            public void copyProperties(Object source, Object target) {
047                    try {
048                            BeanCopy beanCopy = BeanCopy.beans(source, target);
049    
050                            beanCopy.copy();
051                    }
052                    catch (Exception e) {
053                            _log.error(e, e);
054                    }
055            }
056    
057            public void copyProperties(
058                    Object source, Object target, Class<?> editable) {
059    
060                    try {
061                            BeanCopy beanCopy = BeanCopy.beans(source, target);
062    
063                            beanCopy.includeAs(editable);
064    
065                            beanCopy.copy();
066                    }
067                    catch (Exception e) {
068                            _log.error(e, e);
069                    }
070            }
071    
072            public void copyProperties(
073                    Object source, Object target, String[] ignoreProperties) {
074    
075                    try {
076                            BeanCopy beanCopy = BeanCopy.beans(source, target);
077    
078                            beanCopy.exclude(ignoreProperties);
079    
080                            beanCopy.copy();
081                    }
082                    catch (Exception e) {
083                            _log.error(e, e);
084                    }
085            }
086    
087            public boolean getBoolean(Object bean, String param) {
088                    return getBoolean(bean, param, GetterUtil.DEFAULT_BOOLEAN);
089            }
090    
091            public boolean getBoolean(Object bean, String param, boolean defaultValue) {
092                    boolean beanValue = defaultValue;
093    
094                    if (bean != null) {
095                            try {
096                                    Object value = BeanUtil.getProperty(bean, param);
097    
098                                    beanValue = Convert.toBooleanValue(value, defaultValue);
099                            }
100                            catch (Exception e) {
101                                    _log.error(e, e);
102                            }
103                    }
104    
105                    return beanValue;
106            }
107    
108            public boolean getBooleanSilent(Object bean, String param) {
109                    return getBooleanSilent(bean, param, GetterUtil.DEFAULT_BOOLEAN);
110            }
111    
112            public boolean getBooleanSilent(
113                    Object bean, String param, boolean defaultValue) {
114    
115                    boolean beanValue = defaultValue;
116    
117                    if (bean != null) {
118                            try {
119                                    Object value = BeanUtil.getProperty(bean, param);
120    
121                                    beanValue = Convert.toBooleanValue(value, defaultValue);
122                            }
123                            catch (Exception e) {
124                            }
125                    }
126    
127                    return beanValue;
128            }
129    
130            public byte getByte(Object bean, String param) {
131                    return getByte(bean, param, GetterUtil.DEFAULT_BYTE);
132            }
133    
134            public byte getByte(Object bean, String param, byte defaultValue) {
135                    byte beanValue = defaultValue;
136    
137                    if (bean != null) {
138                            try {
139                                    Object value = BeanUtil.getProperty(bean, param);
140    
141                                    beanValue = Convert.toByteValue(value, defaultValue);
142                            }
143                            catch (Exception e) {
144                                    _log.error(e, e);
145                            }
146                    }
147    
148                    return beanValue;
149            }
150    
151            public byte getByteSilent(Object bean, String param) {
152                    return getByteSilent(bean, param, GetterUtil.DEFAULT_BYTE);
153            }
154    
155            public byte getByteSilent(Object bean, String param, byte defaultValue) {
156                    byte beanValue = defaultValue;
157    
158                    if (bean != null) {
159                            try {
160                                    Object value = BeanUtil.getProperty(bean, param);
161    
162                                    beanValue = Convert.toByteValue(value, defaultValue);
163                            }
164                            catch (Exception e) {
165                            }
166                    }
167    
168                    return beanValue;
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 = defaultValue;
177    
178                    if (bean != null) {
179                            try {
180                                    Object value = BeanUtil.getProperty(bean, param);
181    
182                                    beanValue = Convert.toDoubleValue(value, defaultValue);
183                            }
184                            catch (Exception e) {
185                                    _log.error(e, e);
186                            }
187                    }
188    
189                    return beanValue;
190            }
191    
192            public double getDoubleSilent(Object bean, String param) {
193                    return getDoubleSilent(bean, param, GetterUtil.DEFAULT_DOUBLE);
194            }
195    
196            public double getDoubleSilent(
197                    Object bean, String param, double defaultValue) {
198    
199                    double beanValue = defaultValue;
200    
201                    if (bean != null) {
202                            try {
203                                    Object value = BeanUtil.getProperty(bean, param);
204    
205                                    beanValue = Convert.toDoubleValue(value, defaultValue);
206                            }
207                            catch (Exception e) {
208                            }
209                    }
210    
211                    return beanValue;
212            }
213    
214            public float getFloat(Object bean, String param) {
215                    return getFloat(bean, param, GetterUtil.DEFAULT_FLOAT);
216            }
217    
218            public float getFloat(Object bean, String param, float defaultValue) {
219                    float beanValue = defaultValue;
220    
221                    if (bean != null) {
222                            try {
223                                    Object value = BeanUtil.getProperty(bean, param);
224    
225                                    beanValue = Convert.toFloatValue(value, defaultValue);
226                            }
227                            catch (Exception e) {
228                                    _log.error(e, e);
229                            }
230                    }
231    
232                    return beanValue;
233            }
234    
235            public float getFloatSilent(Object bean, String param) {
236                    return getFloatSilent(bean, param, GetterUtil.DEFAULT_FLOAT);
237            }
238    
239            public float getFloatSilent(Object bean, String param, float defaultValue) {
240                    float beanValue = defaultValue;
241    
242                    if (bean != null) {
243                            try {
244                                    Object value = BeanUtil.getProperty(bean, param);
245    
246                                    beanValue = Convert.toFloatValue(value, defaultValue);
247                            }
248                            catch (Exception e) {
249                            }
250                    }
251    
252                    return beanValue;
253            }
254    
255            public int getInteger(Object bean, String param) {
256                    return getInteger(bean, param, GetterUtil.DEFAULT_INTEGER);
257            }
258    
259            public int getInteger(Object bean, String param, int defaultValue) {
260                    int beanValue = defaultValue;
261    
262                    if (bean != null) {
263                            try {
264                                    Object value = BeanUtil.getProperty(bean, param);
265    
266                                    beanValue = Convert.toIntValue(value, defaultValue);
267                            }
268                            catch (Exception e) {
269                                    _log.error(e, e);
270                            }
271                    }
272    
273                    return beanValue;
274            }
275    
276            public int getIntegerSilent(Object bean, String param) {
277                    return getIntegerSilent(bean, param, GetterUtil.DEFAULT_INTEGER);
278            }
279    
280            public int getIntegerSilent(Object bean, String param, int defaultValue) {
281                    int beanValue = defaultValue;
282    
283                    if (bean != null) {
284                            try {
285                                    Object value = BeanUtil.getProperty(bean, param);
286    
287                                    beanValue = Convert.toIntValue(value, defaultValue);
288                            }
289                            catch (Exception e) {
290                            }
291                    }
292    
293                    return beanValue;
294            }
295    
296            public long getLong(Object bean, String param) {
297                    return getLong(bean, param, GetterUtil.DEFAULT_LONG);
298            }
299    
300            public long getLong(Object bean, String param, long defaultValue) {
301                    long beanValue = defaultValue;
302    
303                    if (bean != null) {
304                            try {
305                                    Object value = BeanUtil.getProperty(bean, param);
306    
307                                    beanValue = Convert.toLongValue(value, defaultValue);
308                            }
309                            catch (Exception e) {
310                                    _log.error(e, e);
311                            }
312                    }
313    
314                    return beanValue;
315            }
316    
317            public long getLongSilent(Object bean, String param) {
318                    return getLongSilent(bean, param, GetterUtil.DEFAULT_LONG);
319            }
320    
321            public long getLongSilent(Object bean, String param, long defaultValue) {
322                    long beanValue = defaultValue;
323    
324                    if (bean != null) {
325                            try {
326                                    Object value = BeanUtil.getProperty(bean, param);
327    
328                                    beanValue = Convert.toLongValue(value, defaultValue);
329                            }
330                            catch (Exception e) {
331                            }
332                    }
333    
334                    return beanValue;
335            }
336    
337            public Object getObject(Object bean, String param) {
338                    return getObject(bean, param, null);
339            }
340    
341            public Object getObject(Object bean, String param, Object defaultValue) {
342                    Object beanValue = null;
343    
344                    if (bean != null) {
345                            try {
346                                    beanValue = BeanUtil.getProperty(bean, param);
347                            }
348                            catch (Exception e) {
349                                    _log.error(e, e);
350                            }
351                    }
352    
353                    if (beanValue == null) {
354                            return defaultValue;
355                    }
356                    else {
357                            return beanValue;
358                    }
359            }
360    
361            public Object getObjectSilent(Object bean, String param) {
362                    return getObjectSilent(bean, param, null);
363            }
364    
365            public Object getObjectSilent(
366                    Object bean, String param, Object defaultValue) {
367    
368                    Object beanValue = null;
369    
370                    if (bean != null) {
371                            try {
372                                    beanValue = BeanUtil.getProperty(bean, param);
373                            }
374                            catch (Exception e) {
375                            }
376                    }
377    
378                    if (beanValue == null) {
379                            return defaultValue;
380                    }
381                    else {
382                            return beanValue;
383                    }
384            }
385    
386            public Class<?> getObjectType(Object bean, String param) {
387                    return getObjectType(bean, param, null);
388            }
389    
390            public Class<?> getObjectType(
391                    Object bean, String param, Class<?> defaultValue) {
392    
393                    Class<?> beanType = null;
394    
395                    if (bean != null) {
396                            try {
397                                    beanType = BeanUtil.getPropertyType(bean, param);
398                            }
399                            catch (Exception e) {
400                                    _log.error(e, e);
401                            }
402                    }
403    
404                    if (beanType == null) {
405                            return defaultValue;
406                    }
407                    else {
408                            return beanType;
409                    }
410            }
411    
412            public Class<?> getObjectTypeSilent(Object bean, String param) {
413                    return getObjectTypeSilent(bean, param, null);
414            }
415    
416            public Class<?> getObjectTypeSilent(
417                    Object bean, String param, Class<?> defaultValue) {
418    
419                    Class<?> beanType = null;
420    
421                    if (bean != null) {
422                            try {
423                                    beanType = BeanUtil.getPropertyType(bean, param);
424                            }
425                            catch (Exception e) {
426                            }
427                    }
428    
429                    if (beanType == null) {
430                            return defaultValue;
431                    }
432                    else {
433                            return beanType;
434                    }
435            }
436    
437            public short getShort(Object bean, String param) {
438                    return getShort(bean, param, GetterUtil.DEFAULT_SHORT);
439            }
440    
441            public short getShort(Object bean, String param, short defaultValue) {
442                    short beanValue = defaultValue;
443    
444                    if (bean != null) {
445                            try {
446                                    Object value = BeanUtil.getProperty(bean, param);
447    
448                                    beanValue = Convert.toShortValue(value, defaultValue);
449                            }
450                            catch (Exception e) {
451                                    _log.error(e, e);
452                            }
453                    }
454    
455                    return beanValue;
456            }
457    
458            public short getShortSilent(Object bean, String param) {
459                    return getShortSilent(bean, param, GetterUtil.DEFAULT_SHORT);
460            }
461    
462            public short getShortSilent(Object bean, String param, short defaultValue) {
463                    short beanValue = defaultValue;
464    
465                    if (bean != null) {
466                            try {
467                                    Object value = BeanUtil.getProperty(bean, param);
468    
469                                    beanValue = Convert.toShortValue(value, defaultValue);
470                            }
471                            catch (Exception e) {
472                            }
473                    }
474    
475                    return beanValue;
476            }
477    
478            public String getString(Object bean, String param) {
479                    return getString(bean, param, GetterUtil.DEFAULT_STRING);
480            }
481    
482            public String getString(Object bean, String param, String defaultValue) {
483                    String beanValue = defaultValue;
484    
485                    if (bean != null) {
486                            try {
487                                    Object value = BeanUtil.getProperty(bean, param);
488    
489                                    beanValue = Convert.toString(value, defaultValue);
490                            }
491                            catch (Exception e) {
492                                    _log.error(e, e);
493                            }
494                    }
495    
496                    return beanValue;
497            }
498    
499            public String getStringSilent(Object bean, String param) {
500                    return getStringSilent(bean, param, GetterUtil.DEFAULT_STRING);
501            }
502    
503            public String getStringSilent(
504                    Object bean, String param, String defaultValue) {
505    
506                    String beanValue = defaultValue;
507    
508                    if (bean != null) {
509                            try {
510                                    Object value = BeanUtil.getProperty(bean, param);
511    
512                                    beanValue = Convert.toString(value, defaultValue);
513                            }
514                            catch (Exception e) {
515                            }
516                    }
517    
518                    return beanValue;
519            }
520    
521            public void setProperties(Object bean, HttpServletRequest request) {
522                    Enumeration<String> enu = request.getParameterNames();
523    
524                    while (enu.hasMoreElements()) {
525                            String name = enu.nextElement();
526    
527                            String value = request.getParameter(name);
528    
529                            BeanUtil.setPropertyForcedSilent(bean, name, value);
530    
531                            if (name.endsWith("Month")) {
532                                    String dateParam = name.substring(0, name.lastIndexOf("Month"));
533    
534                                    if (request.getParameter(dateParam) != null) {
535                                            continue;
536                                    }
537    
538                                    Class<?> propertyTypeClass = BeanUtil.getPropertyType(
539                                            bean, dateParam);
540    
541                                    if ((propertyTypeClass == null) ||
542                                            !propertyTypeClass.equals(Date.class)) {
543    
544                                            continue;
545                                    }
546    
547                                    Date date = getDate(dateParam, request);
548    
549                                    if (date != null) {
550                                            BeanUtil.setPropertyForcedSilent(bean, dateParam, date);
551                                    }
552                            }
553                    }
554            }
555    
556            public void setProperty(Object bean, String param, Object value) {
557                    try {
558                            BeanUtil.setProperty(bean, param, value);
559                    }
560                    catch (Exception e) {
561                            _log.error(e, e);
562                    }
563            }
564    
565            protected Date getDate(String param, HttpServletRequest request) {
566                    int month = ParamUtil.getInteger(request, param + "Month");
567                    int day = ParamUtil.getInteger(request, param + "Day");
568                    int year = ParamUtil.getInteger(request, param + "Year");
569                    int hour = ParamUtil.getInteger(request, param + "Hour", -1);
570                    int minute = ParamUtil.getInteger(request, param + "Minute");
571    
572                    int amPm = ParamUtil.getInteger(request, param + "AmPm");
573    
574                    if (amPm == Calendar.PM) {
575                            hour += 12;
576                    }
577    
578                    if (hour == -1) {
579                            return PortalUtil.getDate(month, day, year);
580                    }
581    
582                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
583                            WebKeys.THEME_DISPLAY);
584    
585                    User user = themeDisplay.getUser();
586    
587                    try {
588                            return PortalUtil.getDate(
589                                    month, day, year, hour, minute, user.getTimeZone(), null);
590                    }
591                    catch (PortalException pe) {
592                            return null;
593                    }
594            }
595    
596            private static Log _log = LogFactoryUtil.getLog(BeanPropertiesImpl.class);
597    
598    }