Statistics
| Revision:

root / trunk / extensions / extOracleSpatial / src / es / prodevelop / cit / gvsig / jdbc_spatial / ExportToOracle.java @ 36210

History | View | Annotate | Download (11.1 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 java.awt.Component;
46
import java.sql.Types;
47

    
48
import javax.swing.JOptionPane;
49

    
50
import com.iver.andami.PluginServices;
51
import com.iver.cit.gvsig.exceptions.commands.EditionCommandException;
52
import com.iver.cit.gvsig.fmap.MapContext;
53
import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition;
54
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
55
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
56
import com.iver.cit.gvsig.fmap.drivers.IConnection;
57
import com.iver.cit.gvsig.fmap.drivers.db.utils.ConnectionWithParams;
58
import com.iver.cit.gvsig.fmap.edition.IWriter;
59
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
60
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
61
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
62
import com.prodevelop.cit.gvsig.vectorialdb.wizard.ConnectionChooserPanel;
63

    
64
import es.prodevelop.cit.gvsig.fmap.drivers.jdbc.oracle.OracleSpatialDriver;
65
import es.prodevelop.cit.gvsig.fmap.drivers.jdbc.oracle.OracleSpatialUtils;
66
import es.prodevelop.cit.gvsig.fmap.drivers.jdbc.oracle.OracleSpatialWriter;
67

    
68

    
69
/**
70
 * Writes a layer as an Oracle table.
71
 *
72
 *
73
 * @author jldominguez
74
 *
75
 */
76
public class ExportToOracle {
77
    public void toOracle(MapContext mapContext, FLyrVect layer)
78
        throws EditionCommandException, DriverIOException {
79
        try {
80
            String tableName = "";
81
            boolean valid_name = false;
82

    
83
            while (!valid_name) {
84
                tableName = JOptionPane.showInputDialog(((Component)PluginServices.getMainFrame()),PluginServices.getText(
85
                            this, "intro_tablename"));
86
                valid_name = ((tableName == null) ||
87
                    ((tableName.length() <= (OracleSpatialDriver.MAX_ID_LENGTH -
88
                    3)) && (tableName.indexOf(" ") == -1) &&
89
                    (tableName.length() > 0)));
90

    
91
                if (!valid_name) {
92
                    if (tableName.length() > (OracleSpatialDriver.MAX_ID_LENGTH -
93
                            3)) {
94
                        JOptionPane.showMessageDialog(null,
95
                            PluginServices.getText(this,
96
                                "nombre_demasiado_largo"),
97
                            PluginServices.getText(this, "error"),
98
                            JOptionPane.ERROR_MESSAGE);
99
                    }
100
                    else {
101
                        JOptionPane.showMessageDialog(null,
102
                            PluginServices.getText(this, "nombre_no_valido"),
103
                            PluginServices.getText(this, "error"),
104
                            JOptionPane.ERROR_MESSAGE);
105
                    }
106
                }
107
            }
108

    
109
            if (tableName == null) {
110
                return;
111
            }
112

    
113
            tableName = tableName.toUpperCase();
114

    
115
            ConnectionChooserPanel dlg = new ConnectionChooserPanel(OracleSpatialDriver.NAME);
116
            PluginServices.getMDIManager().addWindow(dlg);
117

    
118
            if (!dlg.isOkPressed()) {
119
                return;
120
            }
121

    
122
            ConnectionWithParams cwp = dlg.getSelectedCWP();
123

    
124
            if (cwp == null) {
125
                return;
126
            }
127

    
128
            IConnection conex = cwp.getConnection();
129

    
130
            DBLayerDefinition dbLayerDef = new DBLayerDefinition();
131
            dbLayerDef.setCatalogName(cwp.getDb());
132
            dbLayerDef.setSchema(cwp.getSchema());
133
            dbLayerDef.setTableName(tableName);
134
            dbLayerDef.setName(tableName);
135
            dbLayerDef.setShapeType(layer.getShapeType());
136

    
137
            SelectableDataSource sds = layer.getRecordset();
138
            FieldDescription[] fieldsDescrip = sds.getFieldsDescription();
139
            dbLayerDef.setFieldsDesc(fieldsDescrip);
140

    
141
            // Creamos el driver. OJO: Hay que a?adir el campo ID a la
142
            // definici?n de campos.
143
            boolean bFound = false;
144

    
145
            for (int i = 0; i < fieldsDescrip.length; i++) {
146
                FieldDescription f = fieldsDescrip[i];
147

    
148
                if (f.getFieldName().compareTo(OracleSpatialDriver.DEFAULT_ID_FIELD_CASE_SENSITIVE) == 0) {
149
                    bFound = true;
150
                    break;
151
                }
152
            }
153

    
154
            // Si no est?, lo a?adimos
155
            if (!bFound) {
156
                int numFieldsAnt = fieldsDescrip.length;
157
                FieldDescription[] newFields = new FieldDescription[dbLayerDef.getFieldsDesc().length +
158
                    1];
159

    
160
                for (int i = 0; i < numFieldsAnt; i++) {
161
                    newFields[i] = fieldsDescrip[i];
162
                }
163

    
164
                newFields[numFieldsAnt] = new FieldDescription();
165
                newFields[numFieldsAnt].setFieldDecimalCount(0);
166
                newFields[numFieldsAnt].setFieldType(Types.INTEGER);
167
                newFields[numFieldsAnt].setFieldLength(7);
168
                newFields[numFieldsAnt].setFieldName(OracleSpatialDriver.DEFAULT_ID_FIELD_CASE_SENSITIVE);
169
                dbLayerDef.setFieldsDesc(newFields);
170
            }
171

    
172
            // addStartIfNotPresent(dbLayerDef, OracleSpatialDriver.ORACLE_ID_FIELD);
173
            // addEndIfNotPresent(dbLayerDef, OracleSpatialDriver.DEFAULT_GEO_FIELD);
174
            dbLayerDef.setFieldGeometry(OracleSpatialDriver.DEFAULT_GEO_FIELD);
175
            dbLayerDef.setFieldID(OracleSpatialDriver.ORACLE_ID_FIELD);
176

    
177
            dbLayerDef.setWhereClause("");
178

    
179
            String strSRID = layer.getProjection().getAbrev().substring(5);
180
            strSRID = mapContext.getProjection().getAbrev().substring(5);
181
            dbLayerDef.setSRID_EPSG(strSRID);
182
            dbLayerDef.setConnection(conex);
183

    
184
            OracleSpatialWriter writer = (OracleSpatialWriter) LayerFactory.getWM()
185
                                                                           .getWriter("Oracle Spatial Writer");
186
            writer.setLyrShapeType(layer.getShapeType());
187

    
188
            // writer.setWriteAll(true);
189
            // writer.setCreateTable(true);
190
            writer.initialize(dbLayerDef);
191

    
192
            int opt = JOptionPane.showConfirmDialog(null,
193
                    PluginServices.getText(this, "almacenar_sc_de_vista"),
194
                    PluginServices.getText(this, "exportando_features"),
195
                    JOptionPane.YES_NO_OPTION);
196

    
197
            boolean savesrs = (opt == JOptionPane.YES_OPTION);
198
            writer.setStoreWithSrid(savesrs);
199

    
200
            String orasrid = "";
201
            boolean geo_cs = false;
202
            
203
            try {
204
                    orasrid = OracleSpatialDriver.epsgSridToOracleSrid(strSRID);
205
                    geo_cs = OracleSpatialUtils.getIsGCS(orasrid, savesrs);
206
            } catch (Exception ex) {
207
                    // not found: same
208
                    orasrid = strSRID;
209
                    geo_cs = false;
210
            }
211
            
212
            
213
            
214
            writer.setGeoCS(geo_cs);
215

    
216
            OracleSpatialDriver oDriver = new OracleSpatialDriver();
217

    
218
            oDriver.setDestProjection(strSRID);
219

    
220
            DBLayerDefinition driver_ldef = cloneDBLyrDef(dbLayerDef);
221

    
222
            addStartIfNotPresent(driver_ldef,
223
                OracleSpatialDriver.ORACLE_ID_FIELD);
224
            oDriver.setLyrDef(driver_ldef);
225
            oDriver.setUserName(cwp.getUser().toUpperCase());
226

    
227
            writer.setDriver(oDriver);
228

    
229
            Object[] params = new Object[2];
230
            params[0] = (IConnection) conex;
231
            params[1] = driver_ldef;
232

    
233
            /*
234
            PostProcessSupport.clearList();
235
            Object[] p = new Object[1];
236
            p[0] = params;
237
            PostProcessSupport.addToPostProcess(oDriver, "setData", p, 1);
238
            */
239
            oDriver.setShapeType(layer.getShapeType());
240

    
241
            writeFeatures(mapContext, layer, writer, oDriver, params);
242
        } catch (Exception e) {
243
            throw new EditionCommandException(layer.getName(), e);
244
        }
245
    }
246

    
247
    private void addStartIfNotPresent(DBLayerDefinition ldef,
248
        String default_id_field) {
249
        FieldDescription[] fdec = ldef.getFieldsDesc();
250
        int size = fdec.length;
251

    
252
        for (int i = 0; i < size; i++) {
253
            FieldDescription f = fdec[i];
254

    
255
            if (f.getFieldName().equalsIgnoreCase(default_id_field)) {
256
                return;
257
            }
258
        }
259

    
260
        FieldDescription[] newFields = new FieldDescription[size + 1];
261

    
262
        for (int i = 0; i < size; i++) {
263
            newFields[i + 1] = fdec[i];
264
        }
265

    
266
        newFields[0] = new FieldDescription();
267
        newFields[0].setFieldDecimalCount(0);
268
        newFields[0].setFieldType(Types.VARCHAR);
269
        newFields[0].setFieldLength(20);
270
        newFields[0].setFieldName(default_id_field);
271
        ldef.setFieldsDesc(newFields);
272
    }
273

    
274
    private void writeFeatures(MapContext mapContext, FLyrVect layer,
275
        IWriter writer, OracleSpatialDriver reader, Object[] setDataParams)
276
        throws DriverIOException {
277
        PluginServices.cancelableBackgroundExecution(new OracleWriteTask(
278
                mapContext, layer, (OracleSpatialWriter) writer, reader, setDataParams));
279
    }
280

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

    
300
        return resp;
301
    }
302

    
303

    
304

    
305
}
306

    
307
// [eiel-gestion-conexiones]
308