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

History | View | Annotate | Download (14.8 KB)

1 40559 jjdelcerro
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6 42290 jjdelcerro
 * 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 40559 jjdelcerro
 *
11 42290 jjdelcerro
 * 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 40559 jjdelcerro
 *
16 42290 jjdelcerro
 * 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 40559 jjdelcerro
 *
20 42290 jjdelcerro
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22 40559 jjdelcerro
 */
23 40435 jjdelcerro
package org.gvsig.fmap.dal.feature.impl;
24
25
import java.text.MessageFormat;
26
27
import java.util.Iterator;
28 45154 jjdelcerro
import javax.json.JsonObject;
29 40435 jjdelcerro
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 41335 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator;
36 43739 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureStore;
37 40435 jjdelcerro
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 42293 jjdelcerro
import org.gvsig.tools.ToolsLocator;
41 40435 jjdelcerro
import org.gvsig.tools.evaluator.Evaluator;
42
43 45158 jjdelcerro
@SuppressWarnings("UseSpecificCatch")
44 40435 jjdelcerro
public class DefaultEditableFeatureType extends DefaultFeatureType implements
45 42290 jjdelcerro
        EditableFeatureType {
46 40435 jjdelcerro
47 42290 jjdelcerro
    /**
48
     *
49
     */
50
    private static final long serialVersionUID = -713976880396024389L;
51 40435 jjdelcerro
52 42290 jjdelcerro
    private boolean hasStrongChanges;
53
    private DefaultFeatureType source;
54 40435 jjdelcerro
55 43739 jjdelcerro
    public DefaultEditableFeatureType(FeatureStore store) {
56
        super(store);
57 42290 jjdelcerro
        this.hasStrongChanges = false;
58
        this.source = null;
59
    }
60 40435 jjdelcerro
61 43739 jjdelcerro
    public DefaultEditableFeatureType(FeatureStore store, String id) {
62
        super(store, id);
63 42290 jjdelcerro
        this.hasStrongChanges = false;
64
        this.source = null;
65
    }
66 40435 jjdelcerro
67 42290 jjdelcerro
    protected DefaultEditableFeatureType(DefaultEditableFeatureType other) {
68
        super(other);
69 44582 omartinez
        this.hasStrongChanges = other.hasStrongChanges;
70 42290 jjdelcerro
        this.source = (DefaultFeatureType) other.getSource();
71
    }
72 40435 jjdelcerro
73 42290 jjdelcerro
    protected DefaultEditableFeatureType(DefaultFeatureType other) {
74
        super(other);
75
        this.source = other;
76 44582 omartinez
        this.fixAll();
77 42290 jjdelcerro
    }
78 40435 jjdelcerro
79 43739 jjdelcerro
    @Override
80 44318 jjdelcerro
    protected DefaultFeatureAttributeDescriptor getCopyAttributeDescriptor(DefaultFeatureAttributeDescriptor src) {
81
        DefaultFeatureAttributeDescriptor copy = new DefaultEditableFeatureAttributeDescriptor(src);
82
        copy.setFeatureType(this);
83
        return copy;
84 42290 jjdelcerro
    }
85 40435 jjdelcerro
86 42290 jjdelcerro
    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 43981 omartinez
            if (attr.isComputed()) {
95
                continue;
96
            }
97 42290 jjdelcerro
            if (attr.hasStrongChanges()) {
98
                return true;
99
            }
100
        }
101
        return false;
102
    }
103 40435 jjdelcerro
104 45152 fdiaz
    @Override
105 42290 jjdelcerro
    public FeatureType getCopy() {
106 45152 fdiaz
        DefaultEditableFeatureType copy = new DefaultEditableFeatureType(this);
107
        copy.fixAll();
108
        return copy;
109 42290 jjdelcerro
    }
110 40435 jjdelcerro
111 45152 fdiaz
    @Override
