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

History | View | Annotate | Download (15.3 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
import org.apache.commons.lang3.StringUtils;
30
import org.gvsig.fmap.dal.DataTypeUtils;
31

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

    
46
@SuppressWarnings("UseSpecificCatch")
47
public class DefaultEditableFeatureType extends DefaultFeatureType implements
48
        EditableFeatureType {
49

    
50
    /**
51
     *
52
     */
53
    private static final long serialVersionUID = -713976880396024389L;
54

    
55
    private boolean hasStrongChanges;
56
    private DefaultFeatureType source;
57

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

    
64
    public DefaultEditableFeatureType(FeatureStore store, String id) {
65
        super(store, id);
66
        this.hasStrongChanges = false;
67
        this.source = null;
68
    }
69

    
70
    protected DefaultEditableFeatureType(DefaultEditableFeatureType other) {
71
        super(other);
72
        this.hasStrongChanges = other.hasStrongChanges;
73
        this.source = (DefaultFeatureType) other.getSource();
74
    }
75

    
76
    protected DefaultEditableFeatureType(DefaultFeatureType other) {
77
        super(other);
78
        this.source = other;
79
        this.fixAll();
80
    }
81

    
82
    @Override
83
    protected DefaultFeatureAttributeDescriptor getCopyAttributeDescriptor(DefaultFeatureAttributeDescriptor src) {
84
        DefaultFeatureAttributeDescriptor copy = new DefaultEditableFeatureAttributeDescriptor(src);
85
        copy.setFeatureType(this);
86
        return copy;
87
    }
88

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

    
107
    @Override
108
    public FeatureType getCopy() {
109
        DefaultEditableFeatureType copy = new DefaultEditableFeatureType(this);
110
        copy.fixAll();
111
        return copy;
112
    }
113

    
114
    @Override
115
    public EditableFeatureType getEditable() {
116
        return (EditableFeatureType) this.getCopy();
117
    }
118

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

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

    
153
    @Override
154
    public FeatureType getSource() {
155
        return source;
156
    }
157

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

    
173
    @Override
174
    public EditableFeatureAttributeDescriptor add(String name, int type) {
175
        return this.add(name, type, true);
176
    }
177

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

    
186
    @Override
187
    public void addAll(FeatureType attributes) {
188
        super.addAll(attributes);
189
        this.fixAll();
190
    }
191

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

    
202
        }
203
        DefaultEditableFeatureAttributeDescriptor attr = DefaultDataManager.createEditableFeatureAttributeDescriptor(this, name, type, strongChanges);
204
        attr.setIndex(this.size());
205

    
206
        this.hasStrongChanges = this.hasStrongChanges || strongChanges;
207
        super.add(attr);
208
        this.pk = null;
209
        return attr;
210
    }
211

    
212
    @Override
213
    public EditableFeatureAttributeDescriptor add(String name, int type, int size) {
214
        return this.add(name, type, true).setSize(size);
215
    }
216

    
217
    @Override
218
    public EditableFeatureAttributeDescriptor add(String name, int type,
219
            Evaluator evaluator) {
220
        if (evaluator == null) {
221
            throw new IllegalArgumentException();
222
        }
223
        return this.add(name, type, false).setEvaluator(evaluator);
224
    }
225

    
226
    @Override
227
    public EditableFeatureAttributeDescriptor add(String name, int type,
228
            FeatureAttributeEmulator emulator) {
229
        if (emulator == null) {
230
            throw new IllegalArgumentException();
231
        }
232
        return this.add(name, type, false).setFeatureAttributeEmulator(emulator);
233
    }
234

    
235
    @Override
236
    public EditableFeatureAttributeDescriptor add(String name, String type) {
237
        return this.add(name, ToolsLocator.getDataTypesManager().getType(type));
238
    }
239

    
240
    @Override
241
    public EditableFeatureAttributeDescriptor add(String name, String type, int size) {
242
        return this.add(name, ToolsLocator.getDataTypesManager().getType(type), size);
243
    }
