root / tags / v2_0_0_Build_2047 / libraries / libFMap_dal / src / org / gvsig / fmap / dal / feature / impl / FeatureManager.java @ 38284
History | View | Annotate | Download (11.3 KB)
1 | 19399 | vcaballero | |
---|---|---|---|
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 | 24496 | jmvivo | package org.gvsig.fmap.dal.feature.impl; |
44 | 19399 | vcaballero | |
45 | 38202 | cordinyana | import java.util.Collection; |
46 | 19399 | vcaballero | import java.util.HashMap; |
47 | 20469 | vcaballero | import java.util.Iterator; |
48 | 28421 | jmvivo | import java.util.LinkedHashMap; |
49 | 38202 | cordinyana | import java.util.LinkedHashSet; |
50 | 28421 | jmvivo | import java.util.Map; |
51 | 27741 | vcaballero | import java.util.NoSuchElementException; |
52 | 19399 | vcaballero | |
53 | 24505 | jmvivo | import org.gvsig.fmap.dal.exception.DataException; |
54 | 24496 | jmvivo | 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 | 19399 | vcaballero | |
62 | |||
63 | /**
|
||
64 | * DOCUMENT ME!
|
||
65 | *
|
||
66 | * @author Vicente Caballero Navarro
|
||
67 | */
|
||
68 | public class FeatureManager { |
||
69 | 24132 | vcaballero | private ExpansionAdapter expansionAdapter;
|
70 | 38202 | cordinyana | private Collection deleted = new LinkedHashSet();//<FeatureID> |
71 | 24132 | vcaballero | private int deltaSize=0; |
72 | 28421 | jmvivo | private Map added = new LinkedHashMap(); |
73 | 36366 | jpiera | private Map addedAndDeleted = new LinkedHashMap(); |
74 | 24145 | vcaballero | private HashMap modifiedFromOriginal=new HashMap(); |
75 | 36366 | jpiera | |
76 | public FeatureManager(ExpansionAdapter expansionAdapter){
|
||
77 | 19399 | vcaballero | this.expansionAdapter=expansionAdapter;
|
78 | } |
||
79 | |||
80 | 38236 | jldominguez | /**
|
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 | 24132 | vcaballero | public Feature delete(FeatureReference id) {
|
87 | 24145 | vcaballero | deleted.add(id); |
88 | 27292 | jmvivo | Integer num = (Integer) added.remove(id); |
89 | 27233 | vcaballero | Feature feature=null;
|
90 | 27292 | jmvivo | if (num == null || num.intValue() == -1) { |
91 | 38235 | jldominguez | num = (Integer) modifiedFromOriginal.remove(id);
|
92 | 38236 | jldominguez | if (num != null) { |
93 | feature = (Feature) expansionAdapter.getObject(num.intValue()); |
||
94 | } |
||
95 | // if num is null here, method returns null
|
||
96 | 27233 | vcaballero | }else{
|
97 | 27292 | jmvivo | feature = (Feature) expansionAdapter.getObject(num.intValue()); |
98 | 36366 | jpiera | addedAndDeleted.put(id, num); |
99 | 24181 | jjdelcerro | } |
100 | 24132 | vcaballero | deltaSize--; |
101 | return feature;
|
||
102 | 19399 | vcaballero | } |
103 | |||
104 | /**
|
||
105 | * DOCUMENT ME!
|
||
106 | *
|
||
107 | * @param feature DOCUMENT ME!
|
||
108 | */
|
||
109 | 24132 | vcaballero | public void add(Feature feature) { |
110 | 25751 | vcaballero | int pos = expansionAdapter.addObject(feature);
|
111 | 24145 | vcaballero | added.put(feature.getReference(),new Integer(pos)); |
112 | 27233 | vcaballero | deleted.remove(feature.getReference()); |
113 | 24132 | vcaballero | deltaSize++; |
114 | 19399 | vcaballero | } |
115 | |||
116 | /**
|
||
117 | * DOCUMENT ME!
|
||
118 | * @param id DOCUMENT ME!
|
||
119 | */
|
||
120 | 27233 | vcaballero | 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 | 19399 | vcaballero | |
129 | /**
|
||
130 | 24015 | cordinyana | * Returns a Feature of the default type.
|
131 | 24017 | jjdelcerro | *
|
132 | 24015 | cordinyana | * @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 | 19399 | vcaballero | */
|
140 | 24132 | vcaballero | public Feature get(FeatureReference id, FeatureStore store)
|
141 | 24015 | cordinyana | throws DataException {
|
142 | 24132 | vcaballero | return get(id, store, null); |
143 | 24015 | cordinyana | } |
144 | |||
145 | /**
|
||
146 | * Returns a Feature of the given type.
|
||
147 | 24017 | jjdelcerro | *
|
148 | 24015 | cordinyana | * @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 | 24132 | vcaballero | public Feature get(FeatureReference id, FeatureStore store,
|
159 | 23775 | jjdelcerro | FeatureType featureType) throws DataException {
|
160 | 23820 | jjdelcerro | // 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 | 36366 | jpiera | Integer intNum = ((Integer) added.get(id)); |
167 | 20848 | jmvivo | if (intNum == null){ |
168 | 24145 | vcaballero | intNum =((Integer) modifiedFromOriginal.get(id));
|
169 | 36366 | jpiera | 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 | 24132 | vcaballero | } |
176 | 20848 | jmvivo | } |
177 | int num = intNum.intValue();
|
||
178 | 26005 | jmvivo | if (num==-1) { |
179 | return null; |
||
180 | } |
||
181 | 25751 | vcaballero | Feature feature=(Feature)expansionAdapter.getObject(num); |
182 | 21069 | jmvivo | if (featureType== null){ |
183 | featureType = store.getDefaultFeatureType(); |
||
184 | } |
||
185 | 25751 | vcaballero | return getCorrectFeature(feature, store,featureType);
|
186 | 19399 | vcaballero | } |
187 | |||
188 | 25751 | vcaballero | private Feature getCorrectFeature(Feature feature, FeatureStore store,
|
189 | 23775 | jjdelcerro | FeatureType featureType) throws DataException {
|
190 | 21069 | jmvivo | Iterator iterator=featureType.iterator();
|
191 | 25751 | vcaballero | EditableFeature newFeature=feature.getEditable();//store.createNewFeature(featureType, false);
|
192 | FeatureType orgType = feature.getType(); |
||
193 | 23775 | jjdelcerro | int orgIndex;
|
194 | 20469 | vcaballero | while (iterator.hasNext()) {
|
195 | 21045 | jmvivo | FeatureAttributeDescriptor fad = (FeatureAttributeDescriptor) iterator.next(); |
196 | 23775 | jjdelcerro | orgIndex = orgType.getIndex(fad.getName()); |
197 | if (orgIndex<0){ |
||
198 | continue;
|
||
199 | 20469 | vcaballero | } |
200 | 28908 | jmvivo | 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 | 35953 | jpiera | newFeature.set(orgIndex, value); |
209 | 20469 | vcaballero | } |
210 | 26005 | jmvivo | return newFeature.getNotEditableCopy();
|
211 | 20469 | vcaballero | } |
212 | |||
213 | /**
|
||
214 | 19399 | vcaballero | * DOCUMENT ME!
|
215 | *
|
||
216 | * @param feature DOCUMENT ME!
|
||
217 | * @param oldFeature DOCUMENT ME!
|
||
218 | */
|
||
219 | 24132 | vcaballero | public int update(Feature feature, Feature oldFeature) { |
220 | int oldNum=-1; |
||
221 | 25781 | vcaballero | int num = expansionAdapter.addObject(feature);
|
222 | 24132 | vcaballero | FeatureReference id=feature.getReference(); |
223 | 24145 | vcaballero | if (added.containsKey(id)){
|
224 | oldNum=((Integer)added.get(id)).intValue();
|
||
225 | added.put(id,new Integer(num)); |
||
226 | 24132 | vcaballero | }else{
|
227 | 26005 | jmvivo | if (modifiedFromOriginal.get(id)!=null) { |
228 | oldNum=((Integer)modifiedFromOriginal.get(id)).intValue();
|
||
229 | } |
||
230 | 24145 | vcaballero | modifiedFromOriginal.put(id,new Integer(num)); |
231 | 24132 | vcaballero | } |
232 | return oldNum;
|
||
233 | 19399 | vcaballero | } |
234 | |||
235 | /**
|
||
236 | * DOCUMENT ME!
|
||
237 | *
|
||
238 | * @param id DOCUMENT ME!
|
||
239 | */
|
||
240 | 24132 | vcaballero | public void restore(FeatureReference id) { |
241 | 24145 | vcaballero | deleted.remove(id); |
242 | 24132 | vcaballero | deltaSize++; |
243 | 19399 | vcaballero | } |
244 | 24132 | vcaballero | public void restore(FeatureReference id,int num){ |
245 | 24145 | vcaballero | if (added.containsKey(id)){
|
246 | added.put(id,new Integer(num)); |
||
247 | 24132 | vcaballero | }else{
|
248 | 24145 | vcaballero | modifiedFromOriginal.put(id,new Integer(num)); |
249 | 24132 | vcaballero | } |
250 | 19399 | vcaballero | } |
251 | |||
252 | 24132 | vcaballero | |
253 | 21045 | jmvivo | public boolean isDeleted(Feature feature){ |
254 | 24145 | vcaballero | return deleted.contains(feature.getReference());
|
255 | 19399 | vcaballero | } |
256 | |||
257 | 23842 | jjdelcerro | public boolean isDeleted(FeatureReference featureID) { |
258 | 24145 | vcaballero | return deleted.contains(featureID);
|
259 | 23820 | jjdelcerro | } |
260 | |||
261 | 19488 | vcaballero | public void clear() { |
262 | 24145 | vcaballero | added.clear(); |
263 | modifiedFromOriginal.clear(); |
||
264 | 19488 | vcaballero | expansionAdapter.close(); |
265 | 24145 | vcaballero | deleted.clear();//<FeatureID>
|
266 | 36366 | jpiera | addedAndDeleted.clear(); |
267 | 24132 | vcaballero | deltaSize=0;
|
268 | 19488 | vcaballero | } |
269 | 19399 | vcaballero | |
270 | 19488 | vcaballero | |
271 | 23820 | jjdelcerro | public boolean hasChanges() { |
272 | 24145 | vcaballero | return added.size()>0 || modifiedFromOriginal.size() > 0 || deleted.size() > 0; |
273 | 23820 | jjdelcerro | } |
274 | |||
275 | 24145 | vcaballero | public Iterator getDeleted() { |
276 | return new DeletedIterator(); |
||
277 | 24132 | vcaballero | |
278 | 23820 | jjdelcerro | } |
279 | |||
280 | 28316 | jmvivo | class DeletedIterator implements Iterator { |
281 | private Boolean hasnext = null; |
||
282 | private Iterator iter; |
||
283 | 28660 | jmvivo | private DefaultFeatureReference obj;
|
284 | 24145 | vcaballero | |
285 | 28316 | jmvivo | public DeletedIterator(){
|
286 | 28660 | jmvivo | iter = deleted.iterator(); |
287 | 24145 | vcaballero | } |
288 | |||
289 | 28316 | jmvivo | public boolean hasNext() { |
290 | if (hasnext != null) { |
||
291 | return hasnext.booleanValue();
|
||
292 | } |
||
293 | hasnext = Boolean.FALSE;
|
||
294 | while (iter.hasNext()) {
|
||
295 | 28660 | jmvivo | obj = (DefaultFeatureReference) iter.next(); |
296 | if (obj.isNewFeature()) {
|
||
297 | continue;
|
||
298 | 28316 | jmvivo | } |
299 | 28660 | jmvivo | hasnext = Boolean.TRUE;
|
300 | break;
|
||
301 | 28316 | jmvivo | } |
302 | return hasnext.booleanValue();
|
||
303 | 24145 | vcaballero | } |
304 | 25751 | vcaballero | |
305 | 28316 | jmvivo | public Object next() { |
306 | if (!hasNext()) {
|
||
307 | throw new NoSuchElementException(); |
||
308 | 24145 | vcaballero | } |
309 | 28316 | jmvivo | |
310 | hasnext = null;
|
||
311 | 28660 | jmvivo | return obj;
|
312 | 24145 | vcaballero | } |
313 | |||
314 | 28316 | jmvivo | public void remove() { |
315 | throw new UnsupportedOperationException(); |
||
316 | 24145 | vcaballero | |
317 | 28316 | jmvivo | } |
318 | |||
319 | 24141 | vcaballero | } |
320 | 28316 | jmvivo | |
321 | 24145 | vcaballero | public Iterator getInserted() { |
322 | return new InsertedIterator(); |
||
323 | 24141 | vcaballero | } |
324 | 27741 | vcaballero | |
325 | 28316 | jmvivo | class InsertedIterator implements Iterator { |
326 | 24145 | vcaballero | |
327 | 28316 | jmvivo | private Iterator addedIter; |
328 | private DefaultFeature obj;
|
||
329 | private Boolean hasnext = null; |
||
330 | 27741 | vcaballero | |
331 | 28316 | jmvivo | 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 | 24145 | vcaballero | } |
348 | } |
||
349 | 28316 | jmvivo | return hasnext.booleanValue();
|
350 | 24145 | vcaballero | } |
351 | |||
352 | public Object next() { |
||
353 | 28316 | jmvivo | if (!hasNext()) {
|
354 | 27741 | vcaballero | throw new NoSuchElementException(); |
355 | 24145 | vcaballero | } |
356 | 28316 | jmvivo | hasnext = null;
|
357 | 27741 | vcaballero | return obj.getData();
|
358 | 24145 | vcaballero | } |
359 | |||
360 | public void remove() { |
||
361 | throw new UnsupportedOperationException(); |
||
362 | |||
363 | } |
||
364 | 28316 | jmvivo | |
365 | 24145 | vcaballero | } |
366 | public Iterator getUpdated() { |
||
367 | return new UpdatedIterator(); |
||
368 | } |
||
369 | class UpdatedIterator implements Iterator{ |
||
370 | 28316 | jmvivo | private Boolean hasnext = null; |
371 | private Iterator iter; |
||
372 | private DefaultFeature obj;
|
||
373 | private int pos; |
||
374 | 24145 | vcaballero | |
375 | 28316 | jmvivo | public UpdatedIterator() {
|
376 | iter = expansionAdapter.iterator(); |
||
377 | pos = -1;
|
||
378 | } |
||
379 | |||
380 | 24145 | vcaballero | public boolean hasNext() { |
381 | 28316 | jmvivo | if (hasnext != null) { |
382 | return hasnext.booleanValue();
|
||
383 | } |
||
384 | hasnext = Boolean.FALSE;
|
||
385 | while (iter.hasNext()) {
|
||
386 | 24145 | vcaballero | pos++; |
387 | 28316 | jmvivo | obj = (DefaultFeature) iter.next(); |
388 | 25751 | vcaballero | if (deleted.contains(obj.getReference())){
|
389 | 28316 | jmvivo | continue;
|
390 | 24145 | vcaballero | }else if (!modifiedFromOriginal.containsValue(new Integer(pos))){ |
391 | 28316 | jmvivo | continue;
|
392 | 24145 | vcaballero | }else {
|
393 | 28316 | jmvivo | hasnext = Boolean.TRUE;
|
394 | break;
|
||
395 | 24145 | vcaballero | } |
396 | } |
||
397 | 28316 | jmvivo | return hasnext.booleanValue();
|
398 | 24145 | vcaballero | } |
399 | |||
400 | public Object next() { |
||
401 | 28316 | jmvivo | if (!hasNext()) {
|
402 | throw new NoSuchElementException(); |
||
403 | 24145 | vcaballero | } |
404 | 28316 | jmvivo | hasnext = null;
|
405 | return obj.getData();
|
||
406 | 24145 | vcaballero | } |
407 | |||
408 | public void remove() { |
||
409 | throw new UnsupportedOperationException(); |
||
410 | |||
411 | } |
||
412 | } |
||
413 | |||
414 | 23820 | jjdelcerro | public boolean hasNews() { |
415 | 24145 | vcaballero | return !added.isEmpty();
|
416 | 23820 | jjdelcerro | } |
417 | |||
418 | public long getDeltaSize() { |
||
419 | 24132 | vcaballero | return deltaSize;
|
420 | 23820 | jjdelcerro | } |
421 | 19399 | vcaballero | } |