112 42290 jjdelcerro
    public EditableFeatureType getEditable() {
113 44669 jjdelcerro
        return (EditableFeatureType) this.getCopy();
114 42290 jjdelcerro
    }
115 40435 jjdelcerro
116 42290 jjdelcerro
    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 40435 jjdelcerro
136 45158 jjdelcerro
    @Override
137 42290 jjdelcerro
    public EditableFeatureAttributeDescriptor addLike(
138
            FeatureAttributeDescriptor other) {
139
        DefaultEditableFeatureAttributeDescriptor editableAttr;
140 44801 omartinez
        editableAttr = new DefaultEditableFeatureAttributeDescriptor(
141 45159 jjdelcerro
                (DefaultFeatureAttributeDescriptor) other);
142 42290 jjdelcerro
        super.add(editableAttr);
143 45159 jjdelcerro
        if (!editableAttr.isComputed()) {
144 44669 jjdelcerro
            this.hasStrongChanges = true;
145 45159 jjdelcerro
        }
146 42290 jjdelcerro
        this.fixAll();
147
        return editableAttr;
148
    }
149 40435 jjdelcerro
150 45158 jjdelcerro
    @Override
151 42290 jjdelcerro
    public FeatureType getSource() {
152
        return source;
153
    }
154 40435 jjdelcerro
155 45158 jjdelcerro
    @Override
156 42290 jjdelcerro
    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 44259 jjdelcerro
            DefaultFeatureAttributeDescriptor newattr = new DefaultFeatureAttributeDescriptor(attr);
164
            newattr.setFeatureType(copy);
165
            copy.add(newattr);
166 42027 jjdelcerro
        }
167 42290 jjdelcerro
        return copy;
168
    }
169 42027 jjdelcerro
170 45158 jjdelcerro
    @Override
171 42290 jjdelcerro
    public EditableFeatureAttributeDescriptor add(String name, int type) {
172
        return this.add(name, type, true);
173
    }
174 40435 jjdelcerro
175 44505 jjdelcerro
    @Override
176
    public void removeAll() {
177 45159 jjdelcerro
        while (!super.isEmpty()) {
178 44505 jjdelcerro
            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 45159 jjdelcerro
189 44190 jjdelcerro
    private EditableFeatureAttributeDescriptor add(String name, int type, boolean strongChanges) {
190
        DefaultEditableFeatureAttributeDescriptor attr = new DefaultEditableFeatureAttributeDescriptor(this, strongChanges);
191 42290 jjdelcerro
        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 45158 jjdelcerro
                        MessageFormat.format("Name descriptor {0} duplicated.", name)
197 42290 jjdelcerro
                );
198
            }
199 40435 jjdelcerro
200 42290 jjdelcerro
        }
201
        attr.setName(name);
202
        switch (type) {
203 44669 jjdelcerro
            case DataTypes.DOUBLE:
204
            case DataTypes.FLOAT:
205
            case DataTypes.DECIMAL:
206 42290 jjdelcerro
            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 44253 jjdelcerro
            case DataTypes.LIST: // Solo para campos calculados ???
216 42290 jjdelcerro
            case DataTypes.STRING:
217
            case DataTypes.TIME:
218
            case DataTypes.TIMESTAMP:
219 43283 jjdelcerro
            case DataTypes.URI:
220
            case DataTypes.URL:
221
            case DataTypes.FILE:
222 42290 jjdelcerro
                break;
223 40435 jjdelcerro
224 42290 jjdelcerro
            default:
225
                throw new UnsupportedDataTypeException(name, type);
226
        }
227
        attr.setDataType(type);
228
        attr.setIndex(this.size());
229 45159 jjdelcerro
230 44669 jjdelcerro
        this.hasStrongChanges = this.hasStrongChanges || strongChanges;
231 42290 jjdelcerro
        super.add(attr);
232
        this.pk = null;
233
        return attr;
234
    }
235 40435 jjdelcerro
236 45158 jjdelcerro
    @Override
