svn-gvsig-desktop / trunk / extensions / extCAD / src / com / iver / cit / gvsig / writers / WriterGT2.java @ 6313
History | View | Annotate | Download (7.99 KB)
1 |
/**
|
---|---|
2 |
*
|
3 |
*/
|
4 |
package com.iver.cit.gvsig.writers; |
5 |
|
6 |
import java.io.IOException; |
7 |
import java.sql.Types; |
8 |
|
9 |
import org.geotools.data.DataUtilities; |
10 |
import org.geotools.data.DefaultTransaction; |
11 |
import org.geotools.data.FeatureReader; |
12 |
import org.geotools.data.FeatureStore; |
13 |
import org.geotools.data.Transaction; |
14 |
import org.geotools.factory.FactoryConfigurationError; |
15 |
import org.geotools.feature.AttributeType; |
16 |
import org.geotools.feature.AttributeTypeFactory; |
17 |
import org.geotools.feature.Feature; |
18 |
import org.geotools.feature.FeatureType; |
19 |
import org.geotools.feature.FeatureTypeBuilder; |
20 |
import org.geotools.feature.IllegalAttributeException; |
21 |
import org.geotools.feature.SchemaException; |
22 |
import org.geotools.filter.Filter; |
23 |
import org.geotools.filter.FilterFactory; |
24 |
|
25 |
import com.hardcode.gdbms.engine.data.driver.DriverException; |
26 |
import com.hardcode.gdbms.engine.values.NullValue; |
27 |
import com.iver.cit.gvsig.fmap.core.FShape; |
28 |
import com.iver.cit.gvsig.fmap.core.IFeature; |
29 |
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition; |
30 |
import com.iver.cit.gvsig.fmap.edition.EditionException; |
31 |
import com.iver.cit.gvsig.fmap.edition.IRowEdited; |
32 |
import com.iver.cit.gvsig.fmap.edition.writers.AbstractWriter; |
33 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
34 |
import com.vividsolutions.jts.geom.Geometry; |
35 |
import com.vividsolutions.jts.geom.MultiLineString; |
36 |
import com.vividsolutions.jts.geom.MultiPoint; |
37 |
import com.vividsolutions.jts.geom.MultiPolygon; |
38 |
import com.vividsolutions.jts.geom.Point; |
39 |
|
40 |
/**
|
41 |
* @author fjp
|
42 |
*
|
43 |
* Example of using a Geotools featureStore to write features
|
44 |
* Example of use: Inside the extension, open a dataStore and
|
45 |
* get the featureStore. Then create this class with it.
|
46 |
*
|
47 |
*/
|
48 |
public class WriterGT2 extends AbstractWriter { |
49 |
|
50 |
FilterFactory filterFactory = FilterFactory.createFilterFactory(); |
51 |
FeatureStore featStore; |
52 |
AttributeType[] types;
|
53 |
Transaction t; |
54 |
int numReg = 0; |
55 |
private boolean bWriteAll; |
56 |
|
57 |
public static Class getClassBySqlTYPE(int type) |
58 |
{ |
59 |
switch (type)
|
60 |
{ |
61 |
case Types.SMALLINT: |
62 |
return Integer.class; |
63 |
case Types.INTEGER: |
64 |
return Integer.class; |
65 |
case Types.BIGINT: |
66 |
return Integer.class; |
67 |
case Types.BOOLEAN: |
68 |
return Boolean.class; |
69 |
case Types.DECIMAL: |
70 |
return Double.class; |
71 |
case Types.DOUBLE: |
72 |
return Double.class; |
73 |
case Types.FLOAT: |
74 |
return Double.class; |
75 |
case Types.CHAR: |
76 |
return Character.class; |
77 |
case Types.VARCHAR: |
78 |
return String.class; |
79 |
case Types.LONGVARCHAR: |
80 |
return String.class; |
81 |
} |
82 |
return NullValue.class;
|
83 |
} |
84 |
|
85 |
public static FeatureType getFeatureType(FLyrVect layer, String geomField, String featName) throws DriverException, com.iver.cit.gvsig.fmap.DriverException, FactoryConfigurationError, SchemaException { |
86 |
|
87 |
Class geomType = findBestGeometryClass(layer.getShapeType());
|
88 |
// geomType = Geometry.class;
|
89 |
AttributeType geom = AttributeTypeFactory.newAttributeType(geomField, geomType); |
90 |
int numFields = layer.getRecordset().getFieldCount() + 1; |
91 |
AttributeType[] att = new AttributeType[numFields]; |
92 |
att[0] = geom;
|
93 |
for (int i=1; i < numFields; i++) |
94 |
{ |
95 |
att[i] = AttributeTypeFactory.newAttributeType( |
96 |
layer.getRecordset().getFieldName(i-1),
|
97 |
getClassBySqlTYPE(layer.getRecordset().getFieldType(i-1)));
|
98 |
} |
99 |
FeatureType featType = FeatureTypeBuilder.newFeatureType(att,featName); |
100 |
return featType;
|
101 |
} |
102 |
|
103 |
public static final Class findBestGeometryClass(int layerType) { |
104 |
Class best = Geometry.class;
|
105 |
switch (layerType)
|
106 |
{ |
107 |
case FShape.LINE:
|
108 |
best = MultiLineString.class; |
109 |
break;
|
110 |
case FShape.MULTIPOINT:
|
111 |
best = MultiPoint.class; |
112 |
break;
|
113 |
case FShape.POINT:
|
114 |
best = Point.class;
|
115 |
break;
|
116 |
case FShape.POLYGON:
|
117 |
best = MultiPolygon.class; |
118 |
break;
|
119 |
case FShape.MULTI:
|
120 |
best = Geometry.class; |
121 |
break;
|
122 |
default:
|
123 |
throw new RuntimeException("Unknown gvSigShapeType->GeometryClass : " + layerType); |
124 |
} |
125 |
return best;
|
126 |
} |
127 |
|
128 |
|
129 |
public WriterGT2(FeatureStore featureStore, boolean writeAllFeatures) throws IOException |
130 |
{ |
131 |
this.featStore = featureStore;
|
132 |
this.bWriteAll = writeAllFeatures;
|
133 |
} |
134 |
|
135 |
/* (non-Javadoc)
|
136 |
* @see com.iver.cit.gvsig.fmap.edition.IWriter#preProcess()
|
137 |
*/
|
138 |
public void preProcess() throws EditionException { |
139 |
try {
|
140 |
types = featStore.getSchema().getAttributeTypes(); |
141 |
t = new DefaultTransaction("handle"); |
142 |
featStore.setTransaction(t); |
143 |
|
144 |
t.addAuthorization("handle"); // provide authoriztion |
145 |
|
146 |
|
147 |
} catch (Exception e) { |
148 |
e.printStackTrace(); |
149 |
throw new EditionException(e); |
150 |
} |
151 |
|
152 |
|
153 |
} |
154 |
|
155 |
/* (non-Javadoc)
|
156 |
* @see com.iver.cit.gvsig.fmap.edition.IWriter#process(com.iver.cit.gvsig.fmap.edition.IRowEdited)
|
157 |
*/
|
158 |
public void process(IRowEdited row) throws EditionException { |
159 |
|
160 |
IFeature feat = (IFeature) row.getLinkedRow(); |
161 |
// FeatureType featType = featStore.getSchema();
|
162 |
// TODO: OJO CON EL ORDEN DE LOS CAMPOS, QUE NO ES EL MISMO
|
163 |
Object[] values = new Object[types.length]; |
164 |
values[0] = feat.getGeometry().toJTSGeometry();
|
165 |
for (int i=1; i < types.length; i++) |
166 |
values[i] = feat.getAttribute(i-1);
|
167 |
|
168 |
Filter theFilter = filterFactory.createFidFilter(feat.getID());
|
169 |
try {
|
170 |
// System.out.println("Escribiendo numReg=" + numReg + " con STATUS=" + row.getStatus());
|
171 |
switch (row.getStatus())
|
172 |
{ |
173 |
case IRowEdited.STATUS_ADDED:
|
174 |
Feature featGT2 = featStore.getSchema().create(values); |
175 |
FeatureReader reader = DataUtilities.reader( |
176 |
new Feature[] {featGT2}); |
177 |
featStore.addFeatures(reader); |
178 |
break;
|
179 |
case IRowEdited.STATUS_MODIFIED:
|
180 |
featStore.modifyFeatures(types, values, theFilter); |
181 |
break;
|
182 |
case IRowEdited.STATUS_ORIGINAL:
|
183 |
if (bWriteAll)
|
184 |
{ |
185 |
featGT2 = featStore.getSchema().create(values); |
186 |
reader = DataUtilities.reader( |
187 |
new Feature[] {featGT2}); |
188 |
featStore.addFeatures(reader); |
189 |
} |
190 |
break;
|
191 |
case IRowEdited.STATUS_DELETED:
|
192 |
featStore.removeFeatures(theFilter); |
193 |
break;
|
194 |
} |
195 |
|
196 |
|
197 |
numReg++; |
198 |
} catch (IOException e) { |
199 |
e.printStackTrace(); |
200 |
throw new EditionException(e); |
201 |
} catch (IllegalAttributeException e) {
|
202 |
e.printStackTrace(); |
203 |
throw new EditionException(e); |
204 |
} |
205 |
|
206 |
|
207 |
|
208 |
|
209 |
} |
210 |
|
211 |
/* (non-Javadoc)
|
212 |
* @see com.iver.cit.gvsig.fmap.edition.IWriter#postProcess()
|
213 |
*/
|
214 |
public void postProcess() throws EditionException { |
215 |
try
|
216 |
{ |
217 |
t.commit(); // commit opperations
|
218 |
} |
219 |
catch (IOException io){ |
220 |
try {
|
221 |
t.rollback(); |
222 |
} catch (IOException e) { |
223 |
e.printStackTrace(); |
224 |
throw new EditionException(e); |
225 |
} // cancel opperations
|
226 |
} |
227 |
finally {
|
228 |
try {
|
229 |
t.close(); |
230 |
} catch (IOException e) { |
231 |
e.printStackTrace(); |
232 |
throw new EditionException(e); |
233 |
} // free resources
|
234 |
} |
235 |
|
236 |
} |
237 |
|
238 |
public String getName() { |
239 |
return "JDBC Writer from Geotools"; |
240 |
} |
241 |
|
242 |
public boolean canWriteGeometry(int gvSIGgeometryType) { |
243 |
switch (gvSIGgeometryType)
|
244 |
{ |
245 |
case FShape.POINT:
|
246 |
return true; |
247 |
case FShape.LINE:
|
248 |
return true; |
249 |
case FShape.POLYGON:
|
250 |
return true; |
251 |
case FShape.ARC:
|
252 |
return false; |
253 |
case FShape.ELLIPSE:
|
254 |
return false; |
255 |
case FShape.MULTIPOINT:
|
256 |
return true; |
257 |
case FShape.TEXT:
|
258 |
return false; |
259 |
} |
260 |
return false; |
261 |
} |
262 |
|
263 |
public boolean canWriteAttribute(int sqlType) { |
264 |
switch (sqlType)
|
265 |
{ |
266 |
case Types.DOUBLE: |
267 |
case Types.FLOAT: |
268 |
case Types.INTEGER: |
269 |
case Types.BIGINT: |
270 |
return true; |
271 |
case Types.DATE: |
272 |
return true; |
273 |
case Types.BIT: |
274 |
case Types.BOOLEAN: |
275 |
return true; |
276 |
case Types.VARCHAR: |
277 |
case Types.CHAR: |
278 |
case Types.LONGVARCHAR: |
279 |
return true; // TODO: Revisar esto, porque no creo que admita campos muy grandes |
280 |
|
281 |
} |
282 |
|
283 |
return false; |
284 |
} |
285 |
|
286 |
public void setFlatness(double flatness) { |
287 |
// TODO Auto-generated method stub
|
288 |
|
289 |
} |
290 |
|
291 |
public void initialize(ITableDefinition tableDefinition) throws EditionException { |
292 |
super.initialize(tableDefinition);
|
293 |
|
294 |
} |
295 |
|
296 |
} |