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 @ 40435

History | View | Annotate | Download (3.75 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.exception.OpenException;
34
import org.gvsig.fmap.dal.feature.FeatureType;
35
import org.gvsig.fmap.dal.feature.spi.AbstractFeatureProviderIterator;
36
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
37
import org.gvsig.tools.exception.BaseException;
38

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

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

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

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

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

    
76
    public final boolean hasNext() {
77
        return internalHasNext();
78
    }
79

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

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

    
114
        public void remove() {
115
                throw new UnsupportedOperationException();
116
        }
117

    
118
        protected FeatureProvider createFeatureProvider() throws DataException {
119
                return getFeatureStoreProvider().createFeatureProvider(featureType);
120
        }
121

    
122
        protected FeatureProvider getFeatureProvider() throws DataException {
123
                FeatureProvider data = createFeatureProvider();
124
                getJDBCStoreProvider().loadFeatureProvider(data, resultSetID);
125
                return data;
126
        }
127

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

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

    
146
}