Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2021 / libraries / libFMap_daldb / src / org / gvsig / fmap / dal / store / jdbc / JDBCIterator.java @ 34088

History | View | Annotate | Download (3.41 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
* 2009 IVER T.I   {{Task}}
26
*/
27

    
28
package org.gvsig.fmap.dal.store.jdbc;
29

    
30
import java.util.NoSuchElementException;
31

    
32
import org.gvsig.fmap.dal.exception.DataException;
33
import org.gvsig.fmap.dal.feature.FeatureType;
34
import org.gvsig.fmap.dal.feature.spi.AbstractFeatureProviderIterator;
35
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
36
import org.gvsig.tools.exception.BaseException;
37

    
38
/**
39
 * @author jmvivo
40
 *
41
 */
42
public class JDBCIterator extends AbstractFeatureProviderIterator {
43

    
44
        protected int resultSetID;
45
        protected Boolean hasNext = null;
46
        protected FeatureType featureType;
47
        protected JDBCSetProvider set;
48

    
49
        protected JDBCIterator(JDBCStoreProvider store, JDBCSetProvider set,
50
                        FeatureType featureType,
51
                        int resulsetID) throws DataException {
52
                super(store);
53
                this.resultSetID = resulsetID;
54
                this.featureType = featureType;
55
                this.set = set;
56
                this.hasNext = null;
57
                if (this.set != null && resulsetID >= 0){
58
                this.set.addResulsetReference(resulsetID);
59
                }
60
        }
61

    
62
        private JDBCStoreProvider getJDBCStoreProvider() {
63
                return (JDBCStoreProvider) getFeatureStoreProvider();
64
        }
65

    
66
        @Override
67
        protected boolean internalHasNext() {
68
                if (hasNext == null) {
69
                        try {
70
                                if (getJDBCStoreProvider().resulsetNext(resultSetID)) {
71
                                        hasNext = Boolean.TRUE;
72
                                } else {
73
                                        hasNext = Boolean.FALSE;
74
                                        this.close();
75
                                }
76
                        } catch (DataException e) {
77
                                // FIXME Exception
78
                                throw new RuntimeException(e);
79
                        }
80
                }
81
                return hasNext.booleanValue();
82
        }
83

    
84
        @Override
85
        protected Object internalNext() {
86
                if (!hasNext()) {
87
                        throw new NoSuchElementException();
88
                }
89
                FeatureProvider data;
90
                try {
91
                        data = getFeatureProvider();
92
                } catch (DataException e) {
93
                        // FIXME Exception
94
                        throw new RuntimeException(e);
95
                }
96
                hasNext = null;
97
                return data;
98
        }
99

    
100
        public void remove() {
101
                throw new UnsupportedOperationException();
102
        }
103

    
104
        protected FeatureProvider createFeatureProvider() throws DataException {
105
                return getFeatureStoreProvider().createFeatureProvider(featureType);
106
        }
107

    
108
        protected FeatureProvider getFeatureProvider() throws DataException {
109
                FeatureProvider data = createFeatureProvider();
110
                getJDBCStoreProvider().loadFeatureProvider(data, resultSetID);
111
                return data;
112
        }
113

    
114
        @Override
115
        protected void doDispose() throws BaseException {
116
                if (resultSetID >= 0){
117
                        this.close();
118
                }
119
                this.featureType = null;
120
        }
121

    
122
        protected void close() {
123
                try {
124
                        this.set.removeResulsetReference(resultSetID);
125
                        getJDBCStoreProvider().closeResulset(resultSetID);
126
                        resultSetID = -1;
127
                } catch (DataException e) {
128
                        throw new RuntimeException(e);
129
                }
130
        }
131

    
132
}