Statistics
| Revision:

root / trunk / extensions / extCAD / src / com / iver / cit / gvsig / writers / WriterGT2.java @ 4313

History | View | Annotate | Download (7.84 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.edition.EditionException;
30
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
31
import com.iver.cit.gvsig.fmap.edition.writers.AbstractWriter;
32
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
33
import com.vividsolutions.jts.geom.Geometry;
34
import com.vividsolutions.jts.geom.MultiLineString;
35
import com.vividsolutions.jts.geom.MultiPoint;
36
import com.vividsolutions.jts.geom.MultiPolygon;
37
import com.vividsolutions.jts.geom.Point;
38

    
39
/**
40
 * @author fjp
41
 * 
42
 * Example of using a Geotools featureStore to write features
43
 * Example of use: Inside the extension, open a dataStore and
44
 * get the featureStore. Then create this class with it.
45
 *
46
 */
47
public class WriterGT2 extends AbstractWriter {
48

    
49
        FilterFactory filterFactory = FilterFactory.createFilterFactory();
50
        FeatureStore featStore;
51
        AttributeType[] types;
52
        Transaction t;
53
        int numReg = 0;
54
        private boolean bWriteAll;
55

    
56
    public static Class getClassBySqlTYPE(int type)
57
    {
58
        switch (type)
59
        {
60
            case Types.SMALLINT:
61
                return Integer.class;
62
            case Types.INTEGER:
63
                    return Integer.class;
64
            case Types.BIGINT:
65
                    return Integer.class;
66
            case Types.BOOLEAN:
67
                    return Boolean.class;
68
            case Types.DECIMAL:
69
                    return Double.class;
70
            case Types.DOUBLE:
71
                    return Double.class;
72
            case Types.FLOAT:
73
                    return Double.class;
74
            case Types.CHAR:
75
                    return Character.class;
76
            case Types.VARCHAR:
77
                    return String.class;
78
            case Types.LONGVARCHAR:
79
                    return String.class;
80
        }
81
        return NullValue.class;
82
    }
83

    
84
        public static FeatureType getFeatureType(FLyrVect layer, String geomField, String featName) throws DriverException, com.iver.cit.gvsig.fmap.DriverException, FactoryConfigurationError, SchemaException {
85
                
86
                Class geomType = findBestGeometryClass(layer.getShapeType());
87
                // geomType = Geometry.class;
88
                AttributeType geom = AttributeTypeFactory.newAttributeType(geomField, geomType);
89
                int numFields = layer.getRecordset().getFieldCount() + 1;
90
                AttributeType[] att = new AttributeType[numFields];
91
                att[0] = geom;
92
                for (int i=1; i < numFields; i++)
93
                {
94
                        att[i] = AttributeTypeFactory.newAttributeType(
95
                                        layer.getRecordset().getFieldName(i-1),
96
                                        getClassBySqlTYPE(layer.getRecordset().getFieldType(i-1))); 
97
                }
98
                FeatureType featType = FeatureTypeBuilder.newFeatureType(att,featName);
99
                return featType;
100
        }
101
        
102
          public static final Class findBestGeometryClass(int layerType) {
103
                    Class best = Geometry.class;
104
                    switch (layerType)
105
                    {
106
                    case FShape.LINE:                            
107
                      best = MultiLineString.class;
108
                      break;
109
                    case FShape.MULTIPOINT:
110
                      best = MultiPoint.class;
111
                      break;
112
                    case FShape.POINT:
113
                      best = Point.class;
114
                      break;
115
                    case FShape.POLYGON:
116
                      best = MultiPolygon.class;
117
                      break;
118
                    case FShape.MULTI:
119
                              best = Geometry.class;
120
                              break;                      
121
                    default:
122
                      throw new RuntimeException("Unknown gvSigShapeType->GeometryClass : " + layerType);
123
                    }
124
                    return best;
125
                  }
126
        
127
        
128
        public WriterGT2(FeatureStore featureStore, boolean writeAllFeatures) throws IOException
129
        {
130
                this.featStore = featureStore;
131
                this.bWriteAll = writeAllFeatures;
132
        }
133
        
134
        /* (non-Javadoc)
135
         * @see com.iver.cit.gvsig.fmap.edition.IWriter#preProcess()
136
         */
137
        public void preProcess() throws EditionException {
138
                try {
139
                        types = featStore.getSchema().getAttributeTypes();
140
                        t = new DefaultTransaction("handle");
141
                        featStore.setTransaction(t);
142
              
143
                        t.addAuthorization("handle");  // provide authoriztion
144
                        
145
                        
146
                } catch (Exception e) {
147
                        e.printStackTrace();
148
                        throw new EditionException(e);
149
                }
150

    
151

    
152
        }
153

    
154
        /* (non-Javadoc)
155
         * @see com.iver.cit.gvsig.fmap.edition.IWriter#process(com.iver.cit.gvsig.fmap.edition.IRowEdited)
156
         */
157
        public void process(IRowEdited row) throws EditionException {
158
                
159
                IFeature feat = (IFeature) row.getLinkedRow();
160
                // FeatureType featType = featStore.getSchema();
161
                // TODO: OJO CON EL ORDEN DE LOS CAMPOS, QUE NO ES EL MISMO
162
                Object[] values = new Object[types.length];
163
                values[0] = feat.getGeometry().toJTSGeometry();
164
                for (int i=1; i < types.length; i++)
165
                        values[i] = feat.getAttribute(i-1);
166

    
167
                Filter theFilter = filterFactory.createFidFilter(feat.getID()); 
168
        try {
169
                // System.out.println("Escribiendo numReg=" + numReg + " con STATUS=" + row.getStatus());
170
                switch (row.getStatus())
171
                {
172
                        case IRowEdited.STATUS_ADDED:                                
173
                                Feature featGT2 = featStore.getSchema().create(values);
174
                                FeatureReader reader = DataUtilities.reader(
175
                                                new Feature[] {featGT2});
176
                                featStore.addFeatures(reader);
177
                                break;
178
                        case IRowEdited.STATUS_MODIFIED:
179
                                featStore.modifyFeatures(types, values, theFilter);        
180
                                break;
181
                        case IRowEdited.STATUS_ORIGINAL:
182
                                if (bWriteAll)
183
                                {
184
                                    featGT2 = featStore.getSchema().create(values);
185
                                    reader = DataUtilities.reader(
186
                                                    new Feature[] {featGT2});
187
                                    featStore.addFeatures(reader);
188
                                }                                        
189
                                break;
190
                        case IRowEdited.STATUS_DELETED:
191
                            featStore.removeFeatures(theFilter);                                
192
                                break;
193
                }
194
                        
195

    
196
                        numReg++;
197
                } catch (IOException e) {
198
                        e.printStackTrace();
199
                        throw new EditionException(e);
200
                } catch (IllegalAttributeException e) {
201
                        e.printStackTrace();
202
                        throw new EditionException(e);
203
                }
204
                        
205
                
206
                        
207

    
208
        }
209

    
210
        /* (non-Javadoc)
211
         * @see com.iver.cit.gvsig.fmap.edition.IWriter#postProcess()
212
         */
213
        public void postProcess() throws EditionException {
214
                try
215
                {
216
                        t.commit(); // commit opperations
217
                }
218
                catch (IOException io){
219
                        try {
220
                                t.rollback();
221
                        } catch (IOException e) {
222
                                e.printStackTrace();
223
                                throw new EditionException(e);
224
                        } // cancel opperations
225
                }
226
                finally {
227
                        try {
228
                                t.close();
229
                        } catch (IOException e) {
230
                                e.printStackTrace();
231
                                throw new EditionException(e);
232
                        } // free resources
233
                }
234

    
235
        }
236

    
237
        public String getName() {
238
                return "JDBC Writer from Geotools";
239
        }
240

    
241
        public boolean canWriteGeometry(int gvSIGgeometryType) {
242
                switch (gvSIGgeometryType)
243
                {
244
                case FShape.POINT:
245
                        return true;
246
                case FShape.LINE:
247
                        return true;
248
                case FShape.POLYGON:
249
                        return true;
250
                case FShape.ARC:
251
                        return false;
252
                case FShape.ELLIPSE:
253
                        return false;                        
254
                case FShape.MULTIPOINT:
255
                        return true;                        
256
                case FShape.TEXT:
257
                        return false;                                                
258
                }
259
                return false;
260
        }
261

    
262
        public boolean canWriteAttribute(int sqlType) {
263
                switch (sqlType)
264
                {
265
                case Types.DOUBLE:
266
                case Types.FLOAT: 
267
                case Types.INTEGER:
268
                case Types.BIGINT:
269
                        return true;
270
                case Types.DATE:
271
                        return true;
272
                case Types.BIT:
273
                case Types.BOOLEAN:
274
                        return true;                        
275
                case Types.VARCHAR:
276
                case Types.CHAR: 
277
                case Types.LONGVARCHAR:
278
                        return true; // TODO: Revisar esto, porque no creo que admita campos muy grandes
279

    
280
                }
281
                
282
                return false;
283
        }
284
        
285
}