Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_data / src / org / gvsig / fmap / data / feature / impl / FeatureManager.java @ 23775

History | View | Annotate | Download (7.57 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.fmap.data.feature.impl;
44

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

    
49
import org.gvsig.fmap.data.exceptions.DataException;
50
import org.gvsig.fmap.data.feature.EditableFeature;
51
import org.gvsig.fmap.data.feature.Feature;
52
import org.gvsig.fmap.data.feature.FeatureAttributeDescriptor;
53
import org.gvsig.fmap.data.feature.FeatureID;
54
import org.gvsig.fmap.data.feature.FeatureStore;
55
import org.gvsig.fmap.data.feature.FeatureType;
56
import org.gvsig.fmap.data.feature.impl.expansionadapter.ExpansionAdapter;
57

    
58

    
59
/**
60
 * DOCUMENT ME!
61
 *
62
 * @author Vicente Caballero Navarro
63
 */
64
public class FeatureManager {
65
    protected HashMap relations = new HashMap();//<FeatureID,Integer>
66
    protected ExpansionAdapter expansionAdapter;
67
    protected ArrayList deletedFeatures = new ArrayList();//<FeatureID>
68
    protected int num=0;
69
    public FeatureManager(ExpansionAdapter expansionAdapter){
70
            this.expansionAdapter=expansionAdapter;
71
    }
72

    
73
    public void deleteFeature(FeatureID id) {
74
        deletedFeatures.add(id);
75
               relations.remove(id);
76
        num--;
77
    }
78

    
79
    /**
80
     * DOCUMENT ME!
81
     *
82
     * @param feature DOCUMENT ME!
83
     */
84
    public void addFeature(Feature feature) {
85
        int pos = expansionAdapter.addObject(feature);
86
        relations.put(feature.getID(), new Integer(pos));
87
        num++;
88
    }
89

    
90
    /**
91
     * DOCUMENT ME!
92
     *
93
     * @param id DOCUMENT ME!
94
     */
95
    public void deleteLastFeature(FeatureID id) {
96
        expansionAdapter.deleteLastObject();
97
        relations.remove(id);
98
        num--;
99
    }
100

    
101
    /**
102
     * DOCUMENT ME!
103
     *
104
     * @param id DOCUMENT ME!
105
     *
106
     * @return DOCUMENT ME!
107
     */
108
    public boolean contains(FeatureID id) {
109
        return relations.containsKey(id);
110
    }
111

    
112
    /**
113
     * DOCUMENT ME!
114
     *
115
     * @param id DOCUMENT ME!
116
     *
117
     * @return DOCUMENT ME!
118
     * @throws IsNotFeatureSettingException
119
     */
120
    public Feature getFeature(FeatureID id, FeatureStore store,
121
                        FeatureType featureType) throws DataException {
122
            Integer intNum =((Integer) relations.get(id));
123
            if (intNum == null){
124
                    return null;
125
            }
126
        int num = intNum.intValue();
127

    
128
        Feature feature=(Feature)expansionAdapter.getObject(num);
129
        if (featureType== null){
130
                featureType = store.getDefaultFeatureType();
131
        }
132
               return getCorrectFeature(feature, store,featureType);
133
    }
134

    
135
    private Feature getCorrectFeature(Feature feature, FeatureStore store,
136
                        FeatureType featureType) throws DataException {
137
             Iterator iterator=featureType.iterator();
138
         EditableFeature newFeature=store.createFeature(featureType, false);
139
         FeatureType orgType = feature.getType();
140
         int orgIndex;
141
         while (iterator.hasNext()) {
142
                         FeatureAttributeDescriptor fad = (FeatureAttributeDescriptor) iterator.next();
143
                         orgIndex = orgType.getIndex(fad.getName());
144
                         if (orgIndex<0){
145
                                 continue;
146
                         }
147
                         if (featureType.getDefaultGeometryAttributeName().equals(
148
                                                                        fad.getName())){
149
                                 newFeature.setGeometry(fad.getIndex(), feature.getGeometry(orgIndex));
150
                         } else{
151
                                 newFeature.set(fad.getIndex(), feature.get(orgIndex));
152
                         }
153
                 }
154
         return newFeature.getNotEditableCopy();
155
        }
156

    
157
        /**
158
     * DOCUMENT ME!
159
     *
160
     * @param feature DOCUMENT ME!
161
     * @param oldFeature DOCUMENT ME!
162
     */
163
    public void updateFeature(Feature feature, Feature oldFeature) {
164
        deletedFeatures.add(oldFeature.getID());
165

    
166
        int num = expansionAdapter.updateObject(feature);
167

    
168
        /*
169
         * Actualiza la relaci?n del ?ndice de la geometr?a al ?ndice en el
170
         * fichero de expansi?n.
171
         */
172
        relations.put(feature.getID(), new Integer(num));
173
    }
174

    
175
    /**
176
     * DOCUMENT ME!
177
     *
178
     * @param id DOCUMENT ME!
179
     */
180
    public void restoreFeature(FeatureID id) {
181
        deletedFeatures.remove(id);
182
        num++;
183
    }
184
    public int getNum(){
185
            return num;
186
    }
187

    
188
    public Feature getFeature(int i, FeatureStore store, FeatureType featureType)
189
                        throws DataException {
190
            Feature feature=(Feature)expansionAdapter.getObject(i);
191
            return getCorrectFeature(feature,store,featureType);
192
    }
193
    public boolean isDeleted(Feature feature){
194
            return deletedFeatures.contains(feature.getID());
195
    }
196

    
197
        public void clear() {
198
                relations.clear();//<FeatureID,Integer>
199
            expansionAdapter.close();
200
            deletedFeatures.clear();//<FeatureID>
201
            num=0;
202
        }
203

    
204

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

    
246
}