237
    public EditableFeatureAttributeDescriptor add(String name, int type, int size) {
238 42290 jjdelcerro
        return this.add(name, type, true).setSize(size);
239
    }
240 40435 jjdelcerro
241 45158 jjdelcerro
    @Override
242 42290 jjdelcerro
    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 41335 jjdelcerro
250 45158 jjdelcerro
    @Override
251 42290 jjdelcerro
    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 45159 jjdelcerro
259 45158 jjdelcerro
    @Override
260 42293 jjdelcerro
    public EditableFeatureAttributeDescriptor add(String name, String type) {
261 45159 jjdelcerro
        return this.add(name, ToolsLocator.getDataTypesManager().getType(type));
262 42293 jjdelcerro
    }
263 40435 jjdelcerro
264 45158 jjdelcerro
    @Override
265 42293 jjdelcerro
    public EditableFeatureAttributeDescriptor add(String name, String type, int size) {
266 45159 jjdelcerro
        return this.add(name, ToolsLocator.getDataTypesManager().getType(type), size);
267 42293 jjdelcerro
    }
268 43966 jjdelcerro
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 45159 jjdelcerro
276 43966 jjdelcerro
    public boolean removeAttributeDescriptor(EditableFeatureAttributeDescriptor attribute) {
277
        return this.remove(attribute);
278
    }
279 44669 jjdelcerro
280 43966 jjdelcerro
    @Override
281 44669 jjdelcerro
    public FeatureAttributeDescriptor remove(int index) {
282
        FeatureAttributeDescriptor attr = (FeatureAttributeDescriptor) this.get(index);
283 45159 jjdelcerro
        if (!attr.isComputed()) {
284 42290 jjdelcerro
            hasStrongChanges = true;
285
        }
286 45159 jjdelcerro
        if (index == this.getDefaultGeometryAttributeIndex()) {
287
            this.defaultGeometryAttributeIndex = -1;
288 44669 jjdelcerro
            this.defaultGeometryAttributeName = null;
289 45159 jjdelcerro
        } else if (index == this.getDefaultTimeAttributeIndex()) {
290 44669 jjdelcerro
            this.defaultTimeAttributeIndex = -1;
291
            this.defaultTimeAttributeName = null;
292
        }
293 42290 jjdelcerro
        super.remove(attr);
294
        this.pk = null;
295
        this.fixAll();
296
        return attr;
297
    }
298 45159 jjdelcerro
299 44669 jjdelcerro
    @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 40435 jjdelcerro
309 43610 jjdelcerro
    @Override
310 43966 jjdelcerro
    public boolean remove(EditableFeatureAttributeDescriptor attribute) {
311 44669 jjdelcerro
        this.remove(attribute.getIndex());
312 43966 jjdelcerro
        return true;
313
    }
314
315
    @Override
316 42290 jjdelcerro
    protected void fixAll() {
317 43610 jjdelcerro
        super.fixAll();
318 44582 omartinez
        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 42290 jjdelcerro
    }
