Statistics
| Revision:

svn-gvsig-desktop / branches / v10 / extensions / extOracleSpatial / src / es / prodevelop / cit / gvsig / jdbc_spatial / ExportToOracle.java @ 16230

History | View | Annotate | Download (11.2 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 an 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
            oDriver.setShapeType(layer.getShapeType());
234

    
235
            writeFeatures(mapContext, layer, writer, oDriver, params);
236
        }
237
        catch (DriverException e) {
238
            e.printStackTrace();
239
            throw new EditionException(e);
240
        }
241
        catch (DriverLoadException e) {
242
            throw new EditionException(e);
243
        }
244
        catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
245
            e.printStackTrace();
246
            throw new EditionException(e);
247
        }
248
        catch (Exception e) {
249
            e.printStackTrace();
250
            throw new DriverIOException(e.getMessage());
251
        }
252
    }
253

    
254
    private void addStartIfNotPresent(DBLayerDefinition ldef,
255
        String default_id_field) {
256
        FieldDescription[] fdec = ldef.getFieldsDesc();
257
        int size = fdec.length;
258

    
259
        for (int i = 0; i < size; i++) {
260
            FieldDescription f = fdec[i];
261

    
262
            if (f.getFieldName().equalsIgnoreCase(default_id_field)) {
263
                return;
264
            }
265
        }
266

    
267
        FieldDescription[] newFields = new FieldDescription[size + 1];
268

    
269
        for (int i = 0; i < size; i++) {
270
            newFields[i + 1] = fdec[i];
271
        }
272

    
273
        newFields[0] = new FieldDescription();
274
        newFields[0].setFieldDecimalCount(0);
275
        newFields[0].setFieldType(Types.VARCHAR);
276
        newFields[0].setFieldLength(20);
277
        newFields[0].setFieldName(default_id_field);
278
        ldef.setFieldsDesc(newFields);
279
    }
280

    
281
    private void writeFeatures(MapContext mapContext, FLyrVect layer,
282
        IWriter writer, OracleSpatialDriver reader, Object[] setDataParams)
283
        throws DriverException, DriverIOException {
284
        PluginServices.cancelableBackgroundExecution(new OracleWriteTask(
285
                mapContext, layer, (OracleSpatialWriter) writer, reader, setDataParams));
286
    }
287

    
288
    private DBLayerDefinition cloneDBLyrDef(DBLayerDefinition ldef) {
289
        DBLayerDefinition resp = new DBLayerDefinition();
290
        resp.setCatalogName(ldef.getCatalogName());
291
        resp.setSchema(ldef.getSchema());
292
        resp.setTableName(ldef.getTableName());
293
        resp.setName(ldef.getName());
294
        resp.setShapeType(ldef.getShapeType());
295
        resp.setFieldsDesc(ldef.getFieldsDesc());
296
        resp.setFieldGeometry(ldef.getFieldGeometry());
297
        resp.setFieldID(ldef.getFieldID());
298
        resp.setWhereClause(ldef.getWhereClause());
299
        resp.setSRID_EPSG(ldef.getSRID_EPSG());
300
        resp.setConnection(ldef.getConnection());
301
        // NO USAR ESTA FUNCI?N!!
302
        // TODO: DEPRECARLA DE ALGUNA FORMA, O AVISAR DE QUE NO
303
        // SE USE CON LOS WRITERS.
304
        // HAY QUE USAR SETFIELDSDESC
305
        // resp.setFieldNames(ldef.getFieldNames());
306

    
307
        return resp;
308
    }
309

    
310

    
311

    
312
}