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

History | View | Annotate | Download (15.4 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 void forceStrongChanges() {
90
        this.hasStrongChanges = true;
91
    }
92

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

    
111
    @Override
112
    public FeatureType getCopy() {
113
        DefaultEditableFeatureType copy = new DefaultEditableFeatureType(this);
114
        copy.fixAll();
115
        return copy;
116
    }
117

    
118
    @Override
119
    public EditableFeatureType getEditable() {
120
        return (EditableFeatureType) this.getCopy();
121
    }
122

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

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

    
157
    @Override
158
    public FeatureType getSource() {
159
        return source;
160
    }
161

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

    
177
    @Override
178
    public EditableFeatureAttributeDescriptor add(String name, int type) {
179
        return this.add(name, type, true);
180
    }
181

    
182
    @Override
183
    public void removeAll() {
184
        while (!super.isEmpty()) {
185
            super.remove(0);
186
        }
187
        this.fixAll();
188
    }
189

    
190
    @Override
191
    public void addAll(FeatureType attributes) {
192
        super.addAll(attributes);
193
        this.fixAll();
194
    }
195

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

    
206
        }
207
        DefaultEditableFeatureAttributeDescriptor attr = DefaultDataManager.createEditableFeatureAttributeDescriptor(this, name, type, strongChanges);
208
        attr.setIndex(this.size());
209

    
210
        this.hasStrongChanges = this.hasStrongChanges || strongChanges;
211
        super.add(attr);
212
        this.pk = null;
213
        return attr;
214
    }
215

    
216
    @Override
217
    public EditableFeatureAttributeDescriptor add(String name, int type, int size) {
218
        return this.add(name, type, true).setSize(size);
219
    }
220

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

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

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

    
244
    @Override
245
    public EditableFeatureAttributeDescriptor add(String name, String type, int size) {
246
        return this.add(name, ToolsLocator.getDataTypesManager().getType(type), size);
247
    }
248

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

    
256
    public boolean removeAttributeDescriptor(EditableFeatureAttributeDescriptor attribute) {
257
        return this.remove(attribute);
258
    }
259

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

    
279
    @Override
280
    public Object remove(String name) {
281
        FeatureAttributeDescriptor attr = (FeatureAttributeDescriptor) this.get(name);
282
        if (attr == null) {
283
            return null;
284
        }
285
        this.remove(attr.getIndex());
286
        return attr;
287
    }
288

    
289
    @Override
290
    public boolean remove(EditableFeatureAttributeDescriptor attribute) {
291
        this.remove(attribute.getIndex());
292
        return true;
293
    }
294

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

    
309
    public void checkIntegrity() throws DataListException {
310
        Iterator iter = super.iterator();
311
        FeatureTypeIntegrityException ex = null;
312

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

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

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

    
354
    @Override
355
    public int getDefaultGeometryAttributeIndex() {
356
        this.fixAll();
357
        return this.defaultGeometryAttributeIndex;
358
    }
359

    
360
    @Override
361
    public String getDefaultGeometryAttributeName() {
362
        this.fixAll();
363
        return this.defaultGeometryAttributeName;
364
    }
365

    
366
    @Override
367
    public FeatureAttributeDescriptor getDefaultGeometryAttribute() {
368
        this.fixAll();
369
        return super.getDefaultGeometryAttribute();
370
    }
371

    
372
    @Override
373
    public int getDefaultTimeAttributeIndex() {
374
        this.fixAll();
375
        return this.defaultTimeAttributeIndex;
376
    }
377

    
378
    @Override
379
    public String getDefaultTimeAttributeName() {
380
        this.fixAll();
381
        return this.defaultTimeAttributeName;
382
    }
383

    
384
    @Override
385
    public void setHasOID(boolean hasOID) {
386
        this.hasOID = hasOID;
387
    }
388

    
389
    @Override
390
    protected Iterator getIterator(Iterator iter) {
391
        return new EditableDelegatedIterator(iter, this);
392
    }
393

    
394
    @Override
395
    public EditableFeatureAttributeDescriptor getEditableAttributeDescriptor(String name) {
396
        return (EditableFeatureAttributeDescriptor) this.getAttributeDescriptor(name);
397
    }
398

    
399
    @Override
400
    public EditableFeatureAttributeDescriptor getEditableAttributeDescriptor(int index) {
401
        return (EditableFeatureAttributeDescriptor) this.getAttributeDescriptor(index);
402
    }
403

    
404
    @Override
405
    public void setCheckFeaturesAtFinishEditing(boolean check) {
406
        this.checkFeaturesAtFinishEditing = check;
407
    }
408

    
409
    @Override
410
    public void setCheckFeaturesAtInsert(boolean check) {
411
        this.checkFeaturesAtInsert = check;
412
    }
413

    
414
    protected class EditableDelegatedIterator extends DelegatedIterator {
415

    
416
        private final DefaultEditableFeatureType fType;
417

    
418
        public EditableDelegatedIterator(Iterator iter, DefaultEditableFeatureType fType) {
419
            super(iter);
420
            this.fType = fType;
421
        }
422

    
423
        @Override
424
        public void remove() {
425
            this.iterator.remove();
426
            this.fType.fixAll();
427
        }
428

    
429
    }
430

    
431
    protected void setAllowAutomaticValues(boolean value) {
432
        this.allowAtomaticValues = value;
433
    }
434

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

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