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 / FeatureManager.java @ 40435
History | View | Annotate | Download (11.3 KB)
1 | 40435 | jjdelcerro | |
---|---|---|---|
2 | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
||
3 | *
|
||
4 | * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
|
||
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 2
|
||
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
19 | *
|
||
20 | * For more information, contact:
|
||
21 | *
|
||
22 | * Generalitat Valenciana
|
||
23 | * Conselleria d'Infraestructures i Transport
|
||
24 | * Av. Blasco Ib??ez, 50
|
||
25 | * 46010 VALENCIA
|
||
26 | * SPAIN
|
||
27 | *
|
||
28 | * +34 963862235
|
||
29 | * gvsig@gva.es
|
||
30 | * www.gvsig.gva.es
|
||
31 | *
|
||
32 | * or
|
||
33 | *
|
||
34 | * IVER T.I. S.A
|
||
35 | * Salamanca 50
|
||
36 | * 46005 Valencia
|
||
37 | * Spain
|
||
38 | *
|
||
39 | * +34 963163400
|
||
40 | * dac@iver.es
|
||
41 | */
|
||
42 | |||
43 | package org.gvsig.fmap.dal.feature.impl; |
||
44 | |||
45 | import java.util.Collection; |
||
46 | import java.util.HashMap; |
||
47 | import java.util.Iterator; |
||
48 | import java.util.LinkedHashMap; |
||
49 | import java.util.LinkedHashSet; |
||
50 | import java.util.Map; |
||
51 | import java.util.NoSuchElementException; |
||
52 | |||
53 | import org.gvsig.fmap.dal.exception.DataException; |
||
54 | import org.gvsig.fmap.dal.feature.EditableFeature; |
||
55 | import org.gvsig.fmap.dal.feature.Feature; |
||
56 | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
||
57 | import org.gvsig.fmap.dal.feature.FeatureReference; |
||
58 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
59 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
60 | import org.gvsig.fmap.dal.feature.impl.expansionadapter.ExpansionAdapter; |
||
61 | |||
62 | |||
63 | /**
|
||
64 | * DOCUMENT ME!
|
||
65 | *
|
||
66 | * @author Vicente Caballero Navarro
|
||
67 | */
|
||
68 | public class FeatureManager { |
||
69 | private ExpansionAdapter expansionAdapter;
|
||
70 | private Collection deleted = new LinkedHashSet();//<FeatureID> |
||
71 | private int deltaSize=0; |
||
72 | private Map added = new LinkedHashMap(); |
||
73 | private Map addedAndDeleted = new LinkedHashMap(); |
||
74 | private HashMap modifiedFromOriginal=new HashMap(); |
||
75 | |||
76 | public FeatureManager(ExpansionAdapter expansionAdapter){
|
||
77 | this.expansionAdapter=expansionAdapter;
|
||
78 | } |
||
79 | |||
80 | /**
|
||
81 | * Deletes feature from this manager.
|
||
82 | * @param id
|
||
83 | * @return The deleted feature or null if the
|
||
84 | * feature had not been edited or previously added in the editing session
|
||
85 | */
|
||
86 | public Feature delete(FeatureReference id) {
|
||
87 | deleted.add(id); |
||
88 | Integer num = (Integer) added.remove(id); |
||
89 | Feature feature=null;
|
||
90 | if (num == null || num.intValue() == -1) { |
||
91 | num = (Integer) modifiedFromOriginal.remove(id);
|
||
92 | if (num != null) { |
||
93 | feature = (Feature) expansionAdapter.getObject(num.intValue()); |
||
94 | } |
||
95 | // if num is null here, method returns null
|
||
96 | }else{
|
||
97 | feature = (Feature) expansionAdapter.getObject(num.intValue()); |
||
98 | addedAndDeleted.put(id, num); |
||
99 | } |
||
100 | deltaSize--; |
||
101 | return feature;
|
||
102 | } |
||
103 | |||
104 | /**
|
||
105 | * DOCUMENT ME!
|
||
106 | *
|
||
107 | * @param feature DOCUMENT ME!
|
||
108 | */
|
||
109 | public void add(Feature feature) { |
||
110 | int pos = expansionAdapter.addObject(feature);
|
||
111 | added.put(feature.getReference(),new Integer(pos)); |
||
112 | deleted.remove(feature.getReference()); |
||
113 | deltaSize++; |
||
114 | } |
||
115 | |||
116 | /**
|
||
117 | * DOCUMENT ME!
|
||
118 | * @param id DOCUMENT ME!
|
||
119 | */
|
||
120 | public Feature deleteLastFeature() {
|
||
121 | expansionAdapter.deleteLastObject(); |
||
122 | Feature feature=(Feature)expansionAdapter.getObject(expansionAdapter.getSize()-1);
|
||
123 | added.remove(feature.getReference()); |
||
124 | modifiedFromOriginal.remove(feature.getReference()); |
||
125 | deltaSize--; |
||
126 | return feature;
|
||
127 | } |
||
128 | |||
129 | /**
|
||
130 | * Returns a Feature of the default type.
|
||
131 | *
|
||
132 | * @param id
|
||
133 | * the feature reference
|
||
134 | * @param store
|
||
135 | * the store to get the feature from
|
||
136 | * @return a Feature with the given reference
|
||
137 | * @throws DataException
|
||
138 | * if there is an error getting the Feature
|
||
139 | */
|
||
140 | public Feature get(FeatureReference id, FeatureStore store)
|
||
141 | throws DataException {
|
||
142 | return get(id, store, null); |
||
143 | } |
||
144 | |||
145 | /**
|
||
146 | * Returns a Feature of the given type.
|
||
147 | *
|
||
148 | * @param id
|
||
149 | * the feature reference
|
||
150 | * @param store
|
||
151 | * the store to get the feature from
|
||
152 | * @param featureType
|
||
153 | * the type of the feature to return
|
||
154 | * @return a Feature with the given reference
|
||
155 | * @throws DataException
|
||
156 | * if there is an error getting the Feature
|
||
157 | */
|
||
158 | public Feature get(FeatureReference id, FeatureStore store,
|
||
159 | FeatureType featureType) throws DataException {
|
||
160 | // FIXME: y si el featuretype que paso esta modificado.
|
||
161 | // Deberia buscarlo en el featuretypemanager ?
|
||
162 | //
|
||
163 | // Si no existe feature con ese id... ? retorna null ?
|
||
164 | // en EditedDefaultIterator se hace uso de ese comportamiento.
|
||
165 | //
|
||
166 | Integer intNum = ((Integer) added.get(id)); |
||
167 | if (intNum == null){ |
||
168 | intNum =((Integer) modifiedFromOriginal.get(id));
|
||
169 | if (intNum == null){ |
||
170 | //If the feature has been added and deleted
|
||
171 | intNum = (Integer)addedAndDeleted.get(id);
|
||
172 | if (intNum == null){ |
||
173 | return null; |
||
174 | } |
||
175 | } |
||
176 | } |
||
177 | int num = intNum.intValue();
|
||
178 | if (num==-1) { |
||
179 | return null; |
||
180 | } |
||
181 | Feature feature=(Feature)expansionAdapter.getObject(num); |
||
182 | if (featureType== null){ |
||
183 | featureType = store.getDefaultFeatureType(); |
||
184 | } |
||
185 | return getCorrectFeature(feature, store,featureType);
|
||
186 | } |
||
187 | |||
188 | private Feature getCorrectFeature(Feature feature, FeatureStore store,
|
||
189 | FeatureType featureType) throws DataException {
|
||
190 | Iterator iterator=featureType.iterator();
|
||
191 | EditableFeature newFeature=feature.getEditable();//store.createNewFeature(featureType, false);
|
||
192 | FeatureType orgType = feature.getType(); |
||
193 | int orgIndex;
|
||
194 | while (iterator.hasNext()) {
|
||
195 | FeatureAttributeDescriptor fad = (FeatureAttributeDescriptor) iterator.next(); |
||
196 | orgIndex = orgType.getIndex(fad.getName()); |
||
197 | if (orgIndex<0){ |
||
198 | continue;
|
||
199 | } |
||
200 | if (fad.isAutomatic() || fad.isReadOnly()
|
||
201 | || fad.getEvaluator() != null) {
|
||
202 | continue;
|
||
203 | } |
||
204 | Object value = feature.get(orgIndex);
|
||
205 | if (value == null && !fad.allowNull()) { |
||
206 | continue;
|
||
207 | } |
||
208 | newFeature.set(orgIndex, value); |
||
209 | } |
||
210 | return newFeature.getNotEditableCopy();
|
||
211 | } |
||
212 | |||
213 | /**
|
||
214 | * DOCUMENT ME!
|
||
215 | *
|
||
216 | * @param feature DOCUMENT ME!
|
||
217 | * @param oldFeature DOCUMENT ME!
|
||
218 | */
|
||
219 | public int update(Feature feature, Feature oldFeature) { |
||
220 | int oldNum=-1; |
||
221 | int num = expansionAdapter.addObject(feature);
|
||
222 | FeatureReference id=feature.getReference(); |
||
223 | if (added.containsKey(id)){
|
||
224 | oldNum=((Integer)added.get(id)).intValue();
|
||
225 | added.put(id,new Integer(num)); |
||
226 | }else{
|
||
227 | if (modifiedFromOriginal.get(id)!=null) { |
||
228 | oldNum=((Integer)modifiedFromOriginal.get(id)).intValue();
|
||
229 | } |
||
230 | modifiedFromOriginal.put(id,new Integer(num)); |
||
231 | } |
||
232 | return oldNum;
|
||
233 | } |
||
234 | |||
235 | /**
|
||
236 | * DOCUMENT ME!
|
||
237 | *
|
||
238 | * @param id DOCUMENT ME!
|
||
239 | */
|
||
240 | public void restore(FeatureReference id) { |
||
241 | deleted.remove(id); |
||
242 | deltaSize++; |
||
243 | } |
||
244 | public void restore(FeatureReference id,int num){ |
||
245 | if (added.containsKey(id)){
|
||
246 | added.put(id,new Integer(num)); |
||
247 | }else{
|
||
248 | modifiedFromOriginal.put(id,new Integer(num)); |
||
249 | } |
||
250 | } |
||
251 | |||
252 | |||
253 | public boolean isDeleted(Feature feature){ |
||
254 | return deleted.contains(feature.getReference());
|
||
255 | } |
||
256 | |||
257 | public boolean isDeleted(FeatureReference featureID) { |
||
258 | return deleted.contains(featureID);
|
||
259 | } |
||
260 | |||
261 | public void clear() { |
||
262 | added.clear(); |
||
263 | modifiedFromOriginal.clear(); |
||
264 | expansionAdapter.close(); |
||
265 | deleted.clear();//<FeatureID>
|
||
266 | addedAndDeleted.clear(); |
||
267 | deltaSize=0;
|
||
268 | } |
||
269 | |||
270 | |||
271 | public boolean hasChanges() { |
||
272 | return added.size()>0 || modifiedFromOriginal.size() > 0 || deleted.size() > 0; |
||
273 | } |
||
274 | |||
275 | public Iterator getDeleted() { |
||
276 | return new DeletedIterator(); |
||
277 | |||
278 | } |
||
279 | |||
280 | class DeletedIterator implements Iterator { |
||
281 | private Boolean hasnext = null; |
||
282 | private Iterator iter; |
||
283 | private DefaultFeatureReference obj;
|
||
284 | |||
285 | public DeletedIterator(){
|
||
286 | iter = deleted.iterator(); |
||
287 | } |
||
288 | |||
289 | public boolean hasNext() { |
||
290 | if (hasnext != null) { |
||
291 | return hasnext.booleanValue();
|
||
292 | } |
||
293 | hasnext = Boolean.FALSE;
|
||
294 | while (iter.hasNext()) {
|
||
295 | obj = (DefaultFeatureReference) iter.next(); |
||
296 | if (obj.isNewFeature()) {
|
||
297 | continue;
|
||
298 | } |
||
299 | hasnext = Boolean.TRUE;
|
||
300 | break;
|
||
301 | } |
||
302 | return hasnext.booleanValue();
|
||
303 | } |
||
304 | |||
305 | public Object next() { |
||
306 | if (!hasNext()) {
|
||
307 | throw new NoSuchElementException(); |
||
308 | } |
||
309 | |||
310 | hasnext = null;
|
||
311 | return obj;
|
||
312 | } |
||
313 | |||
314 | public void remove() { |
||
315 | throw new UnsupportedOperationException(); |
||
316 | |||
317 | } |
||
318 | |||
319 | } |
||
320 | |||
321 | public Iterator getInserted() { |
||
322 | return new InsertedIterator(); |
||
323 | } |
||
324 | |||
325 | class InsertedIterator implements Iterator { |
||
326 | |||
327 | private Iterator addedIter; |
||
328 | private DefaultFeature obj;
|
||
329 | private Boolean hasnext = null; |
||
330 | |||
331 | public InsertedIterator(){
|
||
332 | addedIter = added.values().iterator(); |
||
333 | } |
||
334 | |||
335 | public boolean hasNext() { |
||
336 | if (hasnext != null) { |
||
337 | return hasnext.booleanValue();
|
||
338 | } |
||
339 | hasnext = Boolean.FALSE;
|
||
340 | int pos;
|
||
341 | while (addedIter.hasNext()) {
|
||
342 | pos = ((Integer) addedIter.next()).intValue();
|
||
343 | obj = (DefaultFeature) expansionAdapter.getObject(pos); |
||
344 | if (!deleted.contains(obj.getReference())) {
|
||
345 | hasnext = Boolean.TRUE;
|
||
346 | break;
|
||
347 | } |
||
348 | } |
||
349 | return hasnext.booleanValue();
|
||
350 | } |
||
351 | |||
352 | public Object next() { |
||
353 | if (!hasNext()) {
|
||
354 | throw new NoSuchElementException(); |
||
355 | } |
||
356 | hasnext = null;
|
||
357 | return obj.getData();
|
||
358 | } |
||
359 | |||
360 | public void remove() { |
||
361 | addedIter.remove(); |
||
362 | } |
||
363 | |||
364 | } |
||
365 | public Iterator getUpdated() { |
||
366 | return new UpdatedIterator(); |
||
367 | } |
||
368 | class UpdatedIterator implements Iterator{ |
||
369 | private Boolean hasnext = null; |
||
370 | private Iterator iter; |
||
371 | private DefaultFeature obj;
|
||
372 | private int pos; |
||
373 | |||
374 | public UpdatedIterator() {
|
||
375 | iter = expansionAdapter.iterator(); |
||
376 | pos = -1;
|
||
377 | } |
||
378 | |||
379 | public boolean hasNext() { |
||
380 | if (hasnext != null) { |
||
381 | return hasnext.booleanValue();
|
||
382 | } |
||
383 | hasnext = Boolean.FALSE;
|
||
384 | while (iter.hasNext()) {
|
||
385 | pos++; |
||
386 | obj = (DefaultFeature) iter.next(); |
||
387 | if (deleted.contains(obj.getReference())){
|
||
388 | continue;
|
||
389 | }else if (!modifiedFromOriginal.containsValue(new Integer(pos))){ |
||
390 | continue;
|
||
391 | }else {
|
||
392 | hasnext = Boolean.TRUE;
|
||
393 | break;
|
||
394 | } |
||
395 | } |
||
396 | return hasnext.booleanValue();
|
||
397 | } |
||
398 | |||
399 | public Object next() { |
||
400 | if (!hasNext()) {
|
||
401 | throw new NoSuchElementException(); |
||
402 | } |
||
403 | hasnext = null;
|
||
404 | return obj.getData();
|
||
405 | } |
||
406 | |||
407 | public void remove() { |
||
408 | throw new UnsupportedOperationException(); |
||
409 | |||
410 | } |
||
411 | } |
||
412 | |||
413 | public boolean hasNews() { |
||
414 | return !added.isEmpty();
|
||
415 | } |
||
416 | |||
417 | public long getDeltaSize() { |
||
418 | return deltaSize;
|
||
419 | } |
||
420 | } |