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

History | View | Annotate | Download (15.1 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
import javax.json.JsonObject;
29

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

    
43
@SuppressWarnings("UseSpecificCatch")
44
public class DefaultEditableFeatureType extends DefaultFeatureType implements
45
        EditableFeatureType {
46

    
47
    /**
48
     *
49
     */
50
    private static final long serialVersionUID = -713976880396024389L;
51

    
52
    private boolean hasStrongChanges;
53
    private DefaultFeatureType source;
54

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

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

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

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

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

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

    
104
    @Override
105
    public FeatureType getCopy() {
106
        DefaultEditableFeatureType copy = new DefaultEditableFeatureType(this);
107
        copy.fixAll();
108
        return copy;
109
    }
110

    
111
    @Override
112
    public EditableFeatureType getEditable() {
113
        return (EditableFeatureType) this.getCopy();
114
    }
115

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

    
136
    @Override
137
    public EditableFeatureAttributeDescriptor addLike(
138
            FeatureAttributeDescriptor other) {
139
        DefaultEditableFeatureAttributeDescriptor editableAttr;
140
        editableAttr = new DefaultEditableFeatureAttributeDescriptor(
141
                (DefaultFeatureAttributeDescriptor) other);
142
        super.add(editableAttr);
143
        if (!editableAttr.isComputed()) {
144
            this.hasStrongChanges = true;
145
        }
146
        this.fixAll();
147
        return editableAttr;
148
    }
149

    
150
    @Override
151
    public FeatureType getSource() {
152
        return source;
153
    }
154

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

    
170
    @Override
171
    public EditableFeatureAttributeDescriptor add(String name, int type) {
172
        return this.add(name, type, true);
173
    }
174

    
175
    @Override
176
    public void removeAll() {
177
        while (!super.isEmpty()) {
178
            super.remove(0);
179
        }
180
        this.fixAll();
181
    }
182

    
183
    @Override
184
    public void addAll(FeatureType attributes) {
185
        super.addAll(attributes);
186
        this.fixAll();
187
    }
188

    
189
    private EditableFeatureAttributeDescriptor add(String name, int type, boolean strongChanges) {
190
        DefaultEditableFeatureAttributeDescriptor attr = new DefaultEditableFeatureAttributeDescriptor(this, strongChanges);
191
        Iterator iter = this.iterator();
192
        while (iter.hasNext()) {
193
            EditableFeatureAttributeDescriptor descriptor = (EditableFeatureAttributeDescriptor) iter.next();
194
            if (descriptor.getName().equalsIgnoreCase(name)) {
195
                throw new RuntimeException(
196
                        MessageFormat.format("Name descriptor {0} duplicated.", name)
197
                );
198
            }
199

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

    
224
            default:
225
                throw new UnsupportedDataTypeException(name, type);
226
        }
227
        attr.setDataType(type);
228
        attr.setIndex(this.size());
229

    
230
        this.hasStrongChanges = this.hasStrongChanges || strongChanges;
231
        super.add(attr);
232
        this.pk = null;
233
        return attr;
234
    }
235

    
236
    @Override
237
    public EditableFeatureAttributeDescriptor add(String name, int type, int size) {
238
        return this.add(name, type, true).setSize(size);
239
    }
240

    
241
    @Override
242
    public EditableFeatureAttributeDescriptor add(String name, int type,
243
            Evaluator evaluator) {
244
        if (evaluator == null) {
245
            throw new IllegalArgumentException();
246
        }
247
        return this.add(name, type, false).setEvaluator(evaluator);
248
    }
249

    
250
    @Override
251
    public EditableFeatureAttributeDescriptor add(String name, int type,
252
            FeatureAttributeEmulator emulator) {
253
        if (emulator == null) {
254
            throw new IllegalArgumentException();
255
        }
256
        return this.add(name, type, false).setFeatureAttributeEmulator(emulator);
257
    }
258

    
259
    @Override
260
    public EditableFeatureAttributeDescriptor add(String name, String type) {
261
        return this.add(name, ToolsLocator.getDataTypesManager().getType(type));
262
    }
263

    
264
    @Override
265
    public EditableFeatureAttributeDescriptor add(String name, String type, int size) {
266
        return this.add(name, ToolsLocator.getDataTypesManager().getType(type), size);
267
    }
268

    
269
    public Object removeAttributeDescriptor(String name) {
270
//    Hemos metido los metodos removeAttributeDescriptor ya que existe
271
//    un problema desde python al llamar a los metodos remove. En lugar de llamar
272
//    a estos metodos remove, llama a los de la superclase ArrayList.
273
        return this.remove(name);
274
    }
275

    
276
    public boolean removeAttributeDescriptor(EditableFeatureAttributeDescriptor attribute) {
277
        return this.remove(attribute);
278
    }
279

    
280
    @Override
281
    public FeatureAttributeDescriptor remove(int index) {
282
        FeatureAttributeDescriptor attr = (FeatureAttributeDescriptor) this.get(index);
283
        if (!attr.isComputed()) {
284
            hasStrongChanges = true;
285
        }
286
        if (index == this.getDefaultGeometryAttributeIndex()) {
287
            this.defaultGeometryAttributeIndex = -1;
288
            this.defaultGeometryAttributeName = null;
289
        } else if (index == this.getDefaultTimeAttributeIndex()) {
290
            this.defaultTimeAttributeIndex = -1;
291
            this.defaultTimeAttributeName = null;
292
        }
293
        super.remove(attr);
294
        this.pk = null;
295
        this.fixAll();
296
        return attr;
297
    }
298

    
299
    @Override
300
    public Object remove(String name) {
301
        FeatureAttributeDescriptor attr = (FeatureAttributeDescriptor) this.get(name);
302
        if (attr == null) {
303
            return null;
304
        }
305
        this.remove(attr.getIndex());
306
        return attr;
307
    }
308

    
309
    @Override
310
    public boolean remove(EditableFeatureAttributeDescriptor attribute) {
311
        this.remove(attribute.getIndex());
312
        return true;
313
    }
314

    
315
    @Override
316
    protected void fixAll() {
317
        super.fixAll();
318
        for (FeatureAttributeDescriptor attr : this) {
319
            if (attr instanceof DefaultEditableFeatureAttributeDescriptor) {
320
                DefaultEditableFeatureAttributeDescriptor attr2 = (DefaultEditableFeatureAttributeDescriptor) attr;
321
                if (attr2.hasStrongChanges()) {
322
                    this.hasStrongChanges = true;
323
                    break;
324
                }
325
            }
326
        }
327
    }
328

    
329
    public void checkIntegrity() throws DataListException {
330
        Iterator iter = super.iterator();
331
        FeatureTypeIntegrityException ex = null;
332

    
333
        while (iter.hasNext()) {
334
            DefaultEditableFeatureAttributeDescriptor attr = (DefaultEditableFeatureAttributeDescriptor) iter
335
                    .next();
336
            try {
337
                attr.checkIntegrity();
338
            } catch (Exception e) {
339
                if (ex == null) {
340
                    ex = new FeatureTypeIntegrityException(this.getId());
341
                }
342
                ex.add(e);
343
            }
344
        }
345
        if (ex != null) {
346
            throw ex;
347
        }
348
    }
349

    
350
    @Override
351
    public void setDefaultGeometryType(int type, int subType) {
352
        EditableFeatureAttributeDescriptor descr = (EditableFeatureAttributeDescriptor) this.getDefaultGeometryAttribute();
353
        if (descr == null) {
354
            throw new IllegalStateException("Default geometry attribute not set.");
355
        }
356
        descr.setGeometryType(type, subType);
357
    }
358

    
359
    @Override
360
    public void setDefaultGeometryAttributeName(String name) {
361
        if (name == null || name.length() == 0) {
362
            this.defaultGeometryAttributeName = null;
363
            this.defaultGeometryAttributeIndex = -1;
364
            return;
365
        }
366
        FeatureAttributeDescriptor attr = this.getAttributeDescriptor(name);
367
        if (attr == null) {
368
            throw new IllegalArgumentException("Attribute '" + name + "' not found.");
369
        }
370
        this.defaultGeometryAttributeName = name;
371
        this.defaultGeometryAttributeIndex = attr.getIndex();
372
    }
373

    
374
    @Override
375
    public int getDefaultGeometryAttributeIndex() {
376
        this.fixAll();
377
        return this.defaultGeometryAttributeIndex;
378
    }
379

    
380
    @Override
381
    public String getDefaultGeometryAttributeName() {
382
        this.fixAll();
383
        return this.defaultGeometryAttributeName;
384
    }
385

    
386
    @Override
387
    public FeatureAttributeDescriptor getDefaultGeometryAttribute() {
388
        this.fixAll();
389
        return super.getDefaultGeometryAttribute();
390
    }
391

    
392
    @Override
393
    public int getDefaultTimeAttributeIndex() {
394
        this.fixAll();
395
        return this.defaultTimeAttributeIndex;
396
    }
397

    
398
    @Override
399
    public String getDefaultTimeAttributeName() {
400
        this.fixAll();
401
        return this.defaultTimeAttributeName;
402
    }
403

    
404
    @Override
405
    public void setHasOID(boolean hasOID) {
406
        this.hasOID = hasOID;
407
    }
408

    
409
    @Override
410
    protected Iterator getIterator(Iterator iter) {
411
        return new EditableDelegatedIterator(iter, this);
412
    }
413

    
414
    @Override
415
    public EditableFeatureAttributeDescriptor getEditableAttributeDescriptor(String name) {
416
        return (EditableFeatureAttributeDescriptor) this.getAttributeDescriptor(name);
417
    }
418

    
419
    @Override
420
    public EditableFeatureAttributeDescriptor getEditableAttributeDescriptor(int index) {
421
        return (EditableFeatureAttributeDescriptor) this.getAttributeDescriptor(index);
422
    }
423

    
424
    @Override
425
    public void setCheckFeaturesAtFinishEditing(boolean check) {
426
        this.checkFeaturesAtFinishEditing = check;
427
    }
428

    
429
    @Override
430
    public void setCheckFeaturesAtInsert(boolean check) {
431
        this.checkFeaturesAtInsert = check;
432
    }
433

    
434
    protected class EditableDelegatedIterator extends DelegatedIterator {
435

    
436
        private final DefaultEditableFeatureType fType;
437

    
438
        public EditableDelegatedIterator(Iterator iter, DefaultEditableFeatureType fType) {
439
            super(iter);
440
            this.fType = fType;
441
        }
442

    
443
        @Override
444
        public void remove() {
445
            this.iterator.remove();
446
            this.fType.fixAll();
447
        }
448

    
449
    }
450

    
451
    protected void setAllowAutomaticValues(boolean value) {
452
        this.allowAtomaticValues = value;
453
    }
454

    
455
    @Override
456
    public void copyFrom(FeatureType other) {
457
        super.copyFrom(other);
458
        if (other instanceof EditableFeatureType) {
459
            DefaultEditableFeatureType other2 = (DefaultEditableFeatureType) other;
460
            this.hasStrongChanges = other2.hasStrongChanges();
461
            this.source = (DefaultFeatureType) other2.getSource();
462
        } else {
463
            this.hasStrongChanges = false;
464
            this.source = (DefaultFeatureType) other;
465
        }
466
    }
467

    
468
    public void copyFrom(JsonObject json) {
469

    
470
    }
471
}