Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.db / org.gvsig.fmap.dal.db.jdbc / src / main / java / org / gvsig / fmap / dal / store / jdbc / JDBCIterator.java @ 40596

History | View | Annotate | Download (3.76 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24

    
25
package org.gvsig.fmap.dal.store.jdbc;
26

    
27
import java.util.NoSuchElementException;
28

    
29
import org.gvsig.fmap.dal.exception.DataException;
30
import org.gvsig.fmap.dal.exception.OpenException;
31
import org.gvsig.fmap.dal.feature.FeatureType;
32
import org.gvsig.fmap.dal.feature.spi.AbstractFeatureProviderIterator;
33
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
34
import org.gvsig.tools.exception.BaseException;
35

    
36
/**
37
 * @author jmvivo
38
 *
39
 */
40
public class JDBCIterator extends AbstractFeatureProviderIterator {
41

    
42
        protected int resultSetID;
43
        protected Boolean hasNext = null;
44
        protected FeatureType featureType;
45
        protected JDBCSetProvider set;
46

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

    
60
        private JDBCStoreProvider getJDBCStoreProvider() {
61
                return (JDBCStoreProvider) getFeatureStoreProvider();
62
        }
63

    
64
    public final Object next() {
65
        try {
66
            getDataStoreProvider().open();
67
        } catch (OpenException e) {
68
            throw new RuntimeException(e);
69
        }
70
        return internalNext();
71
    }
72

    
73
    public final boolean hasNext() {
74
        return internalHasNext();
75
    }
76

    
77
        @Override
78
        protected boolean internalHasNext() {
79
                if (hasNext == null) {
80
                        try {
81
                                if (getJDBCStoreProvider().resulsetNext(resultSetID)) {
82
                                        hasNext = Boolean.TRUE;
83
                                } else {
84
                                        hasNext = Boolean.FALSE;
85
                                        this.close();
86
                                }
87
                        } catch (DataException e) {
88
                                // FIXME Exception
89
                                throw new RuntimeException(e);
90
                        }
91
                }
92
                return hasNext.booleanValue();
93
        }
94

    
95
        @Override
96
        protected Object internalNext() {
97
                if (!hasNext()) {
98
                        throw new NoSuchElementException();
99
                }
100
                FeatureProvider data;
101
                try {
102
                        data = getFeatureProvider();
103
                } catch (DataException e) {
104
                        // FIXME Exception
105
                        throw new RuntimeException(e);
106
                }
107
                hasNext = null;
108
                return data;
109
        }
110

    
111
        public void remove() {
112
                throw new UnsupportedOperationException();
113
        }
114

    
115
        protected FeatureProvider createFeatureProvider() throws DataException {
116
                return getFeatureStoreProvider().createFeatureProvider(featureType);
117
        }
118

    
119
        protected FeatureProvider getFeatureProvider() throws DataException {
120
                FeatureProvider data = createFeatureProvider();
121
                getJDBCStoreProvider().loadFeatureProvider(data, resultSetID);
122
                return data;
123
        }
124

    
125
        @Override
126
        protected void doDispose() throws BaseException {
127
                if (resultSetID >= 0){
128
                        this.close();
129
                }
130
                this.featureType = null;
131
        }
132

    
133
        protected void close() {
134
                try {
135
                        this.set.removeResulsetReference(resultSetID);
136
                        getJDBCStoreProvider().closeResulset(resultSetID);
137
                        resultSetID = -1;
138
                } catch (DataException e) {
139
                        throw new RuntimeException(e);
140
                }
141
        }
142

    
143
}