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