1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 package pl.kernelpanic.dbmonster.generator;
48
49 import java.math.BigDecimal;
50 import java.sql.Date;
51 import java.sql.Time;
52 import java.sql.Timestamp;
53 import java.text.SimpleDateFormat;
54 import java.util.Random;
55 import java.util.TimeZone;
56
57 import pl.kernelpanic.dbmonster.DBMonster;
58 import pl.kernelpanic.dbmonster.DBMonsterContext;
59
60 /***
61 * The generator which prouces dates.
62 *
63 * @author Piotr Maj <piotr.maj@kernelpanic.pl>
64 *
65 * @version $Id: DateTimeGenerator.html,v 1.1 2007/06/21 08:38:13 sbahloul Exp $
66 */
67 public class DateTimeGenerator extends BasicDataGenerator implements Initializable {
68
69 /***
70 * Returned type should be date.
71 */
72 public static final int DATE = 0;
73
74 /***
75 * Returned type should be time.
76 */
77 public static final int TIME = 1;
78
79 /***
80 * Returned type should be timestamp.
81 */
82 public static final int TIMESTAMP = 2;
83
84 /***
85 * Minimal value.
86 */
87 private BigDecimal minValue = new BigDecimal("0");
88
89 /***
90 * Maximal value.
91 */
92 private BigDecimal maxValue = new BigDecimal("1423453127");
93
94 /***
95 * Returned time.
96 */
97 private int returnedType = DATE;
98
99 /***
100 * Random number generator.
101 */
102 private Random random = null;
103
104 /***
105 * Date format.
106 */
107 public static final SimpleDateFormat DATE_FORMAT =
108 new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S Z");
109
110 /***
111 * Initializes the generator.
112 *
113 * @param ctx context
114 *
115 * @throws Exception if generator contains errors.
116 *
117 * @todo dopisac validacje zakresow!!!
118 */
119 public final void initialize(final DBMonsterContext ctx) throws Exception {
120 random = (Random) ctx.getProperty(DBMonster.RANDOM_KEY);
121 if (minValue.compareTo(maxValue) > 0) {
122 throw new Exception("MinValue < maxValue");
123 }
124 }
125
126 /***
127 * Generates random date or time.
128 *
129 * @return value.
130 */
131 public final Object generate() {
132
133 if (nulls != 0 && random.nextInt(101) <= nulls) {
134 return null;
135 }
136
137 BigDecimal retValue = null;
138 BigDecimal length = maxValue.subtract(minValue);
139 BigDecimal factor = new BigDecimal(random.nextDouble());
140 retValue = length.multiply(factor).add(minValue);
141 if (returnedType == TIME) {
142 return new Time(retValue.toBigInteger().longValue());
143 } else if (returnedType == TIMESTAMP) {
144 return new Timestamp(retValue.toBigInteger().longValue());
145 } else {
146 return new Date(retValue.toBigInteger().longValue());
147 }
148 }
149
150 /***
151 * Returns start Date.
152 *
153 * @return start date
154 */
155 public final String getStartDate() {
156 return DATE_FORMAT.format(new Date(minValue.longValue()));
157 }
158
159 /***
160 * Sets the start date.
161 *
162 * @param start start date
163 *
164 * @throws Exception on errors
165 */
166 public final void setStartDate(final String start) throws Exception {
167 minValue = new BigDecimal("" + dateToLong(start));
168 }
169
170 /***
171 * Returns the end date.
172 *
173 * @return end date
174 */
175 public final String getEndDate() {
176 return DATE_FORMAT.format(new Date(maxValue.longValue()));
177 }
178
179 /***
180 * Sets the end date.
181 *
182 * @param end end date
183 *
184 * @throws Exception on errors
185 */
186 public final void setEndDate(final String end) throws Exception {
187 maxValue = new BigDecimal("" + dateToLong(end));
188 }
189
190 /***
191 * Returns type.
192 *
193 * @return string representation of returned time
194 */
195 public final String getReturnedType() {
196 if (returnedType == TIME) {
197 return "time";
198 }
199 if (returnedType == TIMESTAMP) {
200 return "timestamp";
201 }
202 return "date";
203 }
204
205 /***
206 * Sets the returned type.
207 *
208 * @param type type
209 */
210 public final void setReturnedType(final String type) {
211 if ("time".equals(type)) {
212 returnedType = TIME;
213 } else if ("timestamp".equals(type)) {
214 returnedType = TIMESTAMP;
215 } else {
216 returnedType = DATE;
217 }
218 }
219
220 /***
221 * Resets the generator.
222 */
223 public final void reset() {
224 }
225
226 /***
227 * Converts date to long.
228 *
229 * @param dateStr date
230 *
231 * @return long
232 *
233 * @throws Exception exception
234 */
235 private final long dateToLong(final String dateStr) throws Exception {
236 String date = dateStr;
237 if (date == null) {
238 return 0L;
239 }
240 if (date.length() == 10) {
241 date += " 00:00:00";
242 }
243 if (date.length() == 16) {
244 date += ":00";
245 }
246 if (date.length() == 19) {
247 date += ".000";
248 }
249 if (date.length() == 23) {
250 date += " " + TimeZone.getDefault().getDisplayName();
251 }
252
253
254 java.util.Date d = DATE_FORMAT.parse(date);
255 return d.getTime();
256 }
257 }