44 |
44 |
import java.sql.DriverManager;
|
45 |
45 |
import java.sql.SQLException;
|
46 |
46 |
import java.util.StringTokenizer;
|
|
47 |
import java.util.TreeMap;
|
47 |
48 |
|
48 |
49 |
import com.hardcode.driverManager.DriverLoadException;
|
49 |
50 |
import com.iver.cit.gvsig.fmap.drivers.DefaultDBDriver;
|
50 |
51 |
import com.iver.cit.gvsig.fmap.drivers.VectorialJDBCDriver;
|
|
52 |
import com.iver.cit.gvsig.fmap.drivers.jdbc.postgis.PostGisDriver;
|
51 |
53 |
import com.iver.cit.gvsig.fmap.layers.FLayer;
|
52 |
54 |
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
|
53 |
55 |
|
... | ... | |
84 |
86 |
} catch (DriverLoadException e) {
|
85 |
87 |
// TODO Auto-generated catch block
|
86 |
88 |
e.printStackTrace();
|
87 |
|
} catch (InstantiationException e) {
|
88 |
|
// TODO Auto-generated catch block
|
89 |
|
e.printStackTrace();
|
90 |
|
} catch (IllegalAccessException e) {
|
91 |
|
// TODO Auto-generated catch block
|
92 |
|
e.printStackTrace();
|
93 |
89 |
} catch (ClassNotFoundException e) {
|
94 |
90 |
// TODO Auto-generated catch block
|
95 |
91 |
e.printStackTrace();
|
... | ... | |
108 |
104 |
* @throws SQLException
|
109 |
105 |
* @throws DriverLoadException
|
110 |
106 |
* @throws ClassNotFoundException
|
111 |
|
* @throws IllegalAccessException
|
112 |
|
* @throws InstantiationException
|
113 |
107 |
*/
|
114 |
|
private FLayer createPostgisLayer(String jdbcUrl, String sLayer) throws SQLException, DriverLoadException, InstantiationException, IllegalAccessException, ClassNotFoundException {
|
|
108 |
private FLayer createPostgisLayer(String jdbcUrl, String table) throws SQLException, DriverLoadException, ClassNotFoundException {
|
115 |
109 |
//jdbc:postgresql://sercartlin/carto_300k?user=gis&password=gis
|
|
110 |
//NAME=Comunicaciones&TABLENAME=comunic_lin_300k&ID=6&FIELDS=entity,layer,codigo,tipo,gid&GEOMFIELD=the_geom
|
116 |
111 |
StringTokenizer sti = new StringTokenizer(jdbcUrl,"?");
|
117 |
112 |
String dbURL = sti.nextToken();
|
118 |
113 |
|
119 |
|
sti = new StringTokenizer(sti.nextToken(),"&");
|
120 |
|
String user = sti.nextToken();
|
121 |
|
String pwd = sti.nextToken();
|
|
114 |
String p = sti.nextToken();
|
|
115 |
|
|
116 |
TreeMap credentials = separateParams(p);
|
|
117 |
String user = (String) credentials.get((String) "USER");
|
|
118 |
String pwd = (String) credentials.get((String) "PASSWORD");
|
122 |
119 |
|
123 |
|
dbURL = "jdbc:postgresql://sercartlin/carto_300k";
|
124 |
|
user = "gis";
|
125 |
|
pwd = "gis";
|
126 |
|
|
127 |
|
String layerName = "the_geom";
|
128 |
|
int fidField = 0;
|
129 |
|
String fields = "gid,entity,layer,codigo,tipo,the_geom";
|
130 |
|
String tableName = "comunic_lin_300k";
|
|
120 |
TreeMap params = separateParams(table);
|
|
121 |
String layerName = (String) params.get((String) "NAME");
|
|
122 |
int fidField = Integer.valueOf((String) params.get((String) "ID")).intValue();
|
|
123 |
String fields = getFieldsClause((String) params.get((String) "FIELDS"),
|
|
124 |
(String) params.get((String) "GEOMFIELD"));
|
|
125 |
String tableName = (String) params.get((String) "TABLENAME");
|
131 |
126 |
String whereClause = "";
|
132 |
|
|
133 |
|
|
|
127 |
|
|
128 |
|
134 |
129 |
Connection conn = DriverManager.getConnection(dbURL, user, pwd);
|
135 |
130 |
conn.setAutoCommit(false);
|
136 |
131 |
|
137 |
|
VectorialJDBCDriver driver = (VectorialJDBCDriver) Class.forName("org.postgresql.Driver").newInstance();
|
138 |
|
if (driver instanceof DefaultDBDriver)
|
139 |
|
{
|
140 |
|
DefaultDBDriver dbDriver = (DefaultDBDriver) driver;
|
141 |
|
dbDriver.setData(conn, tableName, fields, whereClause, fidField);
|
142 |
|
//if (dbLayerDefinition.getWorkingArea() != null){
|
143 |
|
// driver.setWorkingArea(dbLayerDefinition.getWorkingArea());
|
144 |
|
//}
|
145 |
|
}
|
146 |
|
|
147 |
|
return LayerFactory.createDBLayer(driver, layerName, null);
|
148 |
|
|
149 |
|
|
|
132 |
PostGisDriver pgd = new PostGisDriver();
|
|
133 |
pgd.setData(conn, tableName, fields, whereClause, fidField);
|
|
134 |
|
|
135 |
return LayerFactory.createDBLayer(pgd, layerName, null);
|
|
136 |
}
|
|
137 |
|
|
138 |
private String getFieldsClause(String fields,String geomField){
|
|
139 |
return "ASBINARY(" + geomField + ", 'XDR')," + fields;
|
150 |
140 |
}
|
|
141 |
|
|
142 |
private TreeMap separateParams(String pairValues){
|
|
143 |
TreeMap map = new TreeMap();
|
|
144 |
String[] params = pairValues.split("&");
|
|
145 |
for (int i = 0; i < params.length; i++) {
|
|
146 |
String[] nameValue = params[i].split("=");
|
|
147 |
map.put(nameValue[0].toUpperCase(), nameValue[1]);
|
|
148 |
}
|
|
149 |
return map;
|
|
150 |
}
|
151 |
151 |
}
|