root / trunk / org.gvsig.postgresql / org.gvsig.postgresql.provider / src / main / java / org / gvsig / postgresql / dal / operations / PostgreSQLFetchFeatureTypeOperation.java @ 433
History | View | Annotate | Download (8.82 KB)
1 | 362 | jjdelcerro | |
---|---|---|---|
2 | package org.gvsig.postgresql.dal.operations; |
||
3 | |||
4 | import java.sql.Connection; |
||
5 | import java.sql.ResultSet; |
||
6 | import java.sql.ResultSetMetaData; |
||
7 | import java.sql.SQLException; |
||
8 | import java.sql.Statement; |
||
9 | import java.util.HashMap; |
||
10 | import java.util.List; |
||
11 | import java.util.Map; |
||
12 | import org.apache.commons.lang3.StringUtils; |
||
13 | import org.cresques.cts.IProjection; |
||
14 | import org.gvsig.fmap.dal.DataTypes; |
||
15 | import org.gvsig.fmap.dal.exception.DataException; |
||
16 | import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
||
17 | import org.gvsig.fmap.dal.feature.EditableFeatureType; |
||
18 | import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
||
19 | import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
||
20 | import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
||
21 | 433 | jjdelcerro | import org.gvsig.fmap.dal.store.jdbc2.spi.SRSSolver; |
22 | 362 | jjdelcerro | import org.gvsig.fmap.dal.store.jdbc2.spi.operations.FetchFeatureTypeOperation; |
23 | import org.gvsig.fmap.geom.Geometry; |
||
24 | import org.gvsig.fmap.geom.GeometryLocator; |
||
25 | import org.gvsig.fmap.geom.GeometryManager; |
||
26 | import org.gvsig.fmap.geom.type.GeometryType; |
||
27 | |||
28 | public class PostgreSQLFetchFeatureTypeOperation extends FetchFeatureTypeOperation { |
||
29 | |||
30 | private static Map<String,GeometryType>databaseGeometryTypes = null; |
||
31 | |||
32 | public PostgreSQLFetchFeatureTypeOperation(
|
||
33 | JDBCHelper helper |
||
34 | ) { |
||
35 | super(helper);
|
||
36 | } |
||
37 | |||
38 | private GeometryType getGT(
|
||
39 | GeometryManager manager, |
||
40 | int type,
|
||
41 | int subtype
|
||
42 | ) { |
||
43 | try {
|
||
44 | return manager.getGeometryType(type, subtype);
|
||
45 | } catch (Exception ex) { |
||
46 | return null; |
||
47 | } |
||
48 | } |
||
49 | |||
50 | public PostgreSQLFetchFeatureTypeOperation(
|
||
51 | JDBCHelper helper, |
||
52 | EditableFeatureType featureType, |
||
53 | String dbname,
|
||
54 | String schema,
|
||
55 | String table,
|
||
56 | List<String> primaryKeys, |
||
57 | String defaultGeometryColumn,
|
||
58 | IProjection crs |
||
59 | ) { |
||
60 | super(helper, featureType, dbname, schema, table, primaryKeys, defaultGeometryColumn, crs);
|
||
61 | } |
||
62 | |||
63 | @Override
|
||
64 | public void fetch(EditableFeatureType featureType, Connection conn, String dbname, String schema, String table, List<String> pks, String defaultGeometryColumn, IProjection crs) throws DataException { |
||
65 | super.fetch(featureType, conn, dbname, schema, table, pks, defaultGeometryColumn, crs);
|
||
66 | } |
||
67 | |||
68 | @Override
|
||
69 | protected int getDataTypeFromMetadata( |
||
70 | ResultSetMetaData rsMetadata,
|
||
71 | int colIndex
|
||
72 | ) throws SQLException { |
||
73 | |||
74 | return super.getDataTypeFromMetadata(rsMetadata, colIndex); |
||
75 | } |
||
76 | |||
77 | @Override
|
||
78 | protected void fetchGeometryTypeAndSRS( |
||
79 | EditableFeatureAttributeDescriptor attr, |
||
80 | ResultSetMetaData rsMetadata,
|
||
81 | int colIndex
|
||
82 | ) { |
||
83 | if( attr.getType()!=DataTypes.GEOMETRY ) {
|
||
84 | return;
|
||
85 | } |
||
86 | try {
|
||
87 | JDBCSQLBuilderBase sqlbuilder = this.createSQLBuilder();
|
||
88 | sqlbuilder.select().column().name("f_table_catalog");
|
||
89 | sqlbuilder.select().column().name("f_table_schema");
|
||
90 | sqlbuilder.select().column().name("f_table_name");
|
||
91 | sqlbuilder.select().column().name("f_geometry_column");
|
||
92 | sqlbuilder.select().column().name("coord_dimension");
|
||
93 | sqlbuilder.select().column().name("srid");
|
||
94 | sqlbuilder.select().column().name("type");
|
||
95 | sqlbuilder.select().where().set( |
||
96 | sqlbuilder.eq( |
||
97 | sqlbuilder.column("f_table_name"),
|
||
98 | sqlbuilder.constant(this.getTablename())
|
||
99 | ) |
||
100 | ); |
||
101 | sqlbuilder.select().where().and( |
||
102 | sqlbuilder.eq( |
||
103 | sqlbuilder.column("f_geometry_column"),
|
||
104 | sqlbuilder.constant(attr.getName()) |
||
105 | ) |
||
106 | ); |
||
107 | sqlbuilder.select().from().table().name("geometry_columns");
|
||
108 | Statement st = null; |
||
109 | ResultSet rs = null; |
||
110 | |||
111 | String srsid = null; |
||
112 | String geometryTypeName = null; |
||
113 | try {
|
||
114 | st = this.getConnection().createStatement();
|
||
115 | rs = JDBCUtils.executeQuery(st, sqlbuilder.toString()); |
||
116 | if (rs.next()) {
|
||
117 | srsid = rs.getString("srid");
|
||
118 | geometryTypeName = rs.getString("type");
|
||
119 | } |
||
120 | } finally {
|
||
121 | JDBCUtils.closeQuietly(rs); |
||
122 | JDBCUtils.closeQuietly(st); |
||
123 | } |
||
124 | if( !StringUtils.isEmpty(geometryTypeName) ) {
|
||
125 | GeometryType gt = getGeometryTypeFromDatabaseTypeName(geometryTypeName); |
||
126 | attr.setGeometryType(gt); |
||
127 | } |
||
128 | if( !StringUtils.isEmpty(srsid) ) {
|
||
129 | 433 | jjdelcerro | SRSSolver srssolver = this.helper.getSRSSolver();
|
130 | attr.setSRS(srssolver.getProjection(this.getConnection(),srsid));
|
||
131 | 362 | jjdelcerro | } |
132 | 364 | jjdelcerro | } catch (Exception ex) { |
133 | 362 | jjdelcerro | logger.debug("Can't get geometry type and srs from column '"+attr.getName()+"'.",ex); |
134 | } |
||
135 | } |
||
136 | |||
137 | private GeometryType getGeometryTypeFromDatabaseTypeName(String typeName) { |
||
138 | if( databaseGeometryTypes==null ) { |
||
139 | GeometryManager manager = GeometryLocator.getGeometryManager(); |
||
140 | databaseGeometryTypes = new HashMap<>(); |
||
141 | databaseGeometryTypes.put("POINT", getGT(manager, Geometry.TYPES.POINT,Geometry.SUBTYPES.GEOM2D));
|
||
142 | databaseGeometryTypes.put("POINTZ", getGT(manager, Geometry.TYPES.POINT,Geometry.SUBTYPES.GEOM3D));
|
||
143 | databaseGeometryTypes.put("POINTM", getGT(manager, Geometry.TYPES.POINT,Geometry.SUBTYPES.GEOM2DM));
|
||
144 | databaseGeometryTypes.put("POINTZM", getGT(manager, Geometry.TYPES.POINT,Geometry.SUBTYPES.GEOM3DM));
|
||
145 | |||
146 | databaseGeometryTypes.put("LINESTRING", getGT(manager, Geometry.TYPES.LINE,Geometry.SUBTYPES.GEOM2D));
|
||
147 | databaseGeometryTypes.put("LINESTRINGZ", getGT(manager, Geometry.TYPES.LINE,Geometry.SUBTYPES.GEOM3D));
|
||
148 | databaseGeometryTypes.put("LINESTRINGM", getGT(manager, Geometry.TYPES.LINE,Geometry.SUBTYPES.GEOM2DM));
|
||
149 | databaseGeometryTypes.put("LINESTRINGZM", getGT(manager, Geometry.TYPES.LINE,Geometry.SUBTYPES.GEOM3DM));
|
||
150 | |||
151 | databaseGeometryTypes.put("POLYGON", getGT(manager, Geometry.TYPES.POLYGON,Geometry.SUBTYPES.GEOM2D));
|
||
152 | databaseGeometryTypes.put("POLYGONZ", getGT(manager, Geometry.TYPES.POLYGON,Geometry.SUBTYPES.GEOM3D));
|
||
153 | databaseGeometryTypes.put("POLYGONM", getGT(manager, Geometry.TYPES.POLYGON,Geometry.SUBTYPES.GEOM2DM));
|
||
154 | databaseGeometryTypes.put("POLYGONZM", getGT(manager, Geometry.TYPES.POLYGON,Geometry.SUBTYPES.GEOM3DM));
|
||
155 | |||
156 | databaseGeometryTypes.put("MULTIPOINT", getGT(manager, Geometry.TYPES.MULTIPOINT,Geometry.SUBTYPES.GEOM2D));
|
||
157 | databaseGeometryTypes.put("MULTIPOINTZ", getGT(manager, Geometry.TYPES.MULTIPOINT,Geometry.SUBTYPES.GEOM3D));
|
||
158 | databaseGeometryTypes.put("MULTIPOINTM", getGT(manager, Geometry.TYPES.MULTIPOINT,Geometry.SUBTYPES.GEOM2DM));
|
||
159 | databaseGeometryTypes.put("MULTIPOINTZM", getGT(manager, Geometry.TYPES.MULTIPOINT,Geometry.SUBTYPES.GEOM3DM));
|
||
160 | |||
161 | databaseGeometryTypes.put("MULTILINESTRING", getGT(manager, Geometry.TYPES.MULTILINE,Geometry.SUBTYPES.GEOM2D));
|
||
162 | databaseGeometryTypes.put("MULTILINESTRINGZ", getGT(manager, Geometry.TYPES.MULTILINE,Geometry.SUBTYPES.GEOM3D));
|
||
163 | databaseGeometryTypes.put("MULTILINESTRINGM", getGT(manager, Geometry.TYPES.MULTILINE,Geometry.SUBTYPES.GEOM2DM));
|
||
164 | databaseGeometryTypes.put("MULTILINESTRINGZM", getGT(manager, Geometry.TYPES.MULTILINE,Geometry.SUBTYPES.GEOM3DM));
|
||
165 | |||
166 | databaseGeometryTypes.put("MULTIPOLYGON", getGT(manager, Geometry.TYPES.MULTIPOLYGON,Geometry.SUBTYPES.GEOM2D));
|
||
167 | databaseGeometryTypes.put("MULTIPOLYGONZ", getGT(manager, Geometry.TYPES.MULTIPOLYGON,Geometry.SUBTYPES.GEOM3D));
|
||
168 | databaseGeometryTypes.put("MULTIPOLYGONM", getGT(manager, Geometry.TYPES.MULTIPOLYGON,Geometry.SUBTYPES.GEOM2DM));
|
||
169 | databaseGeometryTypes.put("MULTIPOLYGONZM", getGT(manager, Geometry.TYPES.MULTIPOLYGON,Geometry.SUBTYPES.GEOM3DM));
|
||
170 | |||
171 | databaseGeometryTypes.put("GEOMETRY", getGT(manager, Geometry.TYPES.GEOMETRY,Geometry.SUBTYPES.GEOM2D));
|
||
172 | databaseGeometryTypes.put("GEOMETRYZ", getGT(manager, Geometry.TYPES.GEOMETRY,Geometry.SUBTYPES.GEOM3D));
|
||
173 | databaseGeometryTypes.put("GEOMETRYM", getGT(manager, Geometry.TYPES.GEOMETRY,Geometry.SUBTYPES.GEOM2DM));
|
||
174 | databaseGeometryTypes.put("GEOMETRYZM", getGT(manager, Geometry.TYPES.GEOMETRY,Geometry.SUBTYPES.GEOM3DM));
|
||
175 | } |
||
176 | return databaseGeometryTypes.get(typeName);
|
||
177 | } |
||
178 | |||
179 | } |