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 / jdbc2 / impl / JDBCSetProvider.java @ 47779

History | View | Annotate | Download (10.2 KB)

1 43020 jjdelcerro
/**
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 modify it under
7
 * the terms of the GNU General Public License as published by the Free Software
8
 * Foundation; either version 3 of the License, or (at your option) any later
9
 * version.
10
 *
11
 * This program is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
 * details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with
17
 * this program; if not, write to the Free Software Foundation, Inc., 51
18
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.fmap.dal.store.jdbc2.impl;
24
25
import java.util.ArrayList;
26
import java.util.List;
27
import java.util.NoSuchElementException;
28
import org.apache.commons.collections.CollectionUtils;
29
import org.apache.commons.lang3.BooleanUtils;
30 47029 fdiaz
import org.gvsig.fmap.dal.DataStoreParameters;
31 43020 jjdelcerro
import org.gvsig.fmap.dal.exception.DataException;
32
import org.gvsig.fmap.dal.feature.FeatureQuery;
33
import org.gvsig.fmap.dal.feature.FeatureType;
34
35
import org.gvsig.fmap.dal.feature.spi.AbstractFeatureSetProvider;
36
import org.gvsig.fmap.dal.feature.spi.AbstractFeatureStoreProvider;
37
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters;
38
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
39
import org.gvsig.fmap.dal.store.jdbc2.JDBCStoreProvider;
40
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils;
41
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory;
42
import org.gvsig.fmap.dal.store.jdbc2.ResulSetControler;
43
import org.gvsig.fmap.dal.store.jdbc2.ResulSetControler.ResultSetEntry;
44
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.TableIsEmptyOperation;
45
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.ResultSetForSetProviderOperation;
46
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.CountOperation;
47
import org.gvsig.tools.evaluator.Evaluator;
48
import org.gvsig.tools.exception.BaseException;
49
import org.slf4j.Logger;
50
import org.slf4j.LoggerFactory;
51
52
public class JDBCSetProvider extends AbstractFeatureSetProvider {
53
54 44058 jjdelcerro
    final static protected Logger LOGGER = LoggerFactory.getLogger(JDBCSetProvider.class);
55 43020 jjdelcerro
56
    private class EmptyJDBCIterator extends JDBCIterator {
57
58
        protected EmptyJDBCIterator(JDBCStoreProvider store) throws DataException {
59
            super(store, null, null, null);
60
        }
61
62
        @Override
63
        protected boolean internalHasNext() {
64
            return false;
65
        }
66
67
        @Override
68
        protected Object internalNext() {
69
            throw new NoSuchElementException();
70
        }
71
72
        @Override
73
        protected void doDispose() throws BaseException {
74
            // nothing to do
75
        }
76
77
    }
78
79
80
    protected Long size = null;
81
    protected Boolean isEmpty = null;
82
    protected List<ResultSetEntry> resultSets;
83 47029 fdiaz
    private int defaultFetchSize;
84 43020 jjdelcerro
85
    public JDBCSetProvider(
86
            AbstractFeatureStoreProvider store,
87
            JDBCHelper helper,
88
            FeatureQuery query,
89
            FeatureType featureType
90
    ) throws DataException {
91
        super(store, query, featureType);
92
        this.resultSets = new ArrayList<>();
93 47029 fdiaz
        this.defaultFetchSize = 1000;
94
        try {
95
            DataStoreParameters params = store.getParameters();
96
            Object x = params.getDynValue("defaultFetchSize");
97
            if(x != null){
98
                this.defaultFetchSize = (Integer)x;
99
            }
100
        } catch (Exception e) {
101
            LOGGER.warn("Can't get defaultFetchSize from storeParameters", e);
102
        }
103 43020 jjdelcerro
    }
104
105
    /**
106
     * @return the defaultFetchSize
107
     */
108
    public int getDefaultFetchSize() {
109
        return defaultFetchSize;
110
    }
