svn-gvsig-desktop / branches / v10 / extensions / extJDBC / src / com / iver / cit / gvsig / fmap / drivers / jdbc / postgis / PostGISWriter.java @ 11867
History | View | Annotate | Download (7.47 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 PostGIS postGisSQL = new PostGIS(); |
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.getComposedTableName() + ";"); |
63 |
} catch (SQLException e1) { |
64 |
// Si no existe la tabla, no hay que borrarla.
|
65 |
} |
66 |
|
67 |
String sqlCreate = postGisSQL.getSqlCreateSpatialTable(lyrDef,
|
68 |
lyrDef.getFieldsDesc(), true);
|
69 |
System.out.println("sqlCreate =" + sqlCreate); |
70 |
st.execute(sqlCreate); |
71 |
|
72 |
String sqlAlter = postGisSQL.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 |
conex.commit(); |
89 |
} |
90 |
conex.setAutoCommit(false);
|
91 |
fieldManager = new JdbcFieldManager(conex, lyrDef.getTableName());
|
92 |
|
93 |
} catch (SQLException e) { |
94 |
e.printStackTrace(); |
95 |
throw new EditionException(e); |
96 |
} |
97 |
|
98 |
} |
99 |
|
100 |
public void preProcess() throws EditionException { |
101 |
numRows = 0;
|
102 |
// ATENTION: We will transform (in PostGIS class; doubleQuote())
|
103 |
// to UTF-8 strings. Then, we tell the PostgreSQL server
|
104 |
// that we will use UTF-8, and it can translate
|
105 |
// to its charset
|
106 |
// Note: we have to translate to UTF-8 because
|
107 |
// the server cannot manage UTF-16
|
108 |
|
109 |
ResultSet rsAux;
|
110 |
try {
|
111 |
conex.rollback(); |
112 |
alterTable(); |
113 |
|
114 |
rsAux = st.executeQuery("SHOW server_encoding;");
|
115 |
rsAux.next(); |
116 |
String serverEncoding = rsAux.getString(1); |
117 |
System.out.println("Server encoding = " + serverEncoding); |
118 |
// st.execute("SET CLIENT_ENCODING TO 'UNICODE';");
|
119 |
// Intentamos convertir nuestras cadenas a ese encode.
|
120 |
postGisSQL.setEncoding(serverEncoding); |
121 |
} catch (SQLException e) { |
122 |
// TODO Auto-generated catch block
|
123 |
e.printStackTrace(); |
124 |
} |
125 |
|
126 |
} |
127 |
|
128 |
public void process(IRowEdited row) throws EditionException { |
129 |
|
130 |
String sqlInsert;
|
131 |
try {
|
132 |
// System.out.println("Escribiendo numReg=" + numReg + " con
|
133 |
// STATUS=" + row.getStatus());
|
134 |
switch (row.getStatus()) {
|
135 |
case IRowEdited.STATUS_ADDED:
|
136 |
IFeature feat = (IFeature) row.getLinkedRow(); |
137 |
sqlInsert = postGisSQL.getSqlInsertFeature(lyrDef, feat); |
138 |
System.out.println("sql = " + sqlInsert); |
139 |
st.execute(sqlInsert); |
140 |
|
141 |
break;
|
142 |
case IRowEdited.STATUS_MODIFIED:
|
143 |
IFeature featM = (IFeature) row.getLinkedRow(); |
144 |
if (bWriteAll) {
|
145 |
sqlInsert = postGisSQL.getSqlInsertFeature(lyrDef, featM); |
146 |
System.out.println("sql = " + sqlInsert); |
147 |
st.execute(sqlInsert); |
148 |
} else {
|
149 |
String sqlModify = postGisSQL.getSqlModifyFeature(lyrDef, featM);
|
150 |
System.out.println("sql = " + sqlModify); |
151 |
st.execute(sqlModify); |
152 |
} |
153 |
break;
|
154 |
case IRowEdited.STATUS_ORIGINAL:
|
155 |
IFeature featO = (IFeature) row.getLinkedRow(); |
156 |
if (bWriteAll) {
|
157 |
sqlInsert = postGisSQL.getSqlInsertFeature(lyrDef, featO); |
158 |
st.execute(sqlInsert); |
159 |
} |
160 |
break;
|
161 |
case IRowEdited.STATUS_DELETED:
|
162 |
String sqlDelete = postGisSQL.getSqlDeleteFeature(lyrDef, row);
|
163 |
System.out.println("sql = " + sqlDelete); |
164 |
st.execute(sqlDelete); |
165 |
|
166 |
break;
|
167 |
} |
168 |
|
169 |
numRows++; |
170 |
} catch (SQLException e) { |
171 |
e.printStackTrace(); |
172 |
throw new EditionException(e); |
173 |
} |
174 |
|
175 |
} |
176 |
|
177 |
public void postProcess() throws EditionException { |
178 |
try {
|
179 |
conex.setAutoCommit(true);
|
180 |
} catch (SQLException e) { |
181 |
e.printStackTrace(); |
182 |
throw new EditionException(e); |
183 |
} |
184 |
} |
185 |
|
186 |
public String getName() { |
187 |
return "PostGIS Writer"; |
188 |
} |
189 |
|
190 |
public boolean canWriteGeometry(int gvSIGgeometryType) { |
191 |
switch (gvSIGgeometryType) {
|
192 |
case FShape.POINT:
|
193 |
return true; |
194 |
case FShape.LINE:
|
195 |
return true; |
196 |
case FShape.POLYGON:
|
197 |
return true; |
198 |
case FShape.ARC:
|
199 |
return false; |
200 |
case FShape.ELLIPSE:
|
201 |
return false; |
202 |
case FShape.MULTIPOINT:
|
203 |
return true; |
204 |
case FShape.TEXT:
|
205 |
return false; |
206 |
} |
207 |
return false; |
208 |
} |
209 |
|
210 |
public boolean canWriteAttribute(int sqlType) { |
211 |
switch (sqlType) {
|
212 |
case Types.DOUBLE: |
213 |
case Types.FLOAT: |
214 |
case Types.INTEGER: |
215 |
case Types.BIGINT: |
216 |
return true; |
217 |
case Types.DATE: |
218 |
return true; |
219 |
case Types.BIT: |
220 |
case Types.BOOLEAN: |
221 |
return true; |
222 |
case Types.VARCHAR: |
223 |
case Types.CHAR: |
224 |
case Types.LONGVARCHAR: |
225 |
return true; |
226 |
|
227 |
} |
228 |
|
229 |
return false; |
230 |
} |
231 |
|
232 |
/**
|
233 |
* @return Returns the bCreateTable.
|
234 |
*/
|
235 |
public boolean isCreateTable() { |
236 |
return bCreateTable;
|
237 |
} |
238 |
|
239 |
/**
|
240 |
* @param createTable
|
241 |
* The bCreateTable to set.
|
242 |
*/
|
243 |
public void setCreateTable(boolean createTable) { |
244 |
bCreateTable = createTable; |
245 |
} |
246 |
|
247 |
public FieldDescription[] getOriginalFields() { |
248 |
return lyrDef.getFieldsDesc();
|
249 |
} |
250 |
|
251 |
public void addField(FieldDescription fieldDesc) { |
252 |
fieldManager.addField(fieldDesc); |
253 |
|
254 |
} |
255 |
|
256 |
public FieldDescription removeField(String fieldName) { |
257 |
return fieldManager.removeField(fieldName);
|
258 |
|
259 |
} |
260 |
|
261 |
public void renameField(String antName, String newName) { |
262 |
fieldManager.renameField(antName, newName); |
263 |
|
264 |
} |
265 |
|
266 |
public boolean alterTable() throws EditionException { |
267 |
return fieldManager.alterTable();
|
268 |
} |
269 |
|
270 |
public FieldDescription[] getFields() { |
271 |
return fieldManager.getFields();
|
272 |
} |
273 |
|
274 |
public boolean canAlterTable() { |
275 |
return true; |
276 |
} |
277 |
|
278 |
public boolean canSaveEdits() { |
279 |
// TODO: Revisar los permisos de la tabla en cuesti?n.
|
280 |
try {
|
281 |
return !conex.isReadOnly();
|
282 |
} catch (SQLException e) { |
283 |
// TODO Auto-generated catch block
|
284 |
e.printStackTrace(); |
285 |
return false; |
286 |
} |
287 |
} |
288 |
|
289 |
} |