Revision 43887
branches/org.gvsig.desktop-2018a/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.impl/src/main/java/org/gvsig/fmap/dal/feature/impl/editing/memory/MemoryExpansionAdapter.java | ||
---|---|---|
1 |
/** |
|
2 |
* gvSIG. Desktop Geographic Information System. |
|
3 |
* |
|
4 |
* Copyright (C) 2007-2013 gvSIG Association. |
|
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 3 |
|
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., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
* For any additional information, do not hesitate to contact us |
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com. |
|
23 |
*/ |
|
24 |
package org.gvsig.fmap.dal.feature.impl.editing.memory; |
|
25 |
|
|
26 |
import org.gvsig.fmap.dal.feature.impl.editing.memory.ExpansionAdapter; |
|
27 |
import java.util.ArrayList; |
|
28 |
import java.util.HashMap; |
|
29 |
import java.util.Iterator; |
|
30 |
import java.util.List; |
|
31 |
|
|
32 |
|
|
33 |
/** |
|
34 |
* Implementaci?n en memoria de ExpansionAdapter. |
|
35 |
* |
|
36 |
* @author Vicente Caballero Navarro |
|
37 |
*/ |
|
38 |
public class MemoryExpansionAdapter implements ExpansionAdapter { |
|
39 |
private List objects = new ArrayList(); |
|
40 |
|
|
41 |
public MemoryExpansionAdapter(){ |
|
42 |
} |
|
43 |
|
|
44 |
/* (non-Javadoc) |
|
45 |
* @see org.gvsig.fmap.dal.feature.expansionadapter.ExpansionAdapter#addFeature(org.gvsig.fmap.dal.feature.Feature) |
|
46 |
*/ |
|
47 |
public int addObject(Object obj) { |
|
48 |
// Feature feature=(Feature)obj; |
|
49 |
int newIndex = objects.size(); |
|
50 |
objects.add(obj); |
|
51 |
return newIndex; |
|
52 |
} |
|
53 |
|
|
54 |
// /* (non-Javadoc) |
|
55 |
// * @see org.gvsig.fmap.dal.feature.expansionadapter.ExpansionAdapter#modifyFeature(int, org.gvsig.fmap.dal.feature.Feature) |
|
56 |
// */ |
|
57 |
// public int updateObject(Object obj) { |
|
58 |
//// Feature feature=(Feature)obj; |
|
59 |
// objects.add(obj); |
|
60 |
// return objects.size() - 1; |
|
61 |
// } |
|
62 |
|
|
63 |
/* (non-Javadoc) |
|
64 |
* @see org.gvsig.fmap.dal.feature.expansionadapter.ExpansionAdapter#getFeature(int) |
|
65 |
*/ |
|
66 |
public Object getObject(int index){ |
|
67 |
return objects.get(index); |
|
68 |
} |
|
69 |
|
|
70 |
/* (non-Javadoc) |
|
71 |
* @see org.gvsig.fmap.dal.feature.expansionadapter.ExpansionAdapter#compact(java.util.HashMap) |
|
72 |
*/ |
|
73 |
public void compact(HashMap relations) { |
|
74 |
|
|
75 |
} |
|
76 |
|
|
77 |
/* (non-Javadoc) |
|
78 |
* @see org.gvsig.fmap.dal.feature.expansionadapter.ExpansionAdapter#deleteLastFeature() |
|
79 |
*/ |
|
80 |
public void deleteLastObject() { |
|
81 |
objects.remove(objects.size()-1); |
|
82 |
|
|
83 |
} |
|
84 |
|
|
85 |
/* (non-Javadoc) |
|
86 |
* @see org.gvsig.fmap.dal.feature.expansionadapter.ExpansionAdapter#open() |
|
87 |
*/ |
|
88 |
public void open() { |
|
89 |
// TODO Auto-generated method stub |
|
90 |
|
|
91 |
} |
|
92 |
|
|
93 |
/* (non-Javadoc) |
|
94 |
* @see org.gvsig.fmap.dal.feature.expansionadapter.ExpansionAdapter#close() |
|
95 |
*/ |
|
96 |
public void close() { |
|
97 |
objects.clear(); |
|
98 |
} |
|
99 |
|
|
100 |
/* (non-Javadoc) |
|
101 |
* @see org.gvsig.fmap.dal.feature.expansionadapter.ExpansionAdapter#getSize() |
|
102 |
*/ |
|
103 |
public int getSize() { |
|
104 |
return objects.size(); |
|
105 |
} |
|
106 |
|
|
107 |
public Iterator iterator() { |
|
108 |
return objects.iterator(); |
|
109 |
} |
|
110 |
} |
|
0 | 111 |
branches/org.gvsig.desktop-2018a/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.impl/src/main/java/org/gvsig/fmap/dal/feature/impl/editing/memory/ExpansionAdapter.java | ||
---|---|---|
1 |
/** |
|
2 |
* gvSIG. Desktop Geographic Information System. |
|
3 |
* |
|
4 |
* Copyright (C) 2007-2013 gvSIG Association. |
|
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 3 |
|
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., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
* For any additional information, do not hesitate to contact us |
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com. |
|
23 |
*/ |
|
24 |
package org.gvsig.fmap.dal.feature.impl.editing.memory; |
|
25 |
|
|
26 |
import java.util.HashMap; |
|
27 |
import java.util.Iterator; |
|
28 |
|
|
29 |
|
|
30 |
/** |
|
31 |
* Maneja el fichero de extensi?n en el que se almacenan las modificacionesy adici?nes |
|
32 |
* durante la edici?n. Los ?ndices que devuelve esta clase en sus m?todos |
|
33 |
* addFeature y modifyFeature son invariables, es decir, que si se invoca un |
|
34 |
* m?todo addFeature que retorna un 8, independientemente de las operaciones |
|
35 |
* que se realicen posteriormente, una llamada a getFeature(8) retornar? |
|
36 |
* dicha fila. Si esta geometr?a es eliminada posteriormente, se retornar? un |
|
37 |
* null. Esto ?ltimo se cumple mientras no se invoque el m?todo compact, mediante |
|
38 |
* el que se reorganizan las geometr?as dejando en el fichero s?lo las que tienen |
|
39 |
* validez en el momento de realizar la invocaci?n. |
|
40 |
* |
|
41 |
* @author Vicente Caballero Navarro |
|
42 |
* |
|
43 |
*/ |
|
44 |
public interface ExpansionAdapter { |
|
45 |
/** |
|
46 |
* A?ade una geometria al final del fichero y retorna el ?ndice que ocupa |
|
47 |
* esta geometria en el mismo |
|
48 |
* |
|
49 |
* @param feature |
|
50 |
* @param status TODO |
|
51 |
* @param indexInternalFields fields that where valid when this feature was added. |
|
52 |
* |
|
53 |
* @return calculated index of the new feature. |
|
54 |
*/ |
|
55 |
int addObject(Object obj); |
|
56 |
|
|
57 |
/** |
|
58 |
* Modifica la index-?sima geometr?a del fichero devolviendo la posici?n en |
|
59 |
* la que se pone la geometria modificada. |
|
60 |
* |
|
61 |
* @param calculated index of feature to be modified |
|
62 |
* @param feature newFeature that replaces the old feature. |
|
63 |
* |
|
64 |
* @return new calculated index of the modified feature. |
|
65 |
*/ |
|
66 |
// int updateObject(Object obj); |
|
67 |
|
|
68 |
/** |
|
69 |
* Obtiene la geometria que hay en el ?ndice 'index' o null si la geometr?a |
|
70 |
* ha sido invalidada. |
|
71 |
* |
|
72 |
* @param index caculatedIndex of the feature to be read. |
|
73 |
* @return feature |
|
74 |
*/ |
|
75 |
Object getObject(int index); |
|
76 |
|
|
77 |
/** |
|
78 |
* Realiza una compactaci?n del fichero que maneja esta clase |
|
79 |
* |
|
80 |
* @param relations DOCUMENT ME! |
|
81 |
*/ |
|
82 |
void compact(HashMap relations); |
|
83 |
|
|
84 |
/** |
|
85 |
* Mueve el puntero de escritura de manera que las siguientes escrituras |
|
86 |
* machacar?n la ?ltima fila |
|
87 |
*/ |
|
88 |
void deleteLastObject(); |
|
89 |
|
|
90 |
/** |
|
91 |
* Abre el fichero de expansi?n para comenzar la edici?n |
|
92 |
*/ |
|
93 |
void open(); |
|
94 |
|
|
95 |
/** |
|
96 |
* Cierra el fichero de expansi?n al terminar la edici?n |
|
97 |
*/ |
|
98 |
void close(); |
|
99 |
|
|
100 |
int getSize(); |
|
101 |
|
|
102 |
Iterator iterator(); |
|
103 |
} |
|
0 | 104 |
branches/org.gvsig.desktop-2018a/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.impl/src/main/java/org/gvsig/fmap/dal/feature/impl/editing/memory/FeatureManager.java | ||
---|---|---|
1 |
/** |
|
2 |
* gvSIG. Desktop Geographic Information System. |
|
3 |
* |
|
4 |
* Copyright (C) 2007-2013 gvSIG Association. |
|
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 3 |
|
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., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
* For any additional information, do not hesitate to contact us |
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com. |
|
23 |
*/ |
|
24 |
package org.gvsig.fmap.dal.feature.impl.editing.memory; |
|
25 |
|
|
26 |
import java.util.Collection; |
|
27 |
import java.util.HashMap; |
|
28 |
import java.util.Iterator; |
|
29 |
import java.util.LinkedHashMap; |
|
30 |
import java.util.LinkedHashSet; |
|
31 |
import java.util.Map; |
|
32 |
import java.util.NoSuchElementException; |
|
33 |
|
|
34 |
import org.gvsig.fmap.dal.exception.DataException; |
|
35 |
import org.gvsig.fmap.dal.feature.Feature; |
|
36 |
import org.gvsig.fmap.dal.feature.FeatureReference; |
|
37 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
|
38 |
import org.gvsig.fmap.dal.feature.FeatureType; |
|
39 |
import org.gvsig.fmap.dal.feature.impl.DefaultFeature; |
|
40 |
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureReference; |
|
41 |
|
|
42 |
|
|
43 |
public class FeatureManager { |
|
44 |
|
|
45 |
private int deltaSize; |
|
46 |
private final ExpansionAdapter expansionAdapter; |
|
47 |
private final Collection<FeatureReference> deleted; |
|
48 |
private final Map<FeatureReference, Integer> added; |
|
49 |
private final Map<FeatureReference, Integer> addedAndDeleted; |
|
50 |
private final Map<FeatureReference, Integer> modifiedFromOriginal; |
|
51 |
|
|
52 |
public FeatureManager() { |
|
53 |
deltaSize = 0; |
|
54 |
expansionAdapter = new MemoryExpansionAdapter(); |
|
55 |
deleted = new LinkedHashSet<>(); |
|
56 |
added = new LinkedHashMap<>(); |
|
57 |
addedAndDeleted = new LinkedHashMap<>(); |
|
58 |
modifiedFromOriginal = new HashMap<>(); |
|
59 |
} |
|
60 |
|
|
61 |
/** |
|
62 |
* Deletes feature from this manager. |
|
63 |
* |
|
64 |
* @param id |
|
65 |
* @return The deleted feature or null if the feature had not been edited or |
|
66 |
* previously added in the editing session |
|
67 |
*/ |
|
68 |
public Feature delete(FeatureReference id) { |
|
69 |
deleted.add(id); |
|
70 |
Integer num = added.remove(id); |
|
71 |
Feature feature = null; |
|
72 |
if (num == null || num == -1) { |
|
73 |
num = modifiedFromOriginal.remove(id); |
|
74 |
if (num != null) { |
|
75 |
feature = (Feature) expansionAdapter.getObject(num); |
|
76 |
} |
|
77 |
// if num is null here, method returns null |
|
78 |
} else { |
|
79 |
feature = (Feature) expansionAdapter.getObject(num); |
|
80 |
addedAndDeleted.put(id, num); |
|
81 |
} |
|
82 |
deltaSize--; |
|
83 |
return feature; |
|
84 |
} |
|
85 |
|
|
86 |
public void add(Feature feature) { |
|
87 |
int pos = expansionAdapter.addObject(feature); |
|
88 |
added.put(feature.getReference(), pos); |
|
89 |
deleted.remove(feature.getReference()); |
|
90 |
deltaSize++; |
|
91 |
} |
|
92 |
|
|
93 |
public Feature deleteLastFeature() { |
|
94 |
expansionAdapter.deleteLastObject(); |
|
95 |
Feature feature = (Feature) expansionAdapter.getObject(expansionAdapter.getSize() - 1); |
|
96 |
added.remove(feature.getReference()); |
|
97 |
modifiedFromOriginal.remove(feature.getReference()); |
|
98 |
deltaSize--; |
|
99 |
return feature; |
|
100 |
} |
|
101 |
|
|
102 |
/** |
|
103 |
* Returns a Feature of the default type. |
|
104 |
* |
|
105 |
* @param id the feature reference |
|
106 |
* @param store the store to get the feature from |
|
107 |
* @return a Feature with the given reference |
|
108 |
* @throws DataException if there is an error getting the Feature |
|
109 |
*/ |
|
110 |
public Feature get(FeatureReference id, FeatureStore store) |
|
111 |
throws DataException { |
|
112 |
return get(id, store, null); |
|
113 |
} |
|
114 |
|
|
115 |
/** |
|
116 |
* Returns a Feature of the given type. |
|
117 |
* |
|
118 |
* @param id the feature reference |
|
119 |
* @param store the store to get the feature from |
|
120 |
* @param featureType the type of the feature to return |
|
121 |
* @return a Feature with the given reference |
|
122 |
* @throws DataException if there is an error getting the Feature |
|
123 |
*/ |
|
124 |
public Feature get(FeatureReference id, FeatureStore store, |
|
125 |
FeatureType featureType) throws DataException { |
|
126 |
// FIXME: y si el featuretype que paso esta modificado. |
|
127 |
// Deberia buscarlo en el featuretypemanager ? |
|
128 |
// |
|
129 |
// Si no existe feature con ese id... ? retorna null ? |
|
130 |
// en EditedDefaultIterator se hace uso de ese comportamiento. |
|
131 |
// |
|
132 |
Integer intNum = (added.get(id)); |
|
133 |
if (intNum == null) { |
|
134 |
intNum = (modifiedFromOriginal.get(id)); |
|
135 |
if (intNum == null) { |
|
136 |
//If the feature has been added and deleted |
|
137 |
intNum = addedAndDeleted.get(id); |
|
138 |
if (intNum == null) { |
|
139 |
return null; |
|
140 |
} |
|
141 |
} |
|
142 |
} |
|
143 |
int num = intNum; |
|
144 |
if (num == -1) { |
|
145 |
return null; |
|
146 |
} |
|
147 |
Feature feature = (Feature) expansionAdapter.getObject(num); |
|
148 |
if (featureType == null) { |
|
149 |
featureType = store.getDefaultFeatureType(); |
|
150 |
} |
|
151 |
return new DefaultFeature(featureType, feature); |
|
152 |
} |
|
153 |
|
|
154 |
public int update(Feature feature, Feature oldFeature) { |
|
155 |
int oldNum = -1; |
|
156 |
int num = expansionAdapter.addObject(feature); |
|
157 |
FeatureReference id = feature.getReference(); |
|
158 |
if (added.containsKey(id)) { |
|
159 |
oldNum = (added.get(id)); |
|
160 |
added.put(id, num); |
|
161 |
} else { |
|
162 |
if (modifiedFromOriginal.get(id) != null) { |
|
163 |
oldNum = (modifiedFromOriginal.get(id)); |
|
164 |
} |
|
165 |
modifiedFromOriginal.put(id, num); |
|
166 |
} |
|
167 |
return oldNum; |
|
168 |
} |
|
169 |
|
|
170 |
public void restore(FeatureReference id) { |
|
171 |
deleted.remove(id); |
|
172 |
deltaSize++; |
|
173 |
} |
|
174 |
|
|
175 |
public void restore(FeatureReference id, int num) { |
|
176 |
if (added.containsKey(id)) { |
|
177 |
added.put(id, num); |
|
178 |
} else { |
|
179 |
modifiedFromOriginal.put(id, num); |
|
180 |
} |
|
181 |
} |
|
182 |
|
|
183 |
public boolean isDeleted(Feature feature) { |
|
184 |
return deleted.contains(feature.getReference()); |
|
185 |
} |
|
186 |
|
|
187 |
public boolean isDeleted(FeatureReference featureID) { |
|
188 |
return deleted.contains(featureID); |
|
189 |
} |
|
190 |
|
|
191 |
public void clear() { |
|
192 |
added.clear(); |
|
193 |
modifiedFromOriginal.clear(); |
|
194 |
expansionAdapter.close(); |
|
195 |
deleted.clear(); |
|
196 |
addedAndDeleted.clear(); |
|
197 |
deltaSize = 0; |
|
198 |
} |
|
199 |
|
|
200 |
public boolean hasChanges() { |
|
201 |
return added.size() > 0 || modifiedFromOriginal.size() > 0 || deleted.size() > 0; |
|
202 |
} |
|
203 |
|
|
204 |
public Iterator getDeleted() { |
|
205 |
return new DeletedIterator(); |
|
206 |
|
|
207 |
} |
|
208 |
|
|
209 |
private class DeletedIterator implements Iterator { |
|
210 |
|
|
211 |
private Boolean hasnext = null; |
|
212 |
private final Iterator iter; |
|
213 |
private DefaultFeatureReference obj; |
|
214 |
|
|
215 |
public DeletedIterator() { |
|
216 |
iter = deleted.iterator(); |
|
217 |
} |
|
218 |
|
|
219 |
@Override |
|
220 |
public boolean hasNext() { |
|
221 |
if (hasnext != null) { |
|
222 |
return hasnext; |
|
223 |
} |
|
224 |
hasnext = false; |
|
225 |
while (iter.hasNext()) { |
|
226 |
obj = (DefaultFeatureReference) iter.next(); |
|
227 |
if (obj.isNewFeature()) { |
|
228 |
continue; |
|
229 |
} |
|
230 |
hasnext = false; |
|
231 |
break; |
|
232 |
} |
|
233 |
return hasnext; |
|
234 |
} |
|
235 |
|
|
236 |
@Override |
|
237 |
public Object next() { |
|
238 |
if (!hasNext()) { |
|
239 |
throw new NoSuchElementException(); |
|
240 |
} |
|
241 |
hasnext = null; |
|
242 |
return obj; |
|
243 |
} |
|
244 |
|
|
245 |
@Override |
|
246 |
public void remove() { |
|
247 |
throw new UnsupportedOperationException(); |
|
248 |
} |
|
249 |
|
|
250 |
} |
|
251 |
|
|
252 |
public Iterator getInserted() { |
|
253 |
return new InsertedIterator(); |
|
254 |
} |
|
255 |
|
|
256 |
private class InsertedIterator implements Iterator { |
|
257 |
|
|
258 |
private final Iterator addedIter; |
|
259 |
private DefaultFeature obj; |
|
260 |
private Boolean hasnext = null; |
|
261 |
|
|
262 |
public InsertedIterator() { |
|
263 |
addedIter = added.values().iterator(); |
|
264 |
} |
|
265 |
|
|
266 |
@Override |
|
267 |
public boolean hasNext() { |
|
268 |
if (hasnext != null) { |
|
269 |
return hasnext; |
|
270 |
} |
|
271 |
hasnext = false; |
|
272 |
int pos; |
|
273 |
while (addedIter.hasNext()) { |
|
274 |
pos = ((Integer) addedIter.next()); |
|
275 |
obj = (DefaultFeature) expansionAdapter.getObject(pos); |
|
276 |
if (!deleted.contains(obj.getReference())) { |
|
277 |
hasnext = true; |
|
278 |
break; |
|
279 |
} |
|
280 |
} |
|
281 |
return hasnext; |
|
282 |
} |
|
283 |
|
|
284 |
@Override |
|
285 |
public Object next() { |
|
286 |
if (!hasNext()) { |
|
287 |
throw new NoSuchElementException(); |
|
288 |
} |
|
289 |
hasnext = null; |
|
290 |
return obj.getData(); |
|
291 |
} |
|
292 |
|
|
293 |
@Override |
|
294 |
public void remove() { |
|
295 |
addedIter.remove(); |
|
296 |
} |
|
297 |
|
|
298 |
} |
|
299 |
|
|
300 |
public Iterator getUpdated() { |
|
301 |
return new UpdatedIterator(); |
|
302 |
} |
|
303 |
|
|
304 |
private class UpdatedIterator implements Iterator { |
|
305 |
|
|
306 |
private Boolean hasnext = null; |
|
307 |
private final Iterator iter; |
|
308 |
private DefaultFeature obj; |
|
309 |
private int pos; |
|
310 |
|
|
311 |
public UpdatedIterator() { |
|
312 |
iter = expansionAdapter.iterator(); |
|
313 |
pos = -1; |
|
314 |
} |
|
315 |
|
|
316 |
@Override |
|
317 |
public boolean hasNext() { |
|
318 |
if (hasnext != null) { |
|
319 |
return hasnext; |
|
320 |
} |
|
321 |
hasnext = false; |
|
322 |
while (iter.hasNext()) { |
|
323 |
pos++; |
|
324 |
obj = (DefaultFeature) iter.next(); |
|
325 |
if (deleted.contains(obj.getReference())) { |
|
326 |
continue; |
|
327 |
} else if (!modifiedFromOriginal.containsValue(pos)) { |
|
328 |
continue; |
|
329 |
} else { |
|
330 |
hasnext = true; |
|
331 |
break; |
|
332 |
} |
|
333 |
} |
|
334 |
return hasnext; |
|
335 |
} |
|
336 |
|
|
337 |
@Override |
|
338 |
public Object next() { |
|
339 |
if (!hasNext()) { |
|
340 |
throw new NoSuchElementException(); |
|
341 |
} |
|
342 |
hasnext = null; |
|
343 |
return obj.getData(); |
|
344 |
} |
|
345 |
|
|
346 |
@Override |
|
347 |
public void remove() { |
|
348 |
throw new UnsupportedOperationException(); |
|
349 |
|
|
350 |
} |
|
351 |
} |
|
352 |
|
|
353 |
public boolean hasNews() { |
|
354 |
return !added.isEmpty(); |
|
355 |
} |
|
356 |
|
|
357 |
public long getDeltaSize() { |
|
358 |
return deltaSize; |
|
359 |
} |
|
360 |
|
|
361 |
/** |
|
362 |
* Indicates if any operation has comprimised the selected features. |
|
363 |
* |
|
364 |
* @return |
|
365 |
*/ |
|
366 |
public boolean isSelectionCompromised() { |
|
367 |
//Only deleted features can change order, as added features are added at the end. |
|
368 |
return deleted.size() > 0; |
|
369 |
} |
|
370 |
} |
|
0 | 371 |
branches/org.gvsig.desktop-2018a/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.impl/src/main/java/org/gvsig/fmap/dal/feature/impl/editing/memory/MemoryEditingManager.java | ||
---|---|---|
1 |
|
|
2 |
package org.gvsig.fmap.dal.feature.impl.editing.memory; |
|
3 |
|
|
4 |
import java.util.Iterator; |
|
5 |
import org.gvsig.fmap.dal.exception.DataException; |
|
6 |
import org.gvsig.fmap.dal.feature.Feature; |
|
7 |
import org.gvsig.fmap.dal.feature.FeatureReference; |
|
8 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
|
9 |
import org.gvsig.fmap.dal.feature.FeatureType; |
|
10 |
import org.gvsig.fmap.dal.feature.impl.EditingManager; |
|
11 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
|
12 |
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider; |
|
13 |
import org.gvsig.fmap.geom.primitive.Envelope; |
|
14 |
|
|
15 |
|
|
16 |
public class MemoryEditingManager implements EditingManager { |
|
17 |
|
|
18 |
private final FeatureTypeManager featureTypeManager; |
|
19 |
private final FeatureManager featureManager; |
|
20 |
private final SpatialManager spatialManager; |
|
21 |
|
|
22 |
public MemoryEditingManager(FeatureStore store, Envelope envelope) throws DataException { |
|
23 |
this.featureManager = new FeatureManager(); |
|
24 |
this.featureTypeManager = new FeatureTypeManager(store); |
|
25 |
this.spatialManager = new SpatialManager(store, envelope); |
|
26 |
} |
|
27 |
|
|
28 |
@Override |
|
29 |
public void dispose() { |
|
30 |
this.featureTypeManager.dispose(); |
|
31 |
this.spatialManager.clear(); |
|
32 |
this.featureManager.clear(); |
|
33 |
} |
|
34 |
|
|
35 |
@Override |
|
36 |
public void delete(Feature feature) { |
|
37 |
this.featureManager.delete(feature.getReference()); |
|
38 |
this.spatialManager.deleteFeature(feature); |
|
39 |
} |
|
40 |
|
|
41 |
@Override |
|
42 |
public void delete(FeatureReference featureReference) { |
|
43 |
// ???????? |
|
44 |
this.featureManager.delete(featureReference); |
|
45 |
|
|
46 |
} |
|
47 |
|
|
48 |
@Override |
|
49 |
public void add(Feature newFeature) { |
|
50 |
this.featureManager.add(newFeature); |
|
51 |
this.spatialManager.insertFeature(newFeature); |
|
52 |
} |
|
53 |
|
|
54 |
@Override |
|
55 |
public int update(Feature newFeature, Feature oldFeature) { |
|
56 |
int code = featureManager.update(newFeature, oldFeature); |
|
57 |
spatialManager.updateFeature(newFeature, oldFeature); |
|
58 |
return code; |
|
59 |
} |
|
60 |
|
|
61 |
@Override |
|
62 |
public int update(FeatureType newFeatureType, FeatureType oldFeatureType) { |
|
63 |
int code = featureTypeManager.update(oldFeatureType, oldFeatureType); |
|
64 |
return code; |
|
65 |
} |
|
66 |
|
|
67 |
|
|
68 |
@Override |
|
69 |
public boolean isSelectionCompromised() { |
|
70 |
return this.featureManager.isSelectionCompromised(); |
|
71 |
} |
|
72 |
|
|
73 |
@Override |
|
74 |
public Iterator<FeatureProvider> getDeletedFeatures() { |
|
75 |
return this.featureManager.getDeleted(); |
|
76 |
} |
|
77 |
|
|
78 |
@Override |
|
79 |
public Iterator<FeatureProvider> getInsertedFeatures() { |
|
80 |
return this.featureManager.getInserted(); |
|
81 |
} |
|
82 |
|
|
83 |
@Override |
|
84 |
public Iterator<FeatureProvider> getUpdatedFeatures() { |
|
85 |
return this.featureManager.getUpdated(); |
|
86 |
} |
|
87 |
|
|
88 |
@Override |
|
89 |
public Iterator<FeatureStoreProvider.FeatureTypeChanged> getFeatureTypesChanged() throws DataException { |
|
90 |
return this.featureTypeManager.getFeatureTypesChanged(); |
|
91 |
} |
|
92 |
|
|
93 |
@Override |
|
94 |
public FeatureType getFeatureType(String id) throws DataException { |
|
95 |
return this.featureTypeManager.getType(id); |
|
96 |
} |
|
97 |
|
|
98 |
@Override |
|
99 |
public Feature getFeature(FeatureReference reference, FeatureStore store, FeatureType type) throws DataException { |
|
100 |
return this.featureManager.get(reference, store, type); |
|
101 |
} |
|
102 |
|
|
103 |
@Override |
|
104 |
public void restore(FeatureReference featureReference) { |
|
105 |
this.featureManager.restore(featureReference); |
|
106 |
} |
|
107 |
|
|
108 |
@Override |
|
109 |
public void restore(FeatureReference featureReference, int code) { |
|
110 |
this.featureManager.restore(featureReference, code); |
|
111 |
} |
|
112 |
|
|
113 |
@Override |
|
114 |
public void restore(String featureTypeId) { |
|
115 |
this.featureTypeManager.restore(featureTypeId); |
|
116 |
} |
|
117 |
|
|
118 |
@Override |
|
119 |
public void restore(String featureTypeId, int code) { |
|
120 |
this.featureTypeManager.restore(featureTypeId, code); |
|
121 |
} |
|
122 |
|
|
123 |
@Override |
|
124 |
public boolean isDeleted(Feature feature) { |
|
125 |
return this.featureManager.isDeleted(feature); |
|
126 |
} |
|
127 |
|
|
128 |
@Override |
|
129 |
public boolean isDeleted(FeatureReference featureReference) { |
|
130 |
return this.featureManager.isDeleted(featureReference); |
|
131 |
} |
|
132 |
|
|
133 |
@Override |
|
134 |
public boolean isDeleted(FeatureType type) { |
|
135 |
return this.featureTypeManager.isDeleted(type); |
|
136 |
} |
|
137 |
|
|
138 |
@Override |
|
139 |
public boolean isDeleted(String featureTypeId) { |
|
140 |
return this.featureTypeManager.isDeleted(featureTypeId); |
|
141 |
} |
|
142 |
|
|
143 |
} |
branches/org.gvsig.desktop-2018a/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.impl/src/main/java/org/gvsig/fmap/dal/feature/impl/editing/memory/FeatureTypeManager.java | ||
---|---|---|
1 |
/** |
|
2 |
* gvSIG. Desktop Geographic Information System. |
|
3 |
* |
|
4 |
* Copyright (C) 2007-2013 gvSIG Association. |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or modify it under |
|
7 |
* the terms of the GNU General Public License as published by the Free Software |
|
8 |
* Foundation; either version 3 of the License, or (at your option) any later |
|
9 |
* version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
13 |
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
|
14 |
* details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License along with |
|
17 |
* this program; if not, write to the Free Software Foundation, Inc., 51 |
|
18 |
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
19 |
* |
|
20 |
* For any additional information, do not hesitate to contact us at info AT |
|
21 |
* gvsig.com, or visit our website www.gvsig.com. |
|
22 |
*/ |
|
23 |
package org.gvsig.fmap.dal.feature.impl.editing.memory; |
|
24 |
|
|
25 |
import java.lang.ref.WeakReference; |
|
26 |
import java.util.ArrayList; |
|
27 |
import java.util.Arrays; |
|
28 |
import java.util.HashMap; |
|
29 |
import java.util.Iterator; |
|
30 |
import java.util.List; |
|
31 |
import java.util.Map; |
|
32 |
|
|
33 |
import org.gvsig.fmap.dal.exception.DataException; |
|
34 |
import org.gvsig.fmap.dal.feature.AbstractFeatureStoreTransform; |
|
35 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
|
36 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
|
37 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
|
38 |
import org.gvsig.fmap.dal.feature.Feature; |
|
39 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
|
40 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
|
41 |
import org.gvsig.fmap.dal.feature.FeatureStoreTransform; |
|
42 |
import org.gvsig.fmap.dal.feature.FeatureType; |
|
43 |
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureStoreTransforms; |
|
44 |
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider.FeatureTypeChanged; |
|
45 |
import org.gvsig.tools.persistence.PersistentState; |
|
46 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
|
47 |
|
|
48 |
public class FeatureTypeManager { |
|
49 |
|
|
50 |
private final ExpansionAdapter expansionAdapter; |
|
51 |
private final Map<String,Integer> added = new HashMap(); |
|
52 |
private final Map<String,Integer> modifiedFromOriginal = new HashMap(); |
|
53 |
private final List<String> deleted = new ArrayList(); |
|
54 |
|
|
55 |
private final FeatureTypeManagerFeatureStoreTransforms transforms; |
|
56 |
private final FeatureStore store; |
|
57 |
|
|
58 |
private int deltaSize = 0; |
|
59 |
private boolean first = true; |
|
60 |
private FeatureType originalType = null; |
|
61 |
|
|
62 |
public FeatureTypeManager(FeatureStore store) { |
|
63 |
this.expansionAdapter = new MemoryExpansionAdapter(); |
|
64 |
this.store = store; |
|
65 |
this.transforms = new FeatureTypeManagerFeatureStoreTransforms(); |
|
66 |
this.transforms.setFeatureStore(store); |
|
67 |
} |
|
68 |
|
|
69 |
public void dispose() { |
|
70 |
this.expansionAdapter.close(); |
|
71 |
this.deleted.clear(); |
|
72 |
this.transforms.clear(); |
|
73 |
this.added.clear(); |
|
74 |
this.modifiedFromOriginal.clear(); |
|
75 |
} |
|
76 |
|
|
77 |
public FeatureType getType(String id) throws DataException { |
|
78 |
Integer num = added.get(id); |
|
79 |
if (num == null) { |
|
80 |
num = modifiedFromOriginal.get(id); |
|
81 |
if (num == null) { |
|
82 |
return null; |
|
83 |
} |
|
84 |
} |
|
85 |
if (num == -1) { |
|
86 |
/* |
|
87 |
* This happens for example when we are going back to the |
|
88 |
* original feature type which is not managed by |
|
89 |
* expansionAdapter |
|
90 |
*/ |
|
91 |
return null; |
|
92 |
} |
|
93 |
FeatureType type = (FeatureType) expansionAdapter.getObject(num); |
|
94 |
return type; |
|
95 |
} |
|
96 |
|
|
97 |
public int update(FeatureType type, FeatureType oldType) { |
|
98 |
// deleted.add(oldType.getId()); |
|
99 |
if (first) { |
|
100 |
originalType = oldType; |
|
101 |
first = false; |
|
102 |
} |
|
103 |
int oldNum = -1; |
|
104 |
int num = expansionAdapter.addObject(type); |
|
105 |
String id = type.getId(); |
|
106 |
|
|
107 |
if (added.containsKey(id)) { |
|
108 |
oldNum = added.get(id); |
|
109 |
added.put(id, num); |
|
110 |
} else { |
|
111 |
if (modifiedFromOriginal.get(id) != null) { |
|
112 |
oldNum = modifiedFromOriginal.get(id); |
|
113 |
} |
|
114 |
modifiedFromOriginal.put(id, num); |
|
115 |
} |
|
116 |
|
|
117 |
try { |
|
118 |
this.transforms.add(new UpdateFeatureTypeTransform(this.store, |
|
119 |
oldType, type)); |
|
120 |
} catch (DataException e) { |
|
121 |
throw new RuntimeException(); // FIXME (pero esto no deberia de |
|
122 |
// pasar nunca) |
|
123 |
} |
|
124 |
return oldNum; |
|
125 |
} |
|
126 |
|
|
127 |
private class UpdateFeatureTypeTransform extends AbstractFeatureStoreTransform { |
|
128 |
|
|
129 |
private final FeatureType ftSource; |
|
130 |
|
|
131 |
private EditableFeatureType ftTarget_editable; |
|
132 |
private final FeatureType ftTarget_non_editable; |
|
133 |
|
|
134 |
private WeakReference wkRefStore; |
|
135 |
private List ftypes = null; |
|
136 |
private List attrInSourceToUse; |
|
137 |
|
|
138 |
UpdateFeatureTypeTransform(FeatureStore featureStore, |
|
139 |
FeatureType ftSource, FeatureType ftTarget) { |
|
140 |
this.ftSource = ftSource; |
|
141 |
|
|
142 |
if (ftTarget instanceof EditableFeatureType) { |
|
143 |
|
|
144 |
ftTarget_editable = (EditableFeatureType) ftTarget; |
|
145 |
ftTarget_non_editable = ftTarget_editable.getNotEditableCopy(); |
|
146 |
} else { |
|
147 |
ftTarget_non_editable = ftTarget; |
|
148 |
} |
|
149 |
|
|
150 |
this.wkRefStore = new WeakReference(featureStore); |
|
151 |
this.initializeAttributesToUse(); |
|
152 |
} |
|
153 |
|
|
154 |
private void initializeAttributesToUse() { |
|
155 |
attrInSourceToUse = new ArrayList(); |
|
156 |
|
|
157 |
Iterator iter; |
|
158 |
if (ftTarget_editable != null) { |
|
159 |
iter = ftTarget_editable.iterator(); |
|
160 |
} else { |
|
161 |
iter = ftTarget_non_editable.iterator(); |
|
162 |
} |
|
163 |
|
|
164 |
FeatureAttributeDescriptor tAttr, sAttr; |
|
165 |
EditableFeatureAttributeDescriptor ead; |
|
166 |
while (iter.hasNext()) { |
|
167 |
tAttr = (FeatureAttributeDescriptor) iter.next(); |
|
168 |
sAttr = this.ftSource.getAttributeDescriptor(tAttr.getName()); |
|
169 |
if (sAttr == null) { |
|
170 |
if (tAttr instanceof EditableFeatureAttributeDescriptor) { |
|
171 |
ead = (EditableFeatureAttributeDescriptor) tAttr; |
|
172 |
if (ead.getOriginalName() != null) { |
|
173 |
sAttr = this.ftSource.getAttributeDescriptor(ead.getOriginalName()); |
|
174 |
if (sAttr == null) { |
|
175 |
continue; |
|
176 |
} |
|
177 |
} else { |
|
178 |
continue; |
|
179 |
} |
|
180 |
} else { |
|
181 |
continue; |
|
182 |
} |
|
183 |
} |
|
184 |
if (tAttr.getType() != sAttr.getType()) { |
|
185 |
/* |
|
186 |
* Ignore if type is not the same (user removed field |
|
187 |
* and added another with same name but different type) |
|
188 |
*/ |
|
189 |
continue; |
|
190 |
} |
|
191 |
attrInSourceToUse.add(sAttr.getName()); |
|
192 |
} |
|
193 |
} |
|
194 |
|
|
195 |
@Override |
|
196 |
public void setUp() throws Exception { |
|
197 |
|
|
198 |
} |
|
199 |
|
|
200 |
@Override |
|
201 |
public void applyTransform(Feature source, EditableFeature target) |
|
202 |
throws DataException { |
|
203 |
|
|
204 |
Iterator iter = target.getType().iterator(); |
|
205 |
FeatureAttributeDescriptor tAttr; |
|
206 |
FeatureAttributeDescriptor tAttr_edi; |
|
207 |
FeatureAttributeDescriptor srcAtt; |
|
208 |
|
|
209 |
/* |
|
210 |
* field name in source feature |
|
211 |
*/ |
|
212 |
String s_name; |
|
213 |
|
|
214 |
/* |
|
215 |
* field name in target feature (the same as in source |
|
216 |
* except if renamed) |
|
217 |
*/ |
|
218 |
String t_name; |
|
219 |
|
|
220 |
EditableFeatureAttributeDescriptor eatd; |
|
221 |
while (iter.hasNext()) { |
|
222 |
tAttr = (FeatureAttributeDescriptor) iter.next(); |
|
223 |
|
|
224 |
if (ftTarget_editable != null) { |
|
225 |
/* |
|
226 |
* If target FT is editable, try to get original name |
|
227 |
*/ |
|
228 |
t_name = tAttr.getName(); |
|
229 |
s_name = t_name; |
|
230 |
tAttr_edi = ftTarget_editable.getAttributeDescriptor(t_name); |
|
231 |
if (tAttr_edi instanceof EditableFeatureAttributeDescriptor) { |
|
232 |
eatd = (EditableFeatureAttributeDescriptor) tAttr_edi; |
|
233 |
s_name = eatd.getOriginalName(); |
|
234 |
} |
|
235 |
|
|
236 |
/* |
|
237 |
* If not found, use normal name |
|
238 |
*/ |
|
239 |
if (s_name == null) { |
|
240 |
s_name = tAttr.getName(); |
|
241 |
} |
|
242 |
} else { |
|
243 |
/* |
|
244 |
* If target FT is not editable, use normal name |
|
245 |
*/ |
|
246 |
t_name = tAttr.getName(); |
|
247 |
s_name = t_name; |
|
248 |
} |
|
249 |
|
|
250 |
srcAtt = source.getType().getAttributeDescriptor(s_name); |
|
251 |
|
|
252 |
if (srcAtt != null |
|
253 |
&& /* |
|
254 |
* This prevents the case when user has removed field and |
|
255 |
* added new field with same name and different type. |
|
256 |
* In that case, value will be the default value (else below) |
|
257 |
*/ srcAtt.getType() == tAttr.getType()) { |
|
258 |
|
|
259 |
target.set(t_name, source.get(s_name)); |
|
260 |
|
|
261 |
} else { |
|
262 |
target.set(t_name, tAttr.getDefaultValue()); |
|
263 |
} |
|
264 |
} |
|
265 |
} |
|
266 |
|
|
267 |
@Override |
|
268 |
public FeatureType getDefaultFeatureType() throws DataException { |
|
269 |
return this.ftTarget_non_editable; |
|
270 |
} |
|
271 |
|
|
272 |
@Override |
|
273 |
public FeatureStore getFeatureStore() { |
|
274 |
return (FeatureStore) this.wkRefStore.get(); |
|
275 |
} |
|
276 |
|
|
277 |
@Override |
|
278 |
public List getFeatureTypes() throws DataException { |
|
279 |
if (this.ftypes == null) { |
|
280 |
this.ftypes = Arrays |
|
281 |
.asList(new FeatureType[]{this.ftTarget_non_editable}); |
|
282 |
} |
|
283 |
return this.ftypes; |
|
284 |
} |
|
285 |
|
|
286 |
@Override |
|
287 |
public FeatureType getSourceFeatureTypeFrom( |
|
288 |
FeatureType targetFeatureType) { |
|
289 |
EditableFeatureType orgType = ftSource.getEditable(); |
|
290 |
Iterator iter = orgType.iterator(); |
|
291 |
FeatureAttributeDescriptor attr; |
|
292 |
EditableFeatureAttributeDescriptor efad; |
|
293 |
|
|
294 |
while (iter.hasNext()) { |
|
295 |
attr = (FeatureAttributeDescriptor) iter.next(); |
|
296 |
if (!attrInSourceToUse.contains(attr.getName())) { |
|
297 |
if (attr instanceof EditableFeatureAttributeDescriptor) { |
|
298 |
efad = (EditableFeatureAttributeDescriptor) attr; |
|
299 |
if (efad.getOriginalName() != null |
|
300 |
&& !attrInSourceToUse.contains(efad.getOriginalName())) { |
|
301 |
iter.remove(); |
|
302 |
} |
|
303 |
} else { |
|
304 |
iter.remove(); |
|
305 |
} |
|
306 |
} |
|
307 |
} |
|
308 |
return orgType.getNotEditableCopy(); |
|
309 |
} |
|
310 |
|
|
311 |
@Override |
|
312 |
public void setFeatureStore(FeatureStore featureStore) { |
|
313 |
this.wkRefStore = new WeakReference(featureStore); |
|
314 |
} |
|
315 |
|
|
316 |
@Override |
|
317 |
public boolean isTransformsOriginalValues() { |
|
318 |
return false; |
|
319 |
} |
|
320 |
|
|
321 |
} |
|
322 |
|
|
323 |
public void restore(String id) { |
|
324 |
deleted.remove(id); |
|
325 |
deltaSize++; |
|
326 |
} |
|
327 |
|
|
328 |
public void restore(String id, int num) { |
|
329 |
if (added.containsKey(id)) { |
|
330 |
added.put(id, num); |
|
331 |
} else { |
|
332 |
modifiedFromOriginal.put(id, num); |
|
333 |
} |
|
334 |
} |
|
335 |
|
|
336 |
public boolean isDeleted(FeatureType type) { |
|
337 |
return deleted.contains(type.getId()); |
|
338 |
} |
|
339 |
|
|
340 |
public boolean isDeleted(String id) { |
|
341 |
return deleted.contains(id); |
|
342 |
} |
|
343 |
|
|
344 |
public void clear() { |
|
345 |
added.clear(); |
|
346 |
modifiedFromOriginal.clear(); |
|
347 |
expansionAdapter.close(); |
|
348 |
deleted.clear(); |
|
349 |
deltaSize = 0; |
|
350 |
} |
|
351 |
|
|
352 |
public boolean hasChanges() { |
|
353 |
return added.size() > 0 || modifiedFromOriginal.size() > 0 |
|
354 |
|| deleted.size() > 0; |
|
355 |
} |
|
356 |
|
|
357 |
public Iterator newsIterator() { |
|
358 |
return added.values().iterator(); |
|
359 |
} |
|
360 |
|
|
361 |
public boolean hasNews() { |
|
362 |
return !added.isEmpty(); |
|
363 |
} |
|
364 |
|
|
365 |
public long getDeltaSize() { |
|
366 |
return deltaSize; |
|
367 |
} |
|
368 |
|
|
369 |
public FeatureType getOriginalFeatureType() { |
|
370 |
return originalType; |
|
371 |
} |
|
372 |
|
|
373 |
public DefaultFeatureStoreTransforms getTransforms() { |
|
374 |
return this.transforms; |
|
375 |
} |
|
376 |
|
|
377 |
public class FeatureTypeManagerFeatureStoreTransforms extends |
|
378 |
DefaultFeatureStoreTransforms { |
|
379 |
|
|
380 |
private FeatureTypeManagerFeatureStoreTransforms() { |
|
381 |
|
|
382 |
} |
|
383 |
|
|
384 |
@Override |
|
385 |
protected void checkEditingMode() { |
|
386 |
} |
|
387 |
|
|
388 |
@Override |
|
389 |
protected void notifyChangeToStore() { |
|
390 |
} |
|
391 |
|
|
392 |
public PersistentState getState() throws PersistenceException { |
|
393 |
// FIXME |
|
394 |
throw new UnsupportedOperationException(); |
|
395 |
} |
|
396 |
|
|
397 |
public void loadState(PersistentState state) |
|
398 |
throws PersistenceException { |
|
399 |
// FIXME |
|
400 |
throw new UnsupportedOperationException(); |
|
401 |
} |
|
402 |
|
|
403 |
@Override |
|
404 |
public void loadFromState(PersistentState state) throws PersistenceException { |
|
405 |
// FIXME |
|
406 |
throw new UnsupportedOperationException(); |
|
407 |
} |
|
408 |
|
|
409 |
@Override |
|
410 |
public FeatureStoreTransform add(FeatureStoreTransform transform) |
|
411 |
throws DataException { |
|
412 |
if (!(transform instanceof UpdateFeatureTypeTransform)) { |
|
413 |
// FIXME |
|
414 |
throw new IllegalArgumentException(); |
|
415 |
} |
|
416 |
return super.add(transform); |
|
417 |
} |
|
418 |
|
|
419 |
} |
|
420 |
|
|
421 |
public class FeatureTypesChangedItem implements FeatureTypeChanged { |
|
422 |
|
|
423 |
private final FeatureType source; |
|
424 |
private final FeatureType target; |
|
425 |
|
|
426 |
public FeatureTypesChangedItem(FeatureType source, FeatureType target) { |
|
427 |
this.source = source; |
|
428 |
this.target = target; |
|
429 |
} |
|
430 |
|
|
431 |
@Override |
|
432 |
public FeatureType getSource() { |
|
433 |
return source; |
|
434 |
} |
|
435 |
|
|
436 |
@Override |
|
437 |
public FeatureType getTarget() { |
|
438 |
return target; |
|
439 |
} |
|
440 |
|
|
441 |
} |
|
442 |
|
|
443 |
public Iterator getFeatureTypesChanged() throws DataException { |
|
444 |
// FIXME this don't work for Store.fType.size() > 1 |
|
445 |
List list = new ArrayList(); |
|
446 |
if (modifiedFromOriginal.size() > 0) { |
|
447 |
FeatureType src = this.getOriginalFeatureType(); |
|
448 |
list.add(new FeatureTypesChangedItem(src, this.store |
|
449 |
.getFeatureType(src.getId()))); |
|
450 |
} |
|
451 |
return list.iterator(); |
|
452 |
} |
|
453 |
|
|
454 |
} |
|
0 | 455 |
branches/org.gvsig.desktop-2018a/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.impl/src/main/java/org/gvsig/fmap/dal/feature/impl/editing/memory/SpatialManager.java | ||
---|---|---|
1 |
/** |
|
2 |
* gvSIG. Desktop Geographic Information System. |
|
3 |
* |
|
4 |
* Copyright (C) 2007-2013 gvSIG Association. |
|
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 3 |
|
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., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
* For any additional information, do not hesitate to contact us |
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com. |
|
23 |
*/ |
|
24 |
package org.gvsig.fmap.dal.feature.impl.editing.memory; |
|
25 |
|
|
26 |
import java.util.ArrayList; |
|
27 |
import java.util.ConcurrentModificationException; |
|
28 |
import java.util.Iterator; |
|
29 |
|
|
30 |
import org.gvsig.fmap.dal.exception.DataException; |
|
31 |
import org.gvsig.fmap.dal.feature.Feature; |
|
32 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
|
33 |
import org.gvsig.fmap.dal.feature.FeatureIndex; |
|
34 |
import org.gvsig.fmap.dal.feature.FeatureIndexes; |
|
35 |
import org.gvsig.fmap.dal.feature.FeatureReference; |
|
36 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
|
37 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
|
38 |
import org.gvsig.fmap.dal.feature.FeatureType; |
|
39 |
import org.gvsig.fmap.dal.feature.exception.ConcurrentDataModificationException; |
|
40 |
import org.gvsig.fmap.dal.feature.impl.DefaultFeature; |
|
41 |
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureIndex; |
|
42 |
import org.gvsig.fmap.geom.GeometryLocator; |
|
43 |
import org.gvsig.fmap.geom.primitive.Envelope; |
|
44 |
import org.gvsig.tools.dispose.DisposableIterator; |
|
45 |
import org.slf4j.Logger; |
|
46 |
import org.slf4j.LoggerFactory; |
|
47 |
|
|
48 |
/** |
|
49 |
* DOCUMENT ME! |
|
50 |
* |
|
51 |
* @author Vicente Caballero Navarro |
|
52 |
*/ |
|
53 |
public class SpatialManager { |
|
54 |
private static final Logger LOG = LoggerFactory.getLogger(SpatialManager.class); |
|
55 |
|
|
56 |
protected boolean isFullExtentDirty = true; |
|
57 |
private FeatureStore featureStore; |
|
58 |
private FeatureIndex featureIndex = null; |
|
59 |
private Envelope originalEnvelope = null; |
|
60 |
private Envelope fullEnvelope = null; |
|
61 |
private ArrayList feaOperation=new ArrayList(); |
|
62 |
private boolean noSpatialData = false; |
|
63 |
|
|
64 |
public SpatialManager(FeatureStore featureStore, Envelope originalEnvelope) |
|
65 |
throws DataException { |
|
66 |
this.featureStore=featureStore; |
|
67 |
FeatureIndexes findexes=featureStore.getIndexes(); |
|
68 |
// Comprobamos si hay algun campo espacial a manejar |
|
69 |
|
|
70 |
FeatureType fType = this.featureStore.getDefaultFeatureType(); |
|
71 |
// TODO Multy FType !! |
|
72 |
if (fType.getDefaultGeometryAttributeIndex() < 0) { |
|
73 |
noSpatialData = true; |
|
74 |
return; |
|
75 |
} |
|
76 |
FeatureAttributeDescriptor attr = fType.getAttributeDescriptor(fType |
|
77 |
.getDefaultGeometryAttributeIndex()); |
|
78 |
this.originalEnvelope = originalEnvelope; |
|
79 |
if ((originalEnvelope != null) && (!originalEnvelope.isEmpty())) { |
|
80 |
this.fullEnvelope = originalEnvelope.getGeometry().getEnvelope(); |
|
81 |
} else { |
|
82 |
FeatureAttributeDescriptor geoAttr = fType.getAttributeDescriptor(fType.getDefaultGeometryAttributeIndex()); |
|
83 |
try { |
|
84 |
this.fullEnvelope = GeometryLocator.getGeometryManager() |
|
85 |
.createEnvelope(geoAttr.getGeometrySubType()); |
|
86 |
} catch (Exception e) { |
|
87 |
// FIXME Excpetion |
|
88 |
throw new RuntimeException(e); |
|
89 |
} |
|
90 |
} |
|
91 |
if (!fType.hasOID()) { |
|
92 |
return; |
|
93 |
} |
|
94 |
|
|
95 |
Iterator iterator = findexes.iterator(); |
|
96 |
FeatureIndex index; |
|
97 |
while (iterator.hasNext()) { |
|
98 |
index = (FeatureIndex) iterator.next(); |
|
99 |
if (index.getAttributeNames().size() == 1 |
|
100 |
&& index.getAttributeNames().contains(attr.getName())) { |
|
101 |
featureIndex = index; |
|
102 |
break; |
|
103 |
} |
|
104 |
} |
|
105 |
|
|
106 |
if (featureIndex instanceof DefaultFeatureIndex) { |
|
107 |
((DefaultFeatureIndex) featureIndex).setValid(true); |
|
108 |
} |
|
109 |
} |
|
110 |
|
|
111 |
|
|
112 |
|
|
113 |
/** |
|
114 |
* DOCUMENT ME! |
|
115 |
* |
|
116 |
* @param feature DOCUMENT ME! |
|
117 |
* @param oldFeature DOCUMENT ME! |
|
118 |
*/ |
|
119 |
public void updateFeature(Feature feature, Feature oldFeature) { |
|
120 |
if (noSpatialData) { |
|
121 |
return; |
|
122 |
} |
|
123 |
try { |
|
124 |
if (featureIndex != null) { |
|
125 |
featureIndex.delete(oldFeature); |
|
126 |
} |
|
127 |
feaOperation.add(new FeatureOperation(((DefaultFeature) oldFeature) |
|
128 |
.getReference(), FeatureOperation.DELETE)); |
|
129 |
if (featureIndex != null) { |
|
130 |
featureIndex.insert(feature); |
|
131 |
} |
|
132 |
feaOperation.add(new FeatureOperation(((DefaultFeature) feature) |
|
133 |
.getReference(), FeatureOperation.INSERT)); |
|
134 |
} catch (DataException e) { |
|
135 |
throw new RuntimeException("Exception updating feature: " |
|
136 |
+ feature, e); |
|
137 |
} |
|
138 |
// } else { |
|
139 |
// fullEnvelope.add(feature.getDefaultEnvelope()); |
|
140 |
isFullExtentDirty = true; |
|
141 |
} |
|
142 |
|
|
143 |
/** |
|
144 |
* DOCUMENT ME! |
|
145 |
* |
|
146 |
* @param feature DOCUMENT ME! |
|
147 |
*/ |
|
148 |
public void insertFeature(Feature feature) { |
|
149 |
if (noSpatialData) { |
|
150 |
return; |
|
151 |
} |
|
152 |
try { |
|
153 |
if (featureIndex != null) { |
|
154 |
featureIndex.insert(feature); |
|
155 |
} |
|
156 |
feaOperation.add(new FeatureOperation( |
|
157 |
((DefaultFeature) feature).getReference(), |
|
158 |
FeatureOperation.INSERT)); |
|
159 |
} catch (DataException e) { |
|
160 |
throw new RuntimeException("Exception inserting feature: " |
|
161 |
+ feature, e); |
|
162 |
} |
|
163 |
isFullExtentDirty = true; |
|
164 |
// } else if (!isFullExtentDirty) { |
|
165 |
// fullEnvelope.add(feature.getDefaultEnvelope()); |
|
166 |
// } |
|
167 |
} |
|
168 |
|
|
169 |
/** |
|
170 |
* DOCUMENT ME! |
|
171 |
* |
|
172 |
* @param feature DOCUMENT ME! |
|
173 |
*/ |
|
174 |
public void deleteFeature(Feature feature) { |
|
175 |
if (noSpatialData) { |
|
176 |
return; |
|
177 |
} |
|
178 |
try { |
|
179 |
if (featureIndex != null) { |
|
180 |
featureIndex.delete(feature); |
|
181 |
} |
|
182 |
feaOperation.add(new FeatureOperation(((DefaultFeature) feature) |
|
183 |
.getReference(), FeatureOperation.DELETE)); |
|
184 |
} catch (DataException e) { |
|
185 |
throw new RuntimeException("Exception deleting feature: " |
|
186 |
+ feature, e); |
|
187 |
} |
|
188 |
isFullExtentDirty = true; |
|
189 |
} |
|
190 |
|
|
191 |
public void clear() { |
|
192 |
} |
|
193 |
|
|
194 |
public Envelope getEnvelope() throws DataException { |
|
195 |
if (noSpatialData) { |
|
196 |
return null; |
|
197 |
} |
|
198 |
if (!isFullExtentDirty){ |
|
199 |
return this.fullEnvelope; |
|
200 |
} |
|
201 |
|
|
202 |
// FIXME in every changes when anyone ask for envelope it was regenerated. |
|
203 |
// if we assume that the envelope may not be the minimum in edit mode |
|
204 |
// this call must be very much faster |
|
205 |
|
|
206 |
|
|
207 |
FeatureAttributeDescriptor attr = featureStore.getDefaultFeatureType() |
|
208 |
.getAttributeDescriptor( |
|
209 |
featureStore.getDefaultFeatureType() |
|
210 |
.getDefaultGeometryAttributeIndex()); |
|
211 |
Envelope fullEnvelope = null; |
|
212 |
|
|
213 |
FeatureSet set = null; |
|
214 |
DisposableIterator iterator = null; |
|
215 |
try { |
|
216 |
set = featureStore.getFeatureSet(); |
|
217 |
iterator = set.fastIterator(); |
|
218 |
//First while to initialize the feature envelope |
|
219 |
while (iterator.hasNext()) { |
|
220 |
Feature feature = (Feature) iterator.next(); |
|
221 |
Envelope envelope = feature.getDefaultEnvelope(); |
|
222 |
if (envelope != null){ |
|
223 |
fullEnvelope = (Envelope)envelope.clone(); |
|
224 |
break; |
|
225 |
} |
|
226 |
} |
|
227 |
//Second while to add new evelopes tho the full envelope |
|
228 |
while (iterator.hasNext()) { |
|
229 |
Feature feature = (Feature) iterator.next(); |
|
230 |
Envelope envelope = feature.getDefaultEnvelope(); |
|
231 |
if(envelope!=null){ |
|
232 |
fullEnvelope.add(envelope); |
|
233 |
} |
|
234 |
} |
|
235 |
//Creating an empty envelope by default |
|
236 |
if (fullEnvelope == null){ |
|
237 |
fullEnvelope = GeometryLocator.getGeometryManager().createEnvelope( |
|
238 |
attr.getGeometrySubType()); |
|
239 |
} |
|
240 |
} catch (ConcurrentModificationException e) { |
|
241 |
throw e; |
|
242 |
} catch (ConcurrentDataModificationException e) { |
|
243 |
throw e; |
|
244 |
} catch (Exception e) { |
|
245 |
throw new RuntimeException(e); |
|
246 |
} finally { |
|
247 |
if (iterator != null) { |
|
248 |
iterator.dispose(); |
|
249 |
} |
|
250 |
if (set != null) { |
|
251 |
set.dispose(); |
|
252 |
} |
|
253 |
} |
|
254 |
this.fullEnvelope = fullEnvelope; |
|
255 |
this.isFullExtentDirty = false; |
|
256 |
return fullEnvelope; |
|
257 |
} |
|
258 |
|
|
259 |
|
|
260 |
|
|
261 |
public void cancelModifies() { |
|
262 |
if (noSpatialData) { |
|
263 |
return; |
|
264 |
} |
|
265 |
if (featureIndex != null){ |
|
266 |
for (int i = feaOperation.size()-1 ; i>=0 ; i--){ |
|
267 |
try { |
Also available in: Unified diff