svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dal / src / org / gvsig / fmap / dal / feature / impl / FeatureTypeManager.java @ 36233
History | View | Annotate | Download (11.3 KB)
1 | 24143 | vcaballero | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
|
||
4 | *
|
||
5 | * This program is free software; you can redistribute it and/or
|
||
6 | * modify it under the terms of the GNU General Public License
|
||
7 | * as published by the Free Software Foundation; either version 2
|
||
8 | * of the License, or (at your option) any later version.
|
||
9 | *
|
||
10 | * This program is distributed in the hope that it will be useful,
|
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
13 | * GNU General Public License for more details.
|
||
14 | *
|
||
15 | * You should have received a copy of the GNU General Public License
|
||
16 | * along with this program; if not, write to the Free Software
|
||
17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
18 | *
|
||
19 | * For more information, contact:
|
||
20 | *
|
||
21 | * Generalitat Valenciana
|
||
22 | * Conselleria d'Infraestructures i Transport
|
||
23 | * Av. Blasco Ib??ez, 50
|
||
24 | * 46010 VALENCIA
|
||
25 | * SPAIN
|
||
26 | *
|
||
27 | * +34 963862235
|
||
28 | * gvsig@gva.es
|
||
29 | * www.gvsig.gva.es
|
||
30 | *
|
||
31 | * or
|
||
32 | *
|
||
33 | * IVER T.I. S.A
|
||
34 | * Salamanca 50
|
||
35 | * 46005 Valencia
|
||
36 | * Spain
|
||
37 | *
|
||
38 | * +34 963163400
|
||
39 | * dac@iver.es
|
||
40 | */
|
||
41 | |||
42 | 24496 | jmvivo | package org.gvsig.fmap.dal.feature.impl; |
43 | 24143 | vcaballero | |
44 | 26252 | jmvivo | import java.lang.ref.WeakReference; |
45 | 24143 | vcaballero | import java.util.ArrayList; |
46 | 26252 | jmvivo | import java.util.Arrays; |
47 | 24143 | vcaballero | import java.util.HashMap; |
48 | import java.util.Iterator; |
||
49 | 26252 | jmvivo | import java.util.List; |
50 | 24143 | vcaballero | |
51 | 24505 | jmvivo | import org.gvsig.fmap.dal.exception.DataException; |
52 | 26252 | jmvivo | import org.gvsig.fmap.dal.feature.EditableFeature; |
53 | import org.gvsig.fmap.dal.feature.EditableFeatureType; |
||
54 | import org.gvsig.fmap.dal.feature.Feature; |
||
55 | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
||
56 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
57 | import org.gvsig.fmap.dal.feature.FeatureStoreTransform; |
||
58 | 24496 | jmvivo | import org.gvsig.fmap.dal.feature.FeatureType; |
59 | import org.gvsig.fmap.dal.feature.impl.expansionadapter.ExpansionAdapter; |
||
60 | 29289 | jmvivo | import org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider.FeatureTypeChanged; |
61 | 26252 | jmvivo | import org.gvsig.tools.persistence.PersistentState; |
62 | 32880 | jjdelcerro | import org.gvsig.tools.persistence.exception.PersistenceException; |
63 | 24143 | vcaballero | |
64 | /**
|
||
65 | * DOCUMENT ME!
|
||
66 | *
|
||
67 | * @author Vicente Caballero Navarro
|
||
68 | */
|
||
69 | public class FeatureTypeManager { |
||
70 | 26252 | jmvivo | private ExpansionAdapter expansionAdapter;
|
71 | private ArrayList deleted = new ArrayList();// <FeatureID> |
||
72 | private int deltaSize = 0; |
||
73 | private HashMap added = new HashMap(); |
||
74 | private HashMap modifiedFromOriginal = new HashMap(); |
||
75 | private FeatureType originalType = null; |
||
76 | private boolean first = true; |
||
77 | private FeatureTypeManagerFeatureStoreTransforms transforms;
|
||
78 | private FeatureStore store;
|
||
79 | 24143 | vcaballero | |
80 | 26252 | jmvivo | public FeatureTypeManager(FeatureStore store,
|
81 | ExpansionAdapter expansionAdapter) { |
||
82 | this.expansionAdapter = expansionAdapter;
|
||
83 | this.store = store;
|
||
84 | this.transforms = new FeatureTypeManagerFeatureStoreTransforms(); |
||
85 | this.transforms.setFeatureStore(store);
|
||
86 | } |
||
87 | 24143 | vcaballero | |
88 | 26252 | jmvivo | public void dispose() { |
89 | this.expansionAdapter.close();
|
||
90 | this.expansionAdapter = null; |
||
91 | this.deleted.clear();
|
||
92 | this.deleted = null; |
||
93 | this.transforms.clear();
|
||
94 | } |
||
95 | 24143 | vcaballero | |
96 | 26252 | jmvivo | // public FeatureType delete(String id) {
|
97 | // deleted.add(id);
|
||
98 | // FeatureType type=(FeatureType)added.remove(id);
|
||
99 | // if (type==null) {
|
||
100 | // type=(FeatureType)modifiedFromOriginal.remove(id);
|
||
101 | // }
|
||
102 | // deltaSize--;
|
||
103 | // return type;
|
||
104 | // }
|
||
105 | 24143 | vcaballero | |
106 | 26252 | jmvivo | /**
|
107 | * DOCUMENT ME!
|
||
108 | *
|
||
109 | * @param feature
|
||
110 | * DOCUMENT ME!
|
||
111 | */
|
||
112 | // public void add(FeatureType type) {
|
||
113 | // int pos = expansionAdapter.addObject(type);
|
||
114 | // added.put(type.getId(),new Integer(pos));
|
||
115 | // deltaSize++;
|
||
116 | // }
|
||
117 | /**
|
||
118 | * DOCUMENT ME!
|
||
119 | *
|
||
120 | * @param id
|
||
121 | * DOCUMENT ME!
|
||
122 | */
|
||
123 | // public void deleteLastFeatureType() {
|
||
124 | // expansionAdapter.deleteLastObject();
|
||
125 | // FeatureType
|
||
126 | // type=(FeatureType)expansionAdapter.getObject(expansionAdapter.getSize()-1);
|
||
127 | // added.remove(type.getId());
|
||
128 | // modifiedFromOriginal.remove(type.getId());
|
||
129 | // deltaSize--;
|
||
130 | // }
|
||
131 | /**
|
||
132 | * DOCUMENT ME!
|
||
133 | *
|
||
134 | * @param id
|
||
135 | * DOCUMENT ME!
|
||
136 | *
|
||
137 | * @return DOCUMENT ME!
|
||
138 | * @throws IsNotFeatureSettingException
|
||
139 | */
|
||
140 | public FeatureType getType(String id) throws DataException { |
||
141 | Integer intNum = ((Integer) added.get(id)); |
||
142 | if (intNum == null) { |
||
143 | intNum = ((Integer) modifiedFromOriginal.get(id));
|
||
144 | if (intNum == null) { |
||
145 | return null; |
||
146 | } |
||
147 | } |
||
148 | int num = intNum.intValue();
|
||
149 | 24143 | vcaballero | |
150 | 26252 | jmvivo | FeatureType type = (FeatureType) expansionAdapter.getObject(num); |
151 | return type;
|
||
152 | } |
||
153 | 24143 | vcaballero | |
154 | 26252 | jmvivo | /**
|
155 | * DOCUMENT ME!
|
||
156 | *
|
||
157 | * @param feature
|
||
158 | * DOCUMENT ME!
|
||
159 | * @param oldFeature
|
||
160 | * DOCUMENT ME!
|
||
161 | * @throws DataException
|
||
162 | */
|
||
163 | public int update(FeatureType type, FeatureType oldType) { |
||
164 | // deleted.add(oldType.getId());
|
||
165 | if (first) {
|
||
166 | originalType = oldType; |
||
167 | first = false;
|
||
168 | } |
||
169 | int oldNum = -1; |
||
170 | int num = expansionAdapter.addObject(type);
|
||
171 | String id = type.getId();
|
||
172 | 24143 | vcaballero | |
173 | 26252 | jmvivo | if (added.containsKey(id)) {
|
174 | oldNum = ((Integer) added.get(id)).intValue();
|
||
175 | added.put(id, new Integer(num)); |
||
176 | } else {
|
||
177 | if (modifiedFromOriginal.get(id) != null) { |
||
178 | oldNum = ((Integer) modifiedFromOriginal.get(id)).intValue();
|
||
179 | } |
||
180 | modifiedFromOriginal.put(id, new Integer(num)); |
||
181 | } |
||
182 | |||
183 | try {
|
||
184 | this.transforms.add(new UpdateFeatureTypeTransform(this.store, |
||
185 | type, oldType)); |
||
186 | } catch (DataException e) {
|
||
187 | throw new RuntimeException(); // FIXME (pero esto no deberia de |
||
188 | // pasar nunca)
|
||
189 | } |
||
190 | return oldNum;
|
||
191 | } |
||
192 | |||
193 | 26359 | jmvivo | private class UpdateFeatureTypeTransform implements FeatureStoreTransform { |
194 | 26252 | jmvivo | private FeatureType ftSource;
|
195 | private FeatureType ftTarget;
|
||
196 | private WeakReference wkRefStore; |
||
197 | private List ftypes = null; |
||
198 | private List attrToUse; |
||
199 | |||
200 | 26359 | jmvivo | UpdateFeatureTypeTransform(FeatureStore featureStore, |
201 | 26252 | jmvivo | FeatureType ftSource, FeatureType ftTarget) { |
202 | this.ftSource = ftSource;
|
||
203 | this.ftTarget = ftTarget;
|
||
204 | this.wkRefStore = new WeakReference(featureStore); |
||
205 | this.initializeAttributesToUse();
|
||
206 | } |
||
207 | |||
208 | 35739 | nfrancisco | public String getDescription() { |
209 | //TODO
|
||
210 | return ""; |
||
211 | } |
||
212 | |||
213 | public String getName() { |
||
214 | //TODO
|
||
215 | return ""; |
||
216 | } |
||
217 | |||
218 | 26252 | jmvivo | private void initializeAttributesToUse() { |
219 | attrToUse = new ArrayList(ftTarget.size()); |
||
220 | Iterator iter = ftTarget.iterator();
|
||
221 | FeatureAttributeDescriptor tAttr, sAttr; |
||
222 | while (iter.hasNext()) {
|
||
223 | tAttr = (FeatureAttributeDescriptor) iter.next(); |
||
224 | sAttr = this.ftSource.getAttributeDescriptor(tAttr.getName());
|
||
225 | 26359 | jmvivo | if (sAttr == null) { |
226 | 26252 | jmvivo | continue;
|
227 | } |
||
228 | 26359 | jmvivo | if (tAttr.getDataType() != sAttr.getDataType()) {
|
229 | 26252 | jmvivo | continue;
|
230 | } |
||
231 | attrToUse.add(tAttr.getName()); |
||
232 | |||
233 | } |
||
234 | |||
235 | } |
||
236 | |||
237 | public void applyTransform(Feature source, EditableFeature target) |
||
238 | throws DataException {
|
||
239 | Iterator iter = target.getType().iterator();
|
||
240 | FeatureAttributeDescriptor tAttr; |
||
241 | String name;
|
||
242 | while (iter.hasNext()) {
|
||
243 | tAttr = (FeatureAttributeDescriptor) iter.next(); |
||
244 | name = tAttr.getName(); |
||
245 | if (this.attrToUse.contains(name)) { |
||
246 | target.set(name, source.get(name)); |
||
247 | } else {
|
||
248 | target.set(name, tAttr.getDefaultValue()); |
||
249 | } |
||
250 | } |
||
251 | } |
||
252 | |||
253 | public FeatureType getDefaultFeatureType() throws DataException { |
||
254 | return this.ftTarget; |
||
255 | } |
||
256 | |||
257 | public FeatureStore getFeatureStore() {
|
||
258 | return (FeatureStore) this.wkRefStore.get(); |
||
259 | } |
||
260 | |||
261 | public List getFeatureTypes() throws DataException { |
||
262 | if (this.ftypes == null) { |
||
263 | this.ftypes = Arrays |
||
264 | .asList(new FeatureType[] { this.ftTarget }); |
||
265 | } |
||
266 | return this.ftypes; |
||
267 | } |
||
268 | |||
269 | public FeatureType getSourceFeatureTypeFrom(
|
||
270 | FeatureType targetFeatureType) { |
||
271 | EditableFeatureType orgType = ftSource.getEditable(); |
||
272 | Iterator iter = orgType.iterator();
|
||
273 | FeatureAttributeDescriptor attr; |
||
274 | |||
275 | while (iter.hasNext()) {
|
||
276 | attr = (FeatureAttributeDescriptor) iter.next(); |
||
277 | if (!attrToUse.contains(attr.getName())) {
|
||
278 | iter.remove(); |
||
279 | } |
||
280 | } |
||
281 | |||
282 | return orgType.getNotEditableCopy();
|
||
283 | } |
||
284 | |||
285 | public void setFeatureStore(FeatureStore featureStore) { |
||
286 | this.wkRefStore = new WeakReference(featureStore); |
||
287 | } |
||
288 | |||
289 | public PersistentState getState() throws PersistenceException { |
||
290 | // FIXME
|
||
291 | throw new UnsupportedOperationException(); |
||
292 | } |
||
293 | |||
294 | 28076 | cmartinez | public void saveToState(PersistentState state) |
295 | 26252 | jmvivo | throws PersistenceException {
|
296 | // FIXME
|
||
297 | throw new UnsupportedOperationException(); |
||
298 | } |
||
299 | |||
300 | 28474 | cmartinez | public void loadFromState(PersistentState state) throws PersistenceException { |
301 | 26252 | jmvivo | // FIXME
|
302 | throw new UnsupportedOperationException(); |
||
303 | } |
||
304 | |||
305 | 26359 | jmvivo | public boolean isTransformsOriginalValues() { |
306 | return false; |
||
307 | } |
||
308 | |||
309 | 26252 | jmvivo | } |
310 | |||
311 | 24143 | vcaballero | /**
|
312 | 26252 | jmvivo | * DOCUMENT ME!
|
313 | *
|
||
314 | * @param id
|
||
315 | * DOCUMENT ME!
|
||
316 | */
|
||
317 | public void restore(String id) { |
||
318 | deleted.remove(id); |
||
319 | deltaSize++; |
||
320 | } |
||
321 | 24143 | vcaballero | |
322 | 26252 | jmvivo | public void restore(String id, int num) { |
323 | if (added.containsKey(id)) {
|
||
324 | added.put(id, new Integer(num)); |
||
325 | } else {
|
||
326 | modifiedFromOriginal.put(id, new Integer(num)); |
||
327 | } |
||
328 | } |
||
329 | 24143 | vcaballero | |
330 | 26252 | jmvivo | public boolean isDeleted(FeatureType type) { |
331 | return deleted.contains(type.getId());
|
||
332 | } |
||
333 | 24143 | vcaballero | |
334 | 26252 | jmvivo | public boolean isDeleted(String id) { |
335 | 24143 | vcaballero | return deleted.contains(id);
|
336 | } |
||
337 | |||
338 | public void clear() { |
||
339 | added.clear(); |
||
340 | modifiedFromOriginal.clear(); |
||
341 | 26252 | jmvivo | expansionAdapter.close(); |
342 | deleted.clear();// <FeatureID>
|
||
343 | deltaSize = 0;
|
||
344 | 24143 | vcaballero | } |
345 | |||
346 | public boolean hasChanges() { |
||
347 | 26252 | jmvivo | return added.size() > 0 || modifiedFromOriginal.size() > 0 |
348 | || deleted.size() > 0;
|
||
349 | 24143 | vcaballero | } |
350 | |||
351 | public Iterator newsIterator() { |
||
352 | return added.values().iterator();
|
||
353 | } |
||
354 | |||
355 | public boolean hasNews() { |
||
356 | return !added.isEmpty();
|
||
357 | } |
||
358 | |||
359 | public long getDeltaSize() { |
||
360 | return deltaSize;
|
||
361 | } |
||
362 | 26000 | vcaballero | |
363 | public FeatureType getOriginalFeatureType() {
|
||
364 | return originalType;
|
||
365 | } |
||
366 | 26252 | jmvivo | |
367 | public DefaultFeatureStoreTransforms getTransforms() {
|
||
368 | return this.transforms; |
||
369 | } |
||
370 | |||
371 | 26359 | jmvivo | public class FeatureTypeManagerFeatureStoreTransforms extends |
372 | 26252 | jmvivo | DefaultFeatureStoreTransforms { |
373 | |||
374 | 26359 | jmvivo | private FeatureTypeManagerFeatureStoreTransforms() {
|
375 | |||
376 | } |
||
377 | |||
378 | 26252 | jmvivo | protected void checkEditingMode() { |
379 | return;
|
||
380 | } |
||
381 | |||
382 | protected void notifyChangeToStore() { |
||
383 | return;
|
||
384 | } |
||
385 | |||
386 | public PersistentState getState() throws PersistenceException { |
||
387 | // FIXME
|
||
388 | throw new UnsupportedOperationException(); |
||
389 | } |
||
390 | |||
391 | public void loadState(PersistentState state) |
||
392 | throws PersistenceException {
|
||
393 | // FIXME
|
||
394 | throw new UnsupportedOperationException(); |
||
395 | } |
||
396 | |||
397 | 28482 | cmartinez | public void loadFromState(PersistentState state) throws PersistenceException { |
398 | 26252 | jmvivo | // FIXME
|
399 | throw new UnsupportedOperationException(); |
||
400 | } |
||
401 | |||
402 | 26359 | jmvivo | public FeatureStoreTransform add(FeatureStoreTransform transform)
|
403 | throws DataException {
|
||
404 | if (!(transform instanceof UpdateFeatureTypeTransform)) { |
||
405 | // FIXME
|
||
406 | throw new IllegalArgumentException(); |
||
407 | } |
||
408 | return super.add(transform); |
||
409 | } |
||
410 | |||
411 | 26252 | jmvivo | } |
412 | 28098 | jmvivo | |
413 | 29289 | jmvivo | public class FeatureTypesChangedItem implements FeatureTypeChanged { |
414 | |||
415 | private FeatureType source;
|
||
416 | private FeatureType target;
|
||
417 | |||
418 | public FeatureTypesChangedItem(FeatureType source, FeatureType target) {
|
||
419 | this.source = source;
|
||
420 | this.target = target;
|
||
421 | } |
||
422 | |||
423 | public FeatureType getSource() {
|
||
424 | return source;
|
||
425 | } |
||
426 | |||
427 | public FeatureType getTarget() {
|
||
428 | return target;
|
||
429 | } |
||
430 | |||
431 | } |
||
432 | |||
433 | public Iterator getFeatureTypesChanged() throws DataException { |
||
434 | 28098 | jmvivo | // FIXME this don't work for Store.fType.size() > 1
|
435 | List list = new ArrayList(); |
||
436 | if (modifiedFromOriginal.size() > 0) { |
||
437 | 29289 | jmvivo | FeatureType src = this.getOriginalFeatureType();
|
438 | list.add(new FeatureTypesChangedItem(src, this.store |
||
439 | .getFeatureType(src.getId()))); |
||
440 | 28098 | jmvivo | } |
441 | return list.iterator();
|
||
442 | } |
||
443 | 29289 | jmvivo | |
444 | 24143 | vcaballero | } |