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

History | View | Annotate | Download (15.3 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 45784 jjdelcerro
import org.apache.commons.lang3.StringUtils;
30
import org.gvsig.fmap.dal.DataTypeUtils;
31 40435 jjdelcerro
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 41335 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator;
38 43739 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureStore;
39 40435 jjdelcerro
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 46012 jjdelcerro
import org.gvsig.fmap.dal.impl.DefaultDataManager;
43 42293 jjdelcerro
import org.gvsig.tools.ToolsLocator;
44 40435 jjdelcerro
import org.gvsig.tools.evaluator.Evaluator;
45
46 45158 jjdelcerro
@SuppressWarnings("UseSpecificCatch")
47 40435 jjdelcerro
public class DefaultEditableFeatureType extends DefaultFeatureType implements
48 42290 jjdelcerro
        EditableFeatureType {
49 40435 jjdelcerro
50 42290 jjdelcerro
    /**
51
     *
52
     */
53
    private static final long serialVersionUID = -713976880396024389L;
54 40435 jjdelcerro
55 42290 jjdelcerro
    private boolean hasStrongChanges;
56
    private DefaultFeatureType source;
57 40435 jjdelcerro
58 43739 jjdelcerro
    public DefaultEditableFeatureType(FeatureStore store) {
59
        super(store);
60 42290 jjdelcerro
        this.hasStrongChanges = false;
61
        this.source = null;
62
    }
63 40435 jjdelcerro
64 43739 jjdelcerro
    public DefaultEditableFeatureType(FeatureStore store, String id) {
65
        super(store, id);
66 42290 jjdelcerro
        this.hasStrongChanges = false;
67
        this.source = null;
68
    }
69 40435 jjdelcerro
70 42290 jjdelcerro
    protected DefaultEditableFeatureType(DefaultEditableFeatureType other) {
71
        super(other);
72 44582 omartinez
        this.hasStrongChanges = other.hasStrongChanges;
73 42290 jjdelcerro
        this.source = (DefaultFeatureType) other.getSource();
74
    }
75 40435 jjdelcerro
76 42290 jjdelcerro
    protected DefaultEditableFeatureType(DefaultFeatureType other) {
77
        super(other);
78
        this.source = other;
79 44582 omartinez
        this.fixAll();
80 42290 jjdelcerro
    }
81 40435 jjdelcerro
82 43739 jjdelcerro
    @Override
83 44318 jjdelcerro
    protected DefaultFeatureAttributeDescriptor getCopyAttributeDescriptor(DefaultFeatureAttributeDescriptor src) {
84
        DefaultFeatureAttributeDescriptor copy = new DefaultEditableFeatureAttributeDescriptor(src);
85
        copy.setFeatureType(this);
86
        return copy;
87 42290 jjdelcerro
    }
88 40435 jjdelcerro
89 42290 jjdelcerro
    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 43981 omartinez
            if (attr.isComputed()) {
98
                continue;
99
            }
100 42290 jjdelcerro
            if (attr.hasStrongChanges()) {
101
                return true;
102
            }
103
        }
104
        return false;
105
    }
106 40435 jjdelcerro
107 45152 fdiaz
    @Override
108 42290 jjdelcerro
    public FeatureType getCopy() {
109 45152 fdiaz
        DefaultEditableFeatureType copy = new DefaultEditableFeatureType(this);
110
        copy.fixAll();
111
        return copy;
112 42290 jjdelcerro
    }
113 40435 jjdelcerro
114 45152 fdiaz
    @Override
115 42290 jjdelcerro
    public EditableFeatureType getEditable() {
116 44669 jjdelcerro
        return (EditableFeatureType) this.getCopy();
117 42290 jjdelcerro
    }
118 40435 jjdelcerro
119 42290 jjdelcerro
    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 40435 jjdelcerro
139 45158 jjdelcerro
    @Override
140 42290 jjdelcerro
    public EditableFeatureAttributeDescriptor addLike(
141
            FeatureAttributeDescriptor other) {
142
        DefaultEditableFeatureAttributeDescriptor editableAttr;
143 44801 omartinez
        editableAttr = new DefaultEditableFeatureAttributeDescriptor(
144 45159 jjdelcerro
                (DefaultFeatureAttributeDescriptor) other);
145 42290 jjdelcerro
        super.add(editableAttr);
146 45159 jjdelcerro
        if (!editableAttr.isComputed()) {
147 44669 jjdelcerro
            this.hasStrongChanges = true;
148 45159 jjdelcerro
        }
149 42290 jjdelcerro
        this.fixAll();
150
        return editableAttr;
151
    }
152 40435 jjdelcerro
153 45158 jjdelcerro
    @Override
154 42290 jjdelcerro
    public FeatureType getSource() {
155
        return source;
156
    }
157 40435 jjdelcerro
158 45158 jjdelcerro
    @Override
