Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extJDBC / src / com / iver / cit / gvsig / jdbc_spatial / ImportLayer.java @ 3207

History | View | Annotate | Download (7.89 KB)

1
/*
2
 * Created on 25-oct-2005
3
 *
4
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
5
 * 
6
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
7
 * 
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 *  
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 * 
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21
 *  
22
 * For more information, contact:
23
 *
24
 *  Generalitat Valenciana
25
 *   Conselleria d'Infraestructures i Transport
26
 *   Av. Blasco Ib??ez, 50
27
 *   46010 VALENCIA
28
 *   SPAIN
29
 *
30
 *      +34 963862235
31
 *   gvsig@gva.es
32
 *      www.gvsig.gva.es
33
 * 
34
 *    or
35
 * 
36
 *   IVER T.I. S.A
37
 *   Salamanca 50
38
 *   46005 Valencia
39
 *   Spain
40
 * 
41
 *   +34 963163400
42
 *   dac@iver.es
43
 */
44
package com.iver.cit.gvsig.jdbc_spatial;
45

    
46
import java.sql.Connection;
47
import java.sql.DriverManager;
48
import java.sql.SQLException;
49
import java.sql.Statement;
50
import java.util.ArrayList;
51

    
52
import javax.swing.JOptionPane;
53

    
54
import com.hardcode.driverManager.DriverLoadException;
55
import com.iver.andami.PluginServices;
56
import com.iver.andami.messages.NotificationManager;
57
import com.iver.andami.plugins.Extension;
58
import com.iver.cit.gvsig.fmap.DriverException;
59
import com.iver.cit.gvsig.fmap.MapControl;
60
import com.iver.cit.gvsig.fmap.core.FShape;
61
import com.iver.cit.gvsig.fmap.core.IFeature;
62
import com.iver.cit.gvsig.fmap.core.IGeometry;
63
import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition;
64
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
65
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
66
import com.iver.cit.gvsig.fmap.drivers.VectorialJDBCDriver;
67
import com.iver.cit.gvsig.fmap.layers.FLayer;
68
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
69
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
70
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
71
import com.iver.cit.gvsig.fmap.layers.VectorialAdapter;
72
import com.iver.cit.gvsig.gui.View;
73
import com.iver.cit.gvsig.jdbc_spatial.gui.jdbcwizard.ConnectionSettings;
74
import com.iver.cit.gvsig.jdbc_spatial.util.PostGIS;
75

    
76
public class ImportLayer implements Extension {
77

    
78
    public void inicializar() {
79
        // TODO Auto-generated method stub
80

    
81
    }
82

    
83
    public void execute(String actionCommand) {
84
        View vista = (View) PluginServices.getMDIManager().getActiveView();
85
        MapControl mapCtrl = vista.getMapControl();
86
        FLayer[]  activeLayers = 
87
        mapCtrl.getMapContext().getLayers().getActives();
88
        FLyrVect lyrVect = (FLyrVect) activeLayers[0];
89
        VectorialAdapter adapter =  lyrVect.getSource();
90
        try {
91
            adapter.start();
92
            
93
            SelectableDataSource rsSel = lyrVect.getRecordset();
94
            
95
            int sc = adapter.getShapeCount();
96
            
97
            ArrayList newList = new ArrayList();
98
            DlgConnection dlg = new DlgConnection();
99
            dlg.setModal(true);
100
            dlg.setVisible(true);
101
            ConnectionSettings cs = dlg.getConnSettings();
102
            if (cs == null)
103
                return;
104
            Connection conex = DriverManager.getConnection(cs.getConnectionString(), cs.getUser(),
105
                    cs.getPassw());
106

    
107
            Statement st = conex.createStatement();
108
            DBLayerDefinition dbLayerDef = new DBLayerDefinition();
109

    
110
            dbLayerDef.setCatalogName(cs.getDb());
111
            dbLayerDef.setTableName(lyrVect.getName());
112
            String strGeometryFieldType = "GEOMETRY";
113
           /*  switch (lyrVect.getShapeType())
114
            {
115
                case FShape.POINT:
116
                    strGeometryFieldType = XTypes.fieldTypeToString(XTypes.POINT2D);
117
                    break;
118
                case FShape.LINE:
119
                    strGeometryFieldType = XTypes.fieldTypeToString(XTypes.LINE2D);
120
                    break;
121
                case FShape.POLYGON:
122
                    strGeometryFieldType = XTypes.fieldTypeToString(XTypes.POLYGON2D);
123
                    break;
124
                case FShape.MULTI:
125
                    strGeometryFieldType = XTypes.fieldTypeToString(XTypes.MULTI2D);
126
                    break;
127
                    
128
            } */
129
            dbLayerDef.setFieldGeometry("the_geom");
130
            FieldDescription[] fieldsDescrip = new FieldDescription[rsSel.getFieldNames().length];
131
            dbLayerDef.setFieldNames(rsSel.getFieldNames());
132
            for (int i=0; i < rsSel.getFieldNames().length; i++)
133
            {
134
                fieldsDescrip[i] = new FieldDescription();
135
                fieldsDescrip[i].setFieldType(rsSel.getFieldType(i));
136
                fieldsDescrip[i].setFieldName(rsSel.getFieldName(i));
137
                // TODO: Por ahora le ponemos 200, a falta
138
                // de recompilar GDBMS con la posibilidad
139
                // de obtener el ancho de un campo.
140
                fieldsDescrip[i].setFieldLength(200);
141
            }
142
            String strSRID = lyrVect.getProjection().getAbrev().substring(5);
143
            dbLayerDef.setSRID_EPSG(strSRID);
144
            st.execute("DROP TABLE " + dbLayerDef.getTableName() + ";");
145
            
146
            String sqlCreate = PostGIS.getSqlCreateSpatialTable(dbLayerDef, fieldsDescrip);
147
            System.out.println("sqlCreate =" + sqlCreate);
148
            st.execute(sqlCreate);
149
            int dimension = 2;
150
            
151
            String sqlAlter = PostGIS.getSqlAlterTable(dbLayerDef, strSRID, strGeometryFieldType, dimension);
152
            System.out.println("sqlAlter =" + sqlAlter);
153
            st.execute(sqlAlter);
154
            // CREATE TABLE PARKS ( PARK_ID int4, PARK_NAME varchar(128), PARK_DATE date, PARK_TYPE varchar(2) );
155
            // SELECT AddGeometryColumn('parks_db', 'parks', 'park_geom', 128, 'MULTIPOLYGON', 2 );
156
            
157
            /* BEGIN;
158
            INSERT INTO ROADS_GEOM (ID,GEOM,NAME ) VALUES (1,GeometryFromText('LINESTRING(191232 243118,191108 243242)',-1),'Jeff Rd'); 
159
            INSERT INTO ROADS_GEOM (ID,GEOM,NAME ) VALUES (2,GeometryFromText('LINESTRING(189141 244158,189265 244817)',-1),'Geordie Rd'); 
160
            COMMIT; */       
161
            conex.setAutoCommit(false);
162
            for (int i = 0; i < sc; i++)
163
            {
164
                // IGeometry geom = adapter.getShape(i);
165
                IFeature feat = adapter.getFeature(i);
166
                String sqlInsert = PostGIS.getSqlInsertFeature(dbLayerDef, feat);
167
                // System.out.println("sqlInsert =" + sqlInsert);
168
                st.execute(sqlInsert);
169
            }
170
            conex.setAutoCommit(true);
171
            conex.close();
172
            adapter.stop();
173
            JOptionPane.showMessageDialog(null, "Capa importada.");
174
        } catch (Exception e) {
175
            NotificationManager.addError("Error en la carga.", e);
176
        }
177
        
178

    
179
    }
180
    private String[] getDriverNames(){
181
        Class[] classes = new Class[] { VectorialJDBCDriver.class };
182

    
183
        ArrayList ret = new ArrayList();
184
        String[] driverNames = LayerFactory.getDM().getDriverNames();
185

    
186
        for (int i = 0; i < driverNames.length; i++) {
187
            boolean is = false;
188

    
189
            for (int j = 0; j < classes.length; j++) {
190
                if (LayerFactory.getDM().isA(driverNames[i], classes[j])) {
191
                    ret.add(driverNames[i]);
192
                }
193
            }
194
        }
195

    
196
        return (String[]) ret.toArray(new String[0]);
197
        
198
    }
199

    
200
    public boolean isEnabled() {
201
        // TODO Auto-generated method stub
202
        return true;
203
    }
204

    
205
    public boolean isVisible() {
206
        // TODO Auto-generated method stub
207
        return true;
208
    }
209

    
210
}