Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extCAD / src / com / iver / cit / gvsig / writers / WriterGT2.java @ 10626

History | View | Annotate | Download (8.25 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.feature.AttributeType;
15
import org.geotools.feature.AttributeTypeFactory;
16
import org.geotools.feature.Feature;
17
import org.geotools.feature.FeatureType;
18
import org.geotools.feature.FeatureTypes;
19
import org.geotools.feature.IllegalAttributeException;
20
import org.geotools.feature.SchemaException;
21
import org.geotools.filter.Filter;
22
import org.geotools.filter.FilterFactory;
23

    
24
import com.hardcode.gdbms.driver.exceptions.InitializeWriterException;
25
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
26
import com.hardcode.gdbms.engine.values.NullValue;
27
import com.iver.cit.gvsig.exceptions.visitors.ProcessWriterVisitorException;
28
import com.iver.cit.gvsig.exceptions.visitors.StartWriterVisitorException;
29
import com.iver.cit.gvsig.exceptions.visitors.StopWriterVisitorException;
30
import com.iver.cit.gvsig.fmap.core.FShape;
31
import com.iver.cit.gvsig.fmap.core.IFeature;
32
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition;
33
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
34
import com.iver.cit.gvsig.fmap.edition.writers.AbstractWriter;
35
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
36
import com.vividsolutions.jts.geom.Geometry;
37
import com.vividsolutions.jts.geom.MultiLineString;
38
import com.vividsolutions.jts.geom.MultiPoint;
39
import com.vividsolutions.jts.geom.MultiPolygon;
40
import com.vividsolutions.jts.geom.Point;
41

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

    
52
        FilterFactory filterFactory = FilterFactory.createFilterFactory();
53
        FeatureStore featStore;
54
        AttributeType[] types;
55
        Transaction t;
56
        int numReg = 0;
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 SchemaException, ReadDriverException {
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 = FeatureTypes.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)
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 StartWriterVisitorException{
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 (IOException e) {
148
                        throw new StartWriterVisitorException(getName(),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 ProcessWriterVisitorException {
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
                        throw new ProcessWriterVisitorException(getName(),e);
199
                } catch (IllegalAttributeException e) {
200
                        throw new ProcessWriterVisitorException(getName(),e);
201
                }
202

    
203

    
204

    
205

    
206
        }
207

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

    
231
        }
232

    
233
        public String getName() {
234
                return "JDBC Writer from Geotools";
235
        }
236

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

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

    
276
                }
277

    
278
                return false;
279
        }
280

    
281
        public void setFlatness(double flatness) {
282
                // TODO Auto-generated method stub
283

    
284
        }
285

    
286
        public void initialize(ITableDefinition tableDefinition) throws InitializeWriterException {
287
                super.initialize(tableDefinition);
288

    
289
        }
290

    
291
        public boolean canAlterTable() {
292
                // TODO Auto-generated method stub
293
                return false;
294
        }
295

    
296
        public boolean canSaveEdits() {
297
                // TODO Auto-generated method stub
298
                return true;
299
        }
300

    
301
}