View Javadoc

1   package liquibase.ext.spatial.datatype;
2   
3   import liquibase.database.Database;
4   import liquibase.database.core.DerbyDatabase;
5   import liquibase.database.core.H2Database;
6   import liquibase.database.core.OracleDatabase;
7   import liquibase.database.core.PostgresDatabase;
8   import liquibase.datatype.DataTypeInfo;
9   import liquibase.datatype.DatabaseDataType;
10  import liquibase.datatype.LiquibaseDataType;
11  import liquibase.exception.UnexpectedLiquibaseException;
12  import liquibase.statement.DatabaseFunction;
13  
14  import com.vividsolutions.jts.geom.Geometry;
15  import com.vividsolutions.jts.io.ParseException;
16  import com.vividsolutions.jts.io.WKTReader;
17  
18  /**
19   * The <code>GeometryType</code> assists in defining database-specific geometry types and converting
20   * SQL representations of geometries.
21   */
22  @DataTypeInfo(name = "geometry", aliases = { "com.vividsolutions.jts.geom.Geometry" }, minParameters = 0, maxParameters = 2, priority = LiquibaseDataType.PRIORITY_DEFAULT)
23  public class GeometryType extends LiquibaseDataType {
24     /**
25      * Returns the value geometry type parameter.
26      * 
27      * @return the geometry type or <code>null</code> if not present.
28      */
29     public String getGeometryType() {
30        String geometryType = null;
31        if (getParameters().length > 0 && getParameters()[0] != null) {
32           geometryType = getParameters()[0].toString();
33        }
34        return geometryType;
35     }
36  
37     /**
38      * Returns the value SRID parameter.
39      * 
40      * @return the SRID or <code>null</code> if not present.
41      */
42     public Integer getSRID() {
43        Integer srid = null;
44        if (getParameters().length > 1 && getParameters()[1] != null) {
45           srid = Integer.valueOf(getParameters()[1].toString());
46        }
47        return srid;
48     }
49  
50     /**
51      * Creates the appropriate Geometry <code>DatabaseDataType</code>.
52      */
53     @Override
54     public DatabaseDataType toDatabaseDataType(final Database database) {
55        final DatabaseDataType databaseDataType;
56        if (database instanceof DerbyDatabase) {
57           databaseDataType = new DatabaseDataType("VARCHAR(32672) FOR BIT DATA");
58        } else if (database instanceof H2Database) {
59           // User's wanting to use a BLOB can use modifySql.
60           databaseDataType = new DatabaseDataType("BINARY");
61        } else if (database instanceof OracleDatabase) {
62           databaseDataType = new DatabaseDataType("SDO_GEOMETRY");
63        } else if (database instanceof PostgresDatabase) {
64           databaseDataType = new DatabaseDataType(getName(), getParameters());
65        } else {
66           databaseDataType = new DatabaseDataType("GEOMETRY");
67        }
68        return databaseDataType;
69     }
70  
71     /**
72      * @see liquibase.datatype.LiquibaseDataType#objectToSql(java.lang.Object,
73      *      liquibase.database.Database)
74      */
75     @Override
76     public String objectToSql(final Object value, final Database database) {
77        final String returnValue;
78        if (value instanceof Geometry) {
79           // TODO: Tailor the output for the database.
80           returnValue = ((Geometry) value).toText();
81        } else if (value instanceof String) {
82           returnValue = value.toString();
83        } else if (value instanceof DatabaseFunction) {
84           returnValue = value.toString();
85        } else if (value == null || value.toString().equalsIgnoreCase("null")) {
86           returnValue = null;
87        } else {
88           throw new UnexpectedLiquibaseException("Cannot convert type " + value.getClass()
89                 + " to a Geometry value");
90        }
91        return returnValue;
92     }
93  
94     /**
95      * @see liquibase.datatype.LiquibaseDataType#sqlToObject(java.lang.String,
96      *      liquibase.database.Database)
97      */
98     @Override
99     public Object sqlToObject(final String value, final Database database) {
100       final Geometry returnValue;
101       if (value == null || value.equalsIgnoreCase("null")) {
102          returnValue = null;
103       } else {
104          final WKTReader reader = new WKTReader();
105          try {
106             // TODO: Check for SRID.
107             returnValue = reader.read(value);
108          } catch (final ParseException e) {
109             throw new UnexpectedLiquibaseException("Cannot parse " + value + " to a Geometry", e);
110          }
111       }
112       return returnValue;
113    }
114 }