159 42290 jjdelcerro
    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 44259 jjdelcerro
            DefaultFeatureAttributeDescriptor newattr = new DefaultFeatureAttributeDescriptor(attr);
167
            newattr.setFeatureType(copy);
168
            copy.add(newattr);
169 42027 jjdelcerro
        }
170 42290 jjdelcerro
        return copy;
171
    }
172 42027 jjdelcerro
173 45158 jjdelcerro
    @Override
174 42290 jjdelcerro
    public EditableFeatureAttributeDescriptor add(String name, int type) {
175
        return this.add(name, type, true);
176
    }
177 40435 jjdelcerro
178 44505 jjdelcerro
    @Override
179
    public void removeAll() {
180 45159 jjdelcerro
        while (!super.isEmpty()) {
181 44505 jjdelcerro
            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 45159 jjdelcerro
192 44190 jjdelcerro
    private EditableFeatureAttributeDescriptor add(String name, int type, boolean strongChanges) {
193 42290 jjdelcerro
        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 45158 jjdelcerro
                        MessageFormat.format("Name descriptor {0} duplicated.", name)
199 42290 jjdelcerro
                );
200
            }
201 40435 jjdelcerro
202 42290 jjdelcerro
        }
203 46012 jjdelcerro
        DefaultEditableFeatureAttributeDescriptor attr = DefaultDataManager.createEditableFeatureAttributeDescriptor(name, type, strongChanges);
204 42290 jjdelcerro
        attr.setIndex(this.size());
205 45159 jjdelcerro
206 44669 jjdelcerro
        this.hasStrongChanges = this.hasStrongChanges || strongChanges;
207 42290 jjdelcerro
        super.add(attr);
208
        this.pk = null;
209
        return attr;
210
    }
211 40435 jjdelcerro
212 45158 jjdelcerro
    @Override
213
    public EditableFeatureAttributeDescriptor add(String name, int type, int size) {
214 42290 jjdelcerro
        return this.add(name, type, true).setSize(size);
215
    }
216 40435 jjdelcerro
217 45158 jjdelcerro
    @Override
218 42290 jjdelcerro
    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 41335 jjdelcerro
226 45158 jjdelcerro
    @Override
227 42290 jjdelcerro
    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 45159 jjdelcerro
235 45158 jjdelcerro
    @Override
236 42293 jjdelcerro
    public EditableFeatureAttributeDescriptor add(String name, String type) {
237 45159 jjdelcerro
        return this.add(name, ToolsLocator.getDataTypesManager().getType(type));
238 42293 jjdelcerro
    }
239 40435 jjdelcerro
240 45158 jjdelcerro
    @Override
241 42293 jjdelcerro
    public EditableFeatureAttributeDescriptor add(String name, String type, int size) {
242 45159 jjdelcerro
        return this.add(name, ToolsLocator.getDataTypesManager().getType(type), size);
243 42293 jjdelcerro
    }
244 43966 jjdelcerro
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 45159 jjdelcerro
252 43966 jjdelcerro
    public boolean removeAttributeDescriptor(EditableFeatureAttributeDescriptor attribute) {
253
        return this.remove(attribute);
254
    }
255 44669 jjdelcerro
256 43966 jjdelcerro
    @Override
257 44669 jjdelcerro
    public FeatureAttributeDescriptor remove(int index) {
258
        FeatureAttributeDescriptor attr = (FeatureAttributeDescriptor) this.get(index);
259 45159 jjdelcerro
        if (!attr.isComputed()) {
260 42290 jjdelcerro
            hasStrongChanges = true;
261
        }
262 45159 jjdelcerro
        if (index == this.getDefaultGeometryAttributeIndex()) {
263
            this.defaultGeometryAttributeIndex = -1;
264 44669 jjdelcerro
            this.defaultGeometryAttributeName = null;
265 45159 jjdelcerro
        } else if (index == this.getDefaultTimeAttributeIndex()) {
266 44669 jjdelcerro
            this.defaultTimeAttributeIndex = -1;
267
            this.defaultTimeAttributeName = null;
268
        }
269 42290 jjdelcerro
        super.remove(attr);
270
        this.pk = null;
271
        this.fixAll();
272
        return attr;
273
    }
274 45159 jjdelcerro
275 44669 jjdelcerro
    @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 40435 jjdelcerro
285 43610 jjdelcerro
    @Override
286 43966 jjdelcerro
    public boolean remove(EditableFeatureAttributeDescriptor attribute) {
287 44669 jjdelcerro
        this.remove(attribute.getIndex());
288 43966 jjdelcerro
        return true;
289
    }
290
291
    @Override
292 42290 jjdelcerro
    protected void fixAll() {
293 43610 jjdelcerro
        super.fixAll();
294 44582 omartinez
        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 42290 jjdelcerro
    }
