root / trunk / libraries / libDataSourceDBBaseDrivers / src-test / org / gvsig / data / datastores / vectorial / driver / jdbc / Kiko.java @ 19610
History | View | Annotate | Download (6.96 KB)
1 |
package org.gvsig.data.datastores.vectorial.driver.jdbc; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.sql.Connection; |
5 |
import java.sql.DriverManager; |
6 |
import java.sql.SQLException; |
7 |
import java.sql.Statement; |
8 |
|
9 |
import org.gvsig.data.DataSourceManager; |
10 |
import org.gvsig.data.IDataStoreParameters; |
11 |
import org.gvsig.data.datastores.vectorial.driver.DriverStore; |
12 |
import org.gvsig.data.datastores.vectorial.driver.IDriverParameters; |
13 |
import org.gvsig.data.datastores.vectorial.driver.IDriverStoreParameters; |
14 |
import org.gvsig.data.datastores.vectorial.driver.shp.ShpDriverParameters; |
15 |
import org.gvsig.data.datastores.vectorial.driver.shp.fileshp.SHP; |
16 |
import org.gvsig.data.exception.CloseException; |
17 |
import org.gvsig.data.exception.InitializeException; |
18 |
import org.gvsig.data.exception.OpenException; |
19 |
import org.gvsig.data.exception.ReadException; |
20 |
import org.gvsig.data.exception.WriteException; |
21 |
import org.gvsig.data.vectorial.IFeature; |
22 |
import org.gvsig.data.vectorial.IFeatureAttributeDescriptor; |
23 |
import org.gvsig.data.vectorial.IFeatureCollection; |
24 |
import org.gvsig.data.vectorial.IFeatureStore; |
25 |
import org.gvsig.data.vectorial.IFeatureType; |
26 |
import org.gvsig.exceptions.BaseException; |
27 |
|
28 |
public class Kiko { |
29 |
|
30 |
public static void main(String args[]){ |
31 |
Kiko kiko= new Kiko();
|
32 |
kiko.shp2H2(); |
33 |
} |
34 |
|
35 |
|
36 |
|
37 |
public void shp2H2() { |
38 |
System.out.println("======= SHP =============="); |
39 |
org.gvsig.data.datastores.vectorial.driver.shp.Register.selfRegister(); |
40 |
org.gvsig.data.datastores.vectorial.driver.dbf.Register.selfRegister(); |
41 |
org.gvsig.data.datastores.vectorial.driver.Register.selfRegister(); |
42 |
|
43 |
DataSourceManager dsm=DataSourceManager.getManager(); |
44 |
File shpfile= new File(DataStoreTest.class.getResource("data/prueba.dbf").getFile()); |
45 |
|
46 |
IDriverParameters dp=dsm.createDriverParameters("shp");
|
47 |
((ShpDriverParameters)dp).setSHPFile(shpfile); |
48 |
((ShpDriverParameters)dp).setSHXFile(SHP.getShxFile(shpfile)); |
49 |
((ShpDriverParameters)dp).setDBFFile(SHP.getDbfFile(shpfile)); |
50 |
|
51 |
|
52 |
driverTest(dp,null,null,false); |
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
} |
60 |
private void driverTest(IDriverParameters dp,String filter, String order,boolean testEdit){ |
61 |
DataSourceManager dsm=DataSourceManager.getManager(); |
62 |
|
63 |
IDataStoreParameters dsp=dsm.createDataStoreParameters(DriverStore.DATASTORE_NAME); |
64 |
|
65 |
((IDriverStoreParameters)dsp).setDriverParameters(dp); |
66 |
IFeatureStore fs=createFeatureStore(dp); |
67 |
|
68 |
try {
|
69 |
fs.open(); |
70 |
} catch (OpenException e2) {
|
71 |
e2.printStackTrace(); |
72 |
throw new RuntimeException(e2); |
73 |
} |
74 |
|
75 |
if (fs.isEditable() && testEdit) {
|
76 |
fs.startEditing(); |
77 |
|
78 |
IFeature feature1 = fs.createDefaultFeature(false);
|
79 |
IFeature feature2 = fs.createDefaultFeature(false);
|
80 |
IFeature feature3 = fs.createDefaultFeature(false);
|
81 |
|
82 |
fs.insert(feature1); |
83 |
fs.insert(feature2); |
84 |
|
85 |
fs.update(feature3,feature1); |
86 |
fs.delete(feature3); |
87 |
fs.delete(feature2); |
88 |
} |
89 |
|
90 |
//Mostrar por consola todos los registros.
|
91 |
IFeatureType ft= fs.getDefaultFeatureType(); |
92 |
IFeatureCollection featureCollection; |
93 |
// featureCollection = (IFeatureCollection)fs.getDataCollection();
|
94 |
// featureCollection = (IFeatureCollection)fs.getDataCollection(ft,"NOMBRE = 'CALPE'",null);
|
95 |
// featureCollection = (IFeatureCollection)fs.getDataCollection(ft,"AREA > 3.2213163729E7 and AREA < 3.2213163749E7",null);
|
96 |
featureCollection = (IFeatureCollection)fs.getDataCollection(ft,filter,order); |
97 |
|
98 |
|
99 |
String tableName=createStructure(ft);
|
100 |
|
101 |
|
102 |
SHP2H2FeaturesVisitor visitor=new SHP2H2FeaturesVisitor(ft);
|
103 |
visitor.setTableName(tableName); |
104 |
try {
|
105 |
featureCollection.accept(visitor); |
106 |
} catch (BaseException e1) {
|
107 |
e1.printStackTrace(); |
108 |
} |
109 |
|
110 |
if (fs.isEditable() && testEdit){
|
111 |
try {
|
112 |
fs.finishEditing(); |
113 |
} catch (WriteException e) {
|
114 |
e.printStackTrace(); |
115 |
} catch (ReadException e) {
|
116 |
e.printStackTrace(); |
117 |
} |
118 |
} |
119 |
try {
|
120 |
fs.close(); |
121 |
} catch (CloseException e) {
|
122 |
e.printStackTrace(); |
123 |
} |
124 |
fs.dispose(); |
125 |
} |
126 |
private String createStructure(IFeatureType featureType) { |
127 |
System.out.println("***************** New Feature ******************"); |
128 |
System.out.println("ATTRIBUTES = "); |
129 |
StringBuffer sb=new StringBuffer(); |
130 |
sb.append("CREATE TABLE TablaPrueba");
|
131 |
String s=String.valueOf(System.currentTimeMillis()); |
132 |
String tableName="TablaPrueba"+s.substring(s.length()-5); |
133 |
sb.append(s.substring(s.length()-5));
|
134 |
sb.append(" (");
|
135 |
sb.append("id int, ");
|
136 |
for (int i=0 ; i<featureType.size() ; i++){ |
137 |
|
138 |
IFeatureAttributeDescriptor descriptor=(IFeatureAttributeDescriptor)featureType.get(i); |
139 |
String type = descriptor.getDataType();
|
140 |
|
141 |
if (type.equals(IFeatureAttributeDescriptor.TYPE_BOOLEAN)){
|
142 |
sb.append(descriptor.getName()); |
143 |
sb.append(" bit, ");
|
144 |
}else if (type.equals(IFeatureAttributeDescriptor.TYPE_BYTE)){ |
145 |
sb.append(descriptor.getName()); |
146 |
sb.append(" byte, ");
|
147 |
}else if (type.equals(IFeatureAttributeDescriptor.TYPE_DATE)){ |
148 |
sb.append(descriptor.getName()); |
149 |
sb.append(" date, ");
|
150 |
}else if (type.equals(IFeatureAttributeDescriptor.TYPE_DOUBLE)){ |
151 |
sb.append(descriptor.getName()); |
152 |
sb.append(" double, ");
|
153 |
}else if (type.equals(IFeatureAttributeDescriptor.TYPE_FLOAT)){ |
154 |
sb.append(descriptor.getName()); |
155 |
sb.append(" float, ");
|
156 |
}else if (type.equals(IFeatureAttributeDescriptor.TYPE_INT)){ |
157 |
sb.append(descriptor.getName()); |
158 |
sb.append(" int, ");
|
159 |
}else if (type.equals(IFeatureAttributeDescriptor.TYPE_LONG)){ |
160 |
sb.append(descriptor.getName()); |
161 |
sb.append(" bigint, ");
|
162 |
}else if (type.equals(IFeatureAttributeDescriptor.TYPE_STRING)){ |
163 |
sb.append(descriptor.getName()); |
164 |
sb.append(" varchar, ");
|
165 |
}else if (type.equals(IFeatureAttributeDescriptor.TYPE_GEOMETRY)){ |
166 |
sb.append(descriptor.getName()); |
167 |
sb.append(" other, ");
|
168 |
}else {
|
169 |
System.out.print(" ---- " + "TYPE UNKNOWN"); |
170 |
} |
171 |
System.out.print("\n"); |
172 |
} |
173 |
String createTable=sb.toString();
|
174 |
createTable=createTable.substring(0, createTable.length()-2); |
175 |
createTable+=")";
|
176 |
|
177 |
try{
|
178 |
Class.forName("org.h2.Driver"); |
179 |
Connection con=DriverManager.getConnection("jdbc:h2:tcp://localhost/default", "SA", ""); |
180 |
Statement st=con.createStatement();
|
181 |
if(st.executeUpdate(createTable)>0){ |
182 |
System.out.println("Tutto benne"); |
183 |
} |
184 |
else{
|
185 |
System.out.println("Sisoma el error"); |
186 |
} |
187 |
st.close(); |
188 |
con.close(); |
189 |
} |
190 |
catch(SQLException except){ |
191 |
except.printStackTrace(); |
192 |
} catch (ClassNotFoundException e) { |
193 |
// TODO Auto-generated catch block
|
194 |
e.printStackTrace(); |
195 |
} |
196 |
return tableName;
|
197 |
} |
198 |
|
199 |
|
200 |
|
201 |
private IFeatureStore createFeatureStore(IDriverParameters dp){
|
202 |
DataSourceManager dsm=DataSourceManager.getManager(); |
203 |
|
204 |
IDataStoreParameters dsp=dsm.createDataStoreParameters(DriverStore.DATASTORE_NAME); |
205 |
|
206 |
((IDriverStoreParameters)dsp).setDriverParameters(dp); |
207 |
IFeatureStore fs=null;
|
208 |
try {
|
209 |
fs = (IFeatureStore)dsm.createDataStore(dsp); |
210 |
} catch (InitializeException e) {
|
211 |
e.printStackTrace(); |
212 |
} |
213 |
return fs;
|
214 |
|
215 |
} |
216 |
|
217 |
} |