Statistics
| Revision:

root / trunk / libraries / libFMap_dataDB / src-test / org / gvsig / data / datastores / vectorial / db / jdbc / h2 / SHP2H2FeaturesVisitor.java @ 20920

History | View | Annotate | Download (7.61 KB)

1
package org.gvsig.data.datastores.vectorial.db.jdbc.h2;
2

    
3
import java.io.IOException;
4
import java.sql.Connection;
5
import java.sql.Date;
6
import java.sql.DriverManager;
7
import java.sql.PreparedStatement;
8
import java.sql.SQLException;
9
import java.sql.Statement;
10
import java.util.Iterator;
11

    
12
import org.gvsig.data.datastores.vectorial.db.jdbc.WKBParser2;
13
import org.gvsig.data.datastores.vectorial.db.jdbc.h2.H2StoreParameters;
14
import org.gvsig.data.vectorial.IFeature;
15
import org.gvsig.data.vectorial.IFeatureAttributeDescriptor;
16
import org.gvsig.data.vectorial.IFeatureType;
17
import org.gvsig.data.vectorial.visitor.FeaturesVisitor;
18
import org.gvsig.exceptions.BaseException;
19
import org.gvsig.fmap.geom.Geometry;
20

    
21
import com.vividsolutions.jts.io.WKBWriter;
22

    
23

    
24
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
25
 *
26
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
27
 *
28
 * This program is free software; you can redistribute it and/or
29
 * modify it under the terms of the GNU General Public License
30
 * as published by the Free Software Foundation; either version 2
31
 * of the License, or (at your option) any later version.
32
 *
33
 * This program is distributed in the hope that it will be useful,
34
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36
 * GNU General Public License for more details.
37
 *
38
 * You should have received a copy of the GNU General Public License
39
 * along with this program; if not, write to the Free Software
40
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
41
 *
42
 * For more information, contact:
43
 *
44
 *  Generalitat Valenciana
45
 *   Conselleria d'Infraestructures i Transport
46
 *   Av. Blasco Ib??ez, 50
47
 *   46010 VALENCIA
48
 *   SPAIN
49
 *
50
 *      +34 963862235
51
 *   gvsig@gva.es
52
 *      www.gvsig.gva.es
53
 *
54
 *    or
55
 *
56
 *   IVER T.I. S.A
57
 *   Salamanca 50
58
 *   46005 Valencia
59
 *   Spain
60
 *
61
 *   +34 963163400
62
 *   dac@iver.es
63
 */
64
/* CVS MESSAGES:
65
 *
66
 * $Id$
67
 * $Log$
68
 *
69
 */
70
/**
71
 * This visitor is used only to print a feature by console.
72
 * @author Jorge Piera Llodr? (jorge.piera@iver.es)
73
 */
