svn-gvsig-desktop / trunk / extensions / extJDBC / src / com / iver / cit / gvsig / fmap / drivers / jdbc / postgis / PostGISWriter.java @ 6313
History | View | Annotate | Download (7.4 KB)
1 |
package com.iver.cit.gvsig.fmap.drivers.jdbc.postgis; |
---|---|
2 |
|
3 |
import java.sql.Connection; |
4 |
import java.sql.ResultSet; |
5 |
import java.sql.SQLException; |
6 |
import java.sql.Statement; |
7 |
import java.sql.Types; |
8 |
import java.util.ArrayList; |
9 |
|
10 |
import com.iver.cit.gvsig.fmap.core.FShape; |
11 |
import com.iver.cit.gvsig.fmap.core.IFeature; |
12 |
import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition; |
13 |
import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
14 |
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition; |
15 |
import com.iver.cit.gvsig.fmap.edition.EditionException; |
16 |
import com.iver.cit.gvsig.fmap.edition.IFieldManager; |
17 |
import com.iver.cit.gvsig.fmap.edition.IRowEdited; |
18 |
import com.iver.cit.gvsig.fmap.edition.ISpatialWriter; |
19 |
import com.iver.cit.gvsig.fmap.edition.fieldmanagers.AddFieldCommand; |
20 |
import com.iver.cit.gvsig.fmap.edition.fieldmanagers.FieldCommand; |
21 |
import com.iver.cit.gvsig.fmap.edition.fieldmanagers.JdbcFieldManager; |
22 |
import com.iver.cit.gvsig.fmap.edition.fieldmanagers.RemoveFieldCommand; |
23 |
import com.iver.cit.gvsig.fmap.edition.fieldmanagers.RenameFieldCommand; |
24 |
import com.iver.cit.gvsig.fmap.edition.writers.AbstractWriter; |
25 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
26 |
|
27 |
public class PostGISWriter extends AbstractWriter implements ISpatialWriter, IFieldManager { |
28 |
|
29 |
private int numRows; |
30 |
|
31 |
private DBLayerDefinition lyrDef;
|
32 |
|
33 |
private Connection conex; |
34 |
|
35 |
private Statement st; |
36 |
|
37 |
private boolean bCreateTable; |
38 |
|
39 |
private boolean bWriteAll; |
40 |
private double flatness; |
41 |
|
42 |
private JdbcFieldManager fieldManager;
|
43 |
|
44 |
/**
|
45 |
* Useful to create a layer from scratch Call setFile before using this
|
46 |
* function
|
47 |
*
|
48 |
* @param lyrDef
|
49 |
* @throws IOException
|
50 |
* @throws DriverException
|
51 |
*/
|
52 |
public void initialize(ITableDefinition lyrD) throws EditionException { |
53 |
super.initialize(lyrD);
|
54 |
this.lyrDef = (DBLayerDefinition) lyrD;
|
55 |
conex = lyrDef.getConnection(); |
56 |
|
57 |
try {
|
58 |
st = conex.createStatement(); |
59 |
|
60 |
if (bCreateTable) {
|
61 |
try {
|
62 |
st.execute("DROP TABLE " + lyrDef.getTableName() + ";"); |
63 |
} catch (SQLException e1) { |
64 |
// Si no existe la tabla, no hay que borrarla.
|
65 |
} |
66 |
|
67 |
String sqlCreate = PostGIS.getSqlCreateSpatialTable(lyrDef,
|
68 |
lyrDef.getFieldsDesc(), true);
|
69 |
System.out.println("sqlCreate =" + sqlCreate); |
70 |
st.execute(sqlCreate); |
71 |
|
72 |
String sqlAlter = PostGIS.getSqlAlterTable(lyrDef);
|
73 |
System.out.println("sqlAlter =" + sqlAlter); |
74 |
st.execute(sqlAlter); |
75 |
// CREATE TABLE PARKS ( PARK_ID int4, PARK_NAME varchar(128),
|
76 |
// PARK_DATE date, PARK_TYPE varchar(2) );
|
77 |
// SELECT AddGeometryColumn('parks_db', 'parks', 'park_geom',
|
78 |
// 128,
|
79 |
// 'MULTIPOLYGON', 2 );
|
80 |
|
81 |
/*
|
82 |
* BEGIN; INSERT INTO ROADS_GEOM (ID,GEOM,NAME ) VALUES
|
83 |
* (1,GeometryFromText('LINESTRING(191232 243118,191108
|
84 |
* 243242)',-1),'Jeff Rd'); INSERT INTO ROADS_GEOM (ID,GEOM,NAME )
|
85 |
* VALUES (2,GeometryFromText('LINESTRING(189141 244158,189265
|
86 |
* 244817)',-1),'Geordie Rd'); COMMIT;
|
87 |
*/
|
88 |
} |
89 |
conex.setAutoCommit(false);
|
90 |
fieldManager = new JdbcFieldManager(conex, lyrDef.getTableName());
|
91 |
|
92 |
} catch (SQLException e) { |
93 |
e.printStackTrace(); |
94 |
throw new EditionException(e); |
95 |
} |
96 |
|
97 |
} |
98 |
|
99 |
public void preProcess() throws EditionException { |
100 |
numRows = 0;
|
101 |
// ATENTION: We will transform (in PostGIS class; doubleQuote())
|
102 |
// to UTF-8 strings. Then, we tell the PostgreSQL server
|
103 |
// that we will use UTF-8, and it can translate
|
104 |
// to its charset
|
105 |
// Note: we have to translate to UTF-8 because
|
106 |
// the server cannot manage UTF-16
|
107 |
|
108 |
alterTable(); |
109 |
|
110 |
ResultSet rsAux;
|
111 |
try {
|
112 |
rsAux = st.executeQuery("SHOW server_encoding;");
|
113 |
rsAux.next(); |
114 |
String serverEncoding = rsAux.getString(1); |
115 |
System.out.println("Server encoding = " + serverEncoding); |
116 |
// st.execute("SET CLIENT_ENCODING TO 'UNICODE';");
|
117 |
// Intentamos convertir nuestras cadenas a ese encode.
|
118 |
PostGIS.toEncode = serverEncoding; |
119 |
} catch (SQLException e) { |
120 |
// TODO Auto-generated catch block
|
121 |
e.printStackTrace(); |
122 |
} |
123 |
|
124 |
} |
125 |
|
126 |
public void process(IRowEdited row) throws EditionException { |
127 |
|
128 |
String sqlInsert;
|
129 |
try {
|
130 |
// System.out.println("Escribiendo numReg=" + numReg + " con
|
131 |
// STATUS=" + row.getStatus());
|
132 |
switch (row.getStatus()) {
|
133 |
case IRowEdited.STATUS_ADDED:
|
134 |
IFeature feat = (IFeature) row.getLinkedRow(); |
135 |
sqlInsert = PostGIS.getSqlInsertFeature(lyrDef, feat); |
136 |
System.out.println("sql = " + sqlInsert); |
137 |
st.execute(sqlInsert); |
138 |
|
139 |
break;
|
140 |
case IRowEdited.STATUS_MODIFIED:
|
141 |
IFeature featM = (IFeature) row.getLinkedRow(); |
142 |
if (bWriteAll) {
|
143 |
sqlInsert = PostGIS.getSqlInsertFeature(lyrDef, featM); |
144 |
System.out.println("sql = " + sqlInsert); |
145 |
st.execute(sqlInsert); |
146 |
} else {
|
147 |
String sqlModify = PostGIS
|
148 |
.getSqlModifyFeature(lyrDef, featM); |
149 |
System.out.println("sql = " + sqlModify); |
150 |
st.execute(sqlModify); |
151 |
} |
152 |
break;
|
153 |
case IRowEdited.STATUS_ORIGINAL:
|
154 |
IFeature featO = (IFeature) row.getLinkedRow(); |
155 |
if (bWriteAll) {
|
156 |
sqlInsert = PostGIS.getSqlInsertFeature(lyrDef, featO); |
157 |
st.execute(sqlInsert); |
158 |
} |
159 |
break;
|
160 |
case IRowEdited.STATUS_DELETED:
|
161 |
String sqlDelete = PostGIS.getSqlDeleteFeature(lyrDef, row);
|
162 |
System.out.println("sql = " + sqlDelete); |
163 |
st.execute(sqlDelete); |
164 |
|
165 |
break;
|
166 |
} |
167 |
|
168 |
numRows++; |
169 |
} catch (SQLException e) { |
170 |
e.printStackTrace(); |
171 |
throw new EditionException(e); |
172 |
} |
173 |
|
174 |
} |
175 |
|
176 |
public void postProcess() throws EditionException { |
177 |
try {
|
178 |
conex.setAutoCommit(true);
|
179 |
} catch (SQLException e) { |
180 |
e.printStackTrace(); |
181 |
throw new EditionException(e); |
182 |
} |
183 |
} |
184 |
|
185 |
public String getName() { |
186 |
return "PostGIS Writer"; |
187 |
} |
188 |
|
189 |
public boolean canWriteGeometry(int gvSIGgeometryType) { |
190 |
switch (gvSIGgeometryType) {
|
191 |
case FShape.POINT:
|
192 |
return true; |
193 |
case FShape.LINE:
|
194 |
return true; |
195 |
case FShape.POLYGON:
|
196 |
return true; |
197 |
case FShape.ARC:
|
198 |
return false; |
199 |
case FShape.ELLIPSE:
|
200 |
return false; |
201 |
case FShape.MULTIPOINT:
|
202 |
return true; |
203 |
case FShape.TEXT:
|
204 |
return false; |
205 |
} |
206 |
return false; |
207 |
} |
208 |
|
209 |
public boolean canWriteAttribute(int sqlType) { |
210 |
switch (sqlType) {
|
211 |
case Types.DOUBLE: |
212 |
case Types.FLOAT: |
213 |
case Types.INTEGER: |
214 |
case Types.BIGINT: |
215 |
return true; |
216 |
case Types.DATE: |
217 |
return true; |
218 |
case Types.BIT: |
219 |
case Types.BOOLEAN: |
220 |
return true; |
221 |
case Types.VARCHAR: |
222 |
case Types.CHAR: |
223 |
case Types.LONGVARCHAR: |
224 |
return true; |
225 |
|
226 |
} |
227 |
|
228 |
return false; |
229 |
} |
230 |
|
231 |
/**
|
232 |
* @return Returns the bCreateTable.
|
233 |
*/
|
234 |
public boolean isCreateTable() { |
235 |
return bCreateTable;
|
236 |
} |
237 |
|
238 |
/**
|
239 |
* @param createTable
|
240 |
* The bCreateTable to set.
|
241 |
*/
|
242 |
public void setCreateTable(boolean createTable) { |
243 |
bCreateTable = createTable; |
244 |
} |
245 |
|
246 |
/**
|
247 |
* @return Returns the bWriteAll.
|
248 |
*/
|
249 |
public boolean isWriteAll() { |
250 |
return bWriteAll;
|
251 |
} |
252 |
|
253 |
/**
|
254 |
* @param writeAll
|
255 |
* The bWriteAll to set.
|
256 |
*/
|
257 |
public void setWriteAll(boolean writeAll) { |
258 |
bWriteAll = writeAll; |
259 |
} |
260 |
|
261 |
|
262 |
public void setFlatness(double flatness) { |
263 |
this.flatness = flatness;
|
264 |
} |
265 |
|
266 |
public FieldDescription[] getOriginalFields() { |
267 |
return lyrDef.getFieldsDesc();
|
268 |
} |
269 |
|
270 |
public void addField(FieldDescription fieldDesc) { |
271 |
fieldManager.addField(fieldDesc); |
272 |
|
273 |
} |
274 |
|
275 |
public FieldDescription removeField(String fieldName) { |
276 |
return fieldManager.removeField(fieldName);
|
277 |
|
278 |
} |
279 |
|
280 |
public void renameField(String antName, String newName) { |
281 |
fieldManager.renameField(antName, newName); |
282 |
|
283 |
} |
284 |
|
285 |
public boolean alterTable() throws EditionException { |
286 |
return fieldManager.alterTable();
|
287 |
} |
288 |
|
289 |
public FieldDescription[] getFields() { |
290 |
return fieldManager.getFields();
|
291 |
} |
292 |
|
293 |
} |