svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dataDB / src-test / org / gvsig / data / datastores / vectorial / db / jdbc / h2 / SHP2H2FeaturesVisitor.java @ 21511
History | View | Annotate | Download (7.45 KB)
1 |
package org.gvsig.data.datastores.vectorial.db.jdbc.h2; |
---|---|
2 |
|
3 |
import java.sql.Connection; |
4 |
import java.sql.Date; |
5 |
import java.sql.DriverManager; |
6 |
import java.sql.PreparedStatement; |
7 |
import java.sql.SQLException; |
8 |
import java.sql.Statement; |
9 |
import java.util.Iterator; |
10 |
|
11 |
import org.gvsig.data.vectorial.Feature; |
12 |
import org.gvsig.data.vectorial.FeatureAttributeDescriptor; |
13 |
import org.gvsig.data.vectorial.FeatureType; |
14 |
import org.gvsig.data.vectorial.visitor.BaseFeaturesVisitor; |
15 |
import org.gvsig.exceptions.BaseException; |
16 |
import org.gvsig.fmap.geom.Geometry; |
17 |
import org.gvsig.fmap.geom.operation.towkb.ToWKB; |
18 |
|
19 |
|
20 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
21 |
*
|
22 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
23 |
*
|
24 |
* This program is free software; you can redistribute it and/or
|
25 |
* modify it under the terms of the GNU General Public License
|
26 |
* as published by the Free Software Foundation; either version 2
|
27 |
* of the License, or (at your option) any later version.
|
28 |
*
|
29 |
* This program is distributed in the hope that it will be useful,
|
30 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
31 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
32 |
* GNU General Public License for more details.
|
33 |
*
|
34 |
* You should have received a copy of the GNU General Public License
|
35 |
* along with this program; if not, write to the Free Software
|
36 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
37 |
*
|
38 |
* For more information, contact:
|
39 |
*
|
40 |
* Generalitat Valenciana
|
41 |
* Conselleria d'Infraestructures i Transport
|
42 |
* Av. Blasco Ib??ez, 50
|
43 |
* 46010 VALENCIA
|
44 |
* SPAIN
|
45 |
*
|
46 |
* +34 963862235
|
47 |
* gvsig@gva.es
|
48 |
* www.gvsig.gva.es
|
49 |
*
|
50 |
* or
|
51 |
*
|
52 |
* IVER T.I. S.A
|
53 |
* Salamanca 50
|
54 |
* 46005 Valencia
|
55 |
* Spain
|
56 |
*
|
57 |
* +34 963163400
|
58 |
* dac@iver.es
|
59 |
*/
|
60 |
/* CVS MESSAGES:
|
61 |
*
|
62 |
* $Id$
|
63 |
* $Log$
|
64 |
*
|
65 |
*/
|
66 |
/**
|
67 |
* This visitor is used only to print a feature by console.
|
68 |
* @author Jorge Piera Llodr? (jorge.piera@iver.es)
|
69 |
*/
|
70 |
public class SHP2H2FeaturesVisitor extends BaseFeaturesVisitor{ |
71 |
private FeatureType featureType;
|
72 |
private H2StoreParameters h2Params;
|
73 |
private String tableName; |
74 |
private Connection con; |
75 |
|
76 |
|
77 |
private static int idUnico=0; |
78 |
|
79 |
public SHP2H2FeaturesVisitor(FeatureType featureType,H2StoreParameters h2Params) {
|
80 |
super();
|
81 |
this.featureType = featureType;
|
82 |
this.h2Params=h2Params;
|
83 |
this.tableName = h2Params.tableID();
|
84 |
|
85 |
try {
|
86 |
Class.forName("org.h2.Driver"); |
87 |
this.con=DriverManager.getConnection(h2Params.getUrl(), h2Params.getUser(), h2Params.getPassw()); |
88 |
} catch (ClassNotFoundException e) { |
89 |
// TODO Auto-generated catch block
|
90 |
e.printStackTrace(); |
91 |
} catch (SQLException e) { |
92 |
// TODO Auto-generated catch block
|
93 |
e.printStackTrace(); |
94 |
} |
95 |
|
96 |
} |
97 |
public void setTableName(String name){ |
98 |
this.tableName=name;
|
99 |
} |
100 |
|
101 |
/* (non-Javadoc)
|
102 |
* @see org.gvsig.data.vectorial.visitor.DefaultFeaturesVisitor#visit(org.gvsig.data.vectorial.Feature)
|
103 |
*/
|
104 |
public void visit(Feature feature) throws BaseException { |
105 |
StringBuffer sb=new StringBuffer(); |
106 |
sb.append("INSERT INTO ");
|
107 |
sb.append(this.tableName);
|
108 |
sb.append(" VALUES (");
|
109 |
for (int i=0 ; i<featureType.size() ; i++){ |
110 |
sb.append("?, ");
|
111 |
} |
112 |
sb.append("?)");
|
113 |
|
114 |
PreparedStatement pst=null; |
115 |
try{
|
116 |
pst=con.prepareStatement(sb.toString()); |
117 |
pst.setInt(1, idUnico++);
|
118 |
|
119 |
Iterator iterator=featureType.iterator();
|
120 |
while (iterator.hasNext()) {
|
121 |
FeatureAttributeDescriptor descriptor = (FeatureAttributeDescriptor) iterator.next(); |
122 |
int i=descriptor.ordinal();
|
123 |
String type = descriptor.getDataType();
|
124 |
|
125 |
if (type.equals(FeatureAttributeDescriptor.TYPE_BOOLEAN)) {
|
126 |
pst.setBoolean(i+2, feature.getBoolean(i));
|
127 |
} else if (type.equals(FeatureAttributeDescriptor.TYPE_BYTE)) { |
128 |
pst.setByte(i+2, feature.getByte(i));
|
129 |
} else if (type.equals(FeatureAttributeDescriptor.TYPE_DATE)) { |
130 |
pst.setDate(i+2, new Date(feature.getDate(i).getTime())); |
131 |
} else if (type.equals(FeatureAttributeDescriptor.TYPE_DOUBLE)) { |
132 |
pst.setDouble(i+2, feature.getDouble(i));
|
133 |
} else if (type.equals(FeatureAttributeDescriptor.TYPE_FLOAT)) { |
134 |
pst.setFloat(i+2, feature.getFloat(i));
|
135 |
} else if (type.equals(FeatureAttributeDescriptor.TYPE_INT)) { |
136 |
pst.setInt(i+2, feature.getInt(i));
|
137 |
} else if (type.equals(FeatureAttributeDescriptor.TYPE_LONG)) { |
138 |
pst.setLong(i+2, feature.getLong(i));
|
139 |
} else if (type.equals(FeatureAttributeDescriptor.TYPE_STRING)) { |
140 |
pst.setString(i+2, feature.getString(i));
|
141 |
} else if (type.equals(FeatureAttributeDescriptor.TYPE_GEOMETRY)) { |
142 |
Geometry geom =(Geometry)feature.getGeometry(i); |
143 |
if (geom != null){ |
144 |
// TODO Falta pasar el SRS de la geometr?a para pasarla a WKB.
|
145 |
pst.setBytes( i+2, (byte[])geom.invokeOperation(ToWKB.CODE,null)); |
146 |
}else{
|
147 |
System.out.print("Geometry null!!!"); |
148 |
pst.setObject(i+2, null); |
149 |
} |
150 |
} else {
|
151 |
System.out.print(" ---- " + "TYPE UNKNOWN"); |
152 |
} |
153 |
} |
154 |
pst.execute(); |
155 |
System.out.println("* Inserted Feature: "+idUnico); |
156 |
} |
157 |
catch(SQLException except){ |
158 |
throw new RuntimeException(except); |
159 |
} |
160 |
} |
161 |
|
162 |
public void createStructure() { |
163 |
System.out.println("******* CreateStructure ********"); |
164 |
StringBuffer sb=new StringBuffer(); |
165 |
sb.append("CREATE TABLE ");
|
166 |
// String s=String.valueOf(System.currentTimeMillis());
|
167 |
// String tableName="TablaPrueba"+s.substring(s.length()-5);
|
168 |
sb.append(h2Params.tableID()); |
169 |
sb.append(" (");
|
170 |
sb.append("id int, ");
|
171 |
Iterator iterator=featureType.iterator();
|
172 |
while (iterator.hasNext()) {
|
173 |
FeatureAttributeDescriptor descriptor = (FeatureAttributeDescriptor) iterator.next(); |
174 |
String type = descriptor.getDataType();
|
175 |
|
176 |
if (type.equals(FeatureAttributeDescriptor.TYPE_BOOLEAN)){
|
177 |
sb.append(descriptor.getName()); |
178 |
sb.append(" bit, ");
|
179 |
}else if (type.equals(FeatureAttributeDescriptor.TYPE_BYTE)){ |
180 |
sb.append(descriptor.getName()); |
181 |
sb.append(" byte, ");
|
182 |
}else if (type.equals(FeatureAttributeDescriptor.TYPE_DATE)){ |
183 |
sb.append(descriptor.getName()); |
184 |
sb.append(" date, ");
|
185 |
}else if (type.equals(FeatureAttributeDescriptor.TYPE_DOUBLE)){ |
186 |
sb.append(descriptor.getName()); |
187 |
sb.append(" double, ");
|
188 |
}else if (type.equals(FeatureAttributeDescriptor.TYPE_FLOAT)){ |
189 |
sb.append(descriptor.getName()); |
190 |
sb.append(" float, ");
|
191 |
}else if (type.equals(FeatureAttributeDescriptor.TYPE_INT)){ |
192 |
sb.append(descriptor.getName()); |
193 |
sb.append(" int, ");
|
194 |
}else if (type.equals(FeatureAttributeDescriptor.TYPE_LONG)){ |
195 |
sb.append(descriptor.getName()); |
196 |
sb.append(" bigint, ");
|
197 |
}else if (type.equals(FeatureAttributeDescriptor.TYPE_STRING)){ |
198 |
sb.append(descriptor.getName()); |
199 |
sb.append(" varchar, ");
|
200 |
}else if (type.equals(FeatureAttributeDescriptor.TYPE_GEOMETRY)){ |
201 |
sb.append(descriptor.getName()); |
202 |
sb.append(" other, ");
|
203 |
}else {
|
204 |
System.out.print(" ---- " + "TYPE UNKNOWN"); |
205 |
} |
206 |
} |
207 |
String createTable=sb.toString();
|
208 |
createTable=createTable.substring(0, createTable.length()-2); |
209 |
createTable+=")";
|
210 |
|
211 |
try{
|
212 |
Statement st=this.con.createStatement(); |
213 |
System.out.println("Exec sql:\n"+createTable+"\n\n"); |
214 |
st.execute(createTable); |
215 |
st.close(); |
216 |
} |
217 |
catch(SQLException except){ |
218 |
throw new RuntimeException(except); |
219 |
|
220 |
} |
221 |
System.out.println("******* CreateStructure: OK ********"); |
222 |
} |
223 |
|
224 |
public void close(){ |
225 |
try {
|
226 |
this.con.close();
|
227 |
} catch (SQLException e) { |
228 |
|
229 |
} |
230 |
} |
231 |
} |