Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_dal / src / org / gvsig / fmap / dal / feature / impl / FeatureManager.java @ 36366

History | View | Annotate | Download (10.9 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
import java.util.ArrayList;
46
import java.util.HashMap;
47 20469 vcaballero
import java.util.Iterator;
48 28421 jmvivo
import java.util.LinkedHashMap;
49
import java.util.Map;
50 27741 vcaballero
import java.util.NoSuchElementException;
51 19399 vcaballero
52 24505 jmvivo
import org.gvsig.fmap.dal.exception.DataException;
53 24496 jmvivo
import org.gvsig.fmap.dal.feature.EditableFeature;
54
import org.gvsig.fmap.dal.feature.Feature;
55
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
56
import org.gvsig.fmap.dal.feature.FeatureReference;
57
import org.gvsig.fmap.dal.feature.FeatureStore;
58
import org.gvsig.fmap.dal.feature.FeatureType;
59
import org.gvsig.fmap.dal.feature.impl.expansionadapter.ExpansionAdapter;
60 19399 vcaballero
61
62
/**
63
 * DOCUMENT ME!
64
 *
65
 * @author Vicente Caballero Navarro
66
 */
67
public class FeatureManager {
68 24132 vcaballero
    private ExpansionAdapter expansionAdapter;
69 24145 vcaballero
    private ArrayList deleted = new ArrayList();//<FeatureID>
70 24132 vcaballero
    private int deltaSize=0;
71 28421 jmvivo
        private Map added = new LinkedHashMap();
72 36366 jpiera
        private Map addedAndDeleted = new LinkedHashMap();
73 24145 vcaballero
        private HashMap modifiedFromOriginal=new HashMap();
74 36366 jpiera
75
        public FeatureManager(ExpansionAdapter expansionAdapter){
76 19399 vcaballero
            this.expansionAdapter=expansionAdapter;
77
    }
78
79 24132 vcaballero
    public Feature delete(FeatureReference id) {
80 24145 vcaballero
        deleted.add(id);
81 27292 jmvivo
                Integer num = (Integer) added.remove(id);
82 27233 vcaballero
        Feature feature=null;
83 27292 jmvivo
        if (num == null || num.intValue() == -1) {
84 27233 vcaballero
                feature = (Feature)modifiedFromOriginal.remove(id);
85
                }else{
86 27292 jmvivo
                        feature = (Feature) expansionAdapter.getObject(num.intValue());
87 36366 jpiera
                        addedAndDeleted.put(id, num);
88 24181 jjdelcerro
                }
89 24132 vcaballero
        deltaSize--;
90
        return feature;
91 19399 vcaballero
    }
92
93
    /**
94
     * DOCUMENT ME!
95
     *
96
     * @param feature DOCUMENT ME!
97
     */
98 24132 vcaballero
    public void add(Feature feature) {
99 25751 vcaballero
        int pos = expansionAdapter.addObject(feature);
100 24145 vcaballero
        added.put(feature.getReference(),new Integer(pos));
101 27233 vcaballero
        deleted.remove(feature.getReference());
102 24132 vcaballero
        deltaSize++;
103 19399 vcaballero
    }
104
105
    /**
106
     * DOCUMENT ME!
107
     * @param id DOCUMENT ME!
108
     */
109 27233 vcaballero
    public Feature deleteLastFeature() {
110
        expansionAdapter.deleteLastObject();
111
        Feature feature=(Feature)expansionAdapter.getObject(expansionAdapter.getSize()-1);
112
        added.remove(feature.getReference());
113
        modifiedFromOriginal.remove(feature.getReference());
114
        deltaSize--;
115
        return feature;
116
    }
117 19399 vcaballero
118
    /**
119 24015 cordinyana
     * Returns a Feature of the default type.
120 24017 jjdelcerro
     *
121 24015 cordinyana
     * @param id
122
     *            the feature reference
123
     * @param store
124
     *            the store to get the feature from
125
     * @return a Feature with the given reference
126
     * @throws DataException
127
     *             if there is an error getting the Feature
128 19399 vcaballero
     */
129 24132 vcaballero
    public Feature get(FeatureReference id, FeatureStore store)
130 24015 cordinyana
            throws DataException {
131 24132 vcaballero
        return get(id, store, null);
132 24015 cordinyana
    }
133
134
    /**
135
     * Returns a Feature of the given type.
136 24017 jjdelcerro
     *
137 24015 cordinyana
     * @param id
138
     *            the feature reference
139
     * @param store
140
     *            the store to get the feature from
141
     * @param featureType
142
     *            the type of the feature to return
143
     * @return a Feature with the given reference
144
     * @throws DataException
145
     *             if there is an error getting the Feature
146
     */
147 24132 vcaballero
    public Feature get(FeatureReference id, FeatureStore store,
148 23775 jjdelcerro
                        FeatureType featureType) throws DataException {
149 23820 jjdelcerro
            // FIXME: y si el featuretype que paso esta modificado.
150
            //        Deberia buscarlo en el featuretypemanager ?
151
                //
152
            //        Si no existe feature con ese id... ? retorna null ?
153
            //        en EditedDefaultIterator se hace uso de ese comportamiento.
154
            //
155 36366 jpiera
            Integer intNum = ((Integer) added.get(id));
156 20848 jmvivo
            if (intNum == null){
157 24145 vcaballero
                    intNum =((Integer) modifiedFromOriginal.get(id));
158 36366 jpiera
                if (intNum == null){
159
                    //If the feature has been added and deleted
160
                    intNum = (Integer)addedAndDeleted.get(id);
161
                    if (intNum == null){
162
                        return null;
163
                    }
164 24132 vcaballero
                }
165 20848 jmvivo
            }
166
        int num = intNum.intValue();
167 26005 jmvivo
        if (num==-1) {
168
                        return null;
169
                }
170 25751 vcaballero
        Feature feature=(Feature)expansionAdapter.getObject(num);
171 21069 jmvivo
        if (featureType== null){
172
                featureType = store.getDefaultFeatureType();
173
        }
174 25751 vcaballero
               return getCorrectFeature(feature, store,featureType);
175 19399 vcaballero
    }
176
177 25751 vcaballero
    private Feature getCorrectFeature(Feature feature, FeatureStore store,
178 23775 jjdelcerro
                        FeatureType featureType) throws DataException {
179 21069 jmvivo
             Iterator iterator=featureType.iterator();
180 25751 vcaballero
         EditableFeature newFeature=feature.getEditable();//store.createNewFeature(featureType, false);
181
         FeatureType orgType = feature.getType();
182 23775 jjdelcerro
         int orgIndex;
183 20469 vcaballero
         while (iterator.hasNext()) {
184 21045 jmvivo
                         FeatureAttributeDescriptor fad = (FeatureAttributeDescriptor) iterator.next();
185 23775 jjdelcerro
                         orgIndex = orgType.getIndex(fad.getName());
186
                         if (orgIndex<0){
187
                                 continue;
188 20469 vcaballero
                         }
189 28908 jmvivo
                         if (fad.isAutomatic() || fad.isReadOnly()
190
                                        || fad.getEvaluator() != null) {
191
                                continue;
192
                        }
193
                         Object value = feature.get(orgIndex);
194
                        if (value == null && !fad.allowNull()) {
195
                                continue;
196
                        }
197 35953 jpiera
                        newFeature.set(orgIndex, value);
198 20469 vcaballero
                 }
199 26005 jmvivo
        return newFeature.getNotEditableCopy();
200 20469 vcaballero
        }
201
202
        /**
203 19399 vcaballero
     * DOCUMENT ME!
204
     *
205
     * @param feature DOCUMENT ME!
206
     * @param oldFeature DOCUMENT ME!
207
     */
208 24132 vcaballero
    public int update(Feature feature, Feature oldFeature) {
209
            int oldNum=-1;
210 25781 vcaballero
        int num = expansionAdapter.addObject(feature);
211 24132 vcaballero
        FeatureReference id=feature.getReference();
212 24145 vcaballero
        if (added.containsKey(id)){
213
                oldNum=((Integer)added.get(id)).intValue();
214
                added.put(id,new Integer(num));
215 24132 vcaballero
        }else{
216 26005 jmvivo
                if (modifiedFromOriginal.get(id)!=null) {
217
                                oldNum=((Integer)modifiedFromOriginal.get(id)).intValue();
218
                        }
219 24145 vcaballero
                modifiedFromOriginal.put(id,new Integer(num));
220 24132 vcaballero
        }
221
        return oldNum;
222 19399 vcaballero
    }
223
224
    /**
225
     * DOCUMENT ME!
226
     *
227
     * @param id DOCUMENT ME!
228
     */
229 24132 vcaballero
    public void restore(FeatureReference id) {
230 24145 vcaballero
        deleted.remove(id);
231 24132 vcaballero
        deltaSize++;
232 19399 vcaballero
    }
233 24132 vcaballero
    public void restore(FeatureReference id,int num){
234 24145 vcaballero
            if (added.containsKey(id)){
235
                added.put(id,new Integer(num));
236 24132 vcaballero
        }else{
237 24145 vcaballero
                modifiedFromOriginal.put(id,new Integer(num));
238 24132 vcaballero
        }
239 19399 vcaballero
    }
240
241 24132 vcaballero
242 21045 jmvivo
    public boolean isDeleted(Feature feature){
243 24145 vcaballero
            return deleted.contains(feature.getReference());
244 19399 vcaballero
    }
245
246 23842 jjdelcerro
    public boolean isDeleted(FeatureReference featureID) {
247 24145 vcaballero
                return deleted.contains(featureID);
248 23820 jjdelcerro
        }
249
250 19488 vcaballero
        public void clear() {
251 24145 vcaballero
                added.clear();
252
                modifiedFromOriginal.clear();
253 19488 vcaballero
            expansionAdapter.close();
254 24145 vcaballero
            deleted.clear();//<FeatureID>
255 36366 jpiera
            addedAndDeleted.clear();
256 24132 vcaballero
            deltaSize=0;
257 19488 vcaballero
        }
258 19399 vcaballero
259 19488 vcaballero
260 23820 jjdelcerro
        public boolean hasChanges() {
261 24145 vcaballero
                return added.size()>0 || modifiedFromOriginal.size() > 0 || deleted.size() > 0;
262 23820 jjdelcerro
        }
263
264 24145 vcaballero
        public Iterator getDeleted() {
265
                return new DeletedIterator();
266 24132 vcaballero
267 23820 jjdelcerro
        }
268
269 28316 jmvivo
        class DeletedIterator implements Iterator {
270
                private Boolean hasnext = null;
271
                private Iterator iter;
272 28660 jmvivo
                private DefaultFeatureReference obj;
273 24145 vcaballero
274 28316 jmvivo
                public DeletedIterator(){
275 28660 jmvivo
                        iter = deleted.iterator();
276 24145 vcaballero
                }
277
278 28316 jmvivo
                public boolean hasNext() {
279
                        if (hasnext != null) {
280
                                return hasnext.booleanValue();
281
                        }
282
                        hasnext = Boolean.FALSE;
283
                        while (iter.hasNext()) {
284 28660 jmvivo
                                obj = (DefaultFeatureReference) iter.next();
285
                                if (obj.isNewFeature()) {
286
                                        continue;
287 28316 jmvivo
                                }
288 28660 jmvivo
                                hasnext = Boolean.TRUE;
289
                                break;
290 28316 jmvivo
                        }
291
                        return hasnext.booleanValue();
292 24145 vcaballero
                }
293 25751 vcaballero
294 28316 jmvivo
                public Object next() {
295
                        if (!hasNext()) {
296
                                throw new NoSuchElementException();
297 24145 vcaballero
                        }
298 28316 jmvivo
299
                        hasnext = null;
300 28660 jmvivo
                        return obj;
301 24145 vcaballero
                }
302
303 28316 jmvivo
                public void remove() {
304
                        throw new UnsupportedOperationException();
305 24145 vcaballero
306 28316 jmvivo
                }
307
308 24141 vcaballero
        }
309 28316 jmvivo
310 24145 vcaballero
        public Iterator getInserted() {
311
                return new InsertedIterator();
312 24141 vcaballero
        }
313 27741 vcaballero
314 28316 jmvivo
        class InsertedIterator implements Iterator {
315 24145 vcaballero
316 28316 jmvivo
                private Iterator addedIter;
317
                private DefaultFeature obj;
318
                private Boolean hasnext = null;
319 27741 vcaballero
320 28316 jmvivo
                public InsertedIterator(){
321
                        addedIter = added.values().iterator();
322
                }
323
324
                public boolean hasNext() {
325
                        if (hasnext != null) {
326
                                return hasnext.booleanValue();
327
                        }
328
                        hasnext = Boolean.FALSE;
329
                        int pos;
330
                        while (addedIter.hasNext()) {
331
                                pos = ((Integer) addedIter.next()).intValue();
332
                                obj = (DefaultFeature) expansionAdapter.getObject(pos);
333
                                if (!deleted.contains(obj.getReference())) {
334
                                        hasnext = Boolean.TRUE;
335
                                        break;
336 24145 vcaballero
                                }
337
                        }
338 28316 jmvivo
                        return hasnext.booleanValue();
339 24145 vcaballero
                }
340
341
                public Object next() {
342 28316 jmvivo
                        if (!hasNext()) {
343 27741 vcaballero
                                throw new NoSuchElementException();
344 24145 vcaballero
                        }
345 28316 jmvivo
                        hasnext = null;
346 27741 vcaballero
                        return obj.getData();
347 24145 vcaballero
                }
348
349
                public void remove() {
350
                        throw new UnsupportedOperationException();
351
352
                }
353 28316 jmvivo
354 24145 vcaballero
        }
355
        public Iterator getUpdated() {
356
                return new UpdatedIterator();
357
        }
358
        class UpdatedIterator implements Iterator{
359 28316 jmvivo
                private Boolean hasnext = null;
360
                private Iterator iter;
361
                private DefaultFeature obj;
362
                private int pos;
363 24145 vcaballero
364 28316 jmvivo
                public UpdatedIterator() {
365
                        iter = expansionAdapter.iterator();
366
                        pos = -1;
367
                }
368
369 24145 vcaballero
                public boolean hasNext() {
370 28316 jmvivo
                        if (hasnext != null) {
371
                                return hasnext.booleanValue();
372
                        }
373
                        hasnext = Boolean.FALSE;
374
                        while (iter.hasNext()) {
375 24145 vcaballero
                                pos++;
376 28316 jmvivo
                                obj = (DefaultFeature) iter.next();
377 25751 vcaballero
                                if (deleted.contains(obj.getReference())){
378 28316 jmvivo
                                        continue;
379 24145 vcaballero
                                }else if (!modifiedFromOriginal.containsValue(new Integer(pos))){
380 28316 jmvivo
                                        continue;
381 24145 vcaballero
                                }else {
382 28316 jmvivo
                                        hasnext = Boolean.TRUE;
383
                                        break;
384 24145 vcaballero
                                }
385
                        }
386 28316 jmvivo
                        return hasnext.booleanValue();
387 24145 vcaballero
                }
388
389
                public Object next() {
390 28316 jmvivo
                        if (!hasNext()) {
391
                                throw new NoSuchElementException();
392 24145 vcaballero
                        }
393 28316 jmvivo
                        hasnext = null;
394
                        return obj.getData();
395 24145 vcaballero
                }
396
397
                public void remove() {
398
                        throw new UnsupportedOperationException();
399
400
                }
401
        }
402
403 23820 jjdelcerro
        public boolean hasNews() {
404 24145 vcaballero
                return !added.isEmpty();
405 23820 jjdelcerro
        }
406
407
        public long getDeltaSize() {
408 24132 vcaballero
                return deltaSize;
409 23820 jjdelcerro
        }
410 19399 vcaballero
}