root / branches / v10 / extensions / extPublish / src / org / gvsig / publish / infoproject / datasources / PostGISTableInfo.java @ 19419
History | View | Annotate | Download (8.85 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004-2006 IVER T.I. and Generalitat Valenciana.
|
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Iba?ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* IVER T.I. S.A
|
34 |
* Salamanca 50
|
35 |
* 46005 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963163400
|
39 |
* dac@iver.es
|
40 |
*/
|
41 |
package org.gvsig.publish.infoproject.datasources; |
42 |
|
43 |
import java.awt.geom.Rectangle2D; |
44 |
import java.io.IOException; |
45 |
import java.sql.ResultSet; |
46 |
import java.sql.SQLException; |
47 |
import java.sql.Statement; |
48 |
|
49 |
import org.cresques.cts.ICoordTrans; |
50 |
import org.cresques.cts.IProjection; |
51 |
import org.gvsig.publish.PublishLogger; |
52 |
import org.gvsig.publish.infoproject.IDataSourceInfo; |
53 |
|
54 |
import com.iver.cit.gvsig.fmap.crs.CRSFactory; |
55 |
import com.iver.cit.gvsig.fmap.drivers.ConnectionJDBC; |
56 |
import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition; |
57 |
import com.iver.cit.gvsig.fmap.drivers.db.utils.ConnectionWithParams; |
58 |
import com.iver.cit.gvsig.fmap.drivers.db.utils.SingleVectorialDBConnectionManager; |
59 |
import com.iver.cit.gvsig.fmap.drivers.jdbc.postgis.PostGisDriver; |
60 |
import com.iver.cit.gvsig.fmap.layers.VectorialAdapter; |
61 |
|
62 |
public class PostGISTableInfo implements IDataBaseInfo, IDataSourceInfo { |
63 |
private VectorialAdapter adapter = null; |
64 |
private PostGisDriver driver = null; |
65 |
private ConnectionJDBC connection = null; |
66 |
private DBLayerDefinition layerDef = null; |
67 |
private ConnectionWithParams cwp = null; |
68 |
/**
|
69 |
* Constructor
|
70 |
* @param vectorialAdapter The adapter from gvSIG
|
71 |
*/
|
72 |
public PostGISTableInfo(VectorialAdapter vectorialAdapter) {
|
73 |
this.adapter = vectorialAdapter;
|
74 |
initialize(); |
75 |
} |
76 |
/**
|
77 |
* Initiliazes the object.
|
78 |
* Gets the driver, the connection, the layer definition, the conection with params ...
|
79 |
* TODO: I'm running this code a lot of times ..
|
80 |
*/
|
81 |
private void initialize() { |
82 |
this.driver = (PostGisDriver) adapter.getDriver();
|
83 |
this.connection = (ConnectionJDBC) driver.getConnection();
|
84 |
this.layerDef =driver.getLyrDef();
|
85 |
cwp = SingleVectorialDBConnectionManager.instance().findConnection(this.connection);
|
86 |
//TODO: improve performance, I'm getting all the connections for each layer!
|
87 |
// ConnectionWithParams[] conections = SingleVectorialDBConnectionManager.instance().getAllConnections();
|
88 |
// for (int i = 0; i < conections.length; i++){
|
89 |
// IConnection con = conections[i].getConnection();
|
90 |
// if ( con instanceof ConnectionJDBC){
|
91 |
// ConnectionJDBC conjdbc = (ConnectionJDBC)con;
|
92 |
// if (conjdbc.equals(this.connection)){
|
93 |
// cwp = conections[i];
|
94 |
// }
|
95 |
// }
|
96 |
// }
|
97 |
|
98 |
} |
99 |
|
100 |
/**
|
101 |
*
|
102 |
* @return connection host
|
103 |
* TODO: I think is not good idea to parse the URL with splits ...
|
104 |
* Exceptions
|
105 |
*/
|
106 |
public String getHost() { |
107 |
if (cwp != null){ |
108 |
return cwp.getHost();
|
109 |
}else{
|
110 |
return null; |
111 |
} |
112 |
// try {
|
113 |
// String aux = connection.getURL().split("//")[1];
|
114 |
// return aux.split(":")[0];
|
115 |
// } catch (DBException e) {
|
116 |
// // TODO Auto-generated catch block
|
117 |
// e.printStackTrace();
|
118 |
// }
|
119 |
// return null;
|
120 |
} |
121 |
/**
|
122 |
* @return connection port
|
123 |
*/
|
124 |
public String getPort() { |
125 |
if (cwp != null){ |
126 |
return cwp.getPort();
|
127 |
}else{
|
128 |
return null; |
129 |
} |
130 |
//return driver.getDefaultPort();
|
131 |
} |
132 |
/**
|
133 |
* Gets the user of the connection
|
134 |
* @return connection user
|
135 |
* TODO: Exceptions ...
|
136 |
*/
|
137 |
public String getUser() { |
138 |
if (cwp != null){ |
139 |
return cwp.getUser();
|
140 |
}else{
|
141 |
return null; |
142 |
} |
143 |
// try {
|
144 |
// return connection.getConnection().getMetaData().getUserName();
|
145 |
// } catch (SQLException e) {
|
146 |
// // TODO Auto-generated catch block
|
147 |
// e.printStackTrace();
|
148 |
// }
|
149 |
// return null;
|
150 |
} |
151 |
/**
|
152 |
*
|
153 |
* @return the password or null if I can't get it
|
154 |
*/
|
155 |
public String getPassword(){ |
156 |
if (cwp != null){ |
157 |
return cwp.getPw();
|
158 |
}else{
|
159 |
return null; |
160 |
} |
161 |
// ConnectionWithParams[] conections = SingleVectorialDBConnectionManager.instance().getAllConnections();
|
162 |
// for (int i = 0; i < conections.length; i++){
|
163 |
// IConnection con = conections[i].getConnection();
|
164 |
// if ( con instanceof ConnectionJDBC){
|
165 |
// ConnectionJDBC conjdbc = (ConnectionJDBC)con;
|
166 |
// if (conjdbc.equals(this.connection)){
|
167 |
// return conections[i].getPw();www.google.es/
|
168 |
// }
|
169 |
// }
|
170 |
// }
|
171 |
// return null;
|
172 |
} |
173 |
/*
|
174 |
* (non-Javadoc)
|
175 |
* @see org.gvsig.publish.infoProject.IDataSourceInfo#getBBox()
|
176 |
*/
|
177 |
public Rectangle2D getNativeBBox() { |
178 |
//this.adapter.getFullExtent();
|
179 |
Rectangle2D aux = null; |
180 |
try {
|
181 |
aux = this.driver.getFullExtent();
|
182 |
} catch (IOException e) { |
183 |
PublishLogger.getLog().error("ERROR PostGISTable",e);
|
184 |
} |
185 |
return aux;
|
186 |
} |
187 |
/*
|
188 |
* (non-Javadoc)
|
189 |
* @see org.gvsig.publish.infoProject.IDataSourceInfo#getCRS()
|
190 |
* TODO: MAybe already exists this implementation, but I couldn't find it
|
191 |
*/
|
192 |
public String getEPSG() { |
193 |
try {
|
194 |
String sql = "SELECT srid FROM geometry_columns WHERE f_table_name = '" + getTableName() + "' AND f_geometry_column = '" + getGeometryColumn() + "'"; |
195 |
Statement st = connection.getConnection().createStatement();
|
196 |
ResultSet rs = st.executeQuery(sql);
|
197 |
if (rs.first()){
|
198 |
int aux = rs.getInt("srid"); |
199 |
return "EPSG:" + aux; |
200 |
} |
201 |
} catch (SQLException e) { |
202 |
PublishLogger.getLog().error("ERROR PostGISTableInfo : SQL exception getting the EPSG",e);
|
203 |
} |
204 |
return null; |
205 |
//TODO: I can't get the datasource CRS !!
|
206 |
//return this.driver.getDestProjection();
|
207 |
//return "EPSG:23030";
|
208 |
} |
209 |
/*
|
210 |
* (non-Javadoc)
|
211 |
* @see org.gvsig.publish.infoProject.IDataSourceInfo#getDataType()
|
212 |
* TODO: MAybe already exists this implementation, but I couldn't find it
|
213 |
*/
|
214 |
public String getDataType() { |
215 |
String geomtype = null; |
216 |
try {
|
217 |
String sql = "SELECT type FROM geometry_columns WHERE f_table_name = '" + getTableName() + "' AND f_geometry_column = '" + getGeometryColumn() + "'"; |
218 |
Statement st = connection.getConnection().createStatement();
|
219 |
ResultSet rs = st.executeQuery(sql);
|
220 |
if (rs.first()){
|
221 |
geomtype = rs.getString("type");
|
222 |
} |
223 |
} catch (SQLException e) { |
224 |
PublishLogger.getLog().error("ERROR PostGISTableInfo : SQL exception getting the geometry type",e);
|
225 |
} |
226 |
|
227 |
if (geomtype.equals("POINT") || geomtype.equals("MULTIPOINT")){ |
228 |
return IDataSourceInfo.POINT_DATA;
|
229 |
} |
230 |
if (geomtype.equals("LINESTRING") || geomtype.equals("MULTILINESTRING")){ |
231 |
return IDataSourceInfo.LINE_DATA;
|
232 |
} |
233 |
if (geomtype.equals("POLYGON") || geomtype.equals("MULTIPOLYGON")){ |
234 |
return IDataSourceInfo.POLYGON_DATA;
|
235 |
} |
236 |
return null; |
237 |
} |
238 |
/*
|
239 |
* (non-Javadoc)
|
240 |
* @see org.gvsig.publish.infoProject.IDataSourceInfo#getType()
|
241 |
*/
|
242 |
public String getType() { |
243 |
return IDataSourceInfo.POSTGIS_TYPE;
|
244 |
} |
245 |
/*
|
246 |
* (non-Javadoc)
|
247 |
* @see org.gvsig.publish.infoProject.datasources.IDataBaseInfo#getTableName()
|
248 |
*/
|
249 |
public String getTableName() { |
250 |
return this.driver.getTableName(); |
251 |
} |
252 |
/*
|
253 |
* (non-Javadoc)
|
254 |
* @see org.gvsig.publish.infoProject.datasources.IDataBaseInfo#getGeometryColumn()
|
255 |
*/
|
256 |
public String getGeometryColumn() { |
257 |
return layerDef.getFieldGeometry();
|
258 |
} |
259 |
/*
|
260 |
* (non-Javadoc)
|
261 |
* @see org.gvsig.publish.infoProject.datasources.IDataBaseInfo#getDatabaseName()
|
262 |
*/
|
263 |
public String getDatabaseName() { |
264 |
if (cwp != null){ |
265 |
return cwp.getDb();
|
266 |
}else{
|
267 |
return null; |
268 |
} |
269 |
//return layerDef.getCatalogName();
|
270 |
} |
271 |
/*
|
272 |
* (non-Javadoc)
|
273 |
* @see org.gvsig.publish.infoProject.datasources.IDataBaseInfo#getGID()
|
274 |
*/
|
275 |
public String getGID() { |
276 |
return layerDef.getFieldID();
|
277 |
} |
278 |
/**
|
279 |
*
|
280 |
* @see org.gvsig.publish.infoproject.IDataSourceInfo#getLatLonBBox()
|
281 |
*/
|
282 |
public Rectangle2D getLatLonBBox() { |
283 |
IProjection proj_source = CRSFactory.getCRS(getEPSG()); |
284 |
IProjection proj_dest = CRSFactory.getCRS("EPSG:4326");
|
285 |
ICoordTrans trans = proj_source.getCT(proj_dest); |
286 |
return trans.convert(getNativeBBox());
|
287 |
} |
288 |
/**
|
289 |
* the datastore name is the connection name
|
290 |
* @return connection name
|
291 |
* @see org.gvsig.publish.infoproject.IDataSourceInfo#getConnectionName()
|
292 |
*/
|
293 |
public String getConnectionName() { |
294 |
return cwp.getName();
|
295 |
} |
296 |
/**
|
297 |
* @return schema name
|
298 |
* @see org.gvsig.publish.infoproject.datasources.IDataBaseInfo#getSchemaName()
|
299 |
*/
|
300 |
public String getSchemaName() { |
301 |
String aux = cwp.getSchema();
|
302 |
if (aux == null){ |
303 |
aux = "public";
|
304 |
} |
305 |
return aux;
|
306 |
} |
307 |
} |