svn-gvsig-desktop / tags / dal_time_support_Build_1 / libraries / libFMap_dal / src / org / gvsig / fmap / dal / feature / impl / FeatureManager.java @ 36111
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 HashMap modifiedFromOriginal=new HashMap(); |
73 |
public FeatureManager(ExpansionAdapter expansionAdapter){
|
74 |
this.expansionAdapter=expansionAdapter;
|
75 |
} |
76 |
|
77 |
|
78 |
public Feature delete(FeatureReference id) {
|
79 |
deleted.add(id); |
80 |
Integer num = (Integer) added.remove(id); |
81 |
Feature feature=null;
|
82 |
if (num == null || num.intValue() == -1) { |
83 |
feature = (Feature)modifiedFromOriginal.remove(id); |
84 |
}else{
|
85 |
feature = (Feature) expansionAdapter.getObject(num.intValue()); |
86 |
} |
87 |
deltaSize--; |
88 |
return feature;
|
89 |
} |
90 |
|
91 |
/**
|
92 |
* DOCUMENT ME!
|
93 |
*
|
94 |
* @param feature DOCUMENT ME!
|
95 |
*/
|
96 |
public void add(Feature feature) { |
97 |
int pos = expansionAdapter.addObject(feature);
|
98 |
added.put(feature.getReference(),new Integer(pos)); |
99 |
deleted.remove(feature.getReference()); |
100 |
deltaSize++; |
101 |
} |
102 |
|
103 |
/**
|
104 |
* DOCUMENT ME!
|
105 |
* @param id DOCUMENT ME!
|
106 |
*/
|
107 |
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 |
|
116 |
/**
|
117 |
* Returns a Feature of the default type.
|
118 |
*
|
119 |
* @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 |
*/
|
127 |
public Feature get(FeatureReference id, FeatureStore store)
|
128 |
throws DataException {
|
129 |
return get(id, store, null); |
130 |
} |
131 |
|
132 |
/**
|
133 |
* Returns a Feature of the given type.
|
134 |
*
|
135 |
* @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 |
public Feature get(FeatureReference id, FeatureStore store,
|
146 |
FeatureType featureType) throws DataException {
|
147 |
// 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 |
Integer intNum =((Integer) added.get(id)); |
154 |
if (intNum == null){ |
155 |
intNum =((Integer) modifiedFromOriginal.get(id));
|
156 |
if (intNum == null){ |
157 |
return null; |
158 |
} |
159 |
} |
160 |
int num = intNum.intValue();
|
161 |
if (num==-1) { |
162 |
return null; |
163 |
} |
164 |
Feature feature=(Feature)expansionAdapter.getObject(num); |
165 |
if (featureType== null){ |
166 |
featureType = store.getDefaultFeatureType(); |
167 |
} |
168 |
return getCorrectFeature(feature, store,featureType);
|
169 |
} |
170 |
|
171 |
private Feature getCorrectFeature(Feature feature, FeatureStore store,
|
172 |
FeatureType featureType) throws DataException {
|
173 |
Iterator iterator=featureType.iterator();
|
174 |
EditableFeature newFeature=feature.getEditable();//store.createNewFeature(featureType, false);
|
175 |
FeatureType orgType = feature.getType(); |
176 |
int orgIndex;
|
177 |
while (iterator.hasNext()) {
|
178 |
FeatureAttributeDescriptor fad = (FeatureAttributeDescriptor) iterator.next(); |
179 |
orgIndex = orgType.getIndex(fad.getName()); |
180 |
if (orgIndex<0){ |
181 |
continue;
|
182 |
} |
183 |
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(orgIndex, value); |
192 |
} |
193 |
return newFeature.getNotEditableCopy();
|
194 |
} |
195 |
|
196 |
/**
|
197 |
* DOCUMENT ME!
|
198 |
*
|
199 |
* @param feature DOCUMENT ME!
|
200 |
* @param oldFeature DOCUMENT ME!
|
201 |
*/
|
202 |
public int update(Feature feature, Feature oldFeature) { |
203 |
int oldNum=-1; |
204 |
int num = expansionAdapter.addObject(feature);
|
205 |
FeatureReference id=feature.getReference(); |
206 |
if (added.containsKey(id)){
|
207 |
oldNum=((Integer)added.get(id)).intValue();
|
208 |
added.put(id,new Integer(num)); |
209 |
}else{
|
210 |
if (modifiedFromOriginal.get(id)!=null) { |
211 |
oldNum=((Integer)modifiedFromOriginal.get(id)).intValue();
|
212 |
} |
213 |
modifiedFromOriginal.put(id,new Integer(num)); |
214 |
} |
215 |
return oldNum;
|
216 |
} |
217 |
|
218 |
/**
|
219 |
* DOCUMENT ME!
|
220 |
*
|
221 |
* @param id DOCUMENT ME!
|
222 |
*/
|
223 |
public void restore(FeatureReference id) { |
224 |
deleted.remove(id); |
225 |
deltaSize++; |
226 |
} |
227 |
public void restore(FeatureReference id,int num){ |
228 |
if (added.containsKey(id)){
|
229 |
added.put(id,new Integer(num)); |
230 |
}else{
|
231 |
modifiedFromOriginal.put(id,new Integer(num)); |
232 |
} |
233 |
} |
234 |
|
235 |
|
236 |
public boolean isDeleted(Feature feature){ |
237 |
return deleted.contains(feature.getReference());
|
238 |
} |
239 |
|
240 |
public boolean isDeleted(FeatureReference featureID) { |
241 |
return deleted.contains(featureID);
|
242 |
} |
243 |
|
244 |
public void clear() { |
245 |
added.clear(); |
246 |
modifiedFromOriginal.clear(); |
247 |
expansionAdapter.close(); |
248 |
deleted.clear();//<FeatureID>
|
249 |
deltaSize=0;
|
250 |
} |
251 |
|
252 |
|
253 |
public boolean hasChanges() { |
254 |
return added.size()>0 || modifiedFromOriginal.size() > 0 || deleted.size() > 0; |
255 |
} |
256 |
|
257 |
|
258 |
// 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 |
|
271 |
} |
272 |
|
273 |
class DeletedIterator implements Iterator { |
274 |
private Boolean hasnext = null; |
275 |
private Iterator iter; |
276 |
private DefaultFeatureReference obj;
|
277 |
|
278 |
public DeletedIterator(){
|
279 |
iter = deleted.iterator(); |
280 |
} |
281 |
|
282 |
public boolean hasNext() { |
283 |
if (hasnext != null) { |
284 |
return hasnext.booleanValue();
|
285 |
} |
286 |
hasnext = Boolean.FALSE;
|
287 |
while (iter.hasNext()) {
|
288 |
obj = (DefaultFeatureReference) iter.next(); |
289 |
if (obj.isNewFeature()) {
|
290 |
continue;
|
291 |
} |
292 |
hasnext = Boolean.TRUE;
|
293 |
break;
|
294 |
} |
295 |
return hasnext.booleanValue();
|
296 |
} |
297 |
|
298 |
public Object next() { |
299 |
if (!hasNext()) {
|
300 |
throw new NoSuchElementException(); |
301 |
} |
302 |
|
303 |
hasnext = null;
|
304 |
return obj;
|
305 |
} |
306 |
|
307 |
public void remove() { |
308 |
throw new UnsupportedOperationException(); |
309 |
|
310 |
} |
311 |
|
312 |
} |
313 |
|
314 |
public Iterator getInserted() { |
315 |
return new InsertedIterator(); |
316 |
} |
317 |
|
318 |
class InsertedIterator implements Iterator { |
319 |
|
320 |
private Iterator addedIter; |
321 |
private DefaultFeature obj;
|
322 |
private Boolean hasnext = null; |
323 |
|
324 |
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 |
} |
341 |
} |
342 |
return hasnext.booleanValue();
|
343 |
} |
344 |
|
345 |
public Object next() { |
346 |
if (!hasNext()) {
|
347 |
throw new NoSuchElementException(); |
348 |
} |
349 |
hasnext = null;
|
350 |
return obj.getData();
|
351 |
} |
352 |
|
353 |
public void remove() { |
354 |
throw new UnsupportedOperationException(); |
355 |
|
356 |
} |
357 |
|
358 |
} |
359 |
public Iterator getUpdated() { |
360 |
return new UpdatedIterator(); |
361 |
} |
362 |
class UpdatedIterator implements Iterator{ |
363 |
private Boolean hasnext = null; |
364 |
private Iterator iter; |
365 |
private DefaultFeature obj;
|
366 |
private int pos; |
367 |
|
368 |
public UpdatedIterator() {
|
369 |
iter = expansionAdapter.iterator(); |
370 |
pos = -1;
|
371 |
} |
372 |
|
373 |
public boolean hasNext() { |
374 |
if (hasnext != null) { |
375 |
return hasnext.booleanValue();
|
376 |
} |
377 |
hasnext = Boolean.FALSE;
|
378 |
while (iter.hasNext()) {
|
379 |
pos++; |
380 |
obj = (DefaultFeature) iter.next(); |
381 |
if (deleted.contains(obj.getReference())){
|
382 |
continue;
|
383 |
}else if (!modifiedFromOriginal.containsValue(new Integer(pos))){ |
384 |
continue;
|
385 |
}else {
|
386 |
hasnext = Boolean.TRUE;
|
387 |
break;
|
388 |
} |
389 |
} |
390 |
return hasnext.booleanValue();
|
391 |
} |
392 |
|
393 |
public Object next() { |
394 |
if (!hasNext()) {
|
395 |
throw new NoSuchElementException(); |
396 |
} |
397 |
hasnext = null;
|
398 |
return obj.getData();
|
399 |
} |
400 |
|
401 |
public void remove() { |
402 |
throw new UnsupportedOperationException(); |
403 |
|
404 |
} |
405 |
} |
406 |
|
407 |
public boolean hasNews() { |
408 |
return !added.isEmpty();
|
409 |
} |
410 |
|
411 |
public long getDeltaSize() { |
412 |
return deltaSize;
|
413 |
} |
414 |
} |