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 |
|
---|---|
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.dal.feature.impl; |
44 |
|
45 |
import java.util.ArrayList; |
46 |
import java.util.HashMap; |
47 |
import java.util.Iterator; |
48 |
import java.util.LinkedHashMap; |
49 |
import java.util.Map; |
50 |
import java.util.NoSuchElementException; |
51 |
|
52 |
import org.gvsig.fmap.dal.exception.DataException; |
53 |
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 |
|
61 |
|
62 |
/**
|
63 |
* DOCUMENT ME!
|
64 |
*
|
65 |
* @author Vicente Caballero Navarro
|
66 |
*/
|
67 |
public class FeatureManager { |
68 |
private ExpansionAdapter expansionAdapter;
|
69 |
private ArrayList deleted = new ArrayList();//<FeatureID> |
70 |
private int deltaSize=0; |
71 |
private Map added = new LinkedHashMap(); |
72 |
private Map addedAndDeleted = new LinkedHashMap(); |
73 |
private HashMap modifiedFromOriginal=new HashMap(); |
74 |
|
75 |
public FeatureManager(ExpansionAdapter expansionAdapter){
|
76 |
this.expansionAdapter=expansionAdapter;
|
77 |
} |
78 |
|
79 |
public Feature delete(FeatureReference id) {
|
80 |
deleted.add(id); |
81 |
Integer num = (Integer) added.remove(id); |
82 |
Feature feature=null;
|
83 |
if (num == null || num.intValue() == -1) { |
84 |
feature = (Feature)modifiedFromOriginal.remove(id); |
85 |
}else{
|
86 |
feature = (Feature) expansionAdapter.getObject(num.intValue()); |
87 |
addedAndDeleted.put(id, num); |
88 |
} |
89 |
deltaSize--; |
90 |
return feature;
|
91 |
} |
92 |
|
93 |
/**
|
94 |
* DOCUMENT ME!
|
95 |
*
|
96 |
* @param feature DOCUMENT ME!
|
97 |
*/
|
98 |
public void add(Feature feature) { |
99 |
int pos = expansionAdapter.addObject(feature);
|
100 |
added.put(feature.getReference(),new Integer(pos)); |
101 |
deleted.remove(feature.getReference()); |
102 |
deltaSize++; |
103 |
} |
104 |
|
105 |
/**
|
106 |
* DOCUMENT ME!
|
107 |
* @param id DOCUMENT ME!
|
108 |
*/
|
109 |
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 |
|
118 |
/**
|
119 |
* Returns a Feature of the default type.
|
120 |
*
|
121 |
* @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 |
*/
|
129 |
public Feature get(FeatureReference id, FeatureStore store)
|
130 |
throws DataException {
|
131 |
return get(id, store, null); |
132 |
} |
133 |
|
134 |
/**
|
135 |
* Returns a Feature of the given type.
|
136 |
*
|
137 |
* @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 |
public Feature get(FeatureReference id, FeatureStore store,
|
148 |
FeatureType featureType) throws DataException {
|
149 |
// 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 |
Integer intNum = ((Integer) added.get(id)); |
156 |
if (intNum == null){ |
157 |
intNum =((Integer) modifiedFromOriginal.get(id));
|
158 |
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 |
} |
165 |
} |
166 |
int num = intNum.intValue();
|
167 |
if (num==-1) { |
168 |
return null; |
169 |
} |
170 |
Feature feature=(Feature)expansionAdapter.getObject(num); |
171 |
if (featureType== null){ |
172 |
featureType = store.getDefaultFeatureType(); |
173 |
} |
174 |
return getCorrectFeature(feature, store,featureType);
|
175 |
} |
176 |
|
177 |
private Feature getCorrectFeature(Feature feature, FeatureStore store,
|
178 |
FeatureType featureType) throws DataException {
|
179 |
Iterator iterator=featureType.iterator();
|
180 |
EditableFeature newFeature=feature.getEditable();//store.createNewFeature(featureType, false);
|
181 |
FeatureType orgType = feature.getType(); |
182 |
int orgIndex;
|
183 |
while (iterator.hasNext()) {
|
184 |
FeatureAttributeDescriptor fad = (FeatureAttributeDescriptor) iterator.next(); |
185 |
orgIndex = orgType.getIndex(fad.getName()); |
186 |
if (orgIndex<0){ |
187 |
continue;
|
188 |
} |
189 |
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 |
newFeature.set(orgIndex, value); |
198 |
} |
199 |
return newFeature.getNotEditableCopy();
|
200 |
} |
201 |
|
202 |
/**
|
203 |
* DOCUMENT ME!
|
204 |
*
|
205 |
* @param feature DOCUMENT ME!
|
206 |
* @param oldFeature DOCUMENT ME!
|
207 |
*/
|
208 |
public int update(Feature feature, Feature oldFeature) { |
209 |
int oldNum=-1; |
210 |
int num = expansionAdapter.addObject(feature);
|
211 |
FeatureReference id=feature.getReference(); |
212 |
if (added.containsKey(id)){
|
213 |
oldNum=((Integer)added.get(id)).intValue();
|
214 |
added.put(id,new Integer(num)); |
215 |
}else{
|
216 |
if (modifiedFromOriginal.get(id)!=null) { |
217 |
oldNum=((Integer)modifiedFromOriginal.get(id)).intValue();
|
218 |
} |
219 |
modifiedFromOriginal.put(id,new Integer(num)); |
220 |
} |
221 |
return oldNum;
|
222 |
} |
223 |
|
224 |
/**
|
225 |
* DOCUMENT ME!
|
226 |
*
|
227 |
* @param id DOCUMENT ME!
|
228 |
*/
|
229 |
public void restore(FeatureReference id) { |
230 |
deleted.remove(id); |
231 |
deltaSize++; |
232 |
} |
233 |
public void restore(FeatureReference id,int num){ |
234 |
if (added.containsKey(id)){
|
235 |
added.put(id,new Integer(num)); |
236 |
}else{
|
237 |
modifiedFromOriginal.put(id,new Integer(num)); |
238 |
} |
239 |
} |
240 |
|
241 |
|
242 |
public boolean isDeleted(Feature feature){ |
243 |
return deleted.contains(feature.getReference());
|
244 |
} |
245 |
|
246 |
public boolean isDeleted(FeatureReference featureID) { |
247 |
return deleted.contains(featureID);
|
248 |
} |
249 |
|
250 |
public void clear() { |
251 |
added.clear(); |
252 |
modifiedFromOriginal.clear(); |
253 |
expansionAdapter.close(); |
254 |
deleted.clear();//<FeatureID>
|
255 |
addedAndDeleted.clear(); |
256 |
deltaSize=0;
|
257 |
} |
258 |
|
259 |
|
260 |
public boolean hasChanges() { |
261 |
return added.size()>0 || modifiedFromOriginal.size() > 0 || deleted.size() > 0; |
262 |
} |
263 |
|
264 |
public Iterator getDeleted() { |
265 |
return new DeletedIterator(); |
266 |
|
267 |
} |
268 |
|
269 |
class DeletedIterator implements Iterator { |
270 |
private Boolean hasnext = null; |
271 |
private Iterator iter; |
272 |
private DefaultFeatureReference obj;
|
273 |
|
274 |
public DeletedIterator(){
|
275 |
iter = deleted.iterator(); |
276 |
} |
277 |
|
278 |
public boolean hasNext() { |
279 |
if (hasnext != null) { |
280 |
return hasnext.booleanValue();
|
281 |
} |
282 |
hasnext = Boolean.FALSE;
|
283 |
while (iter.hasNext()) {
|
284 |
obj = (DefaultFeatureReference) iter.next(); |
285 |
if (obj.isNewFeature()) {
|
286 |
continue;
|
287 |
} |
288 |
hasnext = Boolean.TRUE;
|
289 |
break;
|
290 |
} |
291 |
return hasnext.booleanValue();
|
292 |
} |
293 |
|
294 |
public Object next() { |
295 |
if (!hasNext()) {
|
296 |
throw new NoSuchElementException(); |
297 |
} |
298 |
|
299 |
hasnext = null;
|
300 |
return obj;
|
301 |
} |
302 |
|
303 |
public void remove() { |
304 |
throw new UnsupportedOperationException(); |
305 |
|
306 |
} |
307 |
|
308 |
} |
309 |
|
310 |
public Iterator getInserted() { |
311 |
return new InsertedIterator(); |
312 |
} |
313 |
|
314 |
class InsertedIterator implements Iterator { |
315 |
|
316 |
private Iterator addedIter; |
317 |
private DefaultFeature obj;
|
318 |
private Boolean hasnext = null; |
319 |
|
320 |
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 |
} |
337 |
} |
338 |
return hasnext.booleanValue();
|
339 |
} |
340 |
|
341 |
public Object next() { |
342 |
if (!hasNext()) {
|
343 |
throw new NoSuchElementException(); |
344 |
} |
345 |
hasnext = null;
|
346 |
return obj.getData();
|
347 |
} |
348 |
|
349 |
public void remove() { |
350 |
throw new UnsupportedOperationException(); |
351 |
|
352 |
} |
353 |
|
354 |
} |
355 |
public Iterator getUpdated() { |
356 |
return new UpdatedIterator(); |
357 |
} |
358 |
class UpdatedIterator implements Iterator{ |
359 |
private Boolean hasnext = null; |
360 |
private Iterator iter; |
361 |
private DefaultFeature obj;
|
362 |
private int pos; |
363 |
|
364 |
public UpdatedIterator() {
|
365 |
iter = expansionAdapter.iterator(); |
366 |
pos = -1;
|
367 |
} |
368 |
|
369 |
public boolean hasNext() { |
370 |
if (hasnext != null) { |
371 |
return hasnext.booleanValue();
|
372 |
} |
373 |
hasnext = Boolean.FALSE;
|
374 |
while (iter.hasNext()) {
|
375 |
pos++; |
376 |
obj = (DefaultFeature) iter.next(); |
377 |
if (deleted.contains(obj.getReference())){
|
378 |
continue;
|
379 |
}else if (!modifiedFromOriginal.containsValue(new Integer(pos))){ |
380 |
continue;
|
381 |
}else {
|
382 |
hasnext = Boolean.TRUE;
|
383 |
break;
|
384 |
} |
385 |
} |
386 |
return hasnext.booleanValue();
|
387 |
} |
388 |
|
389 |
public Object next() { |
390 |
if (!hasNext()) {
|
391 |
throw new NoSuchElementException(); |
392 |
} |
393 |
hasnext = null;
|
394 |
return obj.getData();
|
395 |
} |
396 |
|
397 |
public void remove() { |
398 |
throw new UnsupportedOperationException(); |
399 |
|
400 |
} |
401 |
} |
402 |
|
403 |
public boolean hasNews() { |
404 |
return !added.isEmpty();
|
405 |
} |
406 |
|
407 |
public long getDeltaSize() { |
408 |
return deltaSize;
|
409 |
} |
410 |
} |