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 / EditedIterator.java @ 46905
History | View | Annotate | Download (7.18 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40559 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | *
|
||
6 | 40435 | jjdelcerro | * This program is free software; you can redistribute it and/or
|
7 | * modify it under the terms of the GNU General Public License
|
||
8 | 40559 | jjdelcerro | * as published by the Free Software Foundation; either version 3
|
9 | 40435 | jjdelcerro | * of the License, or (at your option) any later version.
|
10 | 40559 | jjdelcerro | *
|
11 | 40435 | jjdelcerro | * 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 | 40559 | jjdelcerro | *
|
16 | 40435 | jjdelcerro | * 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 | 40559 | jjdelcerro | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 | 40435 | jjdelcerro | * MA 02110-1301, USA.
|
20 | 40559 | jjdelcerro | *
|
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 | package org.gvsig.fmap.dal.feature.impl.featureset; |
||
25 | |||
26 | 45989 | fdiaz | import java.util.HashSet; |
27 | 40435 | jjdelcerro | import java.util.Iterator; |
28 | 45989 | fdiaz | import java.util.Set; |
29 | 45765 | fdiaz | import java.util.logging.Level; |
30 | import java.util.logging.Logger; |
||
31 | 40435 | jjdelcerro | import org.gvsig.fmap.dal.exception.DataException; |
32 | 45739 | jjdelcerro | import org.gvsig.fmap.dal.feature.EditableFeature; |
33 | 46905 | fdiaz | import org.gvsig.fmap.dal.feature.Feature; |
34 | 45688 | fdiaz | import org.gvsig.fmap.dal.feature.FeatureReference; |
35 | 40435 | jjdelcerro | import org.gvsig.fmap.dal.feature.exception.ConcurrentDataModificationException; |
36 | import org.gvsig.fmap.dal.feature.impl.DefaultFeature; |
||
37 | import org.gvsig.fmap.dal.feature.impl.DefaultFeatureStore; |
||
38 | 43840 | jjdelcerro | import org.gvsig.fmap.dal.feature.impl.editing.memory.FeatureManager; |
39 | 45688 | fdiaz | import org.gvsig.fmap.dal.feature.impl.featurereference.FeatureReferenceFactory; |
40 | 40435 | jjdelcerro | import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
41 | 45989 | fdiaz | import org.gvsig.fmap.dal.feature.spi.FeatureSetProvider; |
42 | 46133 | fdiaz | import org.gvsig.tools.dispose.DisposableIterator; |
43 | import org.gvsig.tools.dispose.DisposeUtils; |
||
44 | 40435 | jjdelcerro | import org.gvsig.tools.exception.BaseException; |
45 | 45522 | fdiaz | import org.gvsig.tools.util.ChainedIterator; |
46 | 40435 | jjdelcerro | |
47 | /**
|
||
48 | * Iterator for stores in edition mode.
|
||
49 | *
|
||
50 | * @author gvSIG Team
|
||
51 | * @version $Id$
|
||
52 | * @deprecated use the {@link FastEditedIterator} instead
|
||
53 | */
|
||
54 | public class EditedIterator extends FilteredIterator { |
||
55 | |||
56 | private DefaultFeatureStore store;
|
||
57 | protected FeatureManager featureManager;
|
||
58 | 45989 | fdiaz | private Set<FeatureReference> visitedReferences; |
59 | |||
60 | 40435 | jjdelcerro | public EditedIterator(DefaultFeatureSet featureSet) {
|
61 | super(featureSet);
|
||
62 | this.store = featureSet.store;
|
||
63 | this.featureManager = this.store.getFeatureManager(); |
||
64 | 45989 | fdiaz | this.visitedReferences = new HashSet<>(); |
65 | 40435 | jjdelcerro | } |
66 | |||
67 | 45989 | fdiaz | public EditedIterator(DefaultFeatureSet featureSet, long index, long elements) |
68 | 40435 | jjdelcerro | throws DataException {
|
69 | this(featureSet);
|
||
70 | 40629 | jldominguez | |
71 | 45739 | jjdelcerro | Iterator<EditableFeature> insertedFeatures = this.featureManager.getInsertedFeatures();//Don't use getInserteds because referenceFeature will be missed |
72 | Iterator<EditableFeature> updatedFeatures = this.featureManager.getUpdatedFeatures(); |
||
73 | 45989 | fdiaz | final Iterator<FeatureProvider> providerIterator; |
74 | FeatureSetProvider provider = featureSet.provider; |
||
75 | boolean canDelegateInProvider = provider.canFilter() && provider.canIterateFromIndex() && !featureManager.hasDeleteds();
|
||
76 | if (canDelegateInProvider) {
|
||
77 | providerIterator = provider.iterator(index, elements); |
||
78 | } else {
|
||
79 | providerIterator = provider.iterator(); |
||
80 | } |
||
81 | |||
82 | |||
83 | 46133 | fdiaz | Iterator<FeatureProvider> wrappedProviderIterator = new DisposableIterator<FeatureProvider>() { |
84 | 45522 | fdiaz | @Override
|
85 | public boolean hasNext() { |
||
86 | return providerIterator.hasNext();
|
||
87 | } |
||
88 | 40629 | jldominguez | |
89 | 45522 | fdiaz | @Override
|
90 | public FeatureProvider next() {
|
||
91 | 45765 | fdiaz | FeatureProvider data = providerIterator.next(); |
92 | FeatureReference ref = FeatureReferenceFactory.createFromFeatureProvider(store, data); |
||
93 | DefaultFeature modified; |
||
94 | try {
|
||
95 | modified = featureManager.get(ref, store); |
||
96 | } catch (DataException ex) {
|
||
97 | return data;
|
||
98 | } |
||
99 | if (modified != null) { |
||
100 | 45989 | fdiaz | visitedReferences.add(ref); |
101 | 45765 | fdiaz | return modified.getData();
|
102 | } |
||
103 | return data;
|
||
104 | 40435 | jjdelcerro | } |
105 | |||
106 | 45522 | fdiaz | @Override
|
107 | public void remove() { |
||
108 | EditedIterator.super.remove(); |
||
109 | } |
||
110 | 46133 | fdiaz | |
111 | @Override
|
||
112 | public void dispose() { |
||
113 | DisposeUtils.disposeQuietly(providerIterator); |
||
114 | } |
||
115 | 45522 | fdiaz | }; |
116 | 45989 | fdiaz | this.iterator = new ChainedIterator(wrappedProviderIterator, insertedFeatures, updatedFeatures); |
117 | if (!canDelegateInProvider) {
|
||
118 | if (index > 0) { |
||
119 | skypto(index); |
||
120 | } |
||
121 | 45522 | fdiaz | } |
122 | 40435 | jjdelcerro | } |
123 | |||
124 | 45522 | fdiaz | @Override
|
125 | 40435 | jjdelcerro | public void remove() { |
126 | 45522 | fdiaz | this.iterator.remove();
|
127 | 40435 | jjdelcerro | } |
128 | |||
129 | 45522 | fdiaz | @Override
|
130 | 40435 | jjdelcerro | public boolean hasNext() { |
131 | if (store.isEditing()) {
|
||
132 | return super.hasNext(); |
||
133 | } else {
|
||
134 | throw new ConcurrentDataModificationException(store.getFullName()); |
||
135 | } |
||
136 | } |
||
137 | |||
138 | 45522 | fdiaz | @Override
|
139 | 40435 | jjdelcerro | public Object next() { |
140 | if (store.isEditing()) {
|
||
141 | 46905 | fdiaz | Feature n = (Feature) super.next();
|
142 | this.visitedReferences.add(n.getReference());
|
||
143 | return n;
|
||
144 | 40435 | jjdelcerro | } else {
|
145 | throw new ConcurrentDataModificationException(store.getFullName()); |
||
146 | } |
||
147 | } |
||
148 | |||
149 | 45522 | fdiaz | @Override
|
150 | protected ChainedIterator getIterator() {
|
||
151 | return (ChainedIterator) this.iterator; |
||
152 | 40435 | jjdelcerro | } |
153 | 45522 | fdiaz | |
154 | @Override
|
||
155 | 40435 | jjdelcerro | protected DefaultFeature createFeature(FeatureProvider data) throws DataException { |
156 | |||
157 | 45647 | fdiaz | DefaultFeature f = this.featureManager.get(data);
|
158 | // DefaultFeature f = null;
|
||
159 | // try {
|
||
160 | // FeatureReference ref = new DefaultFeatureReference(store, data);
|
||
161 | // f= (DefaultFeature) this.featureManager.get(ref, store, data.getType());
|
||
162 | // } catch (DataException e) {
|
||
163 | // RuntimeException ex = new RuntimeException();
|
||
164 | // e.initCause(e);
|
||
165 | // throw ex;
|
||
166 | // }
|
||
167 | 40435 | jjdelcerro | if (f == null) { |
168 | // La feature no se ha editado.
|
||
169 | f = new DefaultFeature(store, data);
|
||
170 | } |
||
171 | if (this.fset.transform.isEmpty()) { |
||
172 | return f;
|
||
173 | } else {
|
||
174 | return (DefaultFeature) this.fset.transform.applyTransform( |
||
175 | f, fset.getDefaultFeatureType()); |
||
176 | } |
||
177 | } |
||
178 | 45522 | fdiaz | |
179 | @Override
|
||
180 | protected boolean skipFeature(FeatureProvider data) { |
||
181 | try {
|
||
182 | 45989 | fdiaz | switch (getIterator().getCurrent()) {
|
183 | case 0: |
||
184 | if (this.featureManager.isDeleted(data)) { |
||
185 | return true; |
||
186 | } |
||
187 | break;
|
||
188 | case 2: |
||
189 | FeatureReference ref = FeatureReferenceFactory.createFromFeatureProvider(store, data); |
||
190 | if (visitedReferences.contains(ref)){
|
||
191 | return true; |
||
192 | } |
||
193 | break;
|
||
194 | 45522 | fdiaz | } |
195 | 45531 | fdiaz | } catch (Exception ex) { |
196 | 45522 | fdiaz | LOGGER.warn("Can't check if must skip feature.", ex);
|
197 | 40435 | jjdelcerro | } |
198 | 45522 | fdiaz | return false; |
199 | 40435 | jjdelcerro | } |
200 | |||
201 | 45522 | fdiaz | @Override
|
202 | 40435 | jjdelcerro | protected void doDispose() throws BaseException { |
203 | super.doDispose();
|
||
204 | this.featureManager = null; |
||
205 | this.store = null; |
||
206 | } |
||
207 | |||
208 | } |