KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > utils > TypeValidityHandler


1 package com.daffodilwoods.daffodildb.utils;
2
3 import com.daffodilwoods.daffodildb.server.datasystem.interfaces.Datatype;
4 import com.daffodilwoods.database.resource.DException;
5 import com.daffodilwoods.daffodildb.utils.comparator.*;
6 import com.daffodilwoods.daffodildb.utils.byteconverter.CCzufDpowfsufs;
7 import com.daffodilwoods.daffodildb.utils.field.*;
8 import java.util.GregorianCalendar JavaDoc;
9 import java.util.Calendar JavaDoc;
10 import java.util.Date JavaDoc;
11 import java.text.ParseException JavaDoc;
12 import java.text.ParsePosition JavaDoc;
13 import java.sql.Time JavaDoc;
14 import java.sql.Timestamp JavaDoc;
15
16 /**
17  *
18  * <p>Title: TypeValidityHandler</p>
19  * <p>Description: This class handles the validation of different data type values. </p>
20  * <p>Copyright: Copyright (c) 2004</p>
21  * <p>Company: </p>
22  * @author not attributable
23  * @version 1.0
24  */

25 public class TypeValidityHandler {
26   static final FieldBase byte_Min = new FieldByte(new BufferRange(CCzufDpowfsufs.
27       getBytes(new Byte JavaDoc(Byte.MIN_VALUE))), Datatype.BYTE);
28   static final FieldBase byte_Max = new FieldByte(new BufferRange(CCzufDpowfsufs.
29       getBytes(new Byte JavaDoc(Byte.MAX_VALUE))), Datatype.BYTE);
30   static final FieldBase short_Min = new FieldShort(new BufferRange(
31       CCzufDpowfsufs.getBytes(new Short JavaDoc(Short.MIN_VALUE))), Datatype.SHORT);
32   static final FieldBase short_Max = new FieldShort(new BufferRange(
33       CCzufDpowfsufs.getBytes(new Short JavaDoc(Short.MAX_VALUE))), Datatype.SHORT);
34   static final FieldBase integer_Min = new FieldInteger(new BufferRange(
35       CCzufDpowfsufs.getBytes(new Integer JavaDoc(Integer.MIN_VALUE))), Datatype.INTEGER);
36   static final FieldBase integer_Max = new FieldInteger(new BufferRange(
37       CCzufDpowfsufs.getBytes(new Integer JavaDoc(Integer.MAX_VALUE))), Datatype.INTEGER);
38   static final FieldBase long_Min = new FieldLong(new BufferRange(CCzufDpowfsufs.
39       getBytes(new Long JavaDoc(Long.MIN_VALUE))), Datatype.LONG);
40   static final FieldBase long_Max = new FieldLong(new BufferRange(CCzufDpowfsufs.
41       getBytes(new Long JavaDoc(Long.MAX_VALUE))), Datatype.LONG);
42   static final FieldBase double_Min = new FieldDouble(new BufferRange(
43       CCzufDpowfsufs.getBytes(new Double JavaDoc(Double.NEGATIVE_INFINITY))),
44       Datatype.DOUBLE);
45   static final FieldBase double_Max = new FieldDouble(new BufferRange(
46       CCzufDpowfsufs.getBytes(new Double JavaDoc(Double.POSITIVE_INFINITY))),
47       Datatype.DOUBLE);
48   static final FieldBase float_Min = new FieldReal(new BufferRange(
49       CCzufDpowfsufs.getBytes(new Float JavaDoc(Float.NEGATIVE_INFINITY))),
50       Datatype.REAL);
51   static final FieldBase float_Max = new FieldReal(new BufferRange(
52       CCzufDpowfsufs.getBytes(new Float JavaDoc(Float.POSITIVE_INFINITY))),
53       Datatype.REAL);
54
55   static COpoEfdjnbmOpoEfdjnbmDpnqbsbups nonDecimalNonDecimalComparator = new
56       COpoEfdjnbmOpoEfdjnbmDpnqbsbups();
57   static CTbnfUzqfDpnqbsbups sameComparator = new CTbnfUzqfDpnqbsbups();
58   static CPckfduDpnqbsbups objectComparator = new CPckfduDpnqbsbups();
59   static CGmpbuOpoEfdjnbmDpnqbsbups floatNonDecimalComparator = new
60       CGmpbuOpoEfdjnbmDpnqbsbups();
61   static CEpvcmfOpoEfdjnbmDpnqbsbups doubleNonDecimalComparator = new
62       CEpvcmfOpoEfdjnbmDpnqbsbups();
63   static CEpvcmfGmpbuDpnqbsbups doubleFloatComparator = new
64       CEpvcmfGmpbuDpnqbsbups();
65   static CSfwfstfDpnqbsbups floatDoubleComparator = new CSfwfstfDpnqbsbups(
66       doubleFloatComparator);
67   static CSfwfstfDpnqbsbups nonDecimalFloatComparator = new CSfwfstfDpnqbsbups(
68       floatNonDecimalComparator);
69   static CSfwfstfDpnqbsbups nonDecimalDoubleComparator = new CSfwfstfDpnqbsbups(
70       doubleNonDecimalComparator);
71
72   static java.text.SimpleDateFormat JavaDoc dateFormat = new java.text.SimpleDateFormat JavaDoc(
73       "yyyy-M-d");
74   static java.text.SimpleDateFormat JavaDoc timeFormat = new java.text.SimpleDateFormat JavaDoc(
75       "H:m:s");
76   static java.text.SimpleDateFormat JavaDoc timestampFormat = new java.text.
77
JavaDoc      SimpleDateFormat("yyyy-M-d H:m:s.S");
78   static java.text.SimpleDateFormat JavaDoc timestampFormat1 = new java.text.
79
JavaDoc      SimpleDateFormat("yyyy-M-d H:m:s");
80
81   static final Calendar JavaDoc cal = new GregorianCalendar JavaDoc();
82
83   static {
84     dateFormat.setLenient(false);
85     timeFormat.setLenient(false);
86     timestampFormat.setLenient(false);
87     timestampFormat1.setLenient(false);
88   }
89
90   public static void checkByteWithNonDecimal(FieldBase object) throws
91       DException {
92     if (nonDecimalNonDecimalComparator.compare( (_DComparator) byte_Min,
93                                                (_DComparator) object) > 0
94         ||
95         nonDecimalNonDecimalComparator.compare( (_DComparator) object,
96                                                (_DComparator) byte_Max) > 0)
97       throw new DException("DSE5545", new Object JavaDoc[] {object.getObject(), "Byte"});
98   }
99
100   public static void checkByteWithDouble(FieldBase object) throws DException {
101     if (nonDecimalDoubleComparator.compare( (_DComparator) byte_Min,
102                                            (_DComparator) object) > 0
103         ||
104         doubleNonDecimalComparator.compare( (_DComparator) object,
105                                            (_DComparator) byte_Max) > 0)
106       throw new DException("DSE5545", new Object JavaDoc[] {object.getObject(), "Byte"});
107   }
108
109   public static void checkByteWithFloat(FieldBase object) throws DException {
110     if (nonDecimalFloatComparator.compare( (_DComparator) byte_Min,
111                                           (_DComparator) object) > 0
112         ||
113         floatNonDecimalComparator.compare( (_DComparator) object,
114                                           (_DComparator) byte_Max) > 0)
115       throw new DException("DSE5545", new Object JavaDoc[] {object.getObject(), "Byte"});
116   }
117
118   public static void checkByteWithBigDecimal(FieldBase object) throws
119       DException {
120     if (objectComparator.compare( (_DComparator) byte_Min,
121                                  (_DComparator) object) > 0
122         ||
123         objectComparator.compare( (_DComparator) object, (_DComparator) byte_Max) >
124         0)
125       throw new DException("DSE5545", new Object JavaDoc[] {object.getObject(), "Byte"});
126   }
127
128   public static void checkShortWithNonDecimal(FieldBase object) throws
129       DException {
130     if (nonDecimalNonDecimalComparator.compare( (_DComparator) short_Min,
131                                                (_DComparator) object) > 0
132         ||
133         nonDecimalNonDecimalComparator.compare( (_DComparator) object,
134                                                (_DComparator) short_Max) > 0)
135       throw new DException("DSE5545", new Object JavaDoc[] {object.getObject(), "Short"});
136   }
137
138   public static void checkShortWithDouble(FieldBase object) throws DException {
139     if (nonDecimalDoubleComparator.compare( (_DComparator) short_Min,
140                                            (_DComparator) object) > 0
141         ||
142         doubleNonDecimalComparator.compare( (_DComparator) object,
143                                            (_DComparator) short_Max) > 0)
144       throw new DException("DSE5545", new Object JavaDoc[] {object.getObject(), "Short"});
145   }
146
147   public static void checkShortWithFloat(FieldBase object) throws DException {
148     if (nonDecimalFloatComparator.compare( (_DComparator) short_Min,
149                                           (_DComparator) object) > 0
150         ||
151         floatNonDecimalComparator.compare( (_DComparator) object,
152                                           (_DComparator) short_Max) > 0)
153       throw new DException("DSE5545", new Object JavaDoc[] {object.getObject(), "Short"});
154   }
155
156   public static void checkShortWithBigDecimal(FieldBase object) throws
157       DException {
158     if (objectComparator.compare( (_DComparator) short_Min,
159                                  (_DComparator) object) > 0
160         ||
161         objectComparator.compare( (_DComparator) object, (_DComparator) short_Max) >
162         0)
163       throw new DException("DSE5545", new Object JavaDoc[] {object.getObject(), "Short"});
164   }
165
166   public static void checkIntegerWithNonDecimal(FieldBase object) throws
167       DException {
168     if (nonDecimalNonDecimalComparator.compare( (_DComparator) integer_Min,
169                                                object) > 0
170         ||
171         nonDecimalNonDecimalComparator.compare( (_DComparator) object,
172                                                (_DComparator) integer_Max) > 0)
173       throw new DException("DSE5545", new Object JavaDoc[] {object.getObject(),
174                            "Integer"});
175   }
176
177   public static void checkIntegerWithDouble(FieldBase object) throws DException {
178     if (nonDecimalDoubleComparator.compare( (_DComparator) integer_Min,
179                                            (_DComparator) object) > 0
180         ||
181         doubleNonDecimalComparator.compare( (_DComparator) object,
182                                            (_DComparator) integer_Max) > 0)
183       throw new DException("DSE5545", new Object JavaDoc[] {object.getObject(),
184                            "Integer"});
185   }
186
187   public static void checkIntegerWithFloat(FieldBase object) throws DException {
188     if (nonDecimalFloatComparator.compare( (_DComparator) integer_Min,
189                                           (_DComparator) object) > 0
190         ||
191         floatNonDecimalComparator.compare( (_DComparator) object,
192                                           (_DComparator) integer_Max) > 0)
193       throw new DException("DSE5545", new Object JavaDoc[] {object.getObject(),
194                            "Integer"});
195   }
196
197   public static void checkIntegerWithBigDecimal(FieldBase object) throws
198       DException {
199     if (objectComparator.compare( (_DComparator) integer_Min,
200                                  (_DComparator) object) > 0
201         ||
202         objectComparator.compare( (_DComparator) object, (_DComparator) integer_Max) >
203         0)
204       throw new DException("DSE5545", new Object JavaDoc[] {object.getObject(),
205                            "Integer"});
206   }
207
208   public static void checkLongWithNonDecimal(FieldBase object) throws
209       DException {
210     if (nonDecimalNonDecimalComparator.compare( (_DComparator) long_Min,
211                                                (_DComparator) object) > 0
212         ||
213         nonDecimalNonDecimalComparator.compare( (_DComparator) object,
214                                                (_DComparator) long_Max) > 0)
215       throw new DException("DSE5545", new Object JavaDoc[] {object.getObject(), "Long"});
216   }
217
218   public static void checkLongWithDouble(FieldBase object) throws DException {
219     if (nonDecimalDoubleComparator.compare( (_DComparator) long_Min,
220                                            (_DComparator) object) > 0
221         ||
222         doubleNonDecimalComparator.compare( (_DComparator) object,
223                                            (_DComparator) long_Max) > 0)
224       throw new DException("DSE5545", new Object JavaDoc[] {object.getObject(), "Long"});
225   }
226
227   public static void checkLongWithFloat(FieldBase object) throws DException {
228     if (nonDecimalFloatComparator.compare( (_DComparator) long_Min,
229                                           (_DComparator) object) > 0
230         ||
231         floatNonDecimalComparator.compare( (_DComparator) object,
232                                           (_DComparator) long_Max) > 0)
233       throw new DException("DSE5545", new Object JavaDoc[] {object.getObject(), "Long"});
234   }
235
236   public static void checkLongWithBigDecimal(FieldBase object) throws
237       DException {
238     if (objectComparator.compare( (_DComparator) long_Min,
239                                  (_DComparator) object) > 0
240         ||
241         objectComparator.compare( (_DComparator) object, (_DComparator) long_Max) >
242         0)
243       throw new DException("DSE5545", new Object JavaDoc[] {object.getObject(), "Long"});
244   }
245
246   public static void checkDoubleWithFloat(FieldBase object) throws DException {
247     if (doubleFloatComparator.compare( (_DComparator) double_Min,
248                                       (_DComparator) object) > 0
249         ||
250         floatDoubleComparator.compare( (_DComparator) object, (_DComparator) double_Max) >
251         0)
252       throw new DException("DSE5545", new Object JavaDoc[] {object.getObject(),
253                            "Double"});
254   }
255
256   public static void checkDoubleWithBigDecimal(FieldBase object) throws
257       DException {
258     if (objectComparator.compare( (_DComparator) double_Min,
259                                  (_DComparator) object) > 0
260         ||
261         objectComparator.compare( (_DComparator) object, (_DComparator) double_Max) >
262         0) {
263       throw new DException("DSE5545", new Object JavaDoc[] {object.getObject(),
264                            "Double"});
265     }
266   }
267
268   public static void checkFloatWithDouble(FieldBase object) throws DException {
269     if (floatDoubleComparator.compare( (_DComparator) float_Min,
270                                       (_DComparator) object) > 0
271         ||
272         doubleFloatComparator.compare( (_DComparator) object, (_DComparator) float_Max) >
273         0)
274       throw new DException("DSE5545", new Object JavaDoc[] {object.getObject(), "Float"});
275   }
276
277   public static void checkFloatWithBigDecimal(FieldBase object) throws
278       DException {
279     if (objectComparator.compare( (_DComparator) float_Min,
280                                  (_DComparator) object) > 0
281         ||
282         objectComparator.compare( (_DComparator) object, (_DComparator) float_Max) >
283         0)
284       throw new DException("DSE5545", new Object JavaDoc[] {object.getObject(), "Float"});
285   }
286
287   public static DBDate getDBDate(Timestamp JavaDoc timestamp) throws DException {
288     cal.setTime(timestamp);
289     cal.set(Calendar.HOUR, 0);
290     cal.set(Calendar.MINUTE, 0);
291     cal.set(Calendar.SECOND, 0);
292     return new DBDate(cal.getTime().getTime());
293   }
294
295   public static DBDate getDBDateFromDateTimestamp(String JavaDoc dateTimestamp) throws
296       DException {
297     checkYearFromDate(dateTimestamp);
298     try {
299       return new DBDate(getParsedDate(dateTimestamp).getTime());
300     }
301     catch (DException ex1) {
302       try {
303         return getDBDate(getTimestamp(dateTimestamp));
304       }
305       catch (DException ex2) {
306         throw new DException("DSE8024", null);
307       }
308     }
309   }
310
311   public static Time JavaDoc getTime(Timestamp JavaDoc timestamp) throws DException {
312     cal.setTime(timestamp);
313     cal.set(Calendar.YEAR, 0);
314     cal.set(Calendar.MONTH, 0);
315     cal.set(Calendar.DATE, 0);
316     return new Time JavaDoc(cal.getTime().getTime());
317   }
318
319   public static Time JavaDoc getTimeFromTimeTimeStamp(String JavaDoc timeTimestamp) throws
320       DException {
321     try {
322       return getTime(timeTimestamp);
323     }
324     catch (DException ex1) {
325       try {
326         return getTime(getTimestamp(timeTimestamp));
327       }
328       catch (DException ex2) {
329         throw new DException("DSE8023", null);
330       }
331     }
332   }
333
334   public static Timestamp JavaDoc getTimeStampFromTimestampDate(String JavaDoc dateTimestamp) throws
335       DException {
336     checkYearFromTimestamp(dateTimestamp);
337     if (dateTimestamp.indexOf(".") != -1) {
338       if (dateTimestamp.length() > 23) {
339         throw new DException("DSE8022", null);
340       }
341       try {
342         return getJDKCompatibleTimestamp(timestampFormat.parse(dateTimestamp).
343                                          getTime());
344       }
345       catch (ParseException JavaDoc ex) {
346         throw new DException("DSE8022", null);
347       }
348     }
349    else {
350       /** This check is added by vibha as suggested by Neeraj sir
351        Date should not be greater than 19 as done in getTimeStamp() method **/

352       if (dateTimestamp.length() > 19) {
353         throw new DException("DSE8022", null);
354       }
355
356       try {
357         return getJDKCompatibleTimestamp(timestampFormat1.parse(dateTimestamp).
358                                          getTime());
359       }
360       catch (ParseException JavaDoc ex1) {
361         try {
362           Timestamp JavaDoc ts = getJDKCompatibleTimestamp(getParsedDate(dateTimestamp).
363               getTime());
364           return ts;
365         }
366         catch (DException ex) {
367           throw new DException("DSE8022", null);
368         }
369       }
370     }
371   }
372
373   /**
374    * This method is responsible to parse the date.
375    * @param dateValue
376    * @return date
377    * @throws DException throws exception if the date is not fully parsed
378    */

379   private static Date JavaDoc getParsedDate(String JavaDoc dateValue) throws DException {
380     try {
381       ParsePosition JavaDoc pp = new ParsePosition JavaDoc(0);
382       Date JavaDoc date = dateFormat.parse(dateValue, pp);
383       if (pp.getIndex() != dateValue.length())
384         throw new DException("DSE8024", null);
385       return date;
386     }
387     catch (Exception JavaDoc ex2) {
388       throw new DException("DSE8024", null);
389     }
390   }
391
392   public static Timestamp JavaDoc getTimestamp(String JavaDoc timestamp) throws DException {
393     checkYearFromTimestamp(timestamp);
394     try {
395       if (timestamp.indexOf(".") != -1) {
396         if (timestamp.length() > 23) {
397           throw new DException("DSE8022", null);
398         }
399         return getJDKCompatibleTimestamp(timestampFormat.parse(timestamp).
400                                          getTime());
401       }
402       else {
403         if (timestamp.length() > 19) {
404           throw new DException("DSE8022", null);
405         }
406         return getJDKCompatibleTimestamp(timestampFormat1.parse(timestamp).
407                                          getTime());
408       }
409     }
410     catch (ParseException JavaDoc ex1) {
411       throw new DException("DSE8022", null);
412     }
413   }
414
415   /**
416    * This method is responsible to perform specific testing for timestamp
417    * depending on jvm version. This is done to recover the problem faced with
418    * prior versions of jvm to jdk1.4.
419    * @param timestampWithNanoSeconds
420    * @return correct value of timestamp
421    * @throws DException
422    */

423   private static Timestamp JavaDoc getJDKCompatibleTimestamp(long
424       timestampWithNanoSeconds) throws DException {
425     Timestamp JavaDoc ts = new Timestamp JavaDoc(timestampWithNanoSeconds);
426     String JavaDoc javaVersion = System.getProperty("java.runtime.version").substring(0,
427         3);
428     try {
429       float version = Float.parseFloat(javaVersion);
430       if (version >= 1.4f)
431         return ts;
432       DTimeStamp dtimestamp = new DTimeStamp(ts.getTime() +
433                                              ts.getNanos() / 1000000);
434       return dtimestamp;
435     }
436     catch (NumberFormatException JavaDoc ex) {
437       return ts;
438     }
439   }
440
441   /*public static Timestamp getTimestamp(String timestamp) throws DException {
442     checkYearFromTimestamp(timestamp);
443     try {
444       return new Timestamp(timestampFormat.parse(timestamp).getTime());
445     }
446     catch (ParseException ex1) {
447       try {
448         return new Timestamp(timestampFormat1.parse(timestamp).getTime());
449       }
450       catch (ParseException ex2) {
451         throw new DException("DSE8022",null);
452       }
453     }
454        }*/

455
456   public static Time JavaDoc getTime(String JavaDoc time) throws DException {
457     try {
458       return new Time JavaDoc(timeFormat.parse(time).getTime());
459     }
460     catch (ParseException JavaDoc ex) {
461       throw new DException("DSE8023", null);
462     }
463   }
464
465   public static DBDate getDBDate(String JavaDoc date) throws DException {
466     checkYearFromDate(date);
467     return new DBDate(getParsedDate(date).getTime());
468   }
469
470   /**
471    * Maximum year supported is of 8 digits. This method performs verification
472    * w.r.t year must not be greater than 8 digits.
473    * @param date date string
474    * @throws DException
475    */

476   private static void checkYearFromDate(String JavaDoc date) throws DException {
477     if (date.indexOf("-") > 8 || date.equalsIgnoreCase(""))
478       throw new DException("DSE8024", null);
479   }
480
481   /**
482    * Maximum year supported is of 8 digits. This method performs verification
483    * w.r.t year must not be greater than 8 digits.
484    * @param timestamp timestamp value
485    * @throws DException
486    */

487   private static void checkYearFromTimestamp(String JavaDoc timestamp) throws
488       DException {
489     if (timestamp.indexOf("-") > 8 || timestamp.equalsIgnoreCase(""))
490       throw new DException("DSE8022", null);
491   }
492
493   /*public static void main(String[] args) {
494     try {
495     }
496     catch (Exception ex) {
497     }
498        }*/

499 }
500
Popular Tags