Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_dal / src / org / gvsig / fmap / dal / feature / impl / FeatureTypeManager.java @ 32880

History | View | Annotate | Download (11.2 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
                private void initializeAttributesToUse() {
209
                        attrToUse = new ArrayList(ftTarget.size());
210
                        Iterator iter = ftTarget.iterator();
211
                        FeatureAttributeDescriptor tAttr, sAttr;
212
                        while (iter.hasNext()) {
213
                                tAttr = (FeatureAttributeDescriptor) iter.next();
214
                                sAttr = this.ftSource.getAttributeDescriptor(tAttr.getName());
215
                                if (sAttr == null) {
216
                                        continue;
217
                                }
218
                                if (tAttr.getDataType() != sAttr.getDataType()) {
219
                                        continue;
220
                                }
221
                                attrToUse.add(tAttr.getName());
222

    
223
                        }
224

    
225
                }
226

    
227
                public void applyTransform(Feature source, EditableFeature target)
228
                                throws DataException {
229
                        Iterator iter = target.getType().iterator();
230
                        FeatureAttributeDescriptor tAttr;
231
                        String name;
232
                        while (iter.hasNext()) {
233
                                tAttr = (FeatureAttributeDescriptor) iter.next();
234
                                name = tAttr.getName();
235
                                if (this.attrToUse.contains(name)) {
236
                                        target.set(name, source.get(name));
237
                                } else {
238
                                        target.set(name, tAttr.getDefaultValue());
239
                                }
240
                        }
241
                }
242

    
243
                public FeatureType getDefaultFeatureType() throws DataException {
244
                        return this.ftTarget;
245
                }
246

    
247
                public FeatureStore getFeatureStore() {
248
                        return (FeatureStore) this.wkRefStore.get();
249
                }
250

    
251
                public List getFeatureTypes() throws DataException {
252
                        if (this.ftypes == null) {
253
                                this.ftypes = Arrays
254
                                                .asList(new FeatureType[] { this.ftTarget });
255
                        }
256
                        return this.ftypes;
257
                }
258

    
259
                public FeatureType getSourceFeatureTypeFrom(
260
                                FeatureType targetFeatureType) {
261
                        EditableFeatureType orgType = ftSource.getEditable();
262
                        Iterator iter = orgType.iterator();
263
                        FeatureAttributeDescriptor attr;
264

    
265
                        while (iter.hasNext()) {
266
                                attr = (FeatureAttributeDescriptor) iter.next();
267
                                if (!attrToUse.contains(attr.getName())) {
268
                                        iter.remove();
269
                                }
270
                        }
271

    
272
                        return orgType.getNotEditableCopy();
273
                }
274

    
275
                public void setFeatureStore(FeatureStore featureStore) {
276
                        this.wkRefStore = new WeakReference(featureStore);
277
                }
278

    
279
                public PersistentState getState() throws PersistenceException {
280
                        // FIXME
281
                        throw new UnsupportedOperationException();
282
                }
283

    
284
                public void saveToState(PersistentState state)
285
                                throws PersistenceException {
286
                        // FIXME
287
                        throw new UnsupportedOperationException();
288
                }
289

    
290
                public void loadFromState(PersistentState state) throws PersistenceException {
291
                        // FIXME
292
                        throw new UnsupportedOperationException();
293
                }
294

    
295
                public boolean isTransformsOriginalValues() {
296
                        return false;
297
                }
298

    
299
        }
300

    
301
        /**
302
         * DOCUMENT ME!
303
         *
304
         * @param id
305
         *            DOCUMENT ME!
306
         */
307
        public void restore(String id) {
308
                deleted.remove(id);
309
                deltaSize++;
310
        }
311

    
312
        public void restore(String id, int num) {
313
                if (added.containsKey(id)) {
314
                        added.put(id, new Integer(num));
315
                } else {
316
                        modifiedFromOriginal.put(id, new Integer(num));
317
                }
318
        }
319

    
320
        public boolean isDeleted(FeatureType type) {
321
                return deleted.contains(type.getId());
322
        }
323

    
324
        public boolean isDeleted(String id) {
325
                return deleted.contains(id);
326
        }
327

    
328
        public void clear() {
329
                added.clear();
330
                modifiedFromOriginal.clear();
331
                expansionAdapter.close();
332
                deleted.clear();// <FeatureID>
333
                deltaSize = 0;
334
        }
335

    
336
        public boolean hasChanges() {
337
                return added.size() > 0 || modifiedFromOriginal.size() > 0
338
                                || deleted.size() > 0;
339
        }
340

    
341
        public Iterator newsIterator() {
342
                return added.values().iterator();
343
        }
344

    
345
        public boolean hasNews() {
346
                return !added.isEmpty();
347
        }
348

    
349
        public long getDeltaSize() {
350
                return deltaSize;
351
        }
352

    
353
        public FeatureType getOriginalFeatureType() {
354
                return originalType;
355
        }
356

    
357
        public DefaultFeatureStoreTransforms getTransforms() {
358
                return this.transforms;
359
        }
360

    
361
        public class FeatureTypeManagerFeatureStoreTransforms extends
362
                        DefaultFeatureStoreTransforms {
363

    
364
                private FeatureTypeManagerFeatureStoreTransforms() {
365

    
366
                }
367

    
368
                protected void checkEditingMode() {
369
                        return;
370
                }
371

    
372
                protected void notifyChangeToStore() {
373
                        return;
374
                }
375

    
376
                public PersistentState getState() throws PersistenceException {
377
                        // FIXME
378
                        throw new UnsupportedOperationException();
379
                }
380

    
381
                public void loadState(PersistentState state)
382
                                throws PersistenceException {
383
                        // FIXME
384
                        throw new UnsupportedOperationException();
385
                }
386

    
387
                public void loadFromState(PersistentState state) throws PersistenceException {
388
                        // FIXME
389
                        throw new UnsupportedOperationException();
390
                }
391

    
392
                public FeatureStoreTransform add(FeatureStoreTransform transform)
393
                                throws DataException {
394
                        if (!(transform instanceof UpdateFeatureTypeTransform)) {
395
                                // FIXME
396
                                throw new IllegalArgumentException();
397
                        }
398
                        return super.add(transform);
399
                }
400

    
401
        }
402

    
403
        public class FeatureTypesChangedItem implements FeatureTypeChanged {
404

    
405
                private FeatureType source;
406
                private FeatureType target;
407

    
408
                public FeatureTypesChangedItem(FeatureType source, FeatureType target) {
409
                        this.source = source;
410
                        this.target = target;
411
                }
412

    
413
                public FeatureType getSource() {
414
                        return source;
415
                }
416

    
417
                public FeatureType getTarget() {
418
                        return target;
419
                }
420

    
421
        }
422

    
423
        public Iterator getFeatureTypesChanged() throws DataException {
424
                // FIXME this don't work for Store.fType.size() > 1
425
                List list = new ArrayList();
426
                if (modifiedFromOriginal.size() > 0) {
427
                        FeatureType src = this.getOriginalFeatureType();
428
                        list.add(new FeatureTypesChangedItem(src, this.store
429
                                        .getFeatureType(src.getId())));
430
                }
431
                return list.iterator();
432
        }
433

    
434
}