Statistics
| Revision:

svn-gvsig-desktop / tags / dal_time_support_Build_1 / libraries / libFMap_dal / src / org / gvsig / fmap / dal / feature / impl / FeatureTypeManager.java @ 36111

History | View | Annotate | Download (11.3 KB)

1
/* 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
package org.gvsig.fmap.dal.feature.impl;
43

    
44
import java.lang.ref.WeakReference;
45
import java.util.ArrayList;
46
import java.util.Arrays;
47
import java.util.HashMap;
48
import java.util.Iterator;
49
import java.util.List;
50

    
51
import org.gvsig.fmap.dal.exception.DataException;
52
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
import org.gvsig.fmap.dal.feature.FeatureType;
59
import org.gvsig.fmap.dal.feature.impl.expansionadapter.ExpansionAdapter;
60
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider.FeatureTypeChanged;
61
import org.gvsig.tools.persistence.PersistentState;
62
import org.gvsig.tools.persistence.exception.PersistenceException;
63

    
64
/**
65
 * DOCUMENT ME!
66
 *
67
 * @author Vicente Caballero Navarro
68
 */
69
public class FeatureTypeManager {
70
        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

    
80
        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

    
88
        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

    
96
        // 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

    
106
        /**
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

    
150
                FeatureType type = (FeatureType) expansionAdapter.getObject(num);
151
                return type;
152
        }
153

    
154
        /**
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

    
173
                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
        private class UpdateFeatureTypeTransform implements FeatureStoreTransform {
194
                private FeatureType ftSource;
195
                private FeatureType ftTarget;
196
                private WeakReference wkRefStore;
197
                private List ftypes = null;
198
                private List attrToUse;
199

    
200
                UpdateFeatureTypeTransform(FeatureStore featureStore,
201
                                FeatureType ftSource, FeatureType ftTarget) {
202
                        this.ftSource = ftSource;
203
                        this.ftTarget = ftTarget;
204
                        this.wkRefStore = new WeakReference(featureStore);
205
                        this.initializeAttributesToUse();
206
                }
207

    
208
                public String getDescription() {
209
                //TODO
210
                    return "";
211
                }
212
                
213
                public String getName() {
214
                //TODO
215
                    return "";
216
                }
217
                
218
                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
                                if (sAttr == null) {
226
                                        continue;
227
                                }
228
                                if (tAttr.getDataType() != sAttr.getDataType()) {
229
                                        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
                public void saveToState(PersistentState state)
295
                                throws PersistenceException {
296
                        // FIXME
297
                        throw new UnsupportedOperationException();
298
                }
299

    
300
                public void loadFromState(PersistentState state) throws PersistenceException {
301
                        // FIXME
302
                        throw new UnsupportedOperationException();
303
                }
304

    
305
                public boolean isTransformsOriginalValues() {
306
                        return false;
307
                }
308

    
309
        }
310

    
311
        /**
312
         * DOCUMENT ME!
313
         *
314
         * @param id
315
         *            DOCUMENT ME!
316
         */
317
        public void restore(String id) {
318
                deleted.remove(id);
319
                deltaSize++;
320
        }
321

    
322
        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

    
330
        public boolean isDeleted(FeatureType type) {
331
                return deleted.contains(type.getId());
332
        }
333

    
334
        public boolean isDeleted(String id) {
335
                return deleted.contains(id);
336
        }
337

    
338
        public void clear() {
339
                added.clear();
340
                modifiedFromOriginal.clear();
341
                expansionAdapter.close();
342
                deleted.clear();// <FeatureID>
343
                deltaSize = 0;
344
        }
345

    
346
        public boolean hasChanges() {
347
                return added.size() > 0 || modifiedFromOriginal.size() > 0
348
                                || deleted.size() > 0;
349
        }
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

    
363
        public FeatureType getOriginalFeatureType() {
364
                return originalType;
365
        }
366

    
367
        public DefaultFeatureStoreTransforms getTransforms() {
368
                return this.transforms;
369
        }
370

    
371
        public class FeatureTypeManagerFeatureStoreTransforms extends
372
                        DefaultFeatureStoreTransforms {
373

    
374
                private FeatureTypeManagerFeatureStoreTransforms() {
375

    
376
                }
377

    
378
                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
                public void loadFromState(PersistentState state) throws PersistenceException {
398
                        // FIXME
399
                        throw new UnsupportedOperationException();
400
                }
401

    
402
                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
        }
412

    
413
        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
                // FIXME this don't work for Store.fType.size() > 1
435
                List list = new ArrayList();
436
                if (modifiedFromOriginal.size() > 0) {
437
                        FeatureType src = this.getOriginalFeatureType();
438
                        list.add(new FeatureTypesChangedItem(src, this.store
439
                                        .getFeatureType(src.getId())));
440
                }
441
                return list.iterator();
442
        }
443

    
444
}