1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.caleigo.core;
20
21 import java.text.DateFormat;
22 import java.text.ParseException;
23 import java.util.*;
24
25 /*** DateProxyData is an IProxyData implementation that can be used to obtain
26 * the current time and/or date-data relative to the current time. It can
27 * for instance be used as the default value on an entity date field.
28 *
29 * @author Dennis Zikovic
30 * @version 1.00
31 *
32 *
33 *
34 * WHEN WHO WHY & WHAT
35 * -----------------------------------------------------------------------------
36 * 003-01-03 Dennis Zikovic Creation
37 */
38 public class DateProxyData implements IProxyData
39 {
40
41
42 /*** Specifies that the current time at the call to getData should be used
43 * with a millisecond resolution.
44 */
45 public static final int NOW = 1;
46
47 /*** Specifies that the current date at the call to getData should be used
48 * with all time data (hour or higher resolution) should be set to zero.
49 */
50 public static final int TODAY = 2;
51
52 private static final String NOW_STRING = "NOW";
53 private static final String TODAY_STRING = "TODAY";
54 private static final String MODIFIER_CHARS = "YMDWhmsS";
55 private static final int[] MODIFIER_FIELDS = {
56 Calendar.YEAR,
57 Calendar.MONTH,
58 Calendar.DAY_OF_YEAR,
59 Calendar.WEEK_OF_YEAR,
60 Calendar.HOUR_OF_DAY,
61 Calendar.MINUTE,
62 Calendar.SECOND,
63 Calendar.MILLISECOND };
64 private static final int DEFAULT_MODIFIER = Calendar.DAY_OF_YEAR;
65
66
67 private int mBaseType;
68 private int mModifierType;
69 private int mModifierAmount;
70
71
72
73 /*** Parses a string to a DateProxyData instance. wlaid strings are
74 * "NOW" or "TODAY" followed by an optional modifier in the format
75 * "+/-<amount>[<type_sufix>]". Valid type sufixes are Y(year), M(month),
76 * D(day), W(week), h(hour), m(minute), s(second) or S(millisecond).
77 * If the suffix is omited day type is assumed as the modifier type.
78 * Examples are TODAY+1 = tomorow midnight or NOW-2h = exactly two hours
79 * ago from now.
80 *
81 * @param typeString the String tha tshould be parsed
82 * @return a DateProxyData instance containing the parsed date
83 */
84 public static DateProxyData parse(String typeString)
85 {
86 typeString = typeString.trim();
87
88
89 int type = 0;
90 if(typeString.startsWith(NOW_STRING) || typeString.startsWith(NOW_STRING.toLowerCase()))
91 {
92 type = NOW;
93 typeString = typeString.substring(NOW_STRING.length());
94 }
95 else if(typeString.startsWith(TODAY_STRING) || typeString.startsWith(TODAY_STRING.toLowerCase()))
96 {
97 type = TODAY;
98 typeString = typeString.substring(TODAY_STRING.length());
99 }
100 else
101 return null;
102
103
104 int modifierType = 0;
105 int modifierAmount = 0;
106 if(typeString.length()>0)
107 {
108
109 int modifierIndex = MODIFIER_CHARS.indexOf(typeString.charAt(typeString.length()-1));
110 if(modifierIndex>=0)
111 {
112 modifierType = MODIFIER_FIELDS[modifierIndex];
113 typeString = typeString.substring(0, typeString.length()-1);
114 }
115 else
116 modifierType = DEFAULT_MODIFIER;
117
118
119 try
120 {
121 if(typeString.charAt(0)=='+')
122 typeString = typeString.substring(1);
123 modifierAmount = Integer.parseInt(typeString);
124 }
125 catch(NumberFormatException e)
126 {
127
128 }
129 }
130
131 return new DateProxyData(type, modifierType, modifierAmount);
132 }
133
134 /***
135 * Used to convert an object to a Date Object, either the java.util.Date object or
136 * a DateProxyData object.
137 *
138 * @param data The data representing a Date
139 * @return Object The created data object
140 */
141 public static Object convertToDateObject(Object data)
142 {
143
144 if(data instanceof Date)
145 return data;
146 else if(data instanceof Calendar)
147 return ((Calendar)data).getTime();
148 else if(data instanceof DateProxyData)
149 return data;
150 else if(data!=null)
151 {
152 data = data.toString();
153 try
154 {
155 return DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).parse((String)data);
156 }
157 catch(ParseException e)
158 {
159 DateProxyData proxyData = DateProxyData.parse((String)data);
160 return proxyData;
161 }
162 }
163 else return null;
164 }
165
166
167
168 /*** Creates a new instance of DateProxyData with the type NOW.
169 */
170 public DateProxyData()
171 {
172 this(NOW, 0, 0);
173 }
174
175 /*** Creates a new instance of DateProxyData with the provided type.
176 *
177 * @param type The type to use NOW or TODAY.
178 */
179 public DateProxyData(int type)
180 {
181 this(type, 0, 0);
182 }
183
184 /*** Creates a new instance of DateProxyData with the provided type
185 * and the provided modifier data.
186 *
187 * @param type The type to use NOW or TODAY.
188 * @param modifierType The modifier type shoult be Calendar field constant.
189 * @param modifierAmount The modifier amount is positive or negative integer.
190 */
191 public DateProxyData(int type, int modifierType, int modifierAmount)
192 {
193 mBaseType = type;
194 mModifierType = modifierType;
195 mModifierAmount = modifierAmount;
196 }
197
198
199 public Object getData()
200 {
201
202 Calendar proxyDate = Calendar.getInstance();
203 if(mBaseType==TODAY)
204 {
205 proxyDate.set(Calendar.HOUR_OF_DAY, 0);
206 proxyDate.set(Calendar.MINUTE, 0);
207 proxyDate.set(Calendar.SECOND, 0);
208 proxyDate.set(Calendar.MILLISECOND, 0);
209 }
210
211
212 if(mModifierAmount!=0)
213 proxyDate.add(mModifierType, mModifierAmount);
214
215 return proxyDate.getTime();
216 }
217
218 public DataType getDataType()
219 {
220 return DataType.DATE;
221 }
222
223
224 public int getType()
225 {
226 return mBaseType;
227 }
228
229 public int mModifierType()
230 {
231 return mModifierType;
232 }
233
234 public int mModifierAmount()
235 {
236 return mModifierAmount;
237 }
238 }