111
112
    /**
113
     * @param defaultFetchSize the defaultFetchSize to set
114
     */
115
    public void setDefaultFetchSize(int defaultFetchSize) {
116
        this.defaultFetchSize = defaultFetchSize;
117
    }
118
119
//    @Override
120
//    protected JDBCStoreProvider getStore() {
121
//        return (JDBCStoreProvider) super.getStore();
122
//    }
123
124
    protected JDBCStoreProvider getJDBCStore() {
125
//        Corregido el problema en AbstractFeatureSetProvider, de que getStore
126
//        debe ser un FeatureStoreProvider y no un AbstractFeatureStoreProvider
127
//        este metodo debe desaparecer y quedar el getStore de arriba que esta
128
//          comentarizado
129
        return (JDBCStoreProvider) super.getStore();
130
    }
131
132
    protected JDBCHelper getHelper() {
133
        return this.getJDBCStore().getHelper();
134
    }
135
136
    protected OperationsFactory getOperations() {
137
        return this.getHelper().getOperations();
138
    }
139
140
    @Override
141
    public boolean canIterateFromIndex() {
142
        return this.getHelper().supportOffsetInSelect();
143
    }
144
145
    @Override
146
    public boolean canFilter() {
147 47436 fdiaz
        return this.getHelper().supportFilter(this.getProviderFeatureType(), getQuery().getFilter());
148 43020 jjdelcerro
    }
149
150
    @Override
151
    public boolean canOrder() {
152 47436 fdiaz
        return this.getHelper().supportOrder(this.getProviderFeatureType(), getQuery().getOrder());
153 43020 jjdelcerro
    }
154
155
    public ResulSetControler getResulSetControler() {
156
        return this.getHelper().getResulSetControler();
157
    }
158
159
    @Override
160
    protected void doDispose() throws BaseException {
161
        if( ! CollectionUtils.isEmpty(this.resultSets) ) {
162
            for( ResultSetEntry resulset : this.resultSets ) {
163
               JDBCUtils.closeQuietly(resulset);
164
            }
165
        }
166
        size = null;
167
        isEmpty = null;
168
    }
169
170
    protected int getFetchSize() {
171
        long pageSize = -1;
172
        if (getQuery() != null) {
173
            pageSize = getQuery().getPageSize();
174
            pageSize = pageSize > Integer.MAX_VALUE ? Integer.MAX_VALUE : pageSize;
175
        }
176
        return (pageSize > 0 ? (int) pageSize : defaultFetchSize);
177
    }
178
179
    @Override
180
    public long getSize() throws DataException {
181
        if (size == null) {
182
            JDBCStoreParameters params = this.getJDBCStore().getParameters();
183
            CountOperation selectCount = this.getOperations().createCount(
184 47436 fdiaz
                    this.getProviderFeatureType(),
185 44058 jjdelcerro
                    this.getOperations().createTableReference(params),
186 43020 jjdelcerro
                    params.getBaseFilter(),
187 44727 jjdelcerro
                    this.getQuery()
188 43020 jjdelcerro
            );
189
            size = (Long) selectCount.perform();
190
        }
191
        return size;
192
    }
193
194
    @Override
195
    public boolean isEmpty() throws DataException {
196
        if (this.isEmpty == null) {
197
            if (this.size == null) {
198
                JDBCStoreParameters params = this.getJDBCStore().getParameters();
199 46010 jjdelcerro
//                String filtersql = null;
200
//                Evaluator filter = this.getQuery().getFilter();
201
//                if( filter != null ) {
202
//                    filtersql = filter.getSQL();
203
//                }
204 44058 jjdelcerro
                TableIsEmptyOperation isEmpty_ = this.getOperations().createTableIsEmpty(
205 47436 fdiaz
                        this.getProviderFeatureType(),
206 44058 jjdelcerro
                        this.getOperations().createTableReference(params),
207 43020 jjdelcerro
                        params.getBaseFilter(),
208 46010 jjdelcerro
                        this.getQuery()
209 43020 jjdelcerro
                );
210 44058 jjdelcerro
                this.isEmpty = (Boolean) isEmpty_.perform();
211 43020 jjdelcerro
            } else {
212
                this.isEmpty = (this.size < 1);
213
            }
214
        }
215
        return isEmpty;
216
    }
