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.lib / src / main / java / org / gvsig / fmap / dal / store / simplereader / SimpleReaderSetProvider.java @ 47669

History | View | Annotate | Download (7.86 KB)

1 40435 jjdelcerro
/**
2 40559 jjdelcerro
 * gvSIG. Desktop Geographic Information System.
3 40435 jjdelcerro
 *
4 40559 jjdelcerro
 * 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 40435 jjdelcerro
 */
24 47634 fdiaz
package org.gvsig.fmap.dal.store.simplereader;
25 40435 jjdelcerro
26 47199 jjdelcerro
import java.util.Iterator;
27 45727 jjdelcerro
import java.util.List;
28 40435 jjdelcerro
import java.util.NoSuchElementException;
29
import org.gvsig.fmap.dal.exception.DataException;
30
import org.gvsig.fmap.dal.exception.ReadRuntimeException;
31
import org.gvsig.fmap.dal.feature.FeatureQuery;
32
import org.gvsig.fmap.dal.feature.FeatureType;
33
import org.gvsig.fmap.dal.feature.spi.AbstractFeatureProviderIterator;
34
import org.gvsig.fmap.dal.feature.spi.AbstractFeatureSetProvider;
35
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
36 47665 fdiaz
import org.gvsig.fmap.geom.SpatialIndex;
37 40435 jjdelcerro
import org.gvsig.tools.exception.BaseException;
38
39
/**
40
 * @author jjdelcerro
41
 *
42
 */
