gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / dataTypes / impl / coercion / CoerceToTimestamp.java @ 728
History | View | Annotate | Download (3.04 KB)
1 |
/* gvSIG. Desktop Geographic Information System.
|
---|---|
2 |
*
|
3 |
* Copyright ? 2007-2013 gvSIG Association
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
18 |
* MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us
|
21 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.tools.dataTypes.impl.coercion; |
24 |
|
25 |
import java.sql.Timestamp; |
26 |
|
27 |
import org.gvsig.tools.dataTypes.CoercionException; |
28 |
import org.gvsig.tools.dataTypes.DataTypesManager.Coercion; |
29 |
|
30 |
|
31 |
/**
|
32 |
* Utility class to force instantiation of Timestamp objects.
|
33 |
* If a plugin provides a datasource with a timestamp type which
|
34 |
* is not java.sql.Timestamp, then the plugin will have
|
35 |
* to perform a conversion or coercion between
|
36 |
* its Timestamp type and java.sql.Timestamp if it's not automatic.
|
37 |
*
|
38 |
* Postgresql JDBC uses java.sql.Timestamp, so no problem
|
39 |
*
|
40 |
* Oracle uses oracle.sql.TIMESTAMP which is automatically
|
41 |
* converted, so this coercion is enough
|
42 |
*
|
43 |
* @author jldominguez
|
44 |
*
|
45 |
*/
|
46 |
public class CoerceToTimestamp extends CoerceToTime { |
47 |
|
48 |
public static final String NOW_STRING = "now"; |
49 |
|
50 |
public Object coerce(Object value) throws CoercionException { |
51 |
if (value instanceof Timestamp) { |
52 |
return value;
|
53 |
} else {
|
54 |
String str = value.toString();
|
55 |
Timestamp resp = null; |
56 |
|
57 |
// ----------- (1) Try normal parsing
|
58 |
try {
|
59 |
resp = Timestamp.valueOf(str);
|
60 |
} catch (Exception ex) { |
61 |
resp = null;
|
62 |
} |
63 |
if (resp != null) { |
64 |
return resp;
|
65 |
} |
66 |
// ----------- (2) Try parsing milliseconds
|
67 |
try {
|
68 |
long ms = Long.parseLong(str.trim()); |
69 |
resp = new Timestamp(ms); |
70 |
} catch (Exception ex) { |
71 |
resp = null;
|
72 |
} |
73 |
if (resp != null) { |
74 |
return resp;
|
75 |
} |
76 |
// ----------- (3) Accept "now"
|
77 |
if (NOW_STRING.compareToIgnoreCase(str.trim()) == 0) { |
78 |
|
79 |
resp = new Timestamp(System.currentTimeMillis()); |
80 |
} |
81 |
|
82 |
if (resp != null) { |
83 |
return resp;
|
84 |
} |
85 |
|
86 |
/*
|
87 |
* Finally try as time
|
88 |
*/
|
89 |
return super.coerce(value); |
90 |
} |
91 |
} |
92 |
|
93 |
} |