Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_dataDB / src / org / gvsig / fmap / data / feature / db / jdbc / AbstractJDBCIterator.java @ 23680

History | View | Annotate | Download (6 KB)

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

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

    
28
package org.gvsig.fmap.data.feature.db.jdbc;
29

    
30
import java.sql.Connection;
31
import java.sql.ResultSet;
32
import java.sql.Statement;
33
import java.util.Iterator;
34
import java.util.NoSuchElementException;
35

    
36
import org.gvsig.fmap.data.ReadException;
37
import org.gvsig.fmap.data.feature.Feature;
38
import org.gvsig.fmap.data.feature.FeatureManager;
39
import org.gvsig.fmap.data.feature.db.DBFeatureType;
40
import org.gvsig.fmap.data.feature.expressionevaluator.Filter;
41

    
42
public abstract class AbstractJDBCIterator implements Iterator {
43

    
44
        protected boolean nextChecked = false;
45
        protected Feature feature;
46
        protected ResultSet rs;
47
        protected JDBCStore store;
48
        protected DBFeatureType featureType;
49
        protected boolean rsEOF = false;
50
        protected boolean memoryEOF = false;
51
        protected int memoryPosition = 0;
52
        protected int fetchSize;
53
        protected String sql;
54
        protected int page;
55
        protected int index;
56
        protected FeatureManager featureManager = null;
57
        protected Filter featureFilter = null;
58
        
59
        private int initialPosition = 1;
60

    
61
    public AbstractJDBCIterator(JDBCStore store, DBFeatureType featureType,
62
            String sql, int fetchSize, FeatureManager featureManager,
63
            Filter featureFilter, int initialPosition) throws ReadException {
64
        this.store = store;
65
        this.featureType = featureType;
66
        this.fetchSize = fetchSize;
67
        this.sql = sql;
68
        this.page = 0;
69
        this.index = 0;
70
        this.featureManager = featureManager;
71
        this.featureFilter = featureFilter;
72
        this.initialPosition = initialPosition;
73
        this.createResulset();
74
    }
75

    
76
        public AbstractJDBCIterator(JDBCStore store, DBFeatureType featureType,
77
            String sql, int fetchSize, FeatureManager featureManager,
78
            Filter featureFilter) throws ReadException {
79
            this(store, featureType, sql, fetchSize, featureManager, featureFilter,
80
                1);
81
    }
82

    
83
        protected Feature nextDBFeature() {
84
                Feature feature = null;
85
                while (true) {
86
                        try {
87
                                if (rs.next()) {
88
                                        feature = this.createFeatureFromTheResulset();
89
                                        this.index++;
90
                                        return feature;
91
                                } else {
92
                                        if (this.index == this.fetchSize) {
93
                                                this.index = 0;
94
                                                this.page++;
95
                                                this.createResulset();
96
                                                continue;
97
                                        } else {
98
                                                return null;
99
                                        }
100

    
101
                                }
102
                        } catch (java.sql.SQLException e) {
103
                                throw new RuntimeException(new ReadException(this.store
104
                                                .getName(), e));
105
                        } catch (ReadException e) {
106
                                throw new RuntimeException(e);
107
                        }
108
                }
109
        }
110

    
111
        protected Feature nextMemoryFeature() {
112
                Feature feature = null;
113
                while (true) {
114
                        if (this.memoryPosition < this.featureManager.getNum()) {
115
                                try {
116
                                        feature = featureManager.getFeature(this.memoryPosition,
117
                                                        store, this.featureType);
118
                                        this.memoryPosition++;
119
                                        if (this.featureFilter == null) {
120
                                                break;
121
                                        }
122
                                        if (!this.featureFilter.evaluate(feature)) {
123
                                                continue;
124
                                        }
125
                                } catch (ReadException e) {
126
                                        throw new RuntimeException(new ReadException(this.store
127
                                                        .getName(), e));
128
                                }
129
                        } else {
130
                                this.memoryEOF = true;
131
                                break;
132
                        }
133
                }
134
                return feature;
135
        }
136

    
137
        protected abstract Feature createFeatureFromTheResulset()
138
                        throws ReadException;
139

    
140
        protected abstract void checkModified();
141

    
142
        protected abstract Connection getConnection() throws ReadException;
143

    
144
        protected abstract String getFinalResulsetSQL();
145

    
146
        protected void createResulset() throws ReadException {
147
                String mSql = this.getFinalResulsetSQL();
148
                Connection conn = this.getConnection();
149
                try {
150
                        if (this.rs != null) {
151
                                this.rs.close();
152
                        }
153

    
154
                        Statement st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,
155
                                        ResultSet.CONCUR_READ_ONLY);
156
                        this.rs = st.executeQuery(mSql);
157
                        
158
                        if (initialPosition > 1) {
159
                rs.absolute(initialPosition);
160
            }
161

    
162
                } catch (java.sql.SQLException e) {
163
                        throw new SQLException(mSql, this.store.getName(), e);
164
                }
165

    
166
        }
167

    
168

    
169
        public boolean hasNext() {
170
                checkModified();
171

    
172
                if (nextChecked) {
173
                        return this.feature != null;
174
                }
175
                Feature feature = null;
176
                nextChecked = true;
177
                if (this.rsEOF && this.memoryEOF) {
178
                        return false;
179
                }
180
                while (true) {
181
                        try {
182
                                if (!this.rsEOF) {
183
                                        feature = nextDBFeature();
184
                                        if (feature == null) {
185
                                                this.close();
186
                                                continue;
187
                                        }
188
                                        if (this.featureManager != null
189
                                                        && this.featureManager.isDeleted(feature)) {
190
                                                continue;
191
                                        }
192
                                        this.feature = feature;
193
                                        return true;
194
                                } else {
195
                                        if (this.featureManager == null) {
196
                                                this.memoryEOF = true;
197
                                                return false;
198
                                        }
199
                                        this.feature = nextMemoryFeature();
200
                                        return this.feature != null;
201
                                }
202
                        } catch (java.sql.SQLException e) {
203
                                throw new RuntimeException(new ReadException(this.store
204
                                                .getName(), e));
205
                        }
206
                }
207
        }
208

    
209
        public Object next() {
210
                checkModified();
211
                if (!nextChecked) {
212
                        hasNext();
213
                }
214
                if (this.feature == null) {
215
                        throw new NoSuchElementException();
216
                }
217
                nextChecked = false;
218
                Feature feature = this.feature;
219
                this.feature = null;
220
                return feature;
221
        }
222

    
223
        public void remove() {
224
                throw new UnsupportedOperationException();
225
        }
226

    
227
        protected void close() throws java.sql.SQLException {
228
                if (rs == null) {
229
                        return;
230
                }
231
                rs.close();
232
                this.rsEOF = true;
233
        }
234
}