View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
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  }