1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.isenshi.util.converter.function;
17
18 import java.lang.reflect.InvocationTargetException;
19 import java.sql.Timestamp;
20 import java.util.Calendar;
21 import java.util.Date;
22 import java.util.GregorianCalendar;
23
24 import org.apache.commons.beanutils.PropertyUtils;
25 import org.seasar.tuigwaa.model.core.impl.TimestampAttribute;
26
27 public class DateConverterFunction extends ConverterFunction {
28
29 public Object evaluate(Object obj) {
30 String src = getSourceField();
31 String target = getTargetField();
32
33 try {
34 Integer[] dateArray = (Integer[]) PropertyUtils.getProperty(obj,
35 src);
36
37 if (dateArray != null && dateArray.length > 2) {
38 int year = dateArray[0].intValue();
39 int month = dateArray[1].intValue();
40 int date = dateArray[2].intValue();
41
42 if (year >= 0 && month >= 0 && date >= 0) {
43 Calendar cal = new GregorianCalendar(year, month - 1, date);
44
45 if (getEntity().getField(getTargetField()) instanceof TimestampAttribute) {
46 Timestamp timestamp = new Timestamp(cal
47 .getTimeInMillis());
48 PropertyUtils.setProperty(obj, target, timestamp);
49 } else {
50 Date dateTime = cal.getTime();
51 PropertyUtils.setProperty(obj, target, dateTime);
52 }
53 } else {
54 PropertyUtils.setProperty(obj, target, null);
55 }
56 } else {
57 PropertyUtils.setProperty(obj, target, null);
58 }
59 } catch (IllegalAccessException e) {
60 e.printStackTrace();
61 throw new RuntimeException(e);
62 } catch (InvocationTargetException e) {
63 e.printStackTrace();
64 throw new RuntimeException(e);
65 } catch (NoSuchMethodException e) {
66 e.printStackTrace();
67 throw new RuntimeException(e);
68 }
69 return obj;
70 }
71 }