Revision 2160

View differences:

org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/DefaultDataTypesManager.java
69 69
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToByte;
70 70
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToBytearray;
71 71
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToDate;
72
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToDateTime;
73 72
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToDouble;
74 73
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToFile;
75 74
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToFloat;
......
79 78
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToObject;
80 79
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToString;
81 80
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToTime;
81
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToTimestamp;
82 82
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToURI;
83 83
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToURL;
84 84
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToVersion;
......
137 137
    );
138 138
    this.addtype(DATE, SUBTYPE_DATE, "Date", Date.class, new CoerceToDate(), "datatype-date");
139 139
    this.addtype(TIME, SUBTYPE_DATE, "Time", Date.class, new CoerceToTime(), "datatype-time");
140
//        this.addtype(TIMESTAMP, null, "Timestamp", Timestamp.class, new CoerceToTimestamp(),"datatype-timestamp");
141
    this.addtype(TIMESTAMP, null, "Timestamp", Date.class, new CoerceToDateTime(), "datatype-timestamp");
140
    this.addtype(TIMESTAMP, SUBTYPE_DATE, "Timestamp", Date.class, new CoerceToTimestamp(), "datatype-timestamp");
142 141

  
143 142
    this.addtype(LOCALE, null, "Locale", Locale.class, new CoerceToLocale(), 
144 143
            "datatype-locale"
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/CoerceToDateTime.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.tools.dataTypes.impl.coercion;
25

  
26
import java.text.DateFormat;
27
import java.text.SimpleDateFormat;
28
import java.util.Date;
29
import java.util.Locale;
30
import org.gvsig.tools.dataTypes.CoercionException;
31
import org.gvsig.tools.dataTypes.CoercionContext;
32

  
33
/**
34
 * Coerces a value to a {@link Date}. If the value is not a {@link Date}, it
35
 * will use the {@link Object#toString()} method to convert the
36
 * resulting {@link String} to a Date object using the current locale default
37
 * formatter for DateTime through the
38
 * {@link DateFormat#getDateTimeInstance(int)} method
39
 * and the {@link DateFormat#SHORT} style for the date and the
40
 * {@link DateFormat#MEDIUM} style for the hour.
41
 * 
42
 * @author gvSIG Team
43
 * @version $Id$
44
 */
45
public class CoerceToDateTime extends AbstractCoerceToDate {
46

  
47
    @Override
48
    protected DateFormat[] getFormatters(Locale locale) {
49
        return new DateFormat[] {
50
            DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.MEDIUM, locale),
51
            DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT, locale),
52
            DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.LONG, locale),
53
            new SimpleDateFormat("yyyyMMddHHmmss") // DBF format
54
        };
55
    }
56
    
57
    @Override
58
    protected Date now() {
59
        Date d = new Date();
60
        return d;
61
    }
62
   
63
    @Override
64
    protected String getDateType() {
65
        return "DateTime or Timestamp";
66
    }
67

  
68
  @Override
69
  public Object coerce(Object value, CoercionContext context) throws CoercionException {
70
    if( value == null || value instanceof Date ) {
71
      return value;
72
    }
73
    return super.coerce(value, context);
74
  }
75

  
76
}
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/CoerceToDate.java
58 58
  }
59 59

  
60 60
  @Override
61
  protected Date now() {
62
    Date d = new Date();
63
    d.setHours(0);
64
    d.setMinutes(0);
65
    d.setSeconds(0);
66
    return d;
61
  protected Date valueOf(Date value) {
62
    java.sql.Date dd = new java.sql.Date(value.getTime());
63
    return dd;
67 64
  }
68 65

  
69 66
  @Override
......
71 68
    return "Date";
72 69
  }
73 70

  
74
  @Override
75
  public Object coerce(Object value, CoercionContext context) throws CoercionException {
76
    if( value == null ) {
77
      return null;
78
    }
79
    if (value instanceof Date) {
80
      Date d = (Date) value;
81
      Date n = new Date(d.getYear(), d.getMonth(), d.getDate(), 0, 0, 0);
82
      return n;
83
    }
84
    return super.coerce(value, context);
85
  }
