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 / featurereference / FeatureReferenceIteratorToFeatureIterator.java @ 46696
History | View | Annotate | Download (5.15 KB)
1 | 46309 | jjdelcerro | /*
|
---|---|---|---|
2 | * To change this license header, choose License Headers in Project Properties.
|
||
3 | * To change this template file, choose Tools | Templates
|
||
4 | * and open the template in the editor.
|
||
5 | */
|
||
6 | package org.gvsig.fmap.dal.feature.impl.featurereference; |
||
7 | |||
8 | import java.util.Collections; |
||
9 | import java.util.Iterator; |
||
10 | import java.util.logging.Level; |
||
11 | import java.util.logging.Logger; |
||
12 | import org.gvsig.expressionevaluator.ExpressionBuilder; |
||
13 | import org.gvsig.expressionevaluator.ExpressionUtils; |
||
14 | import org.gvsig.fmap.dal.DataServerExplorer_v2; |
||
15 | import org.gvsig.fmap.dal.SQLBuilder; |
||
16 | import org.gvsig.fmap.dal.exception.DataException; |
||
17 | import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
||
18 | import org.gvsig.fmap.dal.feature.Feature; |
||
19 | import org.gvsig.fmap.dal.feature.FeatureReference; |
||
20 | import org.gvsig.fmap.dal.feature.FeatureSet; |
||
21 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
22 | import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices; |
||
23 | import org.gvsig.tools.util.ChainedIterator; |
||
24 | |||
25 | /**
|
||
26 | *
|
||
27 | * @author jjdelcerro
|
||
28 | */
|
||
29 | public class FeatureReferenceIteratorToFeatureIterator implements Iterator<Feature> { |
||
30 | |||
31 | private Iterator<FeatureReference> references; |
||
32 | private Iterator<Feature> it; |
||
33 | private final FeatureStore store; |
||
34 | private int maxRecomendedSQLLength; |
||
35 | private int count; |
||
36 | private int maxcount; |
||
37 | |||
38 | public FeatureReferenceIteratorToFeatureIterator(FeatureStore store, Iterator<FeatureReference> references) { |
||
39 | this.count = 0; |
||
40 | this.store = store;
|
||
41 | this.references = references;
|
||
42 | this.it = null; |
||
43 | try {
|
||
44 | SQLBuilder sqlbuilder = ((DataServerExplorer_v2)this.store.getExplorer()).createSQLBuilder();
|
||
45 | this.maxRecomendedSQLLength = sqlbuilder.getMaxRecomendedSQLLength() - 200; |
||
46 | } catch (Exception ex) { |
||
47 | this.maxRecomendedSQLLength = SQLBuilder.DEFAULT_RECOMENDED_SQL_LENGTH - 200; |
||
48 | } |
||
49 | this.maxcount = this.maxRecomendedSQLLength / 50; // Cada getReferenceFilter son aprox 50 |
||
50 | } |
||
51 | |||
52 | @Override
|
||
53 | public boolean hasNext() { |
||
54 | if (this.it != null) { |
||
55 | if (this.it.hasNext()) { |
||
56 | return true; |
||
57 | } |
||
58 | } |
||
59 | this.it = this.getNextPage(); |
||
60 | return this.it.hasNext(); |
||
61 | } |
||
62 | |||
63 | @Override
|
||
64 | public Feature next() {
|
||
65 | if (this.it != null) { |
||
66 | return this.it.next(); |
||
67 | } |
||
68 | this.it = this.getNextPage(); |
||
69 | return this.it.next(); |
||
70 | } |
||
71 | |||
72 | private Iterator<Feature> getNextPage() { |
||
73 | try {
|
||
74 | ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
||
75 | int count = 0; |
||
76 | while (this.references.hasNext()) { |
||
77 | FeatureReferenceProviderServices reference = (FeatureReferenceProviderServices) references.next(); |
||
78 | this.count++;
|
||
79 | if( reference==null ) { |
||
80 | continue;
|
||
81 | } |
||
82 | if( reference instanceof FeatureReferencePK ) { |
||
83 | builder.or(this.getReferenceFilter(reference));
|
||
84 | count++; |
||
85 | if (!references.hasNext() || count > this.maxcount) { // filter.length() > this.maxRecomendedSQLLength) { |
||
86 | String filter = builder.build(); // Ojo, mover arriba del if es lentisimo ?? |
||
87 | if (filter != null) { |
||
88 | FeatureSet set = this.store.getFeatureSet(filter);
|
||
89 | return set.fastIterator(); // Disposes? |
||
90 | } |
||
91 | } |
||
92 | } else {
|
||
93 | String filter = builder.build();
|
||
94 | Feature feature = reference.getFeature(); |
||
95 | if (filter != null) { |
||
96 | FeatureSet set = this.store.getFeatureSet(filter);
|
||
97 | if( feature == null ) { |
||
98 | return set.fastIterator();
|
||
99 | } |
||
100 | return new ChainedIterator<>( |
||
101 | set.fastIterator(), |
||
102 | Collections.singleton(feature).iterator()
|
||
103 | ); |
||
104 | } else {
|
||
105 | if( feature!=null ) { |
||
106 | return Collections.singleton(feature).iterator(); |
||
107 | } |
||
108 | } |
||
109 | } |
||
110 | } |
||
111 | return Collections.EMPTY_LIST.iterator(); |
||
112 | } catch (DataException ex) {
|
||
113 | throw new RuntimeException("Can't get next page of iterator", ex); |
||
114 | } |
||
115 | } |
||
116 | |||
117 | private ExpressionBuilder.Value getReferenceFilter(FeatureReferenceProviderServices reference) {
|
||
118 | ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
||
119 | String[] primaryKeys = reference.getKeyNames(); |
||
120 | for (String name : primaryKeys) { |
||
121 | Object value = reference.getKeyValue(name);
|
||
122 | if (value == null) { |
||
123 | builder.and(builder.is_null(builder.column(name))); |
||
124 | } else {
|
||
125 | builder.and( |
||
126 | builder.eq( |
||
127 | builder.column(name), |
||
128 | builder.constant(value) |
||
129 | ) |
||
130 | ); |
||
131 | } |
||
132 | } |
||
133 | return builder.value();
|
||
134 | } |
||
135 | |||
136 | } |