304 40435 jjdelcerro
305 42290 jjdelcerro
    public void checkIntegrity() throws DataListException {
306
        Iterator iter = super.iterator();
307
        FeatureTypeIntegrityException ex = null;
308 40435 jjdelcerro
309 42290 jjdelcerro
        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 40435 jjdelcerro
326 43074 jjdelcerro
    @Override
327
    public void setDefaultGeometryType(int type, int subType) {
328 45158 jjdelcerro
        EditableFeatureAttributeDescriptor descr = (EditableFeatureAttributeDescriptor) this.getDefaultGeometryAttribute();
329 45159 jjdelcerro
        if (descr == null) {
330 43074 jjdelcerro
            throw new IllegalStateException("Default geometry attribute not set.");
331
        }
332 45158 jjdelcerro
        descr.setGeometryType(type, subType);
333 43074 jjdelcerro
    }
334
335 45158 jjdelcerro
    @Override
336 42290 jjdelcerro
    public void setDefaultGeometryAttributeName(String name) {
337
        if (name == null || name.length() == 0) {
338
            this.defaultGeometryAttributeName = null;
339
            this.defaultGeometryAttributeIndex = -1;
340
            return;
341
        }
342 45158 jjdelcerro
        FeatureAttributeDescriptor attr = this.getAttributeDescriptor(name);
343 42290 jjdelcerro
        if (attr == null) {
344 45159 jjdelcerro
            throw new IllegalArgumentException("Attribute '" + name + "' not found.");
345 42290 jjdelcerro
        }
346
        this.defaultGeometryAttributeName = name;
347
        this.defaultGeometryAttributeIndex = attr.getIndex();
348
    }
349 40435 jjdelcerro
350 44948 jjdelcerro
    @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 45564 jjdelcerro
    public FeatureAttributeDescriptor getDefaultGeometryAttribute() {
364
        this.fixAll();
365
        return super.getDefaultGeometryAttribute();
366
    }
367
368
    @Override
369 44948 jjdelcerro
    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 45158 jjdelcerro
    @Override
381 42290 jjdelcerro
    public void setHasOID(boolean hasOID) {
382
        this.hasOID = hasOID;
383
    }
384 40435 jjdelcerro
385 45158 jjdelcerro
    @Override
386 42290 jjdelcerro
    protected Iterator getIterator(Iterator iter) {
387
        return new EditableDelegatedIterator(iter, this);
388
    }
389
390 45158 jjdelcerro
    @Override
391 41272 jjdelcerro
    public EditableFeatureAttributeDescriptor getEditableAttributeDescriptor(String name) {
392
        return (EditableFeatureAttributeDescriptor) this.getAttributeDescriptor(name);
393
    }
394
395 45158 jjdelcerro
    @Override
396 41272 jjdelcerro
    public EditableFeatureAttributeDescriptor getEditableAttributeDescriptor(int index) {
397 42290 jjdelcerro
        return (EditableFeatureAttributeDescriptor) this.getAttributeDescriptor(index);
398 41272 jjdelcerro
    }
399
400 45739 jjdelcerro
    @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 42290 jjdelcerro
    protected class EditableDelegatedIterator extends DelegatedIterator {
411 40435 jjdelcerro
412 45158 jjdelcerro
        private final DefaultEditableFeatureType fType;
413 40435 jjdelcerro
414 45158 jjdelcerro
        public EditableDelegatedIterator(Iterator iter, DefaultEditableFeatureType fType) {
415 42290 jjdelcerro
            super(iter);
416
            this.fType = fType;
417
        }
418 40435 jjdelcerro
419 45158 jjdelcerro
        @Override
420 42290 jjdelcerro
        public void remove() {
421
            this.iterator.remove();
422
            this.fType.fixAll();
423
        }
424 40435 jjdelcerro
425 42290 jjdelcerro
    }
426 40435 jjdelcerro
427 42290 jjdelcerro
    protected void setAllowAutomaticValues(boolean value) {
428
        this.allowAtomaticValues = value;
429
    }
430 45159 jjdelcerro
431 44582 omartinez
    @Override
432
    public void copyFrom(FeatureType other) {
433
        super.copyFrom(other);
434
        if (other instanceof EditableFeatureType) {
435
            DefaultEditableFeatureType other2 = (DefaultEditableFeatureType) other;
436 44587 omartinez
            this.hasStrongChanges = other2.hasStrongChanges();
437 44582 omartinez
            this.source = (DefaultFeatureType) other2.getSource();
438
        } else {
439
            this.hasStrongChanges = false;
440
            this.source = (DefaultFeatureType) other;
441
        }
442
    }
443 45159 jjdelcerro
444 45154 jjdelcerro
    public void copyFrom(JsonObject json) {
445 45784 jjdelcerro
        // TODO: falta por implementar copyFrom(json)
446 45154 jjdelcerro
    }
447 45784 jjdelcerro
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 40435 jjdelcerro
}