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