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