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  /*
17   * Created on 2005/09/09
18   *
19   * TODO To change the template for this generated file go to
20   * Window - Preferences - Java - Code Style - Code Templates
21   */
22  package org.seasar.tuigwaa.database.function.criteria;
23  
24  import java.util.Calendar;
25  import java.util.Date;
26  
27  import com.isenshi.util.CalendarUtils;
28  
29  /***
30   * @author nishioka
31   * 
32   * TODO To change the template for this generated type comment go to Window -
33   * Preferences - Java - Code Style - Code Templates
34   */
35  public class DateFuncCriteriaFunction extends AbstractCriteriaFunction {
36  
37  	public static final String DATE_TYPE[] = { "today", "yesterday",
38  			"lastmonth", "thismonth", "lastyear", "thisyear", "fromtoday",
39  			"totoday" 
40  			};
41  
42  	public DateFuncCriteriaFunction() {
43  		super();
44  	}
45  
46  	public DateFuncCriteriaFunction(String field) {
47  		super(field);
48  	}
49  
50  	public DateFuncCriteriaFunction(String field, Object value) {
51  		super(field, value);
52  	}
53  
54  	/*
55  	 * (non-Javadoc)
56  	 * 
57  	 * @see com.isenshi.functor.UnaryFunction#evaluate(java.lang.Object)
58  	 */
59  	public Object evaluate(ICriteria criteria) {
60  		return evaluate(criteria, getValue());
61  	}
62  
63  	/*
64  	 */
65  	public Object evaluate(ICriteria criteria, Object value) {
66  		if (DATE_TYPE[6].equals(value)) {
67  			return doFromToday(criteria, value);
68  		} else if (DATE_TYPE[7].equals(value)) {
69  			return doToToday(criteria, value);
70  		} else {
71  			return doBetween(criteria, value);
72  		}
73  	}
74  
75  	private Object doFromToday(ICriteria criteria, Object value) {
76  		Calendar today = CalendarUtils.getToday();
77  		return criteria.addGe(getField(), today.getTime());
78  	}
79  
80  	private Object doToToday(ICriteria criteria, Object value) {
81  		Calendar today = CalendarUtils.getToday();
82  		return criteria.addLe(getField(), today.getTime());
83  	}
84  
85  	private Object doBetween(ICriteria criteria, Object value) {
86  		Calendar cal1 = CalendarUtils.getToday();
87  		Calendar cal2 = CalendarUtils.getToday();
88  
89  		if (DATE_TYPE[0].equals(value)) {
90  			cal2.roll(Calendar.DATE, 1);
91  		} else if (DATE_TYPE[1].equals(value)) {
92  			cal1.roll(Calendar.DATE, -1);
93  		} else if (DATE_TYPE[2].equals(value)) {
94  			cal1.roll(Calendar.MONTH, -1);
95  			cal2.roll(Calendar.MONTH, -1);
96  			cal1.set(Calendar.DATE, 1);
97  			cal2.set(Calendar.DATE, CalendarUtils.getActualMaximumDate());
98  		} else if (DATE_TYPE[3].equals(value)) {
99  			cal1.set(Calendar.DATE, 1);
100 			cal2.set(Calendar.DATE, CalendarUtils.getActualMaximumDate());
101 		} else if (DATE_TYPE[4].equals(value)) {
102 			cal1.roll(Calendar.YEAR, -1);
103 			cal2.roll(Calendar.YEAR, -1);
104 			cal1.set(Calendar.DATE, 1);
105 			cal1.set(Calendar.MONTH, 0);
106 			cal2.set(Calendar.DATE, 31);
107 			cal2.set(Calendar.MONTH, 11);
108 		} else if (DATE_TYPE[5].equals(value)) {
109 			cal1.set(Calendar.DATE, 1);
110 			cal1.set(Calendar.MONTH, 0);
111 			cal2.set(Calendar.DATE, 31);
112 			cal2.set(Calendar.MONTH, 11);
113 		}
114 		return criteria.addBetween(getField(), new Date[] { cal1.getTime(),
115 				cal2.getTime() });
116 	}
117 }