244

    
245
    public Object removeAttributeDescriptor(String name) {
246
//    Hemos metido los metodos removeAttributeDescriptor ya que existe
247
//    un problema desde python al llamar a los metodos remove. En lugar de llamar
248
//    a estos metodos remove, llama a los de la superclase ArrayList.
249
        return this.remove(name);
250
    }
251

    
252
    public boolean removeAttributeDescriptor(EditableFeatureAttributeDescriptor attribute) {
253
        return this.remove(attribute);
254
    }
255

    
256
    @Override
257
    public FeatureAttributeDescriptor remove(int index) {
258
        FeatureAttributeDescriptor attr = (FeatureAttributeDescriptor) this.get(index);
259
        if (!attr.isComputed()) {
260
            hasStrongChanges = true;
261
        }
262
        if (index == this.getDefaultGeometryAttributeIndex()) {
263
            this.defaultGeometryAttributeIndex = -1;
264
            this.defaultGeometryAttributeName = null;
265
        } else if (index == this.getDefaultTimeAttributeIndex()) {
266
            this.defaultTimeAttributeIndex = -1;
267
            this.defaultTimeAttributeName = null;
268
        }
269
        super.remove(attr);
270
        this.pk = null;
271
        this.fixAll();
272
        return attr;
273
    }
274

    
275
    @Override
276
    public Object remove(String name) {
277
        FeatureAttributeDescriptor attr = (FeatureAttributeDescriptor) this.get(name);
278
        if (attr == null) {
279
            return null;
280
        }
281
        this.remove(attr.getIndex());
282
        return attr;
283
    }
284

    
285
    @Override
286
    public boolean remove(EditableFeatureAttributeDescriptor attribute) {
287
        this.remove(attribute.getIndex());
288
        return true;
289
    }
290

    
291
    @Override
292
    protected void fixAll() {
293
        super.fixAll();
294
        for (FeatureAttributeDescriptor attr : this) {
295
            if (attr instanceof DefaultEditableFeatureAttributeDescriptor) {
296
                DefaultEditableFeatureAttributeDescriptor attr2 = (DefaultEditableFeatureAttributeDescriptor) attr;
297
                if (attr2.hasStrongChanges()) {
298
                    this.hasStrongChanges = true;
299
                    break;
300
                }
301
            }
302
        }
303
    }
304

    
305
    public void checkIntegrity() throws DataListException {
306
        Iterator iter = super.iterator();
307
        FeatureTypeIntegrityException ex = null;
308

    
309
        while (iter.hasNext()) {
310
            DefaultEditableFeatureAttributeDescriptor attr = (DefaultEditableFeatureAttributeDescriptor) iter
311
                    .next();
312
            try {
313
                attr.checkIntegrity();
314
            } catch (Exception e) {
315
                if (ex == null) {
316
                    ex = new FeatureTypeIntegrityException(this.getId());
317
                }
318
                ex.add(e);
319
            }
320
        }
321
        if (ex != null) {
322
            throw ex;
323
        }
324
    }
325

    
326
    @Override
327
    public void setDefaultGeometryType(int type, int subType) {
328
        EditableFeatureAttributeDescriptor descr = (EditableFeatureAttributeDescriptor) this.getDefaultGeometryAttribute();
329
        if (descr == null) {
330
            throw new IllegalStateException("Default geometry attribute not set.");
331
        }
332
        descr.setGeometryType(type, subType);
333
    }
334

    
335
    @Override
336
    public void setDefaultGeometryAttributeName(String name) {
337
        if (name == null || name.length() == 0) {
338
            this.defaultGeometryAttributeName = null;
339
            this.defaultGeometryAttributeIndex = -1;
340
            return;
341
        }
342
        FeatureAttributeDescriptor attr = this.getAttributeDescriptor(name);
343
        if (attr == null) {
344
            throw new IllegalArgumentException("Attribute '" + name + "' not found.");
345
        }
346
        this.defaultGeometryAttributeName = name;
347
        this.defaultGeometryAttributeIndex = attr.getIndex();
348
    }
349

    
350
    @Override
351
    public int getDefaultGeometryAttributeIndex() {
352
        this.fixAll();
353
        return this.defaultGeometryAttributeIndex;
354
    }
