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

History | View | Annotate | Download (4.69 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
/* gvSIG. Geographic Information System of the Valencian Government
25
*
26
* Copyright (C) 2007-2008 Infrastructures and Transports Department
27
* of the Valencian Government (CIT)
28
*
29
* This program is free software; you can redistribute it and/or
30
* modify it under the terms of the GNU General Public License
31
* as published by the Free Software Foundation; either version 2
32
* of the License, or (at your option) any later version.
33
*
34
* This program is distributed in the hope that it will be useful,
35
* but WITHOUT ANY WARRANTY; without even the implied warranty of
36
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37
* GNU General Public License for more details.
38
*
39
* You should have received a copy of the GNU General Public License
40
* along with this program; if not, write to the Free Software
41
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
42
* MA  02110-1301, USA.
43
*
44
*/
45

    
46
/*
47
* AUTHORS (In addition to CIT):
48
* 2009 IVER T.I   {{Task}}
49
*/
50

    
51
package org.gvsig.fmap.dal.store.jdbc;
52

    
53
import java.util.NoSuchElementException;
54

    
55
import org.gvsig.fmap.dal.exception.DataException;
56
import org.gvsig.fmap.dal.exception.OpenException;
57
import org.gvsig.fmap.dal.feature.FeatureType;
58
import org.gvsig.fmap.dal.feature.spi.AbstractFeatureProviderIterator;
59
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
60
import org.gvsig.tools.exception.BaseException;
61

    
62
/**
63
 * @author jmvivo
64
 *
65
 */
66
public class JDBCIterator extends AbstractFeatureProviderIterator {
67

    
68
        protected int resultSetID;
69
        protected Boolean hasNext = null;
70
        protected FeatureType featureType;
71
        protected JDBCSetProvider set;
72

    
73
        protected JDBCIterator(JDBCStoreProvider store, JDBCSetProvider set,
74
                        FeatureType featureType,
75
                        int resulsetID) throws DataException {
76
                super(store);
77
                this.resultSetID = resulsetID;
78
                this.featureType = featureType;
79
                this.set = set;
80
                this.hasNext = null;
81
                if (this.set != null && resulsetID >= 0){
82
                this.set.addResulsetReference(resulsetID);
83
                }
84
        }
85

    
86
        private JDBCStoreProvider getJDBCStoreProvider() {
87
                return (JDBCStoreProvider) getFeatureStoreProvider();
88
        }
89

    
90
    public final Object next() {
91
        try {
92
            getDataStoreProvider().open();
93
        } catch (OpenException e) {
94
            throw new RuntimeException(e);
95
        }
96
        return internalNext();
97
    }
98

    
99
    public final boolean hasNext() {
100
        return internalHasNext();
101
    }
102

    
103
        @Override
104
        protected boolean internalHasNext() {
105
                if (hasNext == null) {
106
                        try {
107
                                if (getJDBCStoreProvider().resulsetNext(resultSetID)) {
108
                                        hasNext = Boolean.TRUE;
109
                                } else {
110
                                        hasNext = Boolean.FALSE;
111
                                        this.close();
112
                                }
113
                        } catch (DataException e) {
114
                                // FIXME Exception
115
                                throw new RuntimeException(e);
116
                        }
117
                }
118
                return hasNext.booleanValue();
119
        }
120

    
121
        @Override
122
        protected Object internalNext() {
123
                if (!hasNext()) {
124
                        throw new NoSuchElementException();
125
                }
126
                FeatureProvider data;
127
                try {
128
                        data = getFeatureProvider();
129
                } catch (DataException e) {
130
                        // FIXME Exception
131
                        throw new RuntimeException(e);
132
                }
133
                hasNext = null;
134
                return data;
135
        }
136

    
137
        public void remove() {
138
                throw new UnsupportedOperationException();
139
        }
140

    
141
        protected FeatureProvider createFeatureProvider() throws DataException {
142
                return getFeatureStoreProvider().createFeatureProvider(featureType);
143
        }
144

    
145
        protected FeatureProvider getFeatureProvider() throws DataException {
146
                FeatureProvider data = createFeatureProvider();
147
                getJDBCStoreProvider().loadFeatureProvider(data, resultSetID);
148
                return data;
149
        }
150

    
151
        @Override
152
        protected void doDispose() throws BaseException {
153
                if (resultSetID >= 0){
154
                        this.close();
155
                }
156
                this.featureType = null;
157
        }
158

    
159
        protected void close() {
160
                try {
161
                        this.set.removeResulsetReference(resultSetID);
162
                        getJDBCStoreProvider().closeResulset(resultSetID);
163
                        resultSetID = -1;
164
                } catch (DataException e) {
165
                        throw new RuntimeException(e);
166
                }
167
        }
168

    
169
}