001
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.BeanTool;
036 import jodd.bean.BeanUtil;
037
038 import jodd.typeconverter.Convert;
039
040
043 @DoPrivileged
044 public class BeanPropertiesImpl implements BeanProperties {
045
046 @Override
047 public void copyProperties(Object source, Object target) {
048 try {
049 BeanTool.copyProperties(source, target);
050 }
051 catch (Exception e) {
052 _log.error(e, e);
053 }
054 }
055
056 @Override
057 public void copyProperties(
058 Object source, Object target, Class<?> editable) {
059
060 try {
061 BeanTool.copyProperties(source, target, editable);
062 }
063 catch (Exception e) {
064 _log.error(e, e);
065 }
066 }
067
068 @Override
069 public void copyProperties(
070 Object source, Object target, String[] ignoreProperties) {
071
072 try {
073 BeanTool.copyProperties(source, target, ignoreProperties, false);
074 }
075 catch (Exception e) {
076 _log.error(e, e);
077 }
078 }
079
080 @Override
081 public boolean getBoolean(Object bean, String param) {
082 return getBoolean(bean, param, GetterUtil.DEFAULT_BOOLEAN);
083 }
084
085 @Override
086 public boolean getBoolean(Object bean, String param, boolean defaultValue) {
087 boolean beanValue = defaultValue;
088
089 if (bean != null) {
090 try {
091 Object value = BeanUtil.getProperty(bean, param);
092
093 beanValue = Convert.toBooleanValue(value, defaultValue);
094 }
095 catch (Exception e) {
096 _log.error(e, e);
097 }
098 }
099
100 return beanValue;
101 }
102
103 @Override
104 public boolean getBooleanSilent(Object bean, String param) {
105 return getBooleanSilent(bean, param, GetterUtil.DEFAULT_BOOLEAN);
106 }
107
108 @Override
109 public boolean getBooleanSilent(
110 Object bean, String param, boolean defaultValue) {
111
112 boolean beanValue = defaultValue;
113
114 if (bean != null) {
115 try {
116 Object value = BeanUtil.getProperty(bean, param);
117
118 beanValue = Convert.toBooleanValue(value, defaultValue);
119 }
120 catch (Exception e) {
121 }
122 }
123
124 return beanValue;
125 }
126
127 @Override
128 public byte getByte(Object bean, String param) {
129 return getByte(bean, param, GetterUtil.DEFAULT_BYTE);
130 }
131
132 @Override
133 public byte getByte(Object bean, String param, byte defaultValue) {
134 byte beanValue = defaultValue;
135
136 if (bean != null) {
137 try {
138 Object value = BeanUtil.getProperty(bean, param);
139
140 beanValue = Convert.toByteValue(value, defaultValue);
141 }
142 catch (Exception e) {
143 _log.error(e, e);
144 }
145 }
146
147 return beanValue;
148 }
149
150 @Override
151 public byte getByteSilent(Object bean, String param) {
152 return getByteSilent(bean, param, GetterUtil.DEFAULT_BYTE);
153 }
154
155 @Override
156 public byte getByteSilent(Object bean, String param, byte defaultValue) {
157 byte beanValue = defaultValue;
158
159 if (bean != null) {
160 try {
161 Object value = BeanUtil.getProperty(bean, param);
162
163 beanValue = Convert.toByteValue(value, defaultValue);
164 }
165 catch (Exception e) {
166 }
167 }
168
169 return beanValue;
170 }
171
172 @Override
173 public double getDouble(Object bean, String param) {
174 return getDouble(bean, param, GetterUtil.DEFAULT_DOUBLE);
175 }
176
177 @Override
178 public double getDouble(Object bean, String param, double defaultValue) {
179 double beanValue = defaultValue;
180
181 if (bean != null) {
182 try {
183 Object value = BeanUtil.getProperty(bean, param);
184
185 beanValue = Convert.toDoubleValue(value, defaultValue);
186 }
187 catch (Exception e) {
188 _log.error(e, e);
189 }
190 }
191
192 return beanValue;
193 }
194
195 @Override
196 public double getDoubleSilent(Object bean, String param) {
197 return getDoubleSilent(bean, param, GetterUtil.DEFAULT_DOUBLE);
198 }
199
200 @Override
201 public double getDoubleSilent(
202 Object bean, String param, double defaultValue) {
203
204 double beanValue = defaultValue;
205
206 if (bean != null) {
207 try {
208 Object value = BeanUtil.getProperty(bean, param);
209
210 beanValue = Convert.toDoubleValue(value, defaultValue);
211 }
212 catch (Exception e) {
213 }
214 }
215
216 return beanValue;
217 }
218
219 @Override
220 public float getFloat(Object bean, String param) {
221 return getFloat(bean, param, GetterUtil.DEFAULT_FLOAT);
222 }
223
224 @Override
225 public float getFloat(Object bean, String param, float defaultValue) {
226 float beanValue = defaultValue;
227
228 if (bean != null) {
229 try {
230 Object value = BeanUtil.getProperty(bean, param);
231
232 beanValue = Convert.toFloatValue(value, defaultValue);
233 }
234 catch (Exception e) {
235 _log.error(e, e);
236 }
237 }
238
239 return beanValue;
240 }
241
242 @Override
243 public float getFloatSilent(Object bean, String param) {
244 return getFloatSilent(bean, param, GetterUtil.DEFAULT_FLOAT);
245 }
246
247 @Override
248 public float getFloatSilent(Object bean, String param, float defaultValue) {
249 float beanValue = defaultValue;
250
251 if (bean != null) {
252 try {
253 Object value = BeanUtil.getProperty(bean, param);
254
255 beanValue = Convert.toFloatValue(value, defaultValue);
256 }
257 catch (Exception e) {
258 }
259 }
260
261 return beanValue;
262 }
263
264 @Override
265 public int getInteger(Object bean, String param) {
266 return getInteger(bean, param, GetterUtil.DEFAULT_INTEGER);
267 }
268
269 @Override
270 public int getInteger(Object bean, String param, int defaultValue) {
271 int beanValue = defaultValue;
272
273 if (bean != null) {
274 try {
275 Object value = BeanUtil.getProperty(bean, param);
276
277 beanValue = Convert.toIntValue(value, defaultValue);
278 }
279 catch (Exception e) {
280 _log.error(e, e);
281 }
282 }
283
284 return beanValue;
285 }
286
287 @Override
288 public int getIntegerSilent(Object bean, String param) {
289 return getIntegerSilent(bean, param, GetterUtil.DEFAULT_INTEGER);
290 }
291
292 @Override
293 public int getIntegerSilent(Object bean, String param, int defaultValue) {
294 int beanValue = defaultValue;
295
296 if (bean != null) {
297 try {
298 Object value = BeanUtil.getProperty(bean, param);
299
300 beanValue = Convert.toIntValue(value, defaultValue);
301 }
302 catch (Exception e) {
303 }
304 }
305
306 return beanValue;
307 }
308
309 @Override
310 public long getLong(Object bean, String param) {
311 return getLong(bean, param, GetterUtil.DEFAULT_LONG);
312 }
313
314 @Override
315 public long getLong(Object bean, String param, long defaultValue) {
316 long beanValue = defaultValue;
317
318 if (bean != null) {
319 try {
320 Object value = BeanUtil.getProperty(bean, param);
321
322 beanValue = Convert.toLongValue(value, defaultValue);
323 }
324 catch (Exception e) {
325 _log.error(e, e);
326 }
327 }
328
329 return beanValue;
330 }
331
332 @Override
333 public long getLongSilent(Object bean, String param) {
334 return getLongSilent(bean, param, GetterUtil.DEFAULT_LONG);
335 }
336
337 @Override
338 public long getLongSilent(Object bean, String param, long defaultValue) {
339 long beanValue = defaultValue;
340
341 if (bean != null) {
342 try {
343 Object value = BeanUtil.getProperty(bean, param);
344
345 beanValue = Convert.toLongValue(value, defaultValue);
346 }
347 catch (Exception e) {
348 }
349 }
350
351 return beanValue;
352 }
353
354 @Override
355 public Object getObject(Object bean, String param) {
356 return getObject(bean, param, null);
357 }
358
359 @Override
360 public Object getObject(Object bean, String param, Object defaultValue) {
361 Object beanValue = null;
362
363 if (bean != null) {
364 try {
365 beanValue = BeanUtil.getProperty(bean, param);
366 }
367 catch (Exception e) {
368 _log.error(e, e);
369 }
370 }
371
372 if (beanValue == null) {
373 return defaultValue;
374 }
375 else {
376 return beanValue;
377 }
378 }
379
380 @Override
381 public Object getObjectSilent(Object bean, String param) {
382 return getObjectSilent(bean, param, null);
383 }
384
385 @Override
386 public Object getObjectSilent(
387 Object bean, String param, Object defaultValue) {
388
389 Object beanValue = null;
390
391 if (bean != null) {
392 try {
393 beanValue = BeanUtil.getProperty(bean, param);
394 }
395 catch (Exception e) {
396 }
397 }
398
399 if (beanValue == null) {
400 return defaultValue;
401 }
402 else {
403 return beanValue;
404 }
405 }
406
407 @Override
408 public Class<?> getObjectType(Object bean, String param) {
409 return getObjectType(bean, param, null);
410 }
411
412 @Override
413 public Class<?> getObjectType(
414 Object bean, String param, Class<?> defaultValue) {
415
416 Class<?> beanType = null;
417
418 if (bean != null) {
419 try {
420 beanType = BeanUtil.getPropertyType(bean, param);
421 }
422 catch (Exception e) {
423 _log.error(e, e);
424 }
425 }
426
427 if (beanType == null) {
428 return defaultValue;
429 }
430 else {
431 return beanType;
432 }
433 }
434
435 @Override
436 public Class<?> getObjectTypeSilent(Object bean, String param) {
437 return getObjectTypeSilent(bean, param, null);
438 }
439
440 @Override
441 public Class<?> getObjectTypeSilent(
442 Object bean, String param, Class<?> defaultValue) {
443
444 Class<?> beanType = null;
445
446 if (bean != null) {
447 try {
448 beanType = BeanUtil.getPropertyType(bean, param);
449 }
450 catch (Exception e) {
451 }
452 }
453
454 if (beanType == null) {
455 return defaultValue;
456 }
457 else {
458 return beanType;
459 }
460 }
461
462 @Override
463 public short getShort(Object bean, String param) {
464 return getShort(bean, param, GetterUtil.DEFAULT_SHORT);
465 }
466
467 @Override
468 public short getShort(Object bean, String param, short defaultValue) {
469 short beanValue = defaultValue;
470
471 if (bean != null) {
472 try {
473 Object value = BeanUtil.getProperty(bean, param);
474
475 beanValue = Convert.toShortValue(value, defaultValue);
476 }
477 catch (Exception e) {
478 _log.error(e, e);
479 }
480 }
481
482 return beanValue;
483 }
484
485 @Override
486 public short getShortSilent(Object bean, String param) {
487 return getShortSilent(bean, param, GetterUtil.DEFAULT_SHORT);
488 }
489
490 @Override
491 public short getShortSilent(Object bean, String param, short defaultValue) {
492 short beanValue = defaultValue;
493
494 if (bean != null) {
495 try {
496 Object value = BeanUtil.getProperty(bean, param);
497
498 beanValue = Convert.toShortValue(value, defaultValue);
499 }
500 catch (Exception e) {
501 }
502 }
503
504 return beanValue;
505 }
506
507 @Override
508 public String getString(Object bean, String param) {
509 return getString(bean, param, GetterUtil.DEFAULT_STRING);
510 }
511
512 @Override
513 public String getString(Object bean, String param, String defaultValue) {
514 String beanValue = defaultValue;
515
516 if (bean != null) {
517 try {
518 Object value = BeanUtil.getProperty(bean, param);
519
520 beanValue = Convert.toString(value, defaultValue);
521 }
522 catch (Exception e) {
523 _log.error(e, e);
524 }
525 }
526
527 return beanValue;
528 }
529
530 @Override
531 public String getStringSilent(Object bean, String param) {
532 return getStringSilent(bean, param, GetterUtil.DEFAULT_STRING);
533 }
534
535 @Override
536 public String getStringSilent(
537 Object bean, String param, String defaultValue) {
538
539 String beanValue = defaultValue;
540
541 if (bean != null) {
542 try {
543 Object value = BeanUtil.getProperty(bean, param);
544
545 beanValue = Convert.toString(value, defaultValue);
546 }
547 catch (Exception e) {
548 }
549 }
550
551 return beanValue;
552 }
553
554 @Override
555 public void setProperties(Object bean, HttpServletRequest request) {
556 Enumeration<String> enu = request.getParameterNames();
557
558 while (enu.hasMoreElements()) {
559 String name = enu.nextElement();
560
561 String value = request.getParameter(name);
562
563 BeanUtil.setPropertyForcedSilent(bean, name, value);
564
565 if (name.endsWith("Month")) {
566 String dateParam = name.substring(0, name.lastIndexOf("Month"));
567
568 if (request.getParameter(dateParam) != null) {
569 continue;
570 }
571
572 Class<?> propertyTypeClass = BeanUtil.getPropertyType(
573 bean, dateParam);
574
575 if ((propertyTypeClass == null) ||
576 !propertyTypeClass.equals(Date.class)) {
577
578 continue;
579 }
580
581 Date date = getDate(dateParam, request);
582
583 if (date != null) {
584 BeanUtil.setPropertyForcedSilent(bean, dateParam, date);
585 }
586 }
587 }
588 }
589
590 @Override
591 public void setProperty(Object bean, String param, Object value) {
592 try {
593 BeanUtil.setProperty(bean, param, value);
594 }
595 catch (Exception e) {
596 _log.error(e, e);
597 }
598 }
599
600 protected Date getDate(String param, HttpServletRequest request) {
601 int month = ParamUtil.getInteger(request, param + "Month");
602 int day = ParamUtil.getInteger(request, param + "Day");
603 int year = ParamUtil.getInteger(request, param + "Year");
604 int hour = ParamUtil.getInteger(request, param + "Hour", -1);
605 int minute = ParamUtil.getInteger(request, param + "Minute");
606
607 int amPm = ParamUtil.getInteger(request, param + "AmPm");
608
609 if (amPm == Calendar.PM) {
610 hour += 12;
611 }
612
613 if (hour == -1) {
614 return PortalUtil.getDate(month, day, year);
615 }
616
617 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
618 WebKeys.THEME_DISPLAY);
619
620 User user = themeDisplay.getUser();
621
622 try {
623 return PortalUtil.getDate(
624 month, day, year, hour, minute, user.getTimeZone(),
625 (Class<? extends PortalException>)null);
626 }
627 catch (PortalException pe) {
628 return null;
629 }
630 }
631
632 private static Log _log = LogFactoryUtil.getLog(BeanPropertiesImpl.class);
633
634 }