74
public class SHP2H2FeaturesVisitor extends FeaturesVisitor{
75
        private IFeatureType featureType;
76
        private H2StoreParameters h2Params;
77
        private String tableName;
78
        private Connection con;
79

    
80

    
81
        private static int idUnico=0;
82

    
83
        public SHP2H2FeaturesVisitor(IFeatureType featureType,H2StoreParameters h2Params) {
84
                super();
85
                this.featureType = featureType;
86
                this.h2Params=h2Params;
87
                this.tableName = h2Params.tableID();
88

    
89
                try {
90
                        Class.forName("org.h2.Driver");
91
                        this.con=DriverManager.getConnection(h2Params.getUrl(), h2Params.getUser(), h2Params.getPassw());
92
                } catch (ClassNotFoundException e) {
93
                        // TODO Auto-generated catch block
94
                        e.printStackTrace();
95
                } catch (SQLException e) {
96
                        // TODO Auto-generated catch block
97
                        e.printStackTrace();
98
                }
99

    
100
        }
101
        public void setTableName(String name){
102
                this.tableName=name;
103
        }
104

    
105
        /* (non-Javadoc)
106
         * @see org.gvsig.data.vectorial.visitor.DefaultFeaturesVisitor#visit(org.gvsig.data.vectorial.IFeature)
107
         */
108
        public void visit(IFeature feature) throws BaseException {
109
                WKBParser2 wkbParser = new WKBParser2();
110
                StringBuffer sb=new StringBuffer();
111
                sb.append("INSERT INTO ");
112
                sb.append(this.tableName);
113
                sb.append(" VALUES (");
114
                for (int i=0 ; i<featureType.size() ; i++){
115
                        sb.append("?, ");
116
                }
117
                sb.append("?)");
118

    
119
                PreparedStatement pst=null;
120
                try{
121
                        pst=con.prepareStatement(sb.toString());
122
                        pst.setInt(1, idUnico++);
123

    
124
                        Iterator iterator=featureType.iterator();
125
                        while (iterator.hasNext()) {
126
                                IFeatureAttributeDescriptor descriptor = (IFeatureAttributeDescriptor) iterator.next();
127
                                int i=descriptor.ordinal();
128
                                String type = descriptor.getDataType();
129

    
130
                                if (type.equals(IFeatureAttributeDescriptor.TYPE_BOOLEAN)) {
131
                                        pst.setBoolean(i+2, feature.getBoolean(i));
132
                                } else if (type.equals(IFeatureAttributeDescriptor.TYPE_BYTE)) {
133
                                        pst.setByte(i+2, feature.getByte(i));
134
                                } else if (type.equals(IFeatureAttributeDescriptor.TYPE_DATE)) {
135
                                        pst.setDate(i+2, new Date(feature.getDate(i).getTime()));
136
                                } else if (type.equals(IFeatureAttributeDescriptor.TYPE_DOUBLE)) {
137
                                        pst.setDouble(i+2, feature.getDouble(i));
138
                                } else if (type.equals(IFeatureAttributeDescriptor.TYPE_FLOAT)) {
139
                                        pst.setFloat(i+2, feature.getFloat(i));
140
                                } else if (type.equals(IFeatureAttributeDescriptor.TYPE_INT)) {
141
                                        pst.setInt(i+2, feature.getInt(i));
142
                                } else if (type.equals(IFeatureAttributeDescriptor.TYPE_LONG)) {
143
                                        pst.setLong(i+2, feature.getLong(i));
144
                                } else if (type.equals(IFeatureAttributeDescriptor.TYPE_STRING)) {
145
                                        pst.setString(i+2, feature.getString(i));
146
                                } else if (type.equals(IFeatureAttributeDescriptor.TYPE_GEOMETRY)) {
147
                                        Geometry geom =(Geometry)feature.getGeometry(i);
148
                                        if (geom != null){
149
                                                pst.setBytes( i+2,  new WKBWriter().write(geom.toJTSGeometry()));
150
                                        }else{
151
                                                System.out.print("Geometry null!!!");
152
                                                pst.setObject(i+2, null);
153
                                        }
154
                                } else {
155
                                        System.out.print(" ---- " + "TYPE UNKNOWN");
156
                                }
157
                        }
158
                        pst.execute();
159
                        System.out.println("* Inserted Feature: "+idUnico);
160
                }
161
                catch(SQLException except){
162
                        throw new RuntimeException(except);
163
                }
164
        }
165

    
166
        public void createStructure() {
167
                System.out.println("******* CreateStructure ********");
168
                StringBuffer sb=new StringBuffer();
169
                sb.append("CREATE TABLE ");
170
//                String s=String.valueOf(System.currentTimeMillis());
171
//                String tableName="TablaPrueba"+s.substring(s.length()-5);
172
                sb.append(h2Params.tableID());
173
                sb.append(" (");
174
                sb.append("id int, ");
175
                Iterator iterator=featureType.iterator();
176
                while (iterator.hasNext()) {
177
                        IFeatureAttributeDescriptor descriptor = (IFeatureAttributeDescriptor) iterator.next();
178
                        String type = descriptor.getDataType();
179

    
180
                        if (type.equals(IFeatureAttributeDescriptor.TYPE_BOOLEAN)){
181
                                sb.append(descriptor.getName());
182
                                sb.append(" bit, ");
183
                        }else if (type.equals(IFeatureAttributeDescriptor.TYPE_BYTE)){
184
                                sb.append(descriptor.getName());
185
                                sb.append(" byte, ");
186
                        }else if (type.equals(IFeatureAttributeDescriptor.TYPE_DATE)){
187
                                sb.append(descriptor.getName());
188
                                sb.append(" date, ");
189
                        }else if (type.equals(IFeatureAttributeDescriptor.TYPE_DOUBLE)){
190
                                sb.append(descriptor.getName());
191
                                sb.append(" double, ");
192
                        }else if (type.equals(IFeatureAttributeDescriptor.TYPE_FLOAT)){
193
                                sb.append(descriptor.getName());
194
                                sb.append(" float, ");
195
                        }else if (type.equals(IFeatureAttributeDescriptor.TYPE_INT)){
196
                                sb.append(descriptor.getName());
197
                                sb.append(" int, ");
198
                        }else if (type.equals(IFeatureAttributeDescriptor.TYPE_LONG)){
199
                                sb.append(descriptor.getName());
200
                                sb.append(" bigint, ");
201
                        }else if (type.equals(IFeatureAttributeDescriptor.TYPE_STRING)){
202
                                sb.append(descriptor.getName());
203
                                sb.append(" varchar, ");
204
                        }else if (type.equals(IFeatureAttributeDescriptor.TYPE_GEOMETRY)){
205
                                sb.append(descriptor.getName());
206
                                sb.append(" other, ");
207
                        }else {
208
                                System.out.print(" ---- " + "TYPE UNKNOWN");
209
                        }
210
                }
211
                String createTable=sb.toString();
212
                createTable=createTable.substring(0, createTable.length()-2);
213
                createTable+=")";
214

    
215
                try{
216
                        Statement st=this.con.createStatement();
217
                        System.out.println("Exec sql:\n"+createTable+"\n\n");
218
                        st.execute(createTable);
219
                        st.close();
220
                }
221
                catch(SQLException except){
222
                        throw new RuntimeException(except);
223

    
224
                }
225
                System.out.println("******* CreateStructure: OK ********");
226
        }
227

    
228
        public void close(){
229
                try {
230
                        this.con.close();
231
                } catch (SQLException e) {
232

    
233
                }
234
        }
235
}