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 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 46723 fdiaz
89
    public void forceStrongChanges() {
90
        this.hasStrongChanges = true;
91
    }
92 40435 jjdelcerro
93 42290 jjdelcerro
    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 43981 omartinez
            if (attr.isComputed()) {
102
                continue;
103
            }
104 42290 jjdelcerro
            if (attr.hasStrongChanges()) {
105
                return true;
106
            }
107
        }
108
        return false;
109
    }
110 40435 jjdelcerro
111 45152 fdiaz
    @Override
112 42290 jjdelcerro
    public FeatureType getCopy() {
113 45152 fdiaz
        DefaultEditableFeatureType copy = new DefaultEditableFeatureType(this);
114
        copy.fixAll();
115
        return copy;
116 42290 jjdelcerro
    }
117 40435 jjdelcerro
118 45152 fdiaz
    @Override
119 42290 jjdelcerro
    public EditableFeatureType getEditable() {
120 44669 jjdelcerro
        return (EditableFeatureType) this.getCopy();
121 42290 jjdelcerro
    }
122 40435 jjdelcerro
123 42290 jjdelcerro
    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 40435 jjdelcerro
143 45158 jjdelcerro
    @Override
144 42290 jjdelcerro
    public EditableFeatureAttributeDescriptor addLike(
145
            FeatureAttributeDescriptor other) {
146
        DefaultEditableFeatureAttributeDescriptor editableAttr;
147 44801 omartinez
        editableAttr = new DefaultEditableFeatureAttributeDescriptor(
148 45159 jjdelcerro
                (DefaultFeatureAttributeDescriptor) other);
149 42290 jjdelcerro
        super.add(editableAttr);
150 45159 jjdelcerro
        if (!editableAttr.isComputed()) {
151 44669 jjdelcerro
            this.hasStrongChanges = true;
152 45159 jjdelcerro
        }
153 42290 jjdelcerro
        this.fixAll();
154
        return editableAttr;
155
    }
156 40435 jjdelcerro
157 45158 jjdelcerro
    @Override
158 42290 jjdelcerro
    public FeatureType getSource() {
159
        return source;
160
    }
161 40435 jjdelcerro
162 45158 jjdelcerro
    @Override
163 42290 jjdelcerro
    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 44259 jjdelcerro
            DefaultFeatureAttributeDescriptor newattr = new DefaultFeatureAttributeDescriptor(attr);
171
            newattr.setFeatureType(copy);
172
            copy.add(newattr);
173 42027 jjdelcerro
        }
174 42290 jjdelcerro
        return copy;
175
    }
176 42027 jjdelcerro
177 45158 jjdelcerro
    @Override
178 42290 jjdelcerro
    public EditableFeatureAttributeDescriptor add(String name, int type) {
179
        return this.add(name, type, true);
180
    }
181 40435 jjdelcerro
182 44505 jjdelcerro
    @Override
183
    public void removeAll() {
184 45159 jjdelcerro
        while (!super.isEmpty()) {
185 44505 jjdelcerro
            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 45159 jjdelcerro
196 44190 jjdelcerro
    private EditableFeatureAttributeDescriptor add(String name, int type, boolean strongChanges) {
197 42290 jjdelcerro
        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 45158 jjdelcerro
                        MessageFormat.format("Name descriptor {0} duplicated.", name)
203 42290 jjdelcerro
                );
204
            }
205 40435 jjdelcerro
206 42290 jjdelcerro
        }
207 46104 omartinez
        DefaultEditableFeatureAttributeDescriptor attr = DefaultDataManager.createEditableFeatureAttributeDescriptor(this, name, type, strongChanges);
208 42290 jjdelcerro
        attr.setIndex(this.size());
209 45159 jjdelcerro
210 44669 jjdelcerro
        this.hasStrongChanges = this.hasStrongChanges || strongChanges;
211 42290 jjdelcerro
        super.add(attr);
212
        this.pk = null;
213
        return attr;
214
    }
