svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.impl / src / main / java / org / gvsig / fmap / dal / feature / impl / featureset / DefaultFeatureSet.java @ 43913
History | View | Annotate | Download (19.5 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.feature.impl.featureset; |
25 |
|
26 |
import org.gvsig.fmap.dal.feature.impl.dynobjectutils.DynObjectSetFeatureSetFacade; |
27 |
import java.util.ArrayList; |
28 |
import java.util.Collections; |
29 |
import java.util.Iterator; |
30 |
import java.util.List; |
31 |
import java.util.NoSuchElementException; |
32 |
|
33 |
import org.gvsig.fmap.dal.DataStore; |
34 |
import org.gvsig.fmap.dal.exception.DataException; |
35 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
36 |
import org.gvsig.fmap.dal.feature.Feature; |
37 |
import org.gvsig.fmap.dal.feature.FeatureIndexes; |
38 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
39 |
import org.gvsig.fmap.dal.feature.FeatureQueryOrder; |
40 |
import org.gvsig.fmap.dal.feature.FeatureQueryOrder.FeatureQueryOrderMember; |
41 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
42 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
43 |
import org.gvsig.fmap.dal.feature.FeatureStoreNotification; |
44 |
import org.gvsig.fmap.dal.feature.FeatureType; |
45 |
import org.gvsig.fmap.dal.feature.exception.ConcurrentDataModificationException; |
46 |
import org.gvsig.fmap.dal.feature.exception.FeatureSetInitializeException; |
47 |
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureStore; |
48 |
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureStoreTransforms; |
49 |
import org.gvsig.fmap.dal.feature.spi.FeatureSetProvider; |
50 |
import org.gvsig.tools.dispose.DisposableIterator; |
51 |
import org.gvsig.tools.dynobject.DynObjectSet; |
52 |
import org.gvsig.tools.evaluator.Evaluator; |
53 |
import org.gvsig.tools.exception.BaseException; |
54 |
import org.gvsig.tools.observer.Observable; |
55 |
import org.gvsig.tools.observer.Observer; |
56 |
import org.gvsig.tools.visitor.VisitCanceledException; |
57 |
import org.gvsig.tools.visitor.Visitor; |
58 |
|
59 |
public class DefaultFeatureSet extends AbstractFeatureSet implements |
60 |
FeatureSet, Observer {
|
61 |
|
62 |
private static final int NO_CHECKED = -1; |
63 |
private static final int DEFAULT = 0; |
64 |
private static final int FILTERED = 1; |
65 |
private static final int ORDERED = 2; |
66 |
private static final int ORDERED_FILTERED = 3; |
67 |
private static final int EDITED = 4; |
68 |
private static final int EDITED_FILTERED = 5; |
69 |
private static final int ORDERD_EDITED = 6; |
70 |
private static final int ORDERED_EDITED_FILTER = 7; |
71 |
|
72 |
private boolean sourceStoreModified; |
73 |
private boolean ownFeaturesModified; |
74 |
DefaultFeatureStore store; |
75 |
private List featureTypes; |
76 |
FeatureQuery query; |
77 |
FeatureSetProvider provider; |
78 |
private long size; |
79 |
private int iteratorMode; |
80 |
List orderedData;
|
81 |
private Feature featureToIgnoreNotification;
|
82 |
DefaultFeatureStoreTransforms transform; |
83 |
private FeatureQuery queryForProvider;
|
84 |
private FeatureType defatulFeatureType;
|
85 |
private FeatureType defatulFeatureTypeForProvider;
|
86 |
|
87 |
public DefaultFeatureSet(DefaultFeatureStore store, FeatureQuery query)
|
88 |
throws DataException {
|
89 |
this.featureToIgnoreNotification = null; |
90 |
this.iteratorMode = NO_CHECKED;
|
91 |
this.sourceStoreModified = false; |
92 |
this.ownFeaturesModified = false; |
93 |
this.size = -1; |
94 |
this.orderedData = null; |
95 |
this.store = store;
|
96 |
if (this.store.isEditing()) { |
97 |
this.transform = this.store.getFeatureTypeManager().getTransforms(); |
98 |
} else {
|
99 |
this.transform =
|
100 |
(DefaultFeatureStoreTransforms) store.getTransforms(); |
101 |
} |
102 |
this.query = query;
|
103 |
try {
|
104 |
this.queryForProvider = (FeatureQuery) query.clone();
|
105 |
} catch (CloneNotSupportedException e) { |
106 |
throw new FeatureSetInitializeException(e); |
107 |
} |
108 |
|
109 |
this.featureTypes = new ArrayList(); |
110 |
if (this.query.getFeatureTypeId() == null |
111 |
&& this.query.getAttributeNames() == null) { |
112 |
this.defatulFeatureType = this.store.getDefaultFeatureType(); |
113 |
this.featureTypes.addAll(this.store.getFeatureTypes()); |
114 |
} else {
|
115 |
this.defatulFeatureType = this.store.getFeatureType(this.query); |
116 |
this.featureTypes.add(this.defatulFeatureType); |
117 |
} |
118 |
if (this.transform != null && !this.transform.isEmpty()) { |
119 |
this.fixQueryForProvider(this.queryForProvider, this.transform); |
120 |
} else {
|
121 |
this.defatulFeatureTypeForProvider = this.defatulFeatureType; |
122 |
} |
123 |
|
124 |
FeatureIndexes indexes = store.getIndexes(); |
125 |
if (this.queryForProvider.hasFilter() && indexes != null |
126 |
&& indexes.areValid()) { |
127 |
this.provider =
|
128 |
(FeatureSetProvider) indexes |
129 |
.getFeatureSet(this.queryForProvider.getFilter());
|
130 |
} |
131 |
if (this.provider == null) { |
132 |
this.provider =
|
133 |
this.store.getProvider().createSet(this.queryForProvider, |
134 |
this.defatulFeatureTypeForProvider);
|
135 |
} |
136 |
this.store.addObserver(this); |
137 |
} |
138 |
|
139 |
private void fixQueryForProvider(FeatureQuery theQueryForProvider, |
140 |
DefaultFeatureStoreTransforms transformsToUse) throws DataException {
|
141 |
theQueryForProvider.clearAttributeNames(); |
142 |
FeatureType ftype = |
143 |
transformsToUse.getSourceFeatureTypeFrom(this.defatulFeatureType);
|
144 |
theQueryForProvider.setFeatureTypeId(ftype.getId()); |
145 |
this.defatulFeatureTypeForProvider = ftype;
|
146 |
|
147 |
if (transformsToUse.isTransformsOriginalValues()) {
|
148 |
theQueryForProvider.clearFilter(); |
149 |
FeatureQueryOrder fqo = theQueryForProvider.getOrder(); |
150 |
if (fqo != null) { |
151 |
fqo.clear(); |
152 |
} |
153 |
return;
|
154 |
|
155 |
} |
156 |
|
157 |
// Filter
|
158 |
Evaluator filter = theQueryForProvider.getFilter(); |
159 |
if (filter != null) { |
160 |
boolean canUseFilter = true; |
161 |
if (filter.getFieldsInfo() == null) { |
162 |
canUseFilter = false;
|
163 |
} else {
|
164 |
canUseFilter = areEvaluatorFieldsInAttributes(filter, ftype); |
165 |
} |
166 |
|
167 |
if (!canUseFilter) {
|
168 |
theQueryForProvider.clearFilter(); |
169 |
} |
170 |
|
171 |
} |
172 |
|
173 |
// Order
|
174 |
if (theQueryForProvider.hasOrder()) {
|
175 |
boolean canUseOrder = true; |
176 |
Iterator iter = theQueryForProvider.getOrder().iterator();
|
177 |
FeatureQueryOrderMember item; |
178 |
while (iter.hasNext()) {
|
179 |
item = (FeatureQueryOrderMember) iter.next(); |
180 |
if (item.hasEvaluator()) {
|
181 |
if (!areEvaluatorFieldsInAttributes(item.getEvaluator(),
|
182 |
ftype)) { |
183 |
canUseOrder = false;
|
184 |
break;
|
185 |
} |
186 |
} else {
|
187 |
if (ftype.get(item.getAttributeName()) == null) { |
188 |
canUseOrder = false;
|
189 |
break;
|
190 |
} |
191 |
} |
192 |
} |
193 |
|
194 |
if (!canUseOrder) {
|
195 |
theQueryForProvider.getOrder().clear(); |
196 |
} |
197 |
} |
198 |
|
199 |
} |
200 |
|
201 |
private boolean areEvaluatorFieldsInAttributes(Evaluator evaluator, |
202 |
FeatureType fType) { |
203 |
if (evaluator.getFieldsInfo() == null) { |
204 |
return false; |
205 |
} |
206 |
String[] fieldNames = evaluator.getFieldsInfo().getFieldNames(); |
207 |
if (fieldNames.length == 0) { |
208 |
return false; |
209 |
} else {
|
210 |
for (int i = 0; i < fieldNames.length; i++) { |
211 |
if (fType.get(fieldNames[i]) == null) { |
212 |
return false; |
213 |
} |
214 |
|
215 |
} |
216 |
} |
217 |
return true; |
218 |
} |
219 |
|
220 |
public FeatureType getDefaultFeatureType() {
|
221 |
return this.defatulFeatureType; |
222 |
} |
223 |
|
224 |
public List getFeatureTypes() { |
225 |
return Collections.unmodifiableList(this.featureTypes); |
226 |
} |
227 |
|
228 |
public long getSize() throws DataException { |
229 |
this.checkSourceStoreModified();
|
230 |
if (size < 0) { |
231 |
size = calculateSize(); |
232 |
} |
233 |
return size;
|
234 |
} |
235 |
|
236 |
private long calculateSize() throws DataException { |
237 |
int mode = this.getIteratorMode(); |
238 |
if ((mode & EDITED) != EDITED) {
|
239 |
if (this.provider.isEmpty()) { |
240 |
return 0; |
241 |
} |
242 |
} |
243 |
if ((mode & FILTERED) == FILTERED) {
|
244 |
long mySize = 0; |
245 |
DisposableIterator iter = null;
|
246 |
try {
|
247 |
iter = this.fastIterator();
|
248 |
while (true) { |
249 |
iter.next(); |
250 |
mySize++; |
251 |
} |
252 |
} catch (NoSuchElementException e) { |
253 |
return mySize;
|
254 |
} finally {
|
255 |
iter.dispose(); |
256 |
} |
257 |
} else
|
258 |
if ((mode & EDITED) == EDITED) {
|
259 |
return provider.getSize()
|
260 |
+ store.getFeatureManager().getDeltaSize(); |
261 |
} |
262 |
return provider.getSize();
|
263 |
} |
264 |
|
265 |
public void dispose() { |
266 |
this.store.deleteObserver(this); |
267 |
this.provider.dispose();
|
268 |
this.provider = null; |
269 |
|
270 |
this.featureToIgnoreNotification = null; |
271 |
if (orderedData != null) { |
272 |
orderedData.clear(); |
273 |
} |
274 |
this.orderedData = null; |
275 |
this.store = null; |
276 |
this.transform = null; |
277 |
this.query = null; |
278 |
this.queryForProvider = null; |
279 |
this.featureTypes = null; |
280 |
this.defatulFeatureType = null; |
281 |
this.defatulFeatureTypeForProvider = null; |
282 |
} |
283 |
|
284 |
public void update(Observable obsevable, Object notification) { |
285 |
if (sourceStoreModified) {
|
286 |
return;
|
287 |
} |
288 |
|
289 |
String type = ((FeatureStoreNotification) notification).getType();
|
290 |
|
291 |
if (type.equalsIgnoreCase(FeatureStoreNotification.AFTER_INSERT)
|
292 |
|| type.equalsIgnoreCase(FeatureStoreNotification.AFTER_DELETE) |
293 |
|| type.equalsIgnoreCase(FeatureStoreNotification.AFTER_UPDATE)) { |
294 |
if (this.featureToIgnoreNotification == ((FeatureStoreNotification) notification) |
295 |
.getFeature()) { |
296 |
return;
|
297 |
} |
298 |
sourceStoreModified = true;
|
299 |
return;
|
300 |
} |
301 |
if (type.equalsIgnoreCase(FeatureStoreNotification.AFTER_UPDATE_TYPE)
|
302 |
|| type.equalsIgnoreCase(FeatureStoreNotification.AFTER_REDO) |
303 |
|| type.equalsIgnoreCase(FeatureStoreNotification.AFTER_UNDO) |
304 |
|| type.equalsIgnoreCase(FeatureStoreNotification.AFTER_REFRESH) |
305 |
|| type |
306 |
.equalsIgnoreCase(FeatureStoreNotification.COMPLEX_NOTIFICATION) |
307 |
|| type.equalsIgnoreCase(FeatureStoreNotification.AFTER_CLOSE) |
308 |
|| type.equalsIgnoreCase(FeatureStoreNotification.AFTER_DISPOSE) |
309 |
|| type.equalsIgnoreCase(FeatureStoreNotification.RESOURCE_CHANGED) |
310 |
|| type.equalsIgnoreCase(FeatureStoreNotification.TRANSFORM_CHANGE)) { |
311 |
sourceStoreModified = true;
|
312 |
return;
|
313 |
} |
314 |
if (type.equalsIgnoreCase(FeatureStoreNotification.AFTER_CANCELEDITING)
|
315 |
&& ownFeaturesModified) { |
316 |
sourceStoreModified = true;
|
317 |
return;
|
318 |
} |
319 |
} |
320 |
|
321 |
protected void checkSourceStoreModified() { |
322 |
if (sourceStoreModified) {
|
323 |
throw new ConcurrentDataModificationException(store == null ? "" |
324 |
: store.getName()); |
325 |
} |
326 |
} |
327 |
|
328 |
@Override
|
329 |
public DisposableIterator fastIterator(long index) throws DataException { |
330 |
return fastIterator(index, 0); |
331 |
} |
332 |
|
333 |
@Override
|
334 |
public DisposableIterator fastIterator(long index, long elements) throws DataException { |
335 |
if (index < 0) { |
336 |
throw new IndexOutOfBoundsException("The index (" + index |
337 |
+ ") is less than 0");
|
338 |
} |
339 |
DisposableIterator it; |
340 |
int mode = this.getIteratorMode(); |
341 |
|
342 |
switch (mode) {
|
343 |
case DEFAULT:
|
344 |
it = new FastDefaultIterator(this, index, elements); |
345 |
break;
|
346 |
|
347 |
case FILTERED:
|
348 |
it = new FastFilteredIterator(this, index); |
349 |
break;
|
350 |
|
351 |
case ORDERED:
|
352 |
if (this.orderedData != null) { |
353 |
it = new FastOrderedIterator(this, index); |
354 |
} else {
|
355 |
it = new FastOrderedIterator(this, new FastDefaultIterator(this, 0, elements), index); |
356 |
} |
357 |
break;
|
358 |
|
359 |
case ORDERED_FILTERED:
|
360 |
if (this.orderedData != null) { |
361 |
it = new FastOrderedIterator(this, index); |
362 |
} else {
|
363 |
it = new FastOrderedIterator(this, new FastFilteredIterator( |
364 |
this, 0), index); |
365 |
} |
366 |
break;
|
367 |
|
368 |
case EDITED:
|
369 |
it = new FastEditedIterator(this, index); |
370 |
break;
|
371 |
|
372 |
case EDITED_FILTERED:
|
373 |
it = new FastEditedFilteredIterator(this, index); |
374 |
break;
|
375 |
|
376 |
case ORDERD_EDITED:
|
377 |
if (this.orderedData != null) { |
378 |
it = new FastOrderedIterator(this, index); |
379 |
} else {
|
380 |
it = new FastOrderedIterator(this, new FastEditedIterator( |
381 |
this, 0), index); |
382 |
} |
383 |
break;
|
384 |
|
385 |
case ORDERED_EDITED_FILTER:
|
386 |
if (this.orderedData != null) { |
387 |
it = new FastOrderedIterator(this, index); |
388 |
} else {
|
389 |
it = new FastOrderedIterator(this, |
390 |
new FastEditedFilteredIterator(this, 0), index); |
391 |
} |
392 |
break;
|
393 |
|
394 |
default:
|
395 |
throw new IllegalArgumentException(); |
396 |
} |
397 |
if( this.query!=null && this.query.getLimit()>0 ) { |
398 |
it = new LimitIterator(it,this.query.getLimit()); |
399 |
} |
400 |
return it;
|
401 |
} |
402 |
|
403 |
private class LimitIterator implements DisposableIterator { |
404 |
|
405 |
private final DisposableIterator it; |
406 |
private final long limit; |
407 |
private int count; |
408 |
|
409 |
private LimitIterator(DisposableIterator it, long limit) { |
410 |
this.it = it;
|
411 |
this.limit = limit;
|
412 |
this.count = 0; |
413 |
} |
414 |
|
415 |
@Override
|
416 |
public void dispose() { |
417 |
this.it.dispose();
|
418 |
} |
419 |
|
420 |
@Override
|
421 |
public boolean hasNext() { |
422 |
if( this.count>=this.limit ) { |
423 |
return false; |
424 |
} |
425 |
return this.it.hasNext(); |
426 |
} |
427 |
|
428 |
@Override
|
429 |
public Object next() { |
430 |
if( this.count>=this.limit ) { |
431 |
return null; |
432 |
} |
433 |
this.count++;
|
434 |
return this.it.next(); |
435 |
} |
436 |
|
437 |
@Override
|
438 |
public void remove() { |
439 |
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
440 |
} |
441 |
|
442 |
} |
443 |
|
444 |
@Override
|
445 |
public DisposableIterator iterator(long index) throws DataException { |
446 |
return iterator(index,0); |
447 |
} |
448 |
|
449 |
@Override
|
450 |
public DisposableIterator iterator(long index, long elements) throws DataException { |
451 |
if (index < 0) { |
452 |
throw new IndexOutOfBoundsException("The index (" + index |
453 |
+ ") is less than 0");
|
454 |
} |
455 |
DisposableIterator it; |
456 |
int mode = this.getIteratorMode(); |
457 |
|
458 |
switch (mode) {
|
459 |
case DEFAULT:
|
460 |
it = new DefaultIterator(this, index, elements); |
461 |
break;
|
462 |
|
463 |
case FILTERED:
|
464 |
it = new FilteredIterator(this, index); |
465 |
break;
|
466 |
|
467 |
case ORDERED:
|
468 |
if (orderedData != null) { |
469 |
it = new OrderedIterator(this, index); |
470 |
|
471 |
} else {
|
472 |
it = new OrderedIterator(this, new DefaultIterator(this, 0, elements),index); |
473 |
} |
474 |
break;
|
475 |
|
476 |
case ORDERED_FILTERED:
|
477 |
it = new OrderedIterator(this, new FilteredIterator(this, 0), |
478 |
index); |
479 |
break;
|
480 |
|
481 |
case EDITED:
|
482 |
it = new EditedIterator(this, index); |
483 |
break;
|
484 |
|
485 |
case EDITED_FILTERED:
|
486 |
it = new EditedFilteredIterator(this, index); |
487 |
break;
|
488 |
|
489 |
case ORDERD_EDITED:
|
490 |
it = new OrderedIterator(this, new EditedIterator(this, 0), index); |
491 |
break;
|
492 |
|
493 |
case ORDERED_EDITED_FILTER:
|
494 |
it = new OrderedIterator(this, |
495 |
new EditedFilteredIterator(this, 0), index); |
496 |
break;
|
497 |
|
498 |
default:
|
499 |
throw new IllegalArgumentException(); |
500 |
} |
501 |
|
502 |
if( this.query!=null && this.query.getLimit()>0 ) { |
503 |
it = new LimitIterator(it,this.query.getLimit()); |
504 |
} |
505 |
return it;
|
506 |
} |
507 |
|
508 |
private boolean providerCanOrder() { |
509 |
return this.provider.canOrder(); |
510 |
} |
511 |
|
512 |
private boolean providerCanFilter() { |
513 |
return this.provider.canFilter(); |
514 |
} |
515 |
|
516 |
private int getIteratorMode() { |
517 |
|
518 |
if (this.iteratorMode != NO_CHECKED) { |
519 |
return this.iteratorMode; |
520 |
} |
521 |
|
522 |
// TODO Tener en cuenta las transformaciones ???
|
523 |
|
524 |
if (store.isEditing() && store.getFeatureManager().hasChanges()) {
|
525 |
if (this.query.hasOrder()) { // En edicion siempre ordeno yo. |
526 |
if (this.query.hasFilter()) { |
527 |
return ORDERED_EDITED_FILTER;
|
528 |
} else {
|
529 |
return ORDERD_EDITED;
|
530 |
} |
531 |
} else {
|
532 |
if (this.query.hasFilter()) { |
533 |
return EDITED_FILTERED;
|
534 |
} else {
|
535 |
return EDITED;
|
536 |
} |
537 |
} |
538 |
} else {
|
539 |
boolean useMyFilter = this.query.hasFilter(); |
540 |
boolean useMyOrder = this.query.hasOrder(); |
541 |
if (this.providerCanOrder() && this.transform.isEmpty()) { |
542 |
useMyOrder = false;
|
543 |
} |
544 |
if (this.providerCanFilter() && this.transform.isEmpty()) { |
545 |
useMyFilter = false;
|
546 |
} |
547 |
|
548 |
if (useMyOrder) {
|
549 |
if (useMyFilter) {
|
550 |
return ORDERED_FILTERED;// ORDERED_FILTERED; |
551 |
} else {
|
552 |
return ORDERED;// ORDERED; |
553 |
} |
554 |
} else {
|
555 |
if (useMyFilter) {
|
556 |
return FILTERED;// FILTERED; |
557 |
} else {
|
558 |
return DEFAULT;// DEFAULT; |
559 |
} |
560 |
} |
561 |
} |
562 |
|
563 |
} |
564 |
|
565 |
public void delete(Feature feature) throws DataException { |
566 |
this.featureToIgnoreNotification = feature;
|
567 |
this.store.delete(feature);
|
568 |
if (this.size > 0) { |
569 |
this.size--;
|
570 |
} |
571 |
this.featureToIgnoreNotification = null; |
572 |
this.ownFeaturesModified = true; |
573 |
} |
574 |
|
575 |
public void insert(EditableFeature feature) throws DataException { |
576 |
this.featureToIgnoreNotification = feature;
|
577 |
this.store.insert(feature);
|
578 |
if (this.size >= 0) { |
579 |
this.size++;
|
580 |
} |
581 |
this.featureToIgnoreNotification = null; |
582 |
this.ownFeaturesModified = true; |
583 |
} |
584 |
|
585 |
public void update(EditableFeature feature) throws DataException { |
586 |
this.featureToIgnoreNotification = feature;
|
587 |
this.store.update(feature);
|
588 |
this.featureToIgnoreNotification = null; |
589 |
this.ownFeaturesModified = true; |
590 |
} |
591 |
|
592 |
public FeatureStore getFeatureStore() {
|
593 |
return store;
|
594 |
} |
595 |
|
596 |
} |