Statistics
| Revision:

root / trunk / libraries / libFMap_dataDB / src / org / gvsig / data / datastores / vectorial / db / sde / SDEFeatureCollectionEditing.java @ 20802

History | View | Annotate | Download (9.59 KB)

1

    
2
/* gvSIG. Geographic Information System of the Valencian Government
3
*
4
* Copyright (C) 2007-2008 Infrastructures and Transports Department
5
* of the Valencian Government (CIT)
6
*
7
* This program is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU General Public License
9
* as published by the Free Software Foundation; either version 2
10
* of the License, or (at your option) any later version.
11
*
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
* GNU General Public License for more details.
16
*
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20
* MA  02110-1301, USA.
21
*
22
*/
23

    
24
/*
25
* AUTHORS (In addition to CIT):
26
* ${year} IVER T.I. S.A.   {{Task}}
27
*/
28

    
29
package org.gvsig.data.datastores.vectorial.db.sde;
30

    
31
import java.util.ConcurrentModificationException;
32
import java.util.Iterator;
33
import java.util.NoSuchElementException;
34
import java.util.Vector;
35

    
36
import org.gvsig.data.datastores.vectorial.db.DBAttributeDescriptor;
37
import org.gvsig.data.datastores.vectorial.db.DBDataFeatureCollection;
38
import org.gvsig.data.datastores.vectorial.db.DBFeatureType;
39
import org.gvsig.data.exception.ReadException;
40
import org.gvsig.data.vectorial.FeatureManager;
41
import org.gvsig.data.vectorial.IFeature;
42
import org.gvsig.data.vectorial.IFeatureAttributeDescriptor;
43
import org.gvsig.data.vectorial.IFeatureType;
44

    
45
import com.esri.sde.sdk.client.SeException;
46
import com.esri.sde.sdk.client.SeLayer;
47
import com.esri.sde.sdk.client.SeQuery;
48
import com.esri.sde.sdk.client.SeRow;
49
import com.esri.sde.sdk.client.SeSqlConstruct;
50

    
51

    
52
/**
53
 * DOCUMENT ME!
54
 *
55
 * @author Vicente Caballero Navarro
56
 */