215 40435 jjdelcerro
216 45158 jjdelcerro
    @Override
217
    public EditableFeatureAttributeDescriptor add(String name, int type, int size) {
218 42290 jjdelcerro
        return this.add(name, type, true).setSize(size);
219
    }
220 40435 jjdelcerro
221 45158 jjdelcerro
    @Override
222 42290 jjdelcerro
    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 41335 jjdelcerro
230 45158 jjdelcerro
    @Override
231 42290 jjdelcerro
    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 45159 jjdelcerro
239 45158 jjdelcerro
    @Override
240 42293 jjdelcerro
    public EditableFeatureAttributeDescriptor add(String name, String type) {
241 45159 jjdelcerro
        return this.add(name, ToolsLocator.getDataTypesManager().getType(type));
242 42293 jjdelcerro
    }
243 40435 jjdelcerro
244 45158 jjdelcerro
    @Override
245 42293 jjdelcerro
    public EditableFeatureAttributeDescriptor add(String name, String type, int size) {
246 45159 jjdelcerro
        return this.add(name, ToolsLocator.getDataTypesManager().getType(type), size);
247 42293 jjdelcerro
    }
248 43966 jjdelcerro
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 45159 jjdelcerro
256 43966 jjdelcerro
    public boolean removeAttributeDescriptor(EditableFeatureAttributeDescriptor attribute) {
257
        return this.remove(attribute);
258
    }
259 44669 jjdelcerro
260 43966 jjdelcerro
    @Override
261 44669 jjdelcerro
    public FeatureAttributeDescriptor remove(int index) {
262
        FeatureAttributeDescriptor attr = (FeatureAttributeDescriptor) this.get(index);
263 45159 jjdelcerro
        if (!attr.isComputed()) {
264 42290 jjdelcerro
            hasStrongChanges = true;
265
        }
266 45159 jjdelcerro
        if (index == this.getDefaultGeometryAttributeIndex()) {
267
            this.defaultGeometryAttributeIndex = -1;
268 44669 jjdelcerro
            this.defaultGeometryAttributeName = null;
269 45159 jjdelcerro
        } else if (index == this.getDefaultTimeAttributeIndex()) {
270 44669 jjdelcerro
            this.defaultTimeAttributeIndex = -1;
271
            this.defaultTimeAttributeName = null;
272
        }
273 42290 jjdelcerro
        super.remove(attr);
274
        this.pk = null;
275
        this.fixAll();
276
        return attr;
277
    }
278 45159 jjdelcerro
279 44669 jjdelcerro
    @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 40435 jjdelcerro
289 43610 jjdelcerro
    @Override
290 43966 jjdelcerro
    public boolean remove(EditableFeatureAttributeDescriptor attribute) {
291 44669 jjdelcerro
        this.remove(attribute.getIndex());
292 43966 jjdelcerro
        return true;
293
    }
294
295
    @Override
296 42290 jjdelcerro
    protected void fixAll() {
297 43610 jjdelcerro
        super.fixAll();
298 44582 omartinez
        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 42290 jjdelcerro
    }
