Revision 2080 org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/CoerceToTimestamp.java

View differences:

CoerceToTimestamp.java
24 24
package org.gvsig.tools.dataTypes.impl.coercion;
25 25

  
26 26
import java.sql.Timestamp;
27

  
27
import java.util.Date;
28 28
import org.gvsig.tools.dataTypes.CoercionException;
29
import org.gvsig.tools.dataTypes.DataTypesManager.Coercion;
29
import org.gvsig.tools.dataTypes.CoercionContext;
30 30

  
31
public class CoerceToTimestamp extends CoerceToDateTime {
31 32

  
32
/**
33
 * Utility class to force instantiation of Timestamp objects.
34
 * If a plugin provides a datasource with a timestamp type which
35
 * is not java.sql.Timestamp, then the plugin will have
36
 * to perform a conversion or coercion between
37
 * its Timestamp type and java.sql.Timestamp if it's not automatic.
38
 * 
39
 * Postgresql JDBC uses java.sql.Timestamp, so no problem
40
 * 
41
 * Oracle uses oracle.sql.TIMESTAMP which is automatically
42
 * converted, so this coercion is enough
43
 * 
44
 * @author jldominguez
45
 *
46
 */
47
public class CoerceToTimestamp extends CoerceToTime {
33
  @Override
34
  protected String getDateType() {
35
    return "Timestamp";
36
  }
48 37

  
49
    public static final String NOW_STRING = "now";
50
    
51
    public Object coerce(Object value) throws CoercionException {
52
        if (value instanceof Timestamp) {
53
            return value;
54
        } else {
55
            String str = value.toString();
56
            if( str == null ) {
57
                return null;
58
            }
59
            str = str.trim().toLowerCase();
60
            if( str.length()==0 ) {
61
                return null;
62
            }
63
            // ----------- (1) Try normal parsing
64
            try {
65
                return Timestamp.valueOf(str); 
66
            } catch (Exception ex) {
67
                // Pass exception
68
            }
69
            // ----------- (2) Try parsing milliseconds
70
            try {
71
                long ms = Long.parseLong(str);
72
                return new Timestamp(ms); 
73
            } catch (Exception ex) {
74
                // Pass exception
75
            }
76
            // ----------- (3) Accept "now"
77
            if (NOW_STRING.equalsIgnoreCase(str) ) {                
78
                return new Timestamp(System.currentTimeMillis());
79
            }
80
            /*
81
             * Finally try as time
82
             */
83
            return super.coerce(value);
84
        }
38
  @Override
39
  protected Date now() {
40
    Timestamp n = new Timestamp(System.currentTimeMillis());
41
    return n;
42
  }
43

  
44
  @Override
45
  protected Date valueOf(String value) {
46
    Timestamp n = Timestamp.valueOf(value);
47
    return n;
48
  }
49

  
50
  @Override
51
  public Object coerce(Object value, CoercionContext context) throws CoercionException {
52
    if (value == null) {
53
      return null;
85 54
    }
55
    if (value instanceof Date) {
56
      Timestamp n = new Timestamp(((Date) value).getTime());
57
      return n;
58
    }
59
    return super.coerce(value, context);
60
  }
86 61

  
62
//
63
//    @Override
64
//    protected DateFormat[] getFormatters(Locale locale) {
65
//        return new DateFormat[] {
66
//            DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.MEDIUM, locale),
67
//            DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT, locale),
68
//            DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.LONG, locale),
69
//            new SimpleDateFormat("yyyyMMddHHmmss"), // DBF format
70
//            new SimpleDateFormat("yyyyMMddHHmmss") ?nanoseg?? // DBF format
71
//        };
72
//    }
73
//    
87 74
}

Also available in: Unified diff