Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.impl / src / main / java / org / gvsig / fmap / dal / feature / impl / DefaultEditableFeatureType.java @ 44669

History | View | Annotate | Download (14.2 KB)

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;
24

    
25
import java.text.MessageFormat;
26

    
27
import java.util.Iterator;
28

    
29
import org.gvsig.fmap.dal.DataTypes;
30
import org.gvsig.fmap.dal.exception.DataListException;
31
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
32
import org.gvsig.fmap.dal.feature.EditableFeatureType;
33
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
34
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator;
35
import org.gvsig.fmap.dal.feature.FeatureStore;
36
import org.gvsig.fmap.dal.feature.FeatureType;
37
import org.gvsig.fmap.dal.feature.exception.FeatureTypeIntegrityException;
38
import org.gvsig.fmap.dal.feature.exception.UnsupportedDataTypeException;
39
import org.gvsig.tools.ToolsLocator;
40
import org.gvsig.tools.evaluator.Evaluator;
41

    
42
public class DefaultEditableFeatureType extends DefaultFeatureType implements
43
        EditableFeatureType {
44

    
45
    /**
46
     *
47
     */
48
    private static final long serialVersionUID = -713976880396024389L;
49

    
50
    private boolean hasStrongChanges;
51
    private DefaultFeatureType source;
52

    
53
    public DefaultEditableFeatureType(FeatureStore store) {
54
        super(store);
55
        this.hasStrongChanges = false;
56
        this.source = null;
57
    }
58

    
59
    public DefaultEditableFeatureType(FeatureStore store, String id) {
60
        super(store, id);
61
        this.hasStrongChanges = false;
62
        this.source = null;
63
    }
64

    
65
    protected DefaultEditableFeatureType(DefaultEditableFeatureType other) {
66
        super(other);
67
        this.hasStrongChanges = other.hasStrongChanges;
68
        this.source = (DefaultFeatureType) other.getSource();
69
    }
70

    
71
    protected DefaultEditableFeatureType(DefaultFeatureType other) {
72
        super(other);
73
        this.source = other;
74
        this.fixAll();
75
    }
76

    
77
    @Override
78
    protected DefaultFeatureAttributeDescriptor getCopyAttributeDescriptor(DefaultFeatureAttributeDescriptor src) {
79
        DefaultFeatureAttributeDescriptor copy = new DefaultEditableFeatureAttributeDescriptor(src);
80
        copy.setFeatureType(this);
81
        return copy;
82
    }
83

    
84
    public boolean hasStrongChanges() {
85
        if (hasStrongChanges) {
86
            return true;
87
        }
88
        Iterator iter = this.iterator();
89
        DefaultEditableFeatureAttributeDescriptor attr;
90
        while (iter.hasNext()) {
91
            attr = (DefaultEditableFeatureAttributeDescriptor) iter.next();
92
            if (attr.isComputed()) {
93
                continue;
94
            }
95
            if (attr.hasStrongChanges()) {
96
                return true;
97
            }
98
        }
99
        return false;
100
    }
101

    
102
    public FeatureType getCopy() {
103
        return new DefaultEditableFeatureType(this);
104
    }
105

    
106
    public EditableFeatureType getEditable() {
107
        return (EditableFeatureType) this.getCopy();
108
    }
109

    
110
    public boolean addAll(DefaultFeatureType other) {
111
        Iterator iter = other.iterator();
112
        DefaultFeatureAttributeDescriptor attr;
113
        DefaultEditableFeatureAttributeDescriptor editableAttr;
114
        while (iter.hasNext()) {
115
            attr = (DefaultFeatureAttributeDescriptor) iter.next();
116
            if (attr instanceof DefaultEditableFeatureAttributeDescriptor) {
117
                editableAttr = new DefaultEditableFeatureAttributeDescriptor(
118
                        attr);
119
            } else {
120
                editableAttr = new DefaultEditableFeatureAttributeDescriptor(
121
                        attr);
122
            }
123
            super.add(editableAttr);
124
        }
125
        this.pk = null;
126
        this.fixAll();
127
        return true;
128
    }
129

    
130
    public EditableFeatureAttributeDescriptor addLike(
131
            FeatureAttributeDescriptor other) {
132
        DefaultEditableFeatureAttributeDescriptor editableAttr;
133
        if (other instanceof DefaultEditableFeatureAttributeDescriptor) {
134
            editableAttr = new DefaultEditableFeatureAttributeDescriptor(
135
                    (DefaultFeatureAttributeDescriptor) other);
136
        } else {
137
            editableAttr = new DefaultEditableFeatureAttributeDescriptor(
138
                    (DefaultFeatureAttributeDescriptor) other);
139
        }
140
        super.add(editableAttr);
141
        if( !editableAttr.isComputed() ) {
142
            this.hasStrongChanges = true;
143
        }        
144
        this.fixAll();
145
        return editableAttr;
146

    
147
    }
148

    
149
    public FeatureType getSource() {
150
        return source;
151
    }
152

    
153
    public FeatureType getNotEditableCopy() {
154
        this.fixAll();
155
        DefaultFeatureType copy = new DefaultFeatureType(this, false);
156
        Iterator iter = this.iterator();
157
        DefaultFeatureAttributeDescriptor attr;
158
        while (iter.hasNext()) {
159
            attr = (DefaultFeatureAttributeDescriptor) iter.next();
160
            DefaultFeatureAttributeDescriptor newattr = new DefaultFeatureAttributeDescriptor(attr);
161
            newattr.setFeatureType(copy);
162
            copy.add(newattr);
163
        }
164
        return copy;
165
    }
166

    
167
    public EditableFeatureAttributeDescriptor add(String name, int type) {
168
        return this.add(name, type, true);
169
    }
170

    
171
    @Override
172
    public void removeAll() {
173
        while( !super.isEmpty() ) {
174
            super.remove(0);
175
        }
176
        this.fixAll();
177
    }
178

    
179
    @Override
180
    public void addAll(FeatureType attributes) {
181
        super.addAll(attributes);
182
        this.fixAll();
183
    }
184
    
185
    private EditableFeatureAttributeDescriptor add(String name, int type, boolean strongChanges) {
186
        DefaultEditableFeatureAttributeDescriptor attr = new DefaultEditableFeatureAttributeDescriptor(this, strongChanges);
187
        Iterator iter = this.iterator();
188
        while (iter.hasNext()) {
189
            EditableFeatureAttributeDescriptor descriptor = (EditableFeatureAttributeDescriptor) iter.next();
190
            if (descriptor.getName().equalsIgnoreCase(name)) {
191
                throw new RuntimeException(
192
                        MessageFormat.format("Name descriptor {0} duplicated.", new String[]{name})
193
                );
194
            }
195

    
196
        }
197
        attr.setName(name);
198
        switch (type) {
199
            case DataTypes.DOUBLE:
200
            case DataTypes.FLOAT:
201
            case DataTypes.DECIMAL:
202
            case DataTypes.BOOLEAN:
203
            case DataTypes.BYTE:
204
            case DataTypes.BYTEARRAY:
205
            case DataTypes.CHAR:
206
            case DataTypes.DATE:
207
            case DataTypes.GEOMETRY:
208
            case DataTypes.INT:
209
            case DataTypes.LONG:
210
            case DataTypes.OBJECT:
211
            case DataTypes.LIST: // Solo para campos calculados ???
212
            case DataTypes.STRING:
213
            case DataTypes.TIME:
214
            case DataTypes.TIMESTAMP:
215
            case DataTypes.URI:
216
            case DataTypes.URL:
217
            case DataTypes.FILE:
218
                break;
219

    
220
            default:
221
                throw new UnsupportedDataTypeException(name, type);
222
        }
223
        attr.setDataType(type);
224
        attr.setIndex(this.size());
225
        
226
        this.hasStrongChanges = this.hasStrongChanges || strongChanges;
227
        super.add(attr);
228
        this.pk = null;
229
        return attr;
230
    }
231

    
232
    public EditableFeatureAttributeDescriptor add(String name, int type,
233
            int size) {
234
        return this.add(name, type, true).setSize(size);
235
    }
236

    
237
    public EditableFeatureAttributeDescriptor add(String name, int type,
238
            Evaluator evaluator) {
239
        if (evaluator == null) {
240
            throw new IllegalArgumentException();
241
        }
242
        return this.add(name, type, false).setEvaluator(evaluator);
243
    }
244

    
245
    public EditableFeatureAttributeDescriptor add(String name, int type,
246
            FeatureAttributeEmulator emulator) {
247
        if (emulator == null) {
248
            throw new IllegalArgumentException();
249
        }
250
        return this.add(name, type, false).setFeatureAttributeEmulator(emulator);
251
    }
252
    
253
    public EditableFeatureAttributeDescriptor add(String name, String type) {
254
        return this.add(name,ToolsLocator.getDataTypesManager().getType(type));
255
    }
256

    
257
    public EditableFeatureAttributeDescriptor add(String name, String type, int size) {
258
        return this.add(name,ToolsLocator.getDataTypesManager().getType(type), size);
259
    }
260

    
261
    public Object removeAttributeDescriptor(String name) {
262
//    Hemos metido los metodos removeAttributeDescriptor ya que existe
263
//    un problema desde python al llamar a los metodos remove. En lugar de llamar
264
//    a estos metodos remove, llama a los de la superclase ArrayList.
265
        return this.remove(name);
266
    }
267
    
268
    public boolean removeAttributeDescriptor(EditableFeatureAttributeDescriptor attribute) {
269
        return this.remove(attribute);
270
    }
271

    
272
    @Override
273
    public FeatureAttributeDescriptor remove(int index) {
274
        FeatureAttributeDescriptor attr = (FeatureAttributeDescriptor) this.get(index);
275
        if ( !attr.isComputed() ) {
276
            hasStrongChanges = true;
277
        }
278
        if( index == this.getDefaultGeometryAttributeIndex() ) {
279
            this.defaultGeometryAttributeIndex  = -1;
280
            this.defaultGeometryAttributeName = null;
281
        } else if( index == this.getDefaultTimeAttributeIndex() ) {
282
            this.defaultTimeAttributeIndex = -1;
283
            this.defaultTimeAttributeName = null;
284
        }
285
        super.remove(attr);
286
        this.pk = null;
287
        this.fixAll();
288
        return attr;
289
    }
290
    
291
    @Override
292
    public Object remove(String name) {
293
        FeatureAttributeDescriptor attr = (FeatureAttributeDescriptor) this.get(name);
294
        if (attr == null) {
295
            return null;
296
        }
297
        this.remove(attr.getIndex());
298
        return attr;
299
    }
300

    
301
    @Override
302
    public boolean remove(EditableFeatureAttributeDescriptor attribute) {
303
        this.remove(attribute.getIndex());
304
        return true;
305
    }
306

    
307
    @Override
308
    protected void fixAll() {
309
        super.fixAll();
310
        for (FeatureAttributeDescriptor attr : this) {
311
            if (attr instanceof DefaultEditableFeatureAttributeDescriptor) {
312
                DefaultEditableFeatureAttributeDescriptor attr2 = (DefaultEditableFeatureAttributeDescriptor) attr;
313
                if (attr2.hasStrongChanges()) {
314
                    this.hasStrongChanges = true;
315
                    break;
316
                }
317
            }
318
        }
319
    }
320

    
321
    public void checkIntegrity() throws DataListException {
322
        Iterator iter = super.iterator();
323
        FeatureTypeIntegrityException ex = null;
324

    
325
        while (iter.hasNext()) {
326
            DefaultEditableFeatureAttributeDescriptor attr = (DefaultEditableFeatureAttributeDescriptor) iter
327
                    .next();
328
            try {
329
                attr.checkIntegrity();
330
            } catch (Exception e) {
331
                if (ex == null) {
332
                    ex = new FeatureTypeIntegrityException(this.getId());
333
                }
334
                ex.add(e);
335
            }
336
        }
337
        if (ex != null) {
338
            throw ex;
339
        }
340
    }
341

    
342
    @Override
343
    public void setDefaultGeometryType(int type, int subType) {
344
        DefaultEditableFeatureType descr = (DefaultEditableFeatureType)this.getDefaultGeometryAttribute();
345
        if( descr == null ) {
346
            throw new IllegalStateException("Default geometry attribute not set.");
347
        }
348
        descr.setDefaultGeometryType(type, subType);
349
    }
350

    
351
    public void setDefaultGeometryAttributeName(String name) {
352
        if (name == null || name.length() == 0) {
353
            this.defaultGeometryAttributeName = null;
354
            this.defaultGeometryAttributeIndex = -1;
355
            return;
356
        }
357
        DefaultFeatureAttributeDescriptor attr = (DefaultFeatureAttributeDescriptor) this
358
                .get(name);
359
        if (attr == null) {
360
            throw new IllegalArgumentException("Attribute '" + name
361
                    + "' not found.");
362
        }
363
//        if (attr.getType() != DataTypes.GEOMETRY) {
364
//            throw new IllegalArgumentException("Attribute '" + name
365
//                    + "' is not a geometry.");
366
//        }
367
        this.defaultGeometryAttributeName = name;
368
        this.defaultGeometryAttributeIndex = attr.getIndex();
369
    }
370

    
371
    public void setHasOID(boolean hasOID) {
372
        this.hasOID = hasOID;
373
    }
374

    
375
    protected Iterator getIterator(Iterator iter) {
376
        return new EditableDelegatedIterator(iter, this);
377
    }
378

    
379
    public EditableFeatureAttributeDescriptor getEditableAttributeDescriptor(String name) {
380
        return (EditableFeatureAttributeDescriptor) this.getAttributeDescriptor(name);
381
    }
382

    
383
    public EditableFeatureAttributeDescriptor getEditableAttributeDescriptor(int index) {
384
        return (EditableFeatureAttributeDescriptor) this.getAttributeDescriptor(index);
385
    }
386

    
387
    protected class EditableDelegatedIterator extends DelegatedIterator {
388

    
389
        private DefaultEditableFeatureType fType;
390

    
391
        public EditableDelegatedIterator(Iterator iter,
392
                DefaultEditableFeatureType fType) {
393
            super(iter);
394
            this.fType = fType;
395
        }
396

    
397
        public void remove() {
398
            this.iterator.remove();
399
            this.fType.fixAll();
400
        }
401

    
402
    }
403

    
404
    protected void setAllowAutomaticValues(boolean value) {
405
        this.allowAtomaticValues = value;
406
    }
407
    
408
    @Override
409
    public void copyFrom(FeatureType other) {
410
        super.copyFrom(other);
411
        if (other instanceof EditableFeatureType) {
412
            DefaultEditableFeatureType other2 = (DefaultEditableFeatureType) other;
413
            this.hasStrongChanges = other2.hasStrongChanges();
414
            this.source = (DefaultFeatureType) other2.getSource();
415
        } else {
416
            this.hasStrongChanges = false;
417
            this.source = (DefaultFeatureType) other;
418
        }
419
    }
420

    
421
}