root / branches / v10 / extensions / extOracleSpatial / src / es / prodevelop / cit / gvsig / jdbc_spatial / ExportToOracle.java @ 13991
History | View | Annotate | Download (12 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2006 Prodevelop 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 Ib??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 |
* Prodevelop Integraci?n de Tecnolog?as SL
|
34 |
* Conde Salvatierra de ?lava , 34-10
|
35 |
* 46004 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963 510 612
|
39 |
* +34 963 510 968
|
40 |
* gis@prodevelop.es
|
41 |
* http://www.prodevelop.es
|
42 |
*/
|
43 |
package es.prodevelop.cit.gvsig.jdbc_spatial; |
44 |
|
45 |
import com.hardcode.driverManager.DriverLoadException; |
46 |
|
47 |
import com.iver.andami.PluginServices; |
48 |
|
49 |
import com.iver.cit.gvsig.fmap.DriverException; |
50 |
import com.iver.cit.gvsig.fmap.MapContext; |
51 |
import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition; |
52 |
import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
53 |
import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
54 |
import com.iver.cit.gvsig.fmap.drivers.jdbc.utils.ConnectionWithParams; |
55 |
import com.iver.cit.gvsig.fmap.drivers.jdbc.utils.SingleJDBCConnectionManager; |
56 |
import com.iver.cit.gvsig.fmap.edition.EditionException; |
57 |
import com.iver.cit.gvsig.fmap.edition.IWriter; |
58 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
59 |
import com.iver.cit.gvsig.fmap.layers.LayerFactory; |
60 |
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource; |
61 |
|
62 |
import com.iver.utiles.PostProcessSupport; |
63 |
|
64 |
import es.prodevelop.cit.gvsig.fmap.drivers.jdbc.oracle.OracleSpatialDriver; |
65 |
import es.prodevelop.cit.gvsig.fmap.drivers.jdbc.oracle.OracleSpatialWriter; |
66 |
import es.prodevelop.cit.gvsig.jdbc_spatial.gui.jdbcwizard.JDBConnectionParamsDialog; |
67 |
import es.prodevelop.cit.gvsig.jdbc_spatial.gui.jdbcwizard.OracleConnectionChooserPanel; |
68 |
|
69 |
import java.awt.Component; |
70 |
import java.sql.Connection; |
71 |
import java.sql.SQLException; |
72 |
import java.sql.Types; |
73 |
|
74 |
import javax.swing.JOptionPane; |
75 |
|
76 |
|
77 |
/**
|
78 |
* Writes a layer as a Oracle table.
|
79 |
*
|
80 |
*
|
81 |
* @author jldominguez
|
82 |
*
|
83 |
*/
|
84 |
public class ExportToOracle { |
85 |
public void toOracle(MapContext mapContext, FLyrVect layer) |
86 |
throws EditionException, DriverIOException {
|
87 |
try {
|
88 |
String tableName = ""; |
89 |
boolean valid_name = false; |
90 |
|
91 |
while (!valid_name) {
|
92 |
tableName = JOptionPane.showInputDialog(PluginServices.getText(
|
93 |
this, "intro_tablename")); |
94 |
valid_name = ((tableName == null) ||
|
95 |
((tableName.length() <= (OracleSpatialDriver.MAX_ID_LENGTH - |
96 |
3)) && (tableName.indexOf(" ") == -1) && |
97 |
(tableName.length() > 0)));
|
98 |
|
99 |
if (!valid_name) {
|
100 |
if (tableName.length() > (OracleSpatialDriver.MAX_ID_LENGTH -
|
101 |
3)) {
|
102 |
JOptionPane.showMessageDialog(null, |
103 |
PluginServices.getText(this,
|
104 |
"nombre_demasiado_largo"),
|
105 |
PluginServices.getText(this, "error"), |
106 |
JOptionPane.ERROR_MESSAGE);
|
107 |
} |
108 |
else {
|
109 |
JOptionPane.showMessageDialog(null, |
110 |
PluginServices.getText(this, "nombre_no_valido"), |
111 |
PluginServices.getText(this, "error"), |
112 |
JOptionPane.ERROR_MESSAGE);
|
113 |
} |
114 |
} |
115 |
} |
116 |
|
117 |
if (tableName == null) { |
118 |
return;
|
119 |
} |
120 |
|
121 |
tableName = tableName.toUpperCase(); |
122 |
|
123 |
OracleConnectionChooserPanel dlg = new OracleConnectionChooserPanel();
|
124 |
PluginServices.getMDIManager().addWindow(dlg); |
125 |
|
126 |
if (!dlg.isOkPressed()) {
|
127 |
return;
|
128 |
} |
129 |
|
130 |
ConnectionWithParams cwp = dlg.getSelectedCWP(); |
131 |
|
132 |
if (cwp == null) { |
133 |
return;
|
134 |
} |
135 |
|
136 |
Connection conex = cwp.getConnection();
|
137 |
|
138 |
DBLayerDefinition dbLayerDef = new DBLayerDefinition();
|
139 |
dbLayerDef.setCatalogName(cwp.getDb()); |
140 |
dbLayerDef.setTableName(tableName); |
141 |
dbLayerDef.setName(tableName); |
142 |
dbLayerDef.setShapeType(layer.getShapeType()); |
143 |
|
144 |
SelectableDataSource sds = layer.getRecordset(); |
145 |
FieldDescription[] fieldsDescrip = sds.getFieldsDescription();
|
146 |
dbLayerDef.setFieldsDesc(fieldsDescrip); |
147 |
|
148 |
// Creamos el driver. OJO: Hay que a?adir el campo ID a la
|
149 |
// definici?n de campos.
|
150 |
boolean bFound = false; |
151 |
|
152 |
for (int i = 0; i < fieldsDescrip.length; i++) { |
153 |
FieldDescription f = fieldsDescrip[i]; |
154 |
|
155 |
if (f.getFieldName()
|
156 |
.equalsIgnoreCase(OracleSpatialDriver.DEFAULT_ID_FIELD)) { |
157 |
bFound = true;
|
158 |
|
159 |
break;
|
160 |
} |
161 |
} |
162 |
|
163 |
// Si no est?, lo a?adimos
|
164 |
if (!bFound) {
|
165 |
int numFieldsAnt = fieldsDescrip.length;
|
166 |
FieldDescription[] newFields = new FieldDescription[dbLayerDef.getFieldsDesc().length + |
167 |
1];
|
168 |
|
169 |
for (int i = 0; i < numFieldsAnt; i++) { |
170 |
newFields[i] = fieldsDescrip[i]; |
171 |
} |
172 |
|
173 |
newFields[numFieldsAnt] = new FieldDescription();
|
174 |
newFields[numFieldsAnt].setFieldDecimalCount(0);
|
175 |
newFields[numFieldsAnt].setFieldType(Types.INTEGER);
|
176 |
newFields[numFieldsAnt].setFieldLength(7);
|
177 |
newFields[numFieldsAnt].setFieldName(OracleSpatialDriver.DEFAULT_ID_FIELD); |
178 |
dbLayerDef.setFieldsDesc(newFields); |
179 |
} |
180 |
|
181 |
// addStartIfNotPresent(dbLayerDef, OracleSpatialDriver.ORACLE_ID_FIELD);
|
182 |
// addEndIfNotPresent(dbLayerDef, OracleSpatialDriver.DEFAULT_GEO_FIELD);
|
183 |
dbLayerDef.setFieldGeometry(OracleSpatialDriver.DEFAULT_GEO_FIELD); |
184 |
dbLayerDef.setFieldID(OracleSpatialDriver.ORACLE_ID_FIELD); |
185 |
|
186 |
dbLayerDef.setWhereClause("");
|
187 |
|
188 |
String strSRID = layer.getProjection().getAbrev().substring(5); |
189 |
strSRID = mapContext.getProjection().getAbrev().substring(5);
|
190 |
dbLayerDef.setSRID_EPSG(strSRID); |
191 |
dbLayerDef.setConnection(conex); |
192 |
|
193 |
OracleSpatialWriter writer = (OracleSpatialWriter) LayerFactory.getWM() |
194 |
.getWriter("Oracle Spatial Writer");
|
195 |
writer.setLyrShapeType(layer.getShapeType()); |
196 |
|
197 |
// writer.setWriteAll(true);
|
198 |
// writer.setCreateTable(true);
|
199 |
writer.initialize(dbLayerDef); |
200 |
|
201 |
int opt = JOptionPane.showConfirmDialog(null, |
202 |
PluginServices.getText(this, "almacenar_sc_de_vista"), |
203 |
PluginServices.getText(this, "exportando_features"), |
204 |
JOptionPane.YES_NO_OPTION);
|
205 |
|
206 |
boolean savesrs = (opt == JOptionPane.YES_OPTION); |
207 |
writer.setStoreWithSrid(savesrs); |
208 |
|
209 |
String orasrid = OracleSpatialDriver.epsgSridToOracleSrid(strSRID);
|
210 |
boolean geo_cs = OracleSpatialDriver.getIsGCS(orasrid, savesrs);
|
211 |
writer.setGeoCS(geo_cs); |
212 |
|
213 |
OracleSpatialDriver oDriver = new OracleSpatialDriver();
|
214 |
|
215 |
oDriver.setDestProjection(strSRID); |
216 |
|
217 |
DBLayerDefinition driver_ldef = cloneDBLyrDef(dbLayerDef); |
218 |
|
219 |
addStartIfNotPresent(driver_ldef, |
220 |
OracleSpatialDriver.ORACLE_ID_FIELD); |
221 |
oDriver.setLyrDef(driver_ldef); |
222 |
|
223 |
writer.setDriver(oDriver); |
224 |
|
225 |
Object[] params = new Object[2]; |
226 |
params[0] = (java.sql.Connection) conex;
|
227 |
params[1] = driver_ldef;
|
228 |
|
229 |
/*
|
230 |
PostProcessSupport.clearList();
|
231 |
Object[] p = new Object[1];
|
232 |
p[0] = params;
|
233 |
PostProcessSupport.addToPostProcess(oDriver, "setData", p, 1);
|
234 |
*/
|
235 |
|
236 |
writeFeatures(mapContext, layer, writer, oDriver, params); |
237 |
} |
238 |
catch (DriverException e) {
|
239 |
e.printStackTrace(); |
240 |
throw new EditionException(e); |
241 |
} |
242 |
catch (DriverLoadException e) {
|
243 |
throw new EditionException(e); |
244 |
} |
245 |
catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
|
246 |
e.printStackTrace(); |
247 |
throw new EditionException(e); |
248 |
} |
249 |
catch (Exception e) { |
250 |
e.printStackTrace(); |
251 |
throw new DriverIOException(e.getMessage()); |
252 |
} |
253 |
} |
254 |
|
255 |
private void addEndIfNotPresent(DBLayerDefinition ldef, |
256 |
String default_geo_field) {
|
257 |
FieldDescription[] fdec = ldef.getFieldsDesc();
|
258 |
int size = fdec.length;
|
259 |
|
260 |
for (int i = 0; i < size; i++) { |
261 |
FieldDescription f = fdec[i]; |
262 |
|
263 |
if (f.getFieldName().equalsIgnoreCase(default_geo_field)) {
|
264 |
return;
|
265 |
} |
266 |
} |
267 |
|
268 |
FieldDescription[] newFields = new FieldDescription[size + 1]; |
269 |
|
270 |
for (int i = 0; i < size; i++) { |
271 |
newFields[i] = fdec[i]; |
272 |
} |
273 |
|
274 |
newFields[size] = new FieldDescription();
|
275 |
newFields[size].setFieldDecimalCount(0);
|
276 |
newFields[size].setFieldType(Types.STRUCT);
|
277 |
newFields[size].setFieldLength(10);
|
278 |
newFields[size].setFieldName(default_geo_field); |
279 |
ldef.setFieldsDesc(newFields); |
280 |
} |
281 |
|
282 |
private void addStartIfNotPresent(DBLayerDefinition ldef, |
283 |
String default_id_field) {
|
284 |
FieldDescription[] fdec = ldef.getFieldsDesc();
|
285 |
int size = fdec.length;
|
286 |
|
287 |
for (int i = 0; i < size; i++) { |
288 |
FieldDescription f = fdec[i]; |
289 |
|
290 |
if (f.getFieldName().equalsIgnoreCase(default_id_field)) {
|
291 |
return;
|
292 |
} |
293 |
} |
294 |
|
295 |
FieldDescription[] newFields = new FieldDescription[size + 1]; |
296 |
|
297 |
for (int i = 0; i < size; i++) { |
298 |
newFields[i + 1] = fdec[i];
|
299 |
} |
300 |
|
301 |
newFields[0] = new FieldDescription(); |
302 |
newFields[0].setFieldDecimalCount(0); |
303 |
newFields[0].setFieldType(Types.VARCHAR); |
304 |
newFields[0].setFieldLength(20); |
305 |
newFields[0].setFieldName(default_id_field);
|
306 |
ldef.setFieldsDesc(newFields); |
307 |
} |
308 |
|
309 |
private void writeFeatures(MapContext mapContext, FLyrVect layer, |
310 |
IWriter writer, OracleSpatialDriver reader, Object[] setDataParams) |
311 |
throws DriverException, DriverIOException {
|
312 |
PluginServices.cancelableBackgroundExecution(new OracleWriteTask(
|
313 |
mapContext, layer, (OracleSpatialWriter) writer, reader, setDataParams)); |
314 |
} |
315 |
|
316 |
private DBLayerDefinition cloneDBLyrDef(DBLayerDefinition ldef) {
|
317 |
DBLayerDefinition resp = new DBLayerDefinition();
|
318 |
resp.setCatalogName(ldef.getCatalogName()); |
319 |
resp.setTableName(ldef.getTableName()); |
320 |
resp.setName(ldef.getName()); |
321 |
resp.setShapeType(ldef.getShapeType()); |
322 |
resp.setFieldsDesc(ldef.getFieldsDesc()); |
323 |
resp.setFieldGeometry(ldef.getFieldGeometry()); |
324 |
resp.setFieldID(ldef.getFieldID()); |
325 |
resp.setWhereClause(ldef.getWhereClause()); |
326 |
resp.setSRID_EPSG(ldef.getSRID_EPSG()); |
327 |
resp.setConnection(ldef.getConnection()); |
328 |
// NO USAR ESTA FUNCI?N!!
|
329 |
// TODO: DEPRECARLA DE ALGUNA FORMA, O AVISAR DE QUE NO
|
330 |
// SE USE CON LOS WRITERS.
|
331 |
// HAY QUE USAR SETFIELDSDESC
|
332 |
// resp.setFieldNames(ldef.getFieldNames());
|
333 |
|
334 |
return resp;
|
335 |
} |
336 |
|
337 |
|
338 |
|
339 |
} |