Statistics
| Revision:

root / trunk / libraries / libFMap_data / src / org / gvsig / data / vectorial / FeatureManager.java @ 20898

History | View | Annotate | Download (7.14 KB)

1

    
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.data.vectorial;
44

    
45
import java.util.ArrayList;
46
import java.util.HashMap;
47
import java.util.Iterator;
48

    
49
import org.gvsig.data.exception.InitializeException;
50
import org.gvsig.data.exception.ReadException;
51
import org.gvsig.data.vectorial.expansionadapter.IExpansionAdapter;
52

    
53

    
54
/**
55
 * DOCUMENT ME!
56
 *
57
 * @author Vicente Caballero Navarro
58
 */
59
public class FeatureManager {
60
    protected HashMap relations = new HashMap();//<IFeatureID,Integer>
61
    protected IExpansionAdapter expansionAdapter;
62
    protected ArrayList deletedFeatures = new ArrayList();//<IFeatureID>
63
    protected int num=0;
64
    public FeatureManager(IExpansionAdapter expansionAdapter){
65
            this.expansionAdapter=expansionAdapter;
66
    }
67

    
68
    public void deleteFeature(IFeatureID id) {
69
        deletedFeatures.add(id);
70
               relations.remove(id);
71
        num--;
72
    }
73

    
74
    /**
75
     * DOCUMENT ME!
76
     *
77
     * @param feature DOCUMENT ME!
78
     */
79
    public void addFeature(IFeature feature) {
80
        int pos = expansionAdapter.addObject(feature);
81
        relations.put(feature.getID(), new Integer(pos));
82
        num++;
83
    }
84

    
85
    /**
86
     * DOCUMENT ME!
87
     *
88
     * @param id DOCUMENT ME!
89
     */
90
    public void deleteLastFeature(IFeatureID id) {
91
        expansionAdapter.deleteLastObject();
92
        relations.remove(id);
93
        num--;
94
    }
95

    
96
    /**
97
     * DOCUMENT ME!
98
     *
99
     * @param id DOCUMENT ME!
100
     *
101
     * @return DOCUMENT ME!
102
     */
103
    public boolean contains(IFeatureID id) {
104
        return relations.containsKey(id);
105
    }
106

    
107
    /**
108
     * DOCUMENT ME!
109
     *
110
     * @param id DOCUMENT ME!
111
     *
112
     * @return DOCUMENT ME!
113
     * @throws IsNotFeatureSettingException
114
     */
115
    public IFeature getFeature(IFeatureID id,IFeatureStore store) throws InitializeException {
116
            Integer intNum =((Integer) relations.get(id));
117
            if (intNum == null){
118
                    return null;
119
            }
120
        int num = intNum.intValue();
121

    
122
        IFeature feature=(IFeature)expansionAdapter.getObject(num);
123
               return getCorrectFeature(feature, store);
124
    }
125

    
126
    private IFeature getCorrectFeature(IFeature feature,IFeatureStore store) throws InitializeException {
127
             Iterator iterator=store.getDefaultFeatureType().iterator();
128
         IFeature newFeature=store.createFeature(store.getDefaultFeatureType());
129
         ((Feature)newFeature).loading();
130
         while (iterator.hasNext()) {
131
                         IFeatureAttributeDescriptor fad = (IFeatureAttributeDescriptor) iterator.next();
132
                         int i=((AttributeDescriptor)fad).originalPosition();
133
                         try {
134
                                 if (i<feature.size()){
135
                                         newFeature.set(i,feature.get(i));
136
                                 }else{
137
                                         newFeature.set(i,fad.getDefaultValue());
138
                                 }
139
                         } catch (IsNotFeatureSettingException e) {
140
                                 throw new InitializeException("FeatureManager",e);
141
                         }
142

    
143
                 }
144
         ((Feature)newFeature).stopLoading();
145
         return newFeature;
146
        }
147

    
148
        /**
149
     * DOCUMENT ME!
150
     *
151
     * @param feature DOCUMENT ME!
152
     * @param oldFeature DOCUMENT ME!
153
     */
154
    public void updateFeature(IFeature feature, IFeature oldFeature) {
155
        deletedFeatures.add(oldFeature.getID());
156

    
157
        int num = expansionAdapter.updateObject(feature);
158

    
159
        /*
160
         * Actualiza la relaci?n del ?ndice de la geometr?a al ?ndice en el
161
         * fichero de expansi?n.
162
         */
163
        relations.put(feature.getID(), new Integer(num));
164
    }
165

    
166
    /**
167
     * DOCUMENT ME!
168
     *
169
     * @param id DOCUMENT ME!
170
     */
171
    public void restoreFeature(IFeatureID id) {
172
        deletedFeatures.remove(id);
173
        num++;
174
    }
175
    public int getNum(){
176
            return num;
177
    }
178

    
179
    public IFeature getFeature(int i,IFeatureStore store) throws ReadException{
180
            IFeature feature=(IFeature)expansionAdapter.getObject(i);
181
            return getCorrectFeature(feature,store);
182
    }
183
    public boolean isDeleted(IFeature feature){
184
            return deletedFeatures.contains(feature.getID());
185
    }
186

    
187
        public void clear() {
188
                relations.clear();//<IFeatureID,Integer>
189
            expansionAdapter.close();
190
            deletedFeatures.clear();//<IFeatureID>
191
            num=0;
192
        }
193

    
194

    
195
//        public IFeatureCollection getDataCollection(IDataCollection originalCollection, IFeatureType type, String filter, String order) {
196
//                MemoryFeatureCollection memoryCollection=new MemoryFeatureCollection();
197
////                TODO Filtrar y ordenar, quedar? tambi?n pendiente el IFeatureType que se pasa como
198
//                //par?metro ya que aqu? lo ?nico que introducimos al DataCollection son IFeatureID,
199
//                //todav?a no creamos IFeatures
200
//                for (int i=0;i<relations.size();i++){
201
//                        IFeature feature=(IFeature)expansionAdapter.getObject(i);
202
//                        if (!deletedFeatures.contains(feature.getID())){
203
//                                memoryCollection.add(feature);
204
//                        }
205
//                }
206
//                Iterator iter=originalCollection.iterator();
207
//                int i=0;
208
//                featureStore.open();
209
//                while (iter.hasNext()) {
210
//                        IFeature feature = (IFeature) iter.next();
211
//                        if (!deletedFeatures.contains(feature.getID())){
212
//                                memoryCollection.add(feature);
213
//                        }
214
//                        i++;
215
//                }
216
//
217
//                //TODO en el filtro de las features originales hay que a?adir, que no muestre las que est?n el el HashMap deletedFeatures
218
////                IFeatureCollection featureCollection=(IFeatureCollection)featureStore.getDataCollection(type,filter,order);
219
//
220
//
221
//                EditingFeatureCollection collection=new EditingFeatureCollection(originalCollection, memoryCollection,type);
222
////                collection.
223
////                MemoryFeatureCollection collection=new MemoryFeatureCollection();
224
//                //TODO Filtrar y ordenar, quedar? tambi?n pendiente el IFeatureType que se pasa como
225
//                //par?metro ya que aqu? lo ?nico que introducimos al DataCollection son IFeatureID,
226
//                //todav?a no creamos IFeatures
227
////                featureStore.createDefaultFeature()
228
////                for (int i=0;i<driver.getFeatureCount();i++){
229
////                        MemoryFeatureID id=new MemoryFeatureID(driver.getFeatureByPosition(type,i));
230
////                        collection.add(id);
231
////                }
232
//                return collection;
233
//
234
//        }
235

    
236
}