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 @ 44801

History | View | Annotate | Download (14 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
        editableAttr = new DefaultEditableFeatureAttributeDescriptor(
134
                    (DefaultFeatureAttributeDescriptor) other);
135
        super.add(editableAttr);
136
        if( !editableAttr.isComputed() ) {
137
            this.hasStrongChanges = true;
138
        }        
139
        this.fixAll();
140
        return editableAttr;
141
    }
142

    
143
    public FeatureType getSource() {
144
        return source;
145
    }
146

    
147
    public FeatureType getNotEditableCopy() {
148
        this.fixAll();
149
        DefaultFeatureType copy = new DefaultFeatureType(this, false);
150
        Iterator iter = this.iterator();
151
        DefaultFeatureAttributeDescriptor attr;
152
        while (iter.hasNext()) {
153
            attr = (DefaultFeatureAttributeDescriptor) iter.next();
154
            DefaultFeatureAttributeDescriptor newattr = new DefaultFeatureAttributeDescriptor(attr);
155
            newattr.setFeatureType(copy);
156
            copy.add(newattr);
157
        }
158
        return copy;
159
    }
160

    
161
    public EditableFeatureAttributeDescriptor add(String name, int type) {
162
        return this.add(name, type, true);
163
    }
164

    
165
    @Override
166
    public void removeAll() {
167
        while( !super.isEmpty() ) {
168
            super.remove(0);
169
        }
170
        this.fixAll();
171
    }
172

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

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

    
214
            default:
215
                throw new UnsupportedDataTypeException(name, type);
216
        }
217
        attr.setDataType(type);
218
        attr.setIndex(this.size());
219
        
220
        this.hasStrongChanges = this.hasStrongChanges || strongChanges;
221
        super.add(attr);
222
        this.pk = null;
223
        return attr;
224
    }
225

    
226
    public EditableFeatureAttributeDescriptor add(String name, int type,
227
            int size) {
228
        return this.add(name, type, true).setSize(size);
229
    }
230

    
231
    public EditableFeatureAttributeDescriptor add(String name, int type,
232
            Evaluator evaluator) {
233
        if (evaluator == null) {
234
            throw new IllegalArgumentException();
235
        }
236
        return this.add(name, type, false).setEvaluator(evaluator);
237
    }
238

    
239
    public EditableFeatureAttributeDescriptor add(String name, int type,
240
            FeatureAttributeEmulator emulator) {
241
        if (emulator == null) {
242
            throw new IllegalArgumentException();
243
        }
244
        return this.add(name, type, false).setFeatureAttributeEmulator(emulator);
245
    }
246
    
247
    public EditableFeatureAttributeDescriptor add(String name, String type) {
248
        return this.add(name,ToolsLocator.getDataTypesManager().getType(type));
249
    }
250

    
251
    public EditableFeatureAttributeDescriptor add(String name, String type, int size) {
252
        return this.add(name,ToolsLocator.getDataTypesManager().getType(type), size);
253
    }
254

    
255
    public Object removeAttributeDescriptor(String name) {
256
//    Hemos metido los metodos removeAttributeDescriptor ya que existe
257
//    un problema desde python al llamar a los metodos remove. En lugar de llamar
258
//    a estos metodos remove, llama a los de la superclase ArrayList.
259
        return this.remove(name);
260
    }
261
    
262
    public boolean removeAttributeDescriptor(EditableFeatureAttributeDescriptor attribute) {
263
        return this.remove(attribute);
264
    }
265

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

    
295
    @Override
296
    public boolean remove(EditableFeatureAttributeDescriptor attribute) {
297
        this.remove(attribute.getIndex());
298
        return true;
299
    }
300

    
301
    @Override
302
    protected void fixAll() {
303
        super.fixAll();
304
        for (FeatureAttributeDescriptor attr : this) {
305
            if (attr instanceof DefaultEditableFeatureAttributeDescriptor) {
306
                DefaultEditableFeatureAttributeDescriptor attr2 = (DefaultEditableFeatureAttributeDescriptor) attr;
307
                if (attr2.hasStrongChanges()) {
308
                    this.hasStrongChanges = true;
309
                    break;
310
                }
311
            }
312
        }
313
    }
314

    
315
    public void checkIntegrity() throws DataListException {
316
        Iterator iter = super.iterator();
317
        FeatureTypeIntegrityException ex = null;
318

    
319
        while (iter.hasNext()) {
320
            DefaultEditableFeatureAttributeDescriptor attr = (DefaultEditableFeatureAttributeDescriptor) iter
321
                    .next();
322
            try {
323
                attr.checkIntegrity();
324
            } catch (Exception e) {
325
                if (ex == null) {
326
                    ex = new FeatureTypeIntegrityException(this.getId());
327
                }
328
                ex.add(e);
329
            }
330
        }
331
        if (ex != null) {
332
            throw ex;
333
        }
334
    }
335

    
336
    @Override
337
    public void setDefaultGeometryType(int type, int subType) {
338
        DefaultEditableFeatureType descr = (DefaultEditableFeatureType)this.getDefaultGeometryAttribute();
339
        if( descr == null ) {
340
            throw new IllegalStateException("Default geometry attribute not set.");
341
        }
342
        descr.setDefaultGeometryType(type, subType);
343
    }
344

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

    
365
    public void setHasOID(boolean hasOID) {
366
        this.hasOID = hasOID;
367
    }
368

    
369
    protected Iterator getIterator(Iterator iter) {
370
        return new EditableDelegatedIterator(iter, this);
371
    }
372

    
373
    public EditableFeatureAttributeDescriptor getEditableAttributeDescriptor(String name) {
374
        return (EditableFeatureAttributeDescriptor) this.getAttributeDescriptor(name);
375
    }
376

    
377
    public EditableFeatureAttributeDescriptor getEditableAttributeDescriptor(int index) {
378
        return (EditableFeatureAttributeDescriptor) this.getAttributeDescriptor(index);
379
    }
380

    
381
    protected class EditableDelegatedIterator extends DelegatedIterator {
382

    
383
        private DefaultEditableFeatureType fType;
384

    
385
        public EditableDelegatedIterator(Iterator iter,
386
                DefaultEditableFeatureType fType) {
387
            super(iter);
388
            this.fType = fType;
389
        }
390

    
391
        public void remove() {
392
            this.iterator.remove();
393
            this.fType.fixAll();
394
        }
395

    
396
    }
397

    
398
    protected void setAllowAutomaticValues(boolean value) {
399
        this.allowAtomaticValues = value;
400
    }
401
    
402
    @Override
403
    public void copyFrom(FeatureType other) {
404
        super.copyFrom(other);
405
        if (other instanceof EditableFeatureType) {
406
            DefaultEditableFeatureType other2 = (DefaultEditableFeatureType) other;
407
            this.hasStrongChanges = other2.hasStrongChanges();
408
            this.source = (DefaultFeatureType) other2.getSource();
409
        } else {
410
            this.hasStrongChanges = false;
411
            this.source = (DefaultFeatureType) other;
412
        }
413
    }
414

    
415
}