217
218 43358 jjdelcerro
219 44727 jjdelcerro
    @Override
220 43358 jjdelcerro
    protected JDBCIterator createFastIterator(long index) throws DataException {
221
        return createFastIterator(index, 0);
222
    }
223
224 43020 jjdelcerro
    @Override
225 43358 jjdelcerro
    protected JDBCIterator createFastIterator(long index, long elements) throws DataException {
226 43020 jjdelcerro
        if( BooleanUtils.isTrue(isEmpty) ) {
227
            return new EmptyJDBCIterator(this.getJDBCStore());
228
        }
229
230
        JDBCStoreParameters params = this.getJDBCStore().getParameters();
231
        FeatureType storeType = this.getStore()
232
                .getStoreServices()
233 47436 fdiaz
                .getProviderFeatureType(this.getProviderFeatureType().getId());
234 43020 jjdelcerro
        ResultSetForSetProviderOperation createResultSet
235
            = getOperations().createResultSetForSetProvider(
236 44058 jjdelcerro
                    this.getOperations().createTableReference(params),
237 43020 jjdelcerro
                    params.getBaseFilter(),
238
                    params.getBaseOrder(),
239
                    this.getQuery(),
240
                    storeType,
241 47436 fdiaz
                    this.getProviderFeatureType(),
242 43358 jjdelcerro
                    elements,
243 43020 jjdelcerro
                    index,
244 45735 omartinez
                    this.getFetchSize()
245 43020 jjdelcerro
            );
246
247
        ResultSetEntry resultSetEntry = (ResultSetEntry) createResultSet.perform();
248
249
        this.resultSets.add(resultSetEntry);
250
        return new JDBCFastIterator(
251
                this.getJDBCStore(),
252
                this,
253 47436 fdiaz
                this.getProviderFeatureType(),
254 43020 jjdelcerro
                resultSetEntry
255
        );
256
    }
257
258
    @Override
259
    protected JDBCIterator createIterator(long index) throws DataException {
260 43358 jjdelcerro
        return createIterator(index, -1);
261
    }
262
263
    @Override
264
    protected JDBCIterator createIterator(long index, long elements) throws DataException {
265 43020 jjdelcerro
        if( BooleanUtils.isTrue(isEmpty) ) {
266
            return new EmptyJDBCIterator(this.getJDBCStore());
267
        }
268
        JDBCStoreParameters params = this.getJDBCStore().getParameters();
269
        FeatureType storeType = this.getStore()
270
                .getStoreServices()
271 47436 fdiaz
                .getProviderFeatureType(this.getProviderFeatureType().getId());
272 43020 jjdelcerro
        ResultSetForSetProviderOperation createResultSet
273
            = getOperations().createResultSetForSetProvider(
274 44058 jjdelcerro
                    this.getOperations().createTableReference(params),
275 43020 jjdelcerro
                    params.getBaseFilter(),
276
                    params.getBaseOrder(),
277
                    this.getQuery(),
278
                    storeType,
279 47436 fdiaz
                    this.getProviderFeatureType(),
280 43358 jjdelcerro
                    elements,
281 43020 jjdelcerro
                    index,
282
                    this.getDefaultFetchSize()
283
            );
284
285
        ResultSetEntry resultSetEntry = (ResultSetEntry) createResultSet.perform();
286
        this.resultSets.add(resultSetEntry);
287
        return new JDBCIterator(
288
                this.getJDBCStore(),
289
                this,
290 47436 fdiaz
                this.getProviderFeatureType(),
291 43020 jjdelcerro
                resultSetEntry
292
        );
293
    }
294
295
}