001: package com.bm.testsuite.dataloader;
002:
003: import java.sql.PreparedStatement;
004: import java.sql.SQLException;
005: import java.text.DateFormat;
006: import java.text.ParseException;
007: import java.text.SimpleDateFormat;
008: import java.util.Date;
009:
010: /**
011: * Represents date formats for CVS initial data set.
012: * @author Daniel Wiese
013: * @author Peter Doornbosch
014: *
015: */
016: public enum DateFormats {
017:
018: DEFAULT_DATE_TIME(false, DateFormat.getDateTimeInstance(),
019: SQLTypes.SQL_TIMESTAMP),
020:
021: DEFAULT_DATE(false, DateFormat.getDateInstance(), SQLTypes.SQL_DATE),
022:
023: DEFAULT_TIME(false, DateFormat.getDateTimeInstance(),
024: SQLTypes.SQL_TIME),
025:
026: USER_DATE_TIME(true, null, SQLTypes.SQL_TIMESTAMP),
027:
028: USER_DATE(true, null, SQLTypes.SQL_DATE),
029:
030: USER_TIME(true, null, SQLTypes.SQL_TIME);
031:
032: private DateFormat dateFormatter;
033: private final SQLTypes sqlType;
034: private boolean userDefined;
035:
036: private DateFormats(boolean userDefined, DateFormat dateFormatter,
037: SQLTypes sqlType) {
038: this .userDefined = userDefined;
039: this .dateFormatter = dateFormatter;
040: this .sqlType = sqlType;
041:
042: }
043:
044: /**
045: * Allows to set a user defined formatter.
046: *
047: * @param dateFormatter -
048: * the date formatter
049: * @return this intance for inlining
050: */
051: public DateFormats setUserDefinedFomatter(String pattern) {
052: if (this .userDefined) {
053: this .dateFormatter = new SimpleDateFormat(pattern);
054: } else {
055: throw new IllegalArgumentException(
056: "Only allowed for user defined values");
057: }
058:
059: return this ;
060: }
061:
062: /**
063: * Parse the specified instance
064: *
065: * @param toParse
066: * @return
067: * @throws ParseException
068: */
069: public Date parse(String toParse) throws ParseException {
070: if (dateFormatter == null) {
071: throw new IllegalArgumentException(
072: "Please define your format pattern");
073: }
074: return this .dateFormatter.parse(toParse);
075: }
076:
077: public void parseToPreparedStatemnt(String toParse,
078: PreparedStatement ps, int pos) throws ParseException,
079: SQLException {
080: Date dateValue = parse(toParse);
081: switch (sqlType) {
082: case SQL_TIMESTAMP:
083: ps.setTimestamp(pos, new java.sql.Timestamp(dateValue
084: .getTime()));
085: break;
086: case SQL_DATE:
087: ps.setDate(pos, new java.sql.Date(dateValue.getTime()));
088: break;
089: case SQL_TIME:
090: ps.setTime(pos, new java.sql.Time(dateValue.getTime()));
091: break;
092: default:
093: throw new IllegalArgumentException(
094: "No handling specified foer this SQL type ("
095: + sqlType + ")");
096:
097: }
098: }
099:
100: /**
101: * Returns the pattern for formatting.
102: *
103: * @return th epattern
104: */
105: public String toPattern() {
106: if (this .dateFormatter instanceof SimpleDateFormat) {
107: return ((SimpleDateFormat) this .dateFormatter).toPattern();
108: } else {
109: return "Unknown pattern";
110: }
111: }
112:
113: public static DateFormats[] systemValues() {
114: return new DateFormats[] { DEFAULT_DATE, DEFAULT_DATE_TIME,
115: DEFAULT_TIME };
116: }
117: }
|