View Javadoc

1   /* Version 1.0 based on Apache Software License 1.1
2    *
3    * Copyright (c) 2003 Piotr Maj and DBMonster developers. All rights
4    * reserved.
5    *
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions are
8    * met:
9    *
10   * 1. Redistributions of source code must retain the above copyright
11   *    notice, this list of conditions and the following disclaimer.
12   *
13   * 2. Redistributions in binary form must reproduce the above copyright
14   *    notice, this list of conditions and the following disclaimer in the
15   *    documentation and/or other materials provided with the distribution.
16   *
17   * 3. The end-user documentation included with the redistribution, if any,
18   *    must include the following acknowledgment:
19   *
20   *    "This product includes software developed by DBMonster developers
21   *    (http://dbmonster.kernelpanic.pl/)."
22   *
23   *  Alternately, this acknowledgment may appear in the software itself,
24   *  if and wherever such third-party acknowledgments normally appear.
25   *
26   * 4. The name "DBMonster" must not be used to endorse or promote products
27   *    derived from this software without prior written permission. For
28   *    written permission, please contact piotr.maj@kernelpanic.pl.
29   *
30   * 5. Products derived from this software may not be called "DBMonster",
31   *    nor may "DBMonster" appear in their name, without prior written
32   *    permission of Piotr Maj.
33   *
34   * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
35   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
37   * IN NO EVENT SHALL THE DBMONSTER DEVELOPERS BE LIABLE FOR ANY DIRECT,
38   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
39   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
40   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
42   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
43   * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
44   * POSSIBILITY OF SUCH DAMAGE.
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 }