355

    
356
    @Override
357
    public String getDefaultGeometryAttributeName() {
358
        this.fixAll();
359
        return this.defaultGeometryAttributeName;
360
    }
361

    
362
    @Override
363
    public FeatureAttributeDescriptor getDefaultGeometryAttribute() {
364
        this.fixAll();
365
        return super.getDefaultGeometryAttribute();
366
    }
367

    
368
    @Override
369
    public int getDefaultTimeAttributeIndex() {
370
        this.fixAll();
371
        return this.defaultTimeAttributeIndex;
372
    }
373

    
374
    @Override
375
    public String getDefaultTimeAttributeName() {
376
        this.fixAll();
377
        return this.defaultTimeAttributeName;
378
    }
379

    
380
    @Override
381
    public void setHasOID(boolean hasOID) {
382
        this.hasOID = hasOID;
383
    }
384

    
385
    @Override
386
    protected Iterator getIterator(Iterator iter) {
387
        return new EditableDelegatedIterator(iter, this);
388
    }
389

    
390
    @Override
391
    public EditableFeatureAttributeDescriptor getEditableAttributeDescriptor(String name) {
392
        return (EditableFeatureAttributeDescriptor) this.getAttributeDescriptor(name);
393
    }
394

    
395
    @Override
396
    public EditableFeatureAttributeDescriptor getEditableAttributeDescriptor(int index) {
397
        return (EditableFeatureAttributeDescriptor) this.getAttributeDescriptor(index);
398
    }
399

    
400
    @Override
401
    public void setCheckFeaturesAtFinishEditing(boolean check) {
402
        this.checkFeaturesAtFinishEditing = check;
403
    }
404

    
405
    @Override
406
    public void setCheckFeaturesAtInsert(boolean check) {
407
        this.checkFeaturesAtInsert = check;
408
    }
409

    
410
    protected class EditableDelegatedIterator extends DelegatedIterator {
411

    
412
        private final DefaultEditableFeatureType fType;
413

    
414
        public EditableDelegatedIterator(Iterator iter, DefaultEditableFeatureType fType) {
415
            super(iter);
416
            this.fType = fType;
417
        }
418

    
419
        @Override
420
        public void remove() {
421
            this.iterator.remove();
422
            this.fType.fixAll();
423
        }
424

    
425
    }
426

    
427
    protected void setAllowAutomaticValues(boolean value) {
428
        this.allowAtomaticValues = value;
429
    }
430

    
431
    @Override
432
    public void copyFrom(FeatureType other) {
433
        super.copyFrom(other);
434
        if (other instanceof EditableFeatureType) {
435
            DefaultEditableFeatureType other2 = (DefaultEditableFeatureType) other;
436
            this.hasStrongChanges = other2.hasStrongChanges();
437
            this.source = (DefaultFeatureType) other2.getSource();
438
        } else {
439
            this.hasStrongChanges = false;
440
            this.source = (DefaultFeatureType) other;
441
        }
442
    }
443

    
444
    public void copyFrom(JsonObject json) {
445
        // TODO: falta por implementar copyFrom(json)
446
    }
447
    
448
    @Override
449
    public void set(String name, String value) {
450
        if (StringUtils.isBlank(name)) {
451
            throw new IllegalArgumentException("Name can't be empty");
452
        }
453
        switch (name.trim().toLowerCase()) {
454
            case "checkfeaturesatfinishediting":
455
                this.setCheckFeaturesAtFinishEditing(DataTypeUtils.toBoolean(value, false));
456
                break;
457
            case "checkfeaturesatinsert":
458
                this.setCheckFeaturesAtInsert(DataTypeUtils.toBoolean(value, false));
459
                break;
460
            case "defaultgeometryattributename":
461
            case "defaultgeometryname":
462
            case "defaultgeometry":
463
                this.setDefaultGeometryAttributeName(DataTypeUtils.toString(value, null));
464
                break;
465
            default:
466
                throw new IllegalArgumentException("Name attribute '" + name + "' not valid.");
467
        }
468
    }
469
    
470
}