57
public class SDEFeatureCollectionEditing extends DBDataFeatureCollection {
58
    protected DBFeatureType featureType;
59
    protected String totalFilter;
60
    protected SDEStore store;
61
    private int numReg = -1;
62
    private String sql;
63
    private String sqlCount;
64
    private String totalOrder;
65
    private FeatureManager featureManager;
66
    private SeLayer layer;
67
    private SeQuery query;
68
    private SeRow row;
69
    private boolean bFirst = true;
70

    
71
    SDEFeatureCollectionEditing(FeatureManager fm, SDEStore store,
72
        IFeatureType type) {
73
        this.featureManager = fm;
74
        this.store = store;
75
        this.featureType = (DBFeatureType) type;
76

    
77
        this.totalFilter = store.getBaseWhereClause();
78
        this.totalOrder = store.getBaseOrder();
79

    
80
        this.sql = this.store.getSqlSelectPart();
81
        this.sqlCount = "Select count(*) From " +
82
            ((SDEStoreParameters) this.store.getParameters()).tableID();
83

    
84
        if (!isStringEmpty(this.totalFilter)) {
85
            this.sql = this.sql + " Where " + this.totalFilter;
86
            this.sqlCount = this.sqlCount + " Where " + this.totalFilter;
87
        }
88

    
89
        if (!isStringEmpty(this.totalOrder)) {
90
            this.sql = this.sql + " Order by " + this.totalOrder;
91
        }
92
    }
93

    
94
    /* (non-Javadoc)
95
     * @see java.util.Collection#size()
96
     */
97
    public int size() {
98
        checkModified();
99

    
100
        if (this.numReg < 0) {
101
            this.numReg = 0;
102

    
103
            try {
104
                Iterator iter = this.iterator();
105

    
106
                while (true) {
107
                    iter.next();
108
                    numReg++;
109
                }
110
            } catch (NoSuchElementException e) {
111
                //Normal condition exit
112
            }
113
        }
114

    
115
        return numReg;
116
    }
117

    
118
    /* (non-Javadoc)
119
     * @see java.lang.Iterable#iterator()
120
     */
121
    public Iterator iterator() {
122
        checkModified();
123

    
124
        try {
125
            // Debe ser forward only
126
            int size = featureType.size();
127
            String[] names = new String[size];
128

    
129
            for (int i = 0; i < size; i++) {
130
                names[i] = ((DBAttributeDescriptor) featureType.get(i)).getName();
131
            }
132

    
133
            Vector theLayers = store.getConnection().getLayers();
134

    
135
            for (int i = 0; i < theLayers.size(); i++) {
136
                SeLayer layer = (SeLayer) theLayers.elementAt(i);
137

    
138
                if (layer.getQualifiedName().compareToIgnoreCase(((SDEStoreParameters) this.store.getParameters()).tableID()) == 0) {
139
                    this.layer = layer;
140

    
141
                    break;
142
                }
143
            }
144

    
145
            SeSqlConstruct sqlCons = new SeSqlConstruct(layer.getQualifiedName());
146

    
147
            query = new SeQuery(store.getConnection(), names, sqlCons);
148
            query.prepareQuery();
149
            query.execute();
150

    
151
            row = query.fetch();
152

    
153
            if (row == null) {
154
                bFirst = true;
155
            } else {
156
                bFirst = true;
157
            }
158
        } catch (SeException e) {
159
            throw new RuntimeException(e);
160
        } catch (ReadException e) {
161
                 throw new RuntimeException(e);
162
                }
163

    
164
        SDEIterator dbfIter = new SDEIterator(this.store, this.featureType);
165

    
166
        return dbfIter;
167
    }
168

    
169
    /* (non-Javadoc)
170
     * @see org.gvsig.data.IDataCollection#dispose()
171
     */
172
    public void dispose() {
173
        this.store.deleteObserver(this);
174
        this.store = null;
175
        this.featureManager = null;
176
        this.featureType = null;
177
    }
178

    
179
    protected class SDEIterator implements Iterator {
180
        protected long position = 0;
181
        private boolean nextChecked = false;
182
        private IFeature feature;
183
        private SDEStore store;
184
        private DBFeatureType featureType;
185

    
186
        public SDEIterator(SDEStore store, DBFeatureType featureType) {
187
            this.store = store;
188
            this.featureType = featureType;
189
            position = 0;
190
        }
191

    
192
        protected void checkModified() {
193
            if (modified) {
194
                throw new ConcurrentModificationException(
195
                    "FeatureCollection modified");
196
            }
197
        }
198

    
199
        public boolean hasNext() {
200
            checkModified();
201

    
202
            IFeature feature = null;
203

    
204
            if (nextChecked) {
205
                return feature != null;
206
            }
207

    
208
            nextChecked = true;
209

    
210
            while (true) {
211
                try {
212
                    if (bFirst) {
213
                        bFirst = false;
214
                    } else {
215
                        try {
216
                            row = query.fetch();
217
                        } catch (Exception e) {
218
                            query.close();
219

    
220
                            if (position < featureManager.getNum()) {
221
                                try {
222
                                    feature = featureManager.getFeature((int) position,
223
                                            store);
224
                                } catch (ReadException e1) {
225
                                    throw new RuntimeException(new ReadException(
226
                                            this.store.getName(), e1));
227
                                }
228

    
229
                                position++;
230
                            } else {
231
                                this.feature = null;
232

    
233
                                return false;
234
                            }
235
                        }
236
                    }
237

    
238
                    if (row == null) {
239
                        query.close();
240

    
241
                        if (position < featureManager.getNum()) {
242
                            try {
243
                                feature = featureManager.getFeature((int) position,
244
                                        store);
245
                            } catch (ReadException e) {
246
                                throw new RuntimeException(new ReadException(
247
                                        this.store.getName(), e));
248
                            }
249

    
250
                            position++;
251
                        } else {
252
                            this.feature = null;
253

    
254
                            return false;
255
                        }
256
                    } else {
257
                        feature = nextFeature();
258
                    }
259
                } catch (SeException e) {
260
                    throw new RuntimeException(e);
261
                }
262

    
263
                if (featureManager.isDeleted(feature)) {
264
                    continue;
265
                }
266

    
267
                this.feature = feature;
268

    
269
                return true;
270
            }
271
        }
272
        /*
273
         *  (non-Javadoc)
274
         * @see java.util.Iterator#next()
275
         */
276
        public Object next() {
277
            checkModified();
278

    
279
            if (!nextChecked) {
280
                hasNext();
281
            }
282

    
283
            nextChecked = false;
284

    
285
            if (this.feature == null) {
286
                throw new NoSuchElementException();
287
            }
288

    
289
            IFeature feature = this.feature;
290
            this.feature = null;
291

    
292
            return feature;
293
        }
294

    
295
        private IFeature nextFeature() {
296
            SDEFeature feat = null;
297

    
298
            try {
299
                feat = new SDEFeature(featureType, store, row);
300

    
301
                feat.loading();
302

    
303
                Iterator iterator = featureType.iterator();
304

    
305
                while (iterator.hasNext()) {
306
                    IFeatureAttributeDescriptor descriptor = (IFeatureAttributeDescriptor) iterator.next();
307
                    feat.loadValueFromResulset(row, descriptor);
308
                }
309
            } catch (ReadException e) {
310
                throw new RuntimeException(e);
311
            }
312

    
313
            feat.stopLoading();
314

    
315
            return feat;
316
        }
317
        /*
318
         *  (non-Javadoc)
319
         * @see java.util.Iterator#remove()
320
         */
321
        public void remove() {
322
            throw new UnsupportedOperationException();
323
        }
324
    }
325
}