Statistics
| Revision:

root / trunk / extensions / extJDBC / src / com / iver / cit / gvsig / fmap / drivers / jdbc / mysql / MySQLSpatialWriter.java @ 9765

History | View | Annotate | Download (7.88 KB)

1
/*
2
 * Created on 15-ene-2007
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
/* CVS MESSAGES:
45
*
46
* $Id: MySQLSpatialWriter.java 9765 2007-01-16 20:03:36Z azabala $
47
* $Log$
48
* Revision 1.2  2007-01-16 20:03:36  azabala
49
* change of the writer name
50
*
51
* Revision 1.1  2007/01/16 13:11:20  azabala
52
* first version in cvs
53
*
54
* Revision 1.1  2007/01/15 20:15:35  azabala
55
* *** empty log message ***
56
*
57
*
58
*/
59
package com.iver.cit.gvsig.fmap.drivers.jdbc.mysql;
60
import java.sql.Connection;
61
import java.sql.SQLException;
62
import java.sql.Statement;
63
import java.sql.Types;
64

    
65
import com.iver.cit.gvsig.fmap.core.FShape;
66
import com.iver.cit.gvsig.fmap.core.IFeature;
67
import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition;
68
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
69
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition;
70
import com.iver.cit.gvsig.fmap.edition.EditionException;
71
import com.iver.cit.gvsig.fmap.edition.IFieldManager;
72
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
73
import com.iver.cit.gvsig.fmap.edition.ISpatialWriter;
74
import com.iver.cit.gvsig.fmap.edition.fieldmanagers.JdbcFieldManager;
75
import com.iver.cit.gvsig.fmap.edition.writers.AbstractWriter;
76

    
77
public class MySQLSpatialWriter extends AbstractWriter 
78
                implements ISpatialWriter, IFieldManager {
79

    
80
        private int numRows;
81

    
82
        private DBLayerDefinition lyrDef;
83

    
84
        private Connection conex;
85

    
86
        private Statement st;
87

    
88
        /**
89
         * flag to mark if writer must create the table or it must
90
         * add records to an existing one
91
         * 
92
         */
93
        private boolean bCreateTable;
94

    
95
        private boolean bWriteAll;
96
        
97
        private MySql mySql = new MySql();
98
        
99
        /*
100
         * TODO
101
         * Ver si en MySQL los tipos de datos (numeric, etc.)
102
         * se nombran as? tambi?n
103
         * */
104
        private JdbcFieldManager fieldManager;
105
        
106
        
107
//        public void initialize(Connection conex)throws SQLException{
108
//                this.conex = conex;
109
//                init();
110
//        }
111
        
112
        
113
        
114
        public void initialize(ITableDefinition lyrD) throws EditionException {
115
                super.initialize(lyrD);
116
                this.lyrDef = (DBLayerDefinition) lyrD;
117
                conex = lyrDef.getConnection();
118
                try {
119
                        init();
120
                } catch (SQLException e) {
121
                        throw new EditionException(e);
122
                }
123
        }
124
        
125
        private void init() throws SQLException{
126
                
127
                        st = conex.createStatement();
128
                        
129
                        /*
130
                         * y en caso de que sea false usar "CREATE TABLE IF NOT EXISTS" en el 
131
                         * else
132
                         * (AZO)
133
                         * 
134
                         * */
135
                        if (bCreateTable) {
136
                                try {
137
                                        st.execute("DROP TABLE " + lyrDef.getTableName() + ";");
138
                                } catch (SQLException e1) {
139
                                }
140
                                //In MySQL you can add geometry column in CREATE TABLE statement
141
                                String sqlCreate = mySql.getSqlCreateSpatialTable(lyrDef,
142
                                                                                                lyrDef.getFieldsDesc(), 
143
                                                                                                true);
144
                                st.execute(sqlCreate);
145
                                conex.commit();
146
                        }//if
147
                        conex.setAutoCommit(false);
148
                        fieldManager = new JdbcFieldManager(conex, lyrDef.getTableName());
149
        }
150

    
151
        
152
        public void preProcess() throws EditionException {
153
                numRows = 0;
154
                
155
        // ATENTION: We will transform (in PostGIS class; doubleQuote())
156
        // to UTF-8 strings. Then, we tell the PostgreSQL server
157
        // that we will use UTF-8, and it can translate
158
        // to its charset
159
        // Note: we have to translate to UTF-8 because
160
        // the server cannot manage UTF-16
161
                try {
162
                        conex.setAutoCommit(false);
163
                        conex.rollback();                        
164
                        alterTable();
165
                } catch (SQLException e) {
166
                        // TODO Auto-generated catch block
167
                        e.printStackTrace();
168
                }
169
        }
170

    
171
        public void process(IRowEdited row) throws EditionException {
172

    
173
                String sqlInsert;
174
                try {
175
                        switch (row.getStatus()) {
176
                        case IRowEdited.STATUS_ADDED:
177
                                IFeature feat = (IFeature) row.getLinkedRow();
178
                                sqlInsert = mySql.getSqlInsertFeature(lyrDef, feat);
179
                                st.execute(sqlInsert);
180
                                break;
181
                                
182
                        case IRowEdited.STATUS_MODIFIED:
183
                                IFeature featM = (IFeature) row.getLinkedRow();
184
                                if (bWriteAll) {
185
                                        sqlInsert = mySql.getSqlInsertFeature(lyrDef, featM);
186
                                        System.out.println("sql = " + sqlInsert);
187
                                        st.execute(sqlInsert);
188
                                } else {
189
                                        String sqlModify = mySql.getSqlModifyFeature(lyrDef, featM);
190
                                        st.execute(sqlModify);
191
                                }
192
                                break;
193
                                
194
                        case IRowEdited.STATUS_ORIGINAL:
195
                                IFeature featO = (IFeature) row.getLinkedRow();
196
                                if (bWriteAll) {
197
                                        sqlInsert = mySql.getSqlInsertFeature(lyrDef, featO);
198
                                        st.execute(sqlInsert);
199
                                }
200
                                break;
201
                                
202
                        case IRowEdited.STATUS_DELETED:
203
                                String sqlDelete = mySql.getSqlDeleteFeature(lyrDef, row);
204
                                System.out.println("sql = " + sqlDelete);
205
                                st.execute(sqlDelete);
206

    
207
                                break;
208
                        }
209

    
210
                        numRows++;
211
                } catch (SQLException e) {
212
                        e.printStackTrace();
213
                        throw new EditionException(e);
214
                }
215

    
216
        }
217

    
218
        public void postProcess() throws EditionException {
219
                try {
220
                        conex.setAutoCommit(true);
221
                } catch (SQLException e) {
222
                        e.printStackTrace();
223
                        throw new EditionException(e);
224
                }
225
        }
226

    
227
        public String getName() {
228
                return "MySQL Spatial Writer";
229
        }
230

    
231
        public boolean canWriteGeometry(int gvSIGgeometryType) {
232
                switch (gvSIGgeometryType) {
233
                case FShape.POINT:
234
                        return true;
235
                case FShape.LINE:
236
                        return true;
237
                case FShape.POLYGON:
238
                        return true;
239
                case FShape.ARC:
240
                        return false;
241
                case FShape.ELLIPSE:
242
                        return false;
243
                case FShape.MULTIPOINT:
244
                        return true;
245
                case FShape.TEXT:
246
                        return false;
247
                }
248
                return false;
249
        }
250

    
251
        public boolean canWriteAttribute(int sqlType) {
252
                switch (sqlType) {
253
                case Types.DOUBLE:
254
                case Types.FLOAT:
255
                case Types.INTEGER:
256
                case Types.BIGINT:
257
                        return true;
258
                case Types.DATE:
259
                        return true;
260
                case Types.BIT:
261
                case Types.BOOLEAN:
262
                        return true;
263
                case Types.VARCHAR:
264
                case Types.CHAR:
265
                case Types.LONGVARCHAR:
266
                        return true; 
267

    
268
                }
269

    
270
                return false;
271
        }
272

    
273
        /**
274
         * @return Returns the bCreateTable.
275
         */
276
        public boolean isCreateTable() {
277
                return bCreateTable;
278
        }
279

    
280
        /**
281
         * @param createTable
282
         *            The bCreateTable to set.
283
         */
284
        public void setCreateTable(boolean createTable) {
285
                bCreateTable = createTable;
286
        }
287

    
288
        /**
289
         * @return Returns the bWriteAll.
290
         */
291
        public boolean isWriteAll() {
292
                return bWriteAll;
293
        }
294

    
295
        /**
296
         * @param writeAll
297
         *            The bWriteAll to set.
298
         */
299
        public void setWriteAll(boolean writeAll) {
300
                bWriteAll = writeAll;
301
        }
302

    
303

    
304
        public FieldDescription[] getOriginalFields() {
305
                return lyrDef.getFieldsDesc();
306
        }
307

    
308
        public void addField(FieldDescription fieldDesc) {
309
                fieldManager.addField(fieldDesc);
310
                
311
        }
312

    
313
        public FieldDescription removeField(String fieldName) {
314
                return fieldManager.removeField(fieldName);
315
                
316
        }
317

    
318
        public void renameField(String antName, String newName) {
319
                fieldManager.renameField(antName, newName);
320
                
321
        }
322

    
323
        public boolean alterTable() throws EditionException {
324
                return fieldManager.alterTable();
325
        }
326

    
327
        public FieldDescription[] getFields() {
328
                return fieldManager.getFields();
329
        }
330

    
331
        public boolean canAlterTable() {
332
                return true;
333
        }
334

    
335
        public boolean canSaveEdits() {
336
                // TODO: Revisar los permisos de la tabla en cuesti?n.
337
                try {
338
                        return !conex.isReadOnly();
339
                } catch (SQLException e) {
340
                        // TODO Auto-generated catch block
341
                        e.printStackTrace();
342
                        return false;
343
                }
344
        }
345

    
346
}