Revision 18138 trunk/libraries/libAnimation/src/com/iver/cit/gvsig/animation/dateFilter/DateFilter.java

View differences:

DateFilter.java
5 5
import java.text.SimpleDateFormat;
6 6
import java.util.Calendar;
7 7

  
8
import com.hardcode.gdbms.engine.values.DateValue;
9
import com.hardcode.gdbms.engine.values.NullValue;
10
import com.hardcode.gdbms.engine.values.Value;
11
import com.iver.cit.gvsig.fmap.core.IFeature;
12
import com.iver.cit.gvsig.fmap.layers.ICustomVectorialFilter;
13
import com.iver.utiles.IPersistance;
8
//import com.hardcode.gdbms.engine.values.DateValue;
9
//import com.hardcode.gdbms.engine.values.NullValue;
10
//import com.hardcode.gdbms.engine.values.Value;
11
//import com.iver.cit.gvsig.fmap.core.IFeature;
12
import com.iver.utiles.IPersistence;
14 13
import com.iver.utiles.XMLEntity;
15
import com.sun.media.jai.opimage.AffineCRIF;
16 14

  
17
public class DateFilter implements ICustomVectorialFilter, IPersistance {
18

  
19
	public static int FIELDTYPE_DATE = 0;
20
	public static int FIELDTYPE_DATESTRING = 1;
21

  
22
	public static int BOTH = 2 | 4;
23
	public static int BEFORE_BEGIN = 2;
24
	public static int AFTER_END = 4;
25

  
26
	private int fieldType = -1;
27

  
28
	private int filterFieldIndex = -1;
29
	private Date minDate = null;
30
	private Date maxDate = null;
31
	// derived
32
	private SimpleDateFormat localeDF = null;
33
	private SimpleDateFormat javaDF = null;
34
	private Calendar cl = null;
35

  
36
	private int filterMode = BEFORE_BEGIN;
37

  
38
	public void setFieldIndex(int index) {
39
		filterFieldIndex = index;
40
	}
41

  
42
	public void setFieldType(int type) {
43
		fieldType = type;
44
	}
45

  
46
	public void setMinDate(Date miniDate) {
47
		minDate = miniDate;
48
	}
49

  
50
	public void setMaxDate(Date maxiDate) {
51
		maxDate = maxiDate;
52
	}
53

  
54
	public int getFieldType() {
55
		return fieldType;
56
	}
57

  
58
	public Date getMinDate() {
59
		return minDate;
60
	}
61

  
62
	public Date getMaxDate() {
63
		return maxDate;
64
	}
65

  
66
	public boolean accepts(IFeature feature) {
67

  
68
		if (filterFieldIndex == -1 || minDate == null || maxDate == null)
69
			return false;
70

  
71
		Value val = feature.getAttribute(filterFieldIndex);
72
		if (val instanceof NullValue)
73
			return false;
74

  
75
		Date dateVal = null;
76
		if (fieldType == FIELDTYPE_DATE) {
77
			try {
78
				dateVal = ((DateValue) val).getValue();
79
			} catch (RuntimeException e) {
80
				return false;
81
			}
82
		} else if (fieldType == FIELDTYPE_DATESTRING) {
83
			if (localeDF == null)
84
				localeDF = new SimpleDateFormat("dd/MM/yyyy");
85
			if (javaDF == null)
86
				javaDF = new SimpleDateFormat("yyyy-MM-dd");
87

  
88
			String valStr = val.toString();
89

  
90
			try {
91
				java.util.Date utDate = localeDF.parse(valStr); // reads locale
92
				// dd/MM/yyyy
93
				// strings
94
				String utDateStr = javaDF.format(utDate); // outputs
95
				// yyyy-MM-dd
96
				dateVal = Date.valueOf(utDateStr); // creates Date for
97
				// comparison
98
			} catch (ParseException e) {
99
				return false;
100
			}
101
		}
102

  
103
		try {
104
			String dateValStr = dateVal.toString();
105
			String minDateStr = minDate.toString();
106
			String maxDateStr = maxDate.toString();
107

  
108
			if (((this.filterMode & BEFORE_BEGIN) != BEFORE_BEGIN))
109
				if (dateVal.compareTo(minDate) < 0)
110
					return false;
111

  
112
			if (((this.filterMode & AFTER_END) != AFTER_END))
113
				if (dateVal.compareTo(maxDate) > 0)
114
					return false;
115

  
116
		} catch (Exception e) {
117
			return false;
118
		}
119
		return true;
120
	}
121

  
122
	public String getClassName() {
123
		return this.getClass().getName();
124
	}
125

  
126
	public XMLEntity getXMLEntity() {
127
		XMLEntity xml = new XMLEntity();
128

  
129
		xml.putProperty("fieldType", fieldType);
130
		xml.putProperty("fieldIndex", filterFieldIndex);
131
		xml.putProperty("minDate", minDate.toString());
132
		xml.putProperty("maxDate", maxDate.toString());
133

  
134
		return xml;
135
	}
136

  
137
	public void setXMLEntity(XMLEntity xml) {
138
		if (xml.contains("fieldType"))
139
			fieldType = xml.getIntProperty("fieldType");
140

  
141
		if (xml.contains("fieldIndex"))
142
			filterFieldIndex = xml.getIntProperty("fieldIndex");
143

  
144
		if (xml.contains("minDate"))
145
			minDate = Date.valueOf(xml.getStringProperty("minDate"));
146

  
147
		if (xml.contains("maxDate"))
148
			maxDate = Date.valueOf(xml.getStringProperty("maxDate"));
149
	}
150

  
151
	public String toString() {
152
		String result = "";
153

  
154
		result += minDate.toString() + "\n" + maxDate.toString();
155

  
156
		return result;
157
	}
158

  
159
	public boolean compareTo(ICustomVectorialFilter filter) {
160
		DateFilter filterAux = (DateFilter) filter;
161
		boolean type = this.getFieldType() == filterAux.getFieldType();
162
		boolean min = this.getMinDate().toString().equals(
163
				filterAux.getMinDate().toString());
164
		boolean max = this.getMaxDate().toString().equals(
165
				filterAux.getMaxDate().toString());
166

  
167
		return (type && min && max);
168
	}
169

  
170
	public int getFilterMode() {
171
		return filterMode;
172
	}
173

  
174
	public void setFilterMode(int filterMode) {
175
		this.filterMode = filterMode;
176
	}
15
//public class DateFilter implements ICustomVectorialFilter, IPersistence {
16
	public class DateFilter {
17
//
18
//	public static int FIELDTYPE_DATE = 0;
19
//	public static int FIELDTYPE_DATESTRING = 1;
20
//
21
//	public static int BOTH = 2 | 4;
22
//	public static int BEFORE_BEGIN = 2;
23
//	public static int AFTER_END = 4;
24
//
25
//	private int fieldType = -1;
26
//
27
//	private int filterFieldIndex = -1;
28
//	private Date minDate = null;
29
//	private Date maxDate = null;
30
//	// derived
31
//	private SimpleDateFormat localeDF = null;
32
//	private SimpleDateFormat javaDF = null;
33
//	private Calendar cl = null;
34
//
35
//	private int filterMode = BEFORE_BEGIN;
36
//
37
//	public void setFieldIndex(int index) {
38
//		filterFieldIndex = index;
39
//	}
40
//
41
//	public void setFieldType(int type) {
42
//		fieldType = type;
43
//	}
44
//
45
//	public void setMinDate(Date miniDate) {
46
//		minDate = miniDate;
47
//	}
48
//
49
//	public void setMaxDate(Date maxiDate) {
50
//		maxDate = maxiDate;
51
//	}
52
//
53
//	public int getFieldType() {
54
//		return fieldType;
55
//	}
56
//
57
//	public Date getMinDate() {
58
//		return minDate;
59
//	}
60
//
61
//	public Date getMaxDate() {
62
//		return maxDate;
63
//	}
64
//
65
//	public boolean accepts(IFeature feature) {
66
//
67
//		if (filterFieldIndex == -1 || minDate == null || maxDate == null)
68
//			return false;
69
//
70
//		Value val = feature.getAttribute(filterFieldIndex);
71
//		if (val instanceof NullValue)
72
//			return false;
73
//
74
//		Date dateVal = null;
75
//		if (fieldType == FIELDTYPE_DATE) {
76
//			try {
77
//				dateVal = ((DateValue) val).getValue();
78
//			} catch (RuntimeException e) {
79
//				return false;
80
//			}
81
//		} else if (fieldType == FIELDTYPE_DATESTRING) {
82
//			if (localeDF == null)
83
//				localeDF = new SimpleDateFormat("dd/MM/yyyy");
84
//			if (javaDF == null)
85
//				javaDF = new SimpleDateFormat("yyyy-MM-dd");
86
//
87
//			String valStr = val.toString();
88
//
89
//			try {
90
//				java.util.Date utDate = localeDF.parse(valStr); // reads locale
91
//				// dd/MM/yyyy
92
//				// strings
93
//				String utDateStr = javaDF.format(utDate); // outputs
94
//				// yyyy-MM-dd
95
//				dateVal = Date.valueOf(utDateStr); // creates Date for
96
//				// comparison
97
//			} catch (ParseException e) {
98
//				return false;
99
//			}
100
//		}
101
//
102
//		try {
103
//			String dateValStr = dateVal.toString();
104
//			String minDateStr = minDate.toString();
105
//			String maxDateStr = maxDate.toString();
106
//
107
//			if (((this.filterMode & BEFORE_BEGIN) != BEFORE_BEGIN))
108
//				if (dateVal.compareTo(minDate) < 0)
109
//					return false;
110
//
111
//			if (((this.filterMode & AFTER_END) != AFTER_END))
112
//				if (dateVal.compareTo(maxDate) > 0)
113
//					return false;
114
//
115
//		} catch (Exception e) {
116
//			return false;
117
//		}
118
//		return true;
119
//	}
120
//
121
//	public String getClassName() {
122
//		return this.getClass().getName();
123
//	}
124
//
125
//	public XMLEntity getXMLEntity() {
126
//		XMLEntity xml = new XMLEntity();
127
//
128
//		xml.putProperty("fieldType", fieldType);
129
//		xml.putProperty("fieldIndex", filterFieldIndex);
130
//		xml.putProperty("minDate", minDate.toString());
131
//		xml.putProperty("maxDate", maxDate.toString());
132
//
133
//		return xml;
134
//	}
135
//
136
//	public void setXMLEntity(XMLEntity xml) {
137
//		if (xml.contains("fieldType"))
138
//			fieldType = xml.getIntProperty("fieldType");
139
//
140
//		if (xml.contains("fieldIndex"))
141
//			filterFieldIndex = xml.getIntProperty("fieldIndex");
142
//
143
//		if (xml.contains("minDate"))
144
//			minDate = Date.valueOf(xml.getStringProperty("minDate"));
145
//
146
//		if (xml.contains("maxDate"))
147
//			maxDate = Date.valueOf(xml.getStringProperty("maxDate"));
148
//	}
149
//
150
//	public String toString() {
151
//		String result = "";
152
//
153
//		result += minDate.toString() + "\n" + maxDate.toString();
154
//
155
//		return result;
156
//	}
157
//
158
//	public boolean compareTo(ICustomVectorialFilter filter) {
159
//		DateFilter filterAux = (DateFilter) filter;
160
//		boolean type = this.getFieldType() == filterAux.getFieldType();
161
//		boolean min = this.getMinDate().toString().equals(
162
//				filterAux.getMinDate().toString());
163
//		boolean max = this.getMaxDate().toString().equals(
164
//				filterAux.getMaxDate().toString());
165
//
166
//		return (type && min && max);
167
//	}
168
//
169
//	public int getFilterMode() {
170
//		return filterMode;
171
//	}
172
//
173
//	public void setFilterMode(int filterMode) {
174
//		this.filterMode = filterMode;
175
//	}
177 176
}

Also available in: Unified diff