43 47634 fdiaz
public class SimpleReaderSetProvider extends AbstractFeatureSetProvider {
44 40435 jjdelcerro
45 47634 fdiaz
    private static class SimpleReaderFeatureProvider extends AbstractFeatureProviderLoadedOnDemand {
46 40435 jjdelcerro
47 47634 fdiaz
        protected SimpleReaderStoreProvider store;
48 40435 jjdelcerro
49 47634 fdiaz
        public SimpleReaderFeatureProvider(SimpleReaderStoreProvider store, FeatureType type) {
50 45727 jjdelcerro
            super(type);
51
            this.store = store;
52
        }
53 40435 jjdelcerro
54 45727 jjdelcerro
        @Override
55
        protected void doLoad() {
56 47652 fdiaz
            long index=-1;
57 45727 jjdelcerro
            try {
58 47652 fdiaz
                index = this.getOID();
59 47634 fdiaz
                SimpleReaderStoreProvider.RowToFeatureTranslator translator = this.store.getRowToFeatureTranslator();
60 45727 jjdelcerro
                List<String> row = this.store.getRowByIndex(index);
61
                translator.translate(index, row, this);
62 47652 fdiaz
            } catch (Throwable ex) {
63
                throw new ReadRuntimeException(getStoreFullName(), index, ex);
64 45727 jjdelcerro
            }
65
        }
66 40435 jjdelcerro
67 45727 jjdelcerro
        @Override
68
        public Long getOID() {
69
            return (Long) super.getOID();
70
        }
71 40435 jjdelcerro
72 45727 jjdelcerro
        public void setOID(long oid) {
73
            super.setOID(oid);
74
        }
75 40435 jjdelcerro
76 45727 jjdelcerro
        private String getStoreFullName() {
77
            try {
78
                return this.store.getFullName();
79
            } catch (Exception ex) {
80
                return "unknown";
81
            }
82
        }
83 40435 jjdelcerro
84 47669 fdiaz
        @Override
85
        protected void warn(String message, Throwable t) {
86
            this.store.getTimedLogger().warn(message, t);
87
        }
88 45727 jjdelcerro
    }
89 47669 fdiaz
90 47634 fdiaz
    private class SimpleReaderIterator extends AbstractFeatureProviderIterator {
91 40435 jjdelcerro
92 45727 jjdelcerro
        protected long index;
93
        protected FeatureType type;
94
        protected long count;
95 47199 jjdelcerro
        protected final FeatureQuery query;
96
        protected Iterator spatialIndexIt;
97 40435 jjdelcerro
98 47634 fdiaz
        public SimpleReaderIterator(SimpleReaderStoreProvider store, FeatureQuery query, FeatureType type,
99 45727 jjdelcerro
                long startOn) throws DataException {
100
            super(store);
101
            this.index = startOn;
102
            this.type = type;
103 47199 jjdelcerro
            this.query = query;
104 45727 jjdelcerro
            this.count = store.getFeatureCount();
105 47199 jjdelcerro
            if( this.query!=null ) {
106
                this.spatialIndexIt = createSpatialIterator(
107
                        store.getFeatureStore().getDefaultFeatureTypeQuietly(),
108 47665 fdiaz
                        query
109 47199 jjdelcerro
                );
110
            }
111 45727 jjdelcerro
        }
112 40435 jjdelcerro
113 45727 jjdelcerro
        @Override
114 47655 fdiaz
        protected SimpleReaderStoreProvider getFeatureStoreProvider() {
115
            return (SimpleReaderStoreProvider) super.getFeatureStoreProvider();
116 47199 jjdelcerro
        }
117
118
        @Override
119 45727 jjdelcerro
        protected boolean internalHasNext() {
120 47199 jjdelcerro
            if( this.spatialIndexIt != null ) {
121
                return this.spatialIndexIt.hasNext();
122
            }
123 45727 jjdelcerro
            return this.count > index;
124
        }
125 40435 jjdelcerro
126 45727 jjdelcerro
        @Override
127
        protected FeatureProvider internalNext() {
128 47634 fdiaz
            SimpleReaderFeatureProvider data = new SimpleReaderFeatureProvider(getStore(), type);
129 47199 jjdelcerro
            if( this.spatialIndexIt != null ) {
130
                Object oid = this.spatialIndexIt.next();
131
                data.setOID(oid);
132
            } else {
133
                if (index >= this.count) {
134
                    throw new NoSuchElementException();
135
                }
136
                data.setOID(index++);
137
            }
138 45727 jjdelcerro
            return data;
139
        }
140 40435 jjdelcerro
141 45727 jjdelcerro
        @Override
142
        public void remove() {
143
            throw new UnsupportedOperationException();
144
        }
145 40435 jjdelcerro
146 45727 jjdelcerro
        protected void internalDispose() {
147
            type = null;
148
        }
149 40435 jjdelcerro
150 45727 jjdelcerro
        @Override
151
        protected void doDispose() throws BaseException {
152
            // Nothing to do
153
        }
154
    }
155 40435 jjdelcerro
156 47634 fdiaz
    private class FastCSVIterator extends SimpleReaderIterator {
157 40435 jjdelcerro
158 45727 jjdelcerro
        protected FeatureProvider data;
159 40435 jjdelcerro
160 47634 fdiaz
        public FastCSVIterator(SimpleReaderStoreProvider store, FeatureQuery query, FeatureType type,
161 45727 jjdelcerro
                long startOn) throws DataException {
162 47199 jjdelcerro
            super(store, query, type, startOn);
163 47634 fdiaz
            this.data = new SimpleReaderFeatureProvider(store, type);
164 45727 jjdelcerro
        }
165 40435 jjdelcerro
166 45727 jjdelcerro
        @Override
167
        protected FeatureProvider internalNext() {
168 47199 jjdelcerro
            if( this.spatialIndexIt != null ) {
169
                Object oid = this.spatialIndexIt.next();
170
                data.setOID(oid);
171
            } else {
172
                if (index >= this.count) {
173
                    throw new NoSuchElementException();
174
                }
175
                data.setOID(index++);
176
            }
177
            return data;
178 45727 jjdelcerro
        }
179 40435 jjdelcerro
180 45727 jjdelcerro
        @Override
181
        protected void internalDispose() {
182
            super.internalDispose();
183
            data = null;
184
        }
185
    }
186 40435 jjdelcerro
187 47634 fdiaz
    public SimpleReaderSetProvider(SimpleReaderStoreProvider store, FeatureQuery query,
188 47557 fdiaz
            FeatureType providerFeatureType, FeatureType featureType)
189 45727 jjdelcerro
            throws DataException {
190 47557 fdiaz
        super(store, query, providerFeatureType, featureType);
191 45727 jjdelcerro
    }
192 40435 jjdelcerro
193 45727 jjdelcerro
    @Override
194 47634 fdiaz
    protected SimpleReaderStoreProvider getStore() {
195
        return (SimpleReaderStoreProvider) super.getStore();
196 45727 jjdelcerro
    }
197 40435 jjdelcerro
198 47665 fdiaz
    @Override
199
    protected SpatialIndex getSpatialIndex(String name) {
200
        return getStore().getSpatialIndex(name);
201
    }
202
203 45727 jjdelcerro
    protected String getStoreFullName() {
204
        try {
205
            return this.getStore().getFullName();
206
        } catch (Throwable th) {
207
            return "unknown";
208
        }
209
    }
210
211
    @Override
212
    public boolean canFilter() {
213
        return false;
214
    }
215
216
    @Override
217
    public boolean canIterateFromIndex() {
218
        return true;
219
    }
220
221
    @Override
222
    public boolean canOrder() {
223
        return false;
224
    }
225
226
    @Override
227
    public long getSize() throws DataException {
228
        return getStore().getFeatureCount();
229
    }
230
231
    @Override
232
    public boolean isEmpty() throws DataException {
233
        return getSize() == 0;
234
    }
235
236
    @Override
237
    protected void doDispose() throws BaseException {
238
    }
239
240
    @Override
241
    protected AbstractFeatureProviderIterator createIterator(long index)
242
            throws DataException {
243 47634 fdiaz
        return new SimpleReaderIterator(
244 45727 jjdelcerro
                getStore(),
245 47199 jjdelcerro
                getQuery(),
246 47436 fdiaz
                getProviderFeatureType(),
247 45727 jjdelcerro
                index
248
        );
249
    }
250
251
    @Override
252
    protected AbstractFeatureProviderIterator createFastIterator(long index)
253
            throws DataException {
254
        return new FastCSVIterator(
255
                getStore(),
256 47199 jjdelcerro
                getQuery(),
257 47436 fdiaz
                getProviderFeatureType(),
258 45727 jjdelcerro
                index
259
        );
260
    }
261 40767 jjdelcerro
}