328 40435 jjdelcerro
329 42290 jjdelcerro
    public void checkIntegrity() throws DataListException {
330
        Iterator iter = super.iterator();
331
        FeatureTypeIntegrityException ex = null;
332 40435 jjdelcerro
333 42290 jjdelcerro
        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 40435 jjdelcerro
350 43074 jjdelcerro
    @Override
351
    public void setDefaultGeometryType(int type, int subType) {
352 45158 jjdelcerro
        EditableFeatureAttributeDescriptor descr = (EditableFeatureAttributeDescriptor) this.getDefaultGeometryAttribute();
353 45159 jjdelcerro
        if (descr == null) {
354 43074 jjdelcerro
            throw new IllegalStateException("Default geometry attribute not set.");
355
        }
356 45158 jjdelcerro
        descr.setGeometryType(type, subType);
357 43074 jjdelcerro
    }
358
359 45158 jjdelcerro
    @Override
360 42290 jjdelcerro
    public void setDefaultGeometryAttributeName(String name) {
361
        if (name == null || name.length() == 0) {
362
            this.defaultGeometryAttributeName = null;
363
            this.defaultGeometryAttributeIndex = -1;
364
            return;
365
        }
366 45158 jjdelcerro
        FeatureAttributeDescriptor attr = this.getAttributeDescriptor(name);
367 42290 jjdelcerro
        if (attr == null) {
368 45159 jjdelcerro
            throw new IllegalArgumentException("Attribute '" + name + "' not found.");
369 42290 jjdelcerro
        }
370
        this.defaultGeometryAttributeName = name;
371
        this.defaultGeometryAttributeIndex = attr.getIndex();
372
    }
373 40435 jjdelcerro
374 44948 jjdelcerro
    @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 45564 jjdelcerro
    public FeatureAttributeDescriptor getDefaultGeometryAttribute() {
388
        this.fixAll();
389
        return super.getDefaultGeometryAttribute();
390
    }
391
392
    @Override
393 44948 jjdelcerro
    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 45158 jjdelcerro
    @Override
405 42290 jjdelcerro
    public void setHasOID(boolean hasOID) {
406
        this.hasOID = hasOID;
407
    }
408 40435 jjdelcerro
409 45158 jjdelcerro
    @Override
410 42290 jjdelcerro
    protected Iterator getIterator(Iterator iter) {
411
        return new EditableDelegatedIterator(iter, this);
412
    }
413
414 45158 jjdelcerro
    @Override
415 41272 jjdelcerro
    public EditableFeatureAttributeDescriptor getEditableAttributeDescriptor(String name) {
416
        return (EditableFeatureAttributeDescriptor) this.getAttributeDescriptor(name);
417
    }
418
419 45158 jjdelcerro
    @Override
420 41272 jjdelcerro
    public EditableFeatureAttributeDescriptor getEditableAttributeDescriptor(int index) {
421 42290 jjdelcerro
        return (EditableFeatureAttributeDescriptor) this.getAttributeDescriptor(index);
422 41272 jjdelcerro
    }
423
424 42290 jjdelcerro
    protected class EditableDelegatedIterator extends DelegatedIterator {
425 40435 jjdelcerro
426 45158 jjdelcerro
        private final DefaultEditableFeatureType fType;
427 40435 jjdelcerro
428 45158 jjdelcerro
        public EditableDelegatedIterator(Iterator iter, DefaultEditableFeatureType fType) {
429 42290 jjdelcerro
            super(iter);
430
            this.fType = fType;
431
        }
432 40435 jjdelcerro
433 45158 jjdelcerro
        @Override
434 42290 jjdelcerro
        public void remove() {
435
            this.iterator.remove();
436
            this.fType.fixAll();
437
        }
438 40435 jjdelcerro
439 42290 jjdelcerro
    }
440 40435 jjdelcerro
441 42290 jjdelcerro
    protected void setAllowAutomaticValues(boolean value) {
442
        this.allowAtomaticValues = value;
443
    }
444 45159 jjdelcerro
445 44582 omartinez
    @Override
446
    public void copyFrom(FeatureType other) {
447
        super.copyFrom(other);
448
        if (other instanceof EditableFeatureType) {
449
            DefaultEditableFeatureType other2 = (DefaultEditableFeatureType) other;
450 44587 omartinez
            this.hasStrongChanges = other2.hasStrongChanges();
451 44582 omartinez
            this.source = (DefaultFeatureType) other2.getSource();
452
        } else {
453
            this.hasStrongChanges = false;
454
            this.source = (DefaultFeatureType) other;
455
        }
456
    }
457 45159 jjdelcerro
458 45154 jjdelcerro
    public void copyFrom(JsonObject json) {
459 45159 jjdelcerro
460 45154 jjdelcerro
    }
461 40435 jjdelcerro
}