Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dal / src / org / gvsig / fmap / dal / feature / impl / FeatureManager.java @ 34112

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