Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.file / org.gvsig.fmap.dal.file.dbf / src / main / java / org / gvsig / fmap / dal / store / dbf / DBFSetProvider.java @ 47436

History | View | Annotate | Download (5.15 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
package org.gvsig.fmap.dal.store.dbf;
25

    
26
import java.util.NoSuchElementException;
27

    
28
import org.gvsig.fmap.dal.exception.DataException;
29
import org.gvsig.fmap.dal.exception.ReadRuntimeException;
30
import org.gvsig.fmap.dal.feature.FeatureQuery;
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.AbstractFeatureSetProvider;
34
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
35
import org.gvsig.tools.exception.BaseException;
36

    
37
/**
38
 * @author jjdelcerro
39
 *
40
 */
41
public class DBFSetProvider extends AbstractFeatureSetProvider {
42

    
43
        public DBFSetProvider(DBFStoreProvider store, FeatureQuery query,
44
                        FeatureType providerFeatureType, FeatureType storeFeatureType)
45
                        throws DataException {
46
//                super(store, query, providerFeatureType, storeFeatureType);
47
                super(store, query, getEditable(providerFeatureType), storeFeatureType);
48
        }
49
        
50
        private static FeatureType getEditable(FeatureType type) {
51
            try {
52
//                return type;
53
                return type.getEditable();
54
            } catch (UnsupportedOperationException ex) {
55
                return type.getCopy().getEditable();
56
            }
57
        }
58

    
59
        public boolean canFilter() {
60
                return false;
61
        }
62

    
63
        public boolean canIterateFromIndex() {
64
                return true;
65
        }
66

    
67
        public boolean canOrder() {
68
                return false;
69
        }
70

    
71
        public long getSize() throws DataException {
72
                return getStore().getFeatureCount();
73
        }
74

    
75
        public boolean isEmpty() throws DataException {
76
                return getSize() == 0;
77
        }
78

    
79
        @Override
80
        protected void doDispose() throws BaseException {
81
        }
82

    
83
        protected AbstractFeatureProviderIterator createIterator(long index)
84
                        throws DataException {
85
                return new DBFIterator((DBFStoreProvider) getStore(), getProviderFeatureType(),
86
                                index);
87
        }
88

    
89
        protected AbstractFeatureProviderIterator createFastIterator(long index)
90
                        throws DataException {
91
                return new FastDBFIterator((DBFStoreProvider) getStore(),
92
                                getProviderFeatureType(), index);
93
        }
94

    
95
        protected class DBFIterator extends AbstractFeatureProviderIterator {
96
                protected long index;
97
                protected FeatureType type;
98
                protected long count;
99

    
100
                public DBFIterator(DBFStoreProvider store, FeatureType type,
101
                                long startOn) throws DataException {
102
                        super(store);
103
                        this.index = startOn;
104
                        this.type = type;
105
                        this.count = store.getFeatureCount();
106
                }
107

    
108
                protected boolean internalHasNext() {
109
                        return this.count > index;
110
                }
111

    
112
                protected Object internalNext() {
113
                        if (index >= this.count) {
114
                                throw new NoSuchElementException();
115
                        }
116
                        try {
117

    
118
                                FeatureProvider ret =
119
                                                getDBFStoreProvider().getFeatureProviderByIndex(index,
120
                                                                this.type, getStoreFeatureType());
121
                                index++;
122
                                return ret;
123
                        } catch (DataException e) {
124
                                throw new ReadRuntimeException(getNameForMessages(), index,e);
125
                        }
126
                }
127

    
128
                public void remove() {
129
                        throw new UnsupportedOperationException();
130
                }
131

    
132
                protected void internalDispose() {
133
                        type = null;
134
                }
135

    
136
                protected DBFStoreProvider getDBFStoreProvider() {
137
                        return (DBFStoreProvider) getFeatureStoreProvider();
138
                }
139
                
140
                protected String getNameForMessages() {
141
                    // Solo con proposito de mostrar en mensajes de error.
142
                    try {
143
                        return getFeatureStoreProvider().getName();
144
                    } catch(Exception ex) {
145
                        return "unknown";
146
                    }
147
                }
148

    
149
                @Override
150
                protected void doDispose() throws BaseException {
151
                        // Nothing to do
152
                }
153
        }
154

    
155
        protected class FastDBFIterator extends DBFIterator {
156
                protected FeatureProvider data;
157

    
158
                public FastDBFIterator(DBFStoreProvider store, FeatureType type,
159
                                long startOn) throws DataException {
160
                        super(store, type, startOn);
161
                        this.data = getDBFStoreProvider().createFeatureProvider(type, getStoreFeatureType());
162
                }
163

    
164
                protected Object internalNext() {
165
                        if (index >= this.count) {
166
                                throw new NoSuchElementException();
167
                        }
168

    
169
                        try {
170
                                getDBFStoreProvider().initFeatureProviderByIndex(this.data,
171
                                                index, type);
172
                        } catch (DataException e) {
173
                                throw new ReadRuntimeException(getNameForMessages(), index, e);
174
                        }
175
                        index++;
176
                        return this.data;
177
                }
178

    
179
                protected void internalDispose() {
180
                        super.internalDispose();
181
                        data = null;
182
                }
183
        }
184
}