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