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

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.exception.DataListException;
33
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
34
import org.gvsig.fmap.dal.feature.EditableFeatureType;
35
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
36
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator;
37
import org.gvsig.fmap.dal.feature.FeatureStore;
38
import org.gvsig.fmap.dal.feature.FeatureType;
39
import org.gvsig.fmap.dal.feature.exception.FeatureTypeIntegrityException;
40
import org.gvsig.fmap.dal.impl.DefaultDataManager;
41
import org.gvsig.tools.ToolsLocator;
42
import org.gvsig.tools.evaluator.Evaluator;
43

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

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

    
53
    private boolean hasStrongChanges;
54
    private DefaultFeatureType source;
55

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

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

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

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

    
80
    @Override
81
    protected DefaultFeatureAttributeDescriptor getCopyAttributeDescriptor(DefaultFeatureAttributeDescriptor src) {
82
        DefaultFeatureAttributeDescriptor copy = new DefaultEditableFeatureAttributeDescriptor(src);
83
        copy.setFeatureType(this);
84
        return copy;
85
    }
86
    
87
    public void forceStrongChanges() {
88
        this.hasStrongChanges = true;
89
    }
90

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

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

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

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

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

    
155
    @Override
156
    public FeatureType getSource() {
157
        return source;
158
    }
159

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
412
    protected class EditableDelegatedIterator extends DelegatedIterator {
413

    
414
        private final DefaultEditableFeatureType fType;
415

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

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

    
427
    }
428

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

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

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