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