308 40435 jjdelcerro
309 42290 jjdelcerro
    public void checkIntegrity() throws DataListException {
310
        Iterator iter = super.iterator();
311
        FeatureTypeIntegrityException ex = null;
312 40435 jjdelcerro
313 42290 jjdelcerro
        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 40435 jjdelcerro
330 43074 jjdelcerro
    @Override
331
    public void setDefaultGeometryType(int type, int subType) {
332 45158 jjdelcerro
        EditableFeatureAttributeDescriptor descr = (EditableFeatureAttributeDescriptor) this.getDefaultGeometryAttribute();
333 45159 jjdelcerro
        if (descr == null) {
334 43074 jjdelcerro
            throw new IllegalStateException("Default geometry attribute not set.");
335
        }
336 45158 jjdelcerro
        descr.setGeometryType(type, subType);
337 43074 jjdelcerro
    }
338
339 45158 jjdelcerro
    @Override
340 42290 jjdelcerro
    public void setDefaultGeometryAttributeName(String name) {
341
        if (name == null || name.length() == 0) {
342
            this.defaultGeometryAttributeName = null;
343
            this.defaultGeometryAttributeIndex = -1;
344
            return;
345
        }
346 45158 jjdelcerro
        FeatureAttributeDescriptor attr = this.getAttributeDescriptor(name);
347 42290 jjdelcerro
        if (attr == null) {
348 45159 jjdelcerro
            throw new IllegalArgumentException("Attribute '" + name + "' not found.");
349 42290 jjdelcerro
        }
350
        this.defaultGeometryAttributeName = name;
351
        this.defaultGeometryAttributeIndex = attr.getIndex();
352
    }
353 40435 jjdelcerro
354 44948 jjdelcerro
    @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 45564 jjdelcerro
    public FeatureAttributeDescriptor getDefaultGeometryAttribute() {
368
        this.fixAll();
369
        return super.getDefaultGeometryAttribute();
370
    }
371
372
    @Override
373 44948 jjdelcerro
    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 45158 jjdelcerro
    @Override
385 42290 jjdelcerro
    public void setHasOID(boolean hasOID) {
386
        this.hasOID = hasOID;
387
    }
388 40435 jjdelcerro
389 45158 jjdelcerro
    @Override
390 42290 jjdelcerro
    protected Iterator getIterator(Iterator iter) {
391
        return new EditableDelegatedIterator(iter, this);
392
    }
393
394 45158 jjdelcerro
    @Override
395 41272 jjdelcerro
    public EditableFeatureAttributeDescriptor getEditableAttributeDescriptor(String name) {
396
        return (EditableFeatureAttributeDescriptor) this.getAttributeDescriptor(name);
397
    }
398
399 45158 jjdelcerro
    @Override
400 41272 jjdelcerro
    public EditableFeatureAttributeDescriptor getEditableAttributeDescriptor(int index) {
401 42290 jjdelcerro
        return (EditableFeatureAttributeDescriptor) this.getAttributeDescriptor(index);
402 41272 jjdelcerro
    }
403
404 45739 jjdelcerro
    @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 42290 jjdelcerro
    protected class EditableDelegatedIterator extends DelegatedIterator {
415 40435 jjdelcerro
416 45158 jjdelcerro
        private final DefaultEditableFeatureType fType;
417 40435 jjdelcerro
418 45158 jjdelcerro
        public EditableDelegatedIterator(Iterator iter, DefaultEditableFeatureType fType) {
419 42290 jjdelcerro
            super(iter);
420
            this.fType = fType;
421
        }
422 40435 jjdelcerro
423 45158 jjdelcerro
        @Override
424 42290 jjdelcerro
        public void remove() {
425
            this.iterator.remove();
426
            this.fType.fixAll();
427
        }
428 40435 jjdelcerro
429 42290 jjdelcerro
    }
430 40435 jjdelcerro
431 42290 jjdelcerro
    protected void setAllowAutomaticValues(boolean value) {
432
        this.allowAtomaticValues = value;
433
    }
434 45159 jjdelcerro
435 44582 omartinez
    @Override
436
    public void copyFrom(FeatureType other) {
437
        super.copyFrom(other);
438
        if (other instanceof EditableFeatureType) {
439
            DefaultEditableFeatureType other2 = (DefaultEditableFeatureType) other;
440 44587 omartinez
            this.hasStrongChanges = other2.hasStrongChanges();
441 44582 omartinez
            this.source = (DefaultFeatureType) other2.getSource();
442
        } else {
443
            this.hasStrongChanges = false;
444
            this.source = (DefaultFeatureType) other;
445
        }
446
    }
447 45159 jjdelcerro
448 45154 jjdelcerro
    public void copyFrom(JsonObject json) {
449 45784 jjdelcerro
        // TODO: falta por implementar copyFrom(json)
450 45154 jjdelcerro
    }
451 45784 jjdelcerro
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 40435 jjdelcerro
}