86

  
87 71
}
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/CoerceToTime.java
27 27
import java.text.SimpleDateFormat;
28 28
import java.util.Date;
29 29
import java.util.Locale;
30
import org.gvsig.tools.dataTypes.CoercionException;
31
import org.gvsig.tools.dataTypes.CoercionContext;
32 30

  
33 31
/**
34 32
 * Coerces a value to a {@link Date}. If the value is not a {@link Date}, it
......
55 53
  }
56 54

  
57 55
  @Override
58
  protected Date now() {
59
    Date d = new Date();
60
    d.setYear(0);
61
    d.setMonth(0);
62
    d.setDate(0);
63
    return d;
56
  protected Date valueOf(Date value) {
57
    java.sql.Time dd = new java.sql.Time(value.getTime());
58
    return dd;
64 59
  }
65 60

  
66 61
  @Override
......
68 63
    return "Time";
69 64
  }
70 65

  
71
  @Override
72
  public Object coerce(Object value, CoercionContext context) throws CoercionException {
73
    if( value == null ) {
74
      return null;
75
    }
76
    if (value instanceof Date) {
77
      Date d = (Date) value;
78
      Date n = new Date(0, 0, 0, d.getHours(), d.getMinutes(), d.getSeconds());
79
      return n;
80
    }
81
    return super.coerce(value, context);
82
  }
83

  
84 66
}
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/AbstractCoerceToDate.java
32 32

  
33 33
import org.gvsig.tools.dataTypes.CoercionException;
34 34
import org.gvsig.tools.dataTypes.DataTypeUtils;
35
import org.gvsig.tools.dataTypes.DataTypesManager;
36 35
import org.gvsig.tools.dataTypes.CoercionContext;
37 36
import org.gvsig.tools.dataTypes.CoercionContextLocale;
38 37

  
......
53 52

  
54 53
  @Override
55 54
  public Object coerce(Object value, CoercionContext context) throws CoercionException {
55
    if( value == null ) {
56
      return null;
57
    }
58
    if (value instanceof Date) {
59
      return this.valueOf(((Date) value));
60
    }
56 61
    Locale locale;
57 62
    if (context instanceof CoercionContextLocale) {
58 63
      locale = ((CoercionContextLocale) context).locale();
......
90 95
      try {
91 96
        Date d = formatter.parse(valueStr);
92 97
        if (d != null) {
93
          return d;
98
          return this.valueOf(d);
94 99
        }
95 100
      } catch (ParseException e) {
96 101
        // Continue
......
128 133
  protected abstract DateFormat[] getFormatters(Locale locale);
129 134

  
130 135
  protected Date now() {
131
    Date d = new Date();
136
    Date d = this.valueOf(new Date());
132 137
    return d;
133 138
  }
134 139

  
......
136 141
    return null;
137 142
  }
138 143

  
144
  protected Date valueOf(Date value) {
145
    return value;
146
  }
147

  
139 148
  /**
140 149
   * Returns the name of the Date type being coerced. Ex: Date, DateTime, Time.
141 150
   * Only used for description when an error is produced when coercing the
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/CoerceToTimestamp.java
24 24
package org.gvsig.tools.dataTypes.impl.coercion;
25 25

  
26 26
import java.sql.Timestamp;
27
import java.text.DateFormat;
28
import java.text.SimpleDateFormat;
27 29
import java.util.Date;
28
import org.gvsig.tools.dataTypes.CoercionException;
29
import org.gvsig.tools.dataTypes.CoercionContext;
30
import java.util.Locale;
30 31

  
31
public class CoerceToTimestamp extends CoerceToDateTime {
32
public class CoerceToTimestamp extends AbstractCoerceToDate {
32 33

  
33 34
  @Override
34
  protected String getDateType() {
35
    return "Timestamp";
35
  protected DateFormat[] getFormatters(Locale locale) {
36
    return new DateFormat[]{
37
      DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, locale),
38
      DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, locale),
39
      DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG, locale),
40
      new SimpleDateFormat("yyyyMMddHHmmss") // DBF format
41
    };
36 42
  }
37 43

  
38 44
  @Override
39
  protected Date now() {
40
    Timestamp n = new Timestamp(System.currentTimeMillis());
41
    return n;
45
  protected String getDateType() {
46
    return "Timestamp";
42 47
  }
43 48

  
44 49
  @Override
......
48 53
  }
49 54

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

  
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
//    
74 61
}
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/test/java/org/gvsig/tools/datatypes/impl/coercion/CoerceToDateTimeTest.java
30 30
import org.gvsig.tools.dataTypes.CoercionException;
31 31
import org.gvsig.tools.dataTypes.Coercion;
32 32
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToDate;
33
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToDateTime;
33
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToTimestamp;
34 34

  
35 35
/**
36 36
 * Unit tests for the {@link CoerceToDate} class.
......
41 41
public class CoerceToDateTimeTest extends CoercionTestAbstract {
42 42

  
43 43
    public void testCoerce() throws Exception {
44
        CoerceToDateTime coerce = new CoerceToDateTime();
44
        CoerceToTimestamp coerce = new CoerceToTimestamp();
45 45

  
46 46
        Calendar calendar = Calendar.getInstance();
47 47
        // Remember the month value is in format 0 to 11, instead of 1 to 12,

Also available in: Unified diff