svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dal / src / org / gvsig / fmap / dal / feature / impl / FeatureTypeManager.java @ 28474
History | View | Annotate | Download (10.6 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 | 26252 | jmvivo | import org.gvsig.tools.persistence.PersistenceException; |
61 | import org.gvsig.tools.persistence.PersistentState; |
||
62 | 24143 | vcaballero | |
63 | /**
|
||
64 | * DOCUMENT ME!
|
||
65 | *
|
||
66 | * @author Vicente Caballero Navarro
|
||
67 | */
|
||
68 | public class FeatureTypeManager { |
||
69 | 26252 | jmvivo | private ExpansionAdapter expansionAdapter;
|
70 | private ArrayList deleted = new ArrayList();// <FeatureID> |
||
71 | private int deltaSize = 0; |
||
72 | private HashMap added = new HashMap(); |
||
73 | private HashMap modifiedFromOriginal = new HashMap(); |
||
74 | private FeatureType originalType = null; |
||
75 | private boolean first = true; |
||
76 | private FeatureTypeManagerFeatureStoreTransforms transforms;
|
||
77 | private FeatureStore store;
|
||
78 | 24143 | vcaballero | |
79 | 26252 | jmvivo | public FeatureTypeManager(FeatureStore store,
|
80 | ExpansionAdapter expansionAdapter) { |
||
81 | this.expansionAdapter = expansionAdapter;
|
||
82 | this.store = store;
|
||
83 | this.transforms = new FeatureTypeManagerFeatureStoreTransforms(); |
||
84 | this.transforms.setFeatureStore(store);
|
||
85 | } |
||
86 | 24143 | vcaballero | |
87 | 26252 | jmvivo | public void dispose() { |
88 | this.expansionAdapter.close();
|
||
89 | this.expansionAdapter = null; |
||
90 | this.deleted.clear();
|
||
91 | this.deleted = null; |
||
92 | this.transforms.clear();
|
||
93 | } |
||
94 | 24143 | vcaballero | |
95 | 26252 | jmvivo | // public FeatureType delete(String id) {
|
96 | // deleted.add(id);
|
||
97 | // FeatureType type=(FeatureType)added.remove(id);
|
||
98 | // if (type==null) {
|
||
99 | // type=(FeatureType)modifiedFromOriginal.remove(id);
|
||
100 | // }
|
||
101 | // deltaSize--;
|
||
102 | // return type;
|
||
103 | // }
|
||
104 | 24143 | vcaballero | |
105 | 26252 | jmvivo | /**
|
106 | * DOCUMENT ME!
|
||
107 | *
|
||
108 | * @param feature
|
||
109 | * DOCUMENT ME!
|
||
110 | */
|
||
111 | // public void add(FeatureType type) {
|
||
112 | // int pos = expansionAdapter.addObject(type);
|
||
113 | // added.put(type.getId(),new Integer(pos));
|
||
114 | // deltaSize++;
|
||
115 | // }
|
||
116 | /**
|
||
117 | * DOCUMENT ME!
|
||
118 | *
|
||
119 | * @param id
|
||
120 | * DOCUMENT ME!
|
||
121 | */
|
||
122 | // public void deleteLastFeatureType() {
|
||
123 | // expansionAdapter.deleteLastObject();
|
||
124 | // FeatureType
|
||
125 | // type=(FeatureType)expansionAdapter.getObject(expansionAdapter.getSize()-1);
|
||
126 | // added.remove(type.getId());
|
||
127 | // modifiedFromOriginal.remove(type.getId());
|
||
128 | // deltaSize--;
|
||
129 | // }
|
||
130 | /**
|
||
131 | * DOCUMENT ME!
|
||
132 | *
|
||
133 | * @param id
|
||
134 | * DOCUMENT ME!
|
||
135 | *
|
||
136 | * @return DOCUMENT ME!
|
||
137 | * @throws IsNotFeatureSettingException
|
||
138 | */
|
||
139 | public FeatureType getType(String id) throws DataException { |
||
140 | Integer intNum = ((Integer) added.get(id)); |
||
141 | if (intNum == null) { |
||
142 | intNum = ((Integer) modifiedFromOriginal.get(id));
|
||
143 | if (intNum == null) { |
||
144 | return null; |
||
145 | } |
||
146 | } |
||
147 | int num = intNum.intValue();
|
||
148 | 24143 | vcaballero | |
149 | 26252 | jmvivo | FeatureType type = (FeatureType) expansionAdapter.getObject(num); |
150 | return type;
|
||
151 | } |
||
152 | 24143 | vcaballero | |
153 | 26252 | jmvivo | /**
|
154 | * DOCUMENT ME!
|
||
155 | *
|
||
156 | * @param feature
|
||
157 | * DOCUMENT ME!
|
||
158 | * @param oldFeature
|
||
159 | * DOCUMENT ME!
|
||
160 | * @throws DataException
|
||
161 | */
|
||
162 | public int update(FeatureType type, FeatureType oldType) { |
||
163 | // deleted.add(oldType.getId());
|
||
164 | if (first) {
|
||
165 | originalType = oldType; |
||
166 | first = false;
|
||
167 | } |
||
168 | int oldNum = -1; |
||
169 | int num = expansionAdapter.addObject(type);
|
||
170 | String id = type.getId();
|
||
171 | 24143 | vcaballero | |
172 | 26252 | jmvivo | if (added.containsKey(id)) {
|
173 | oldNum = ((Integer) added.get(id)).intValue();
|
||
174 | added.put(id, new Integer(num)); |
||
175 | } else {
|
||
176 | if (modifiedFromOriginal.get(id) != null) { |
||
177 | oldNum = ((Integer) modifiedFromOriginal.get(id)).intValue();
|
||
178 | } |
||
179 | modifiedFromOriginal.put(id, new Integer(num)); |
||
180 | } |
||
181 | |||
182 | try {
|
||
183 | this.transforms.add(new UpdateFeatureTypeTransform(this.store, |
||
184 | type, oldType)); |
||
185 | } catch (DataException e) {
|
||
186 | throw new RuntimeException(); // FIXME (pero esto no deberia de |
||
187 | // pasar nunca)
|
||
188 | } |
||
189 | return oldNum;
|
||
190 | } |
||
191 | |||
192 | 26359 | jmvivo | private class UpdateFeatureTypeTransform implements FeatureStoreTransform { |
193 | 26252 | jmvivo | private FeatureType ftSource;
|
194 | private FeatureType ftTarget;
|
||
195 | private WeakReference wkRefStore; |
||
196 | private List ftypes = null; |
||
197 | private List attrToUse; |
||
198 | |||
199 | 26359 | jmvivo | UpdateFeatureTypeTransform(FeatureStore featureStore, |
200 | 26252 | jmvivo | FeatureType ftSource, FeatureType ftTarget) { |
201 | this.ftSource = ftSource;
|
||
202 | this.ftTarget = ftTarget;
|
||
203 | this.wkRefStore = new WeakReference(featureStore); |
||
204 | this.initializeAttributesToUse();
|
||
205 | } |
||
206 | |||
207 | private void initializeAttributesToUse() { |
||
208 | attrToUse = new ArrayList(ftTarget.size()); |
||
209 | Iterator iter = ftTarget.iterator();
|
||
210 | FeatureAttributeDescriptor tAttr, sAttr; |
||
211 | while (iter.hasNext()) {
|
||
212 | tAttr = (FeatureAttributeDescriptor) iter.next(); |
||
213 | sAttr = this.ftSource.getAttributeDescriptor(tAttr.getName());
|
||
214 | 26359 | jmvivo | if (sAttr == null) { |
215 | 26252 | jmvivo | continue;
|
216 | } |
||
217 | 26359 | jmvivo | if (tAttr.getDataType() != sAttr.getDataType()) {
|
218 | 26252 | jmvivo | continue;
|
219 | } |
||
220 | attrToUse.add(tAttr.getName()); |
||
221 | |||
222 | } |
||
223 | |||
224 | } |
||
225 | |||
226 | public void applyTransform(Feature source, EditableFeature target) |
||
227 | throws DataException {
|
||
228 | Iterator iter = target.getType().iterator();
|
||
229 | FeatureAttributeDescriptor tAttr; |
||
230 | String name;
|
||
231 | while (iter.hasNext()) {
|
||
232 | tAttr = (FeatureAttributeDescriptor) iter.next(); |
||
233 | name = tAttr.getName(); |
||
234 | if (this.attrToUse.contains(name)) { |
||
235 | target.set(name, source.get(name)); |
||
236 | } else {
|
||
237 | target.set(name, tAttr.getDefaultValue()); |
||
238 | } |
||
239 | } |
||
240 | } |
||
241 | |||
242 | public FeatureType getDefaultFeatureType() throws DataException { |
||
243 | return this.ftTarget; |
||
244 | } |
||
245 | |||
246 | public FeatureStore getFeatureStore() {
|
||
247 | return (FeatureStore) this.wkRefStore.get(); |
||
248 | } |
||
249 | |||
250 | public List getFeatureTypes() throws DataException { |
||
251 | if (this.ftypes == null) { |
||
252 | this.ftypes = Arrays |
||
253 | .asList(new FeatureType[] { this.ftTarget }); |
||
254 | } |
||
255 | return this.ftypes; |
||
256 | } |
||
257 | |||
258 | public FeatureType getSourceFeatureTypeFrom(
|
||
259 | FeatureType targetFeatureType) { |
||
260 | EditableFeatureType orgType = ftSource.getEditable(); |
||
261 | Iterator iter = orgType.iterator();
|
||
262 | FeatureAttributeDescriptor attr; |
||
263 | |||
264 | while (iter.hasNext()) {
|
||
265 | attr = (FeatureAttributeDescriptor) iter.next(); |
||
266 | if (!attrToUse.contains(attr.getName())) {
|
||
267 | iter.remove(); |
||
268 | } |
||
269 | } |
||
270 | |||
271 | return orgType.getNotEditableCopy();
|
||
272 | } |
||
273 | |||
274 | public void setFeatureStore(FeatureStore featureStore) { |
||
275 | this.wkRefStore = new WeakReference(featureStore); |
||
276 | } |
||
277 | |||
278 | public PersistentState getState() throws PersistenceException { |
||
279 | // FIXME
|
||
280 | throw new UnsupportedOperationException(); |
||
281 | } |
||
282 | |||
283 | 28076 | cmartinez | public void saveToState(PersistentState state) |
284 | 26252 | jmvivo | throws PersistenceException {
|
285 | // FIXME
|
||
286 | throw new UnsupportedOperationException(); |
||
287 | } |
||
288 | |||
289 | 28474 | cmartinez | public void loadFromState(PersistentState state) throws PersistenceException { |
290 | 26252 | jmvivo | // FIXME
|
291 | throw new UnsupportedOperationException(); |
||
292 | } |
||
293 | |||
294 | 26359 | jmvivo | public boolean isTransformsOriginalValues() { |
295 | return false; |
||
296 | } |
||
297 | |||
298 | 26252 | jmvivo | } |
299 | |||
300 | 24143 | vcaballero | /**
|
301 | 26252 | jmvivo | * DOCUMENT ME!
|
302 | *
|
||
303 | * @param id
|
||
304 | * DOCUMENT ME!
|
||
305 | */
|
||
306 | public void restore(String id) { |
||
307 | deleted.remove(id); |
||
308 | deltaSize++; |
||
309 | } |
||
310 | 24143 | vcaballero | |
311 | 26252 | jmvivo | public void restore(String id, int num) { |
312 | if (added.containsKey(id)) {
|
||
313 | added.put(id, new Integer(num)); |
||
314 | } else {
|
||
315 | modifiedFromOriginal.put(id, new Integer(num)); |
||
316 | } |
||
317 | } |
||
318 | 24143 | vcaballero | |
319 | 26252 | jmvivo | public boolean isDeleted(FeatureType type) { |
320 | return deleted.contains(type.getId());
|
||
321 | } |
||
322 | 24143 | vcaballero | |
323 | 26252 | jmvivo | public boolean isDeleted(String id) { |
324 | 24143 | vcaballero | return deleted.contains(id);
|
325 | } |
||
326 | |||
327 | public void clear() { |
||
328 | added.clear(); |
||
329 | modifiedFromOriginal.clear(); |
||
330 | 26252 | jmvivo | expansionAdapter.close(); |
331 | deleted.clear();// <FeatureID>
|
||
332 | deltaSize = 0;
|
||
333 | 24143 | vcaballero | } |
334 | |||
335 | public boolean hasChanges() { |
||
336 | 26252 | jmvivo | return added.size() > 0 || modifiedFromOriginal.size() > 0 |
337 | || deleted.size() > 0;
|
||
338 | 24143 | vcaballero | } |
339 | |||
340 | public Iterator newsIterator() { |
||
341 | return added.values().iterator();
|
||
342 | } |
||
343 | |||
344 | public boolean hasNews() { |
||
345 | return !added.isEmpty();
|
||
346 | } |
||
347 | |||
348 | public long getDeltaSize() { |
||
349 | return deltaSize;
|
||
350 | } |
||
351 | 26000 | vcaballero | |
352 | public FeatureType getOriginalFeatureType() {
|
||
353 | return originalType;
|
||
354 | } |
||
355 | 26252 | jmvivo | |
356 | public DefaultFeatureStoreTransforms getTransforms() {
|
||
357 | return this.transforms; |
||
358 | } |
||
359 | |||
360 | 26359 | jmvivo | public class FeatureTypeManagerFeatureStoreTransforms extends |
361 | 26252 | jmvivo | DefaultFeatureStoreTransforms { |
362 | |||
363 | 26359 | jmvivo | private FeatureTypeManagerFeatureStoreTransforms() {
|
364 | |||
365 | } |
||
366 | |||
367 | 26252 | jmvivo | protected void checkEditingMode() { |
368 | return;
|
||
369 | } |
||
370 | |||
371 | protected void notifyChangeToStore() { |
||
372 | return;
|
||
373 | } |
||
374 | |||
375 | public PersistentState getState() throws PersistenceException { |
||
376 | // FIXME
|
||
377 | throw new UnsupportedOperationException(); |
||
378 | } |
||
379 | |||
380 | public void loadState(PersistentState state) |
||
381 | throws PersistenceException {
|
||
382 | // FIXME
|
||
383 | throw new UnsupportedOperationException(); |
||
384 | } |
||
385 | |||
386 | public void setState(PersistentState state) throws PersistenceException { |
||
387 | // FIXME
|
||
388 | throw new UnsupportedOperationException(); |
||
389 | } |
||
390 | |||
391 | 26359 | jmvivo | public FeatureStoreTransform add(FeatureStoreTransform transform)
|
392 | throws DataException {
|
||
393 | if (!(transform instanceof UpdateFeatureTypeTransform)) { |
||
394 | // FIXME
|
||
395 | throw new IllegalArgumentException(); |
||
396 | } |
||
397 | return super.add(transform); |
||
398 | } |
||
399 | |||
400 | 26252 | jmvivo | } |
401 | 28098 | jmvivo | |
402 | public Iterator getOriginalsFeatureTypesUpdated() { |
||
403 | // FIXME this don't work for Store.fType.size() > 1
|
||
404 | List list = new ArrayList(); |
||
405 | if (modifiedFromOriginal.size() > 0) { |
||
406 | list.add(this.getOriginalFeatureType());
|
||
407 | } |
||
408 | return list.iterator();
|
||
409 | } |
||
410 | 24143 | vcaballero | } |