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

History | View | Annotate | Download (14 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
29
import org.gvsig.fmap.dal.DataTypes;
30
import org.gvsig.fmap.dal.exception.DataListException;
31
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
32
import org.gvsig.fmap.dal.feature.EditableFeatureType;
33
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
34 41335 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator;
35 43739 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureStore;
36 40435 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureType;
37
import org.gvsig.fmap.dal.feature.exception.FeatureTypeIntegrityException;
38
import org.gvsig.fmap.dal.feature.exception.UnsupportedDataTypeException;
39 42293 jjdelcerro
import org.gvsig.tools.ToolsLocator;
40 40435 jjdelcerro
import org.gvsig.tools.evaluator.Evaluator;
41
42
public class DefaultEditableFeatureType extends DefaultFeatureType implements
43 42290 jjdelcerro
        EditableFeatureType {
44 40435 jjdelcerro
45 42290 jjdelcerro
    /**
46
     *
47
     */
48
    private static final long serialVersionUID = -713976880396024389L;
49 40435 jjdelcerro
50 42290 jjdelcerro
    private boolean hasStrongChanges;
51
    private DefaultFeatureType source;
52 40435 jjdelcerro
53 43739 jjdelcerro
    public DefaultEditableFeatureType(FeatureStore store) {
54
        super(store);
55 42290 jjdelcerro
        this.hasStrongChanges = false;
56
        this.source = null;
57
    }
58 40435 jjdelcerro
59 43739 jjdelcerro
    public DefaultEditableFeatureType(FeatureStore store, String id) {
60
        super(store, id);
61 42290 jjdelcerro
        this.hasStrongChanges = false;
62
        this.source = null;
63
    }
64 40435 jjdelcerro
65 42290 jjdelcerro
    protected DefaultEditableFeatureType(DefaultEditableFeatureType other) {
66
        super(other);
67 44582 omartinez
        this.hasStrongChanges = other.hasStrongChanges;
68 42290 jjdelcerro
        this.source = (DefaultFeatureType) other.getSource();
69
    }
70 40435 jjdelcerro
71 42290 jjdelcerro
    protected DefaultEditableFeatureType(DefaultFeatureType other) {
72
        super(other);
73
        this.source = other;
74 44582 omartinez
        this.fixAll();
75 42290 jjdelcerro
    }
76 40435 jjdelcerro
77 43739 jjdelcerro
    @Override
78 44318 jjdelcerro
    protected DefaultFeatureAttributeDescriptor getCopyAttributeDescriptor(DefaultFeatureAttributeDescriptor src) {
79
        DefaultFeatureAttributeDescriptor copy = new DefaultEditableFeatureAttributeDescriptor(src);
80
        copy.setFeatureType(this);
81
        return copy;
82 42290 jjdelcerro
    }
83 40435 jjdelcerro
84 42290 jjdelcerro
    public boolean hasStrongChanges() {
85
        if (hasStrongChanges) {
86
            return true;
87
        }
88
        Iterator iter = this.iterator();
89
        DefaultEditableFeatureAttributeDescriptor attr;
90
        while (iter.hasNext()) {
91
            attr = (DefaultEditableFeatureAttributeDescriptor) iter.next();
92 43981 omartinez
            if (attr.isComputed()) {
93
                continue;
94
            }
95 42290 jjdelcerro
            if (attr.hasStrongChanges()) {
96
                return true;
97
            }
98
        }
99
        return false;
100
    }
101 40435 jjdelcerro
102 42290 jjdelcerro
    public FeatureType getCopy() {
103
        return new DefaultEditableFeatureType(this);
104
    }
105 40435 jjdelcerro
106 42290 jjdelcerro
    public EditableFeatureType getEditable() {
107 44669 jjdelcerro
        return (EditableFeatureType) this.getCopy();
108 42290 jjdelcerro
    }
109 40435 jjdelcerro
110 42290 jjdelcerro
    public boolean addAll(DefaultFeatureType other) {
111
        Iterator iter = other.iterator();
112
        DefaultFeatureAttributeDescriptor attr;
113
        DefaultEditableFeatureAttributeDescriptor editableAttr;
114
        while (iter.hasNext()) {
115
            attr = (DefaultFeatureAttributeDescriptor) iter.next();
116
            if (attr instanceof DefaultEditableFeatureAttributeDescriptor) {
117
                editableAttr = new DefaultEditableFeatureAttributeDescriptor(
118
                        attr);
119
            } else {
120
                editableAttr = new DefaultEditableFeatureAttributeDescriptor(
121
                        attr);
122
            }
123
            super.add(editableAttr);
124
        }
125
        this.pk = null;
126
        this.fixAll();
127
        return true;
128
    }
129 40435 jjdelcerro
130 42290 jjdelcerro
    public EditableFeatureAttributeDescriptor addLike(
131
            FeatureAttributeDescriptor other) {
132
        DefaultEditableFeatureAttributeDescriptor editableAttr;
133 44801 omartinez
        editableAttr = new DefaultEditableFeatureAttributeDescriptor(
134 42290 jjdelcerro
                    (DefaultFeatureAttributeDescriptor) other);
135
        super.add(editableAttr);
136 44669 jjdelcerro
        if( !editableAttr.isComputed() ) {
137
            this.hasStrongChanges = true;
138
        }
139 42290 jjdelcerro
        this.fixAll();
140
        return editableAttr;
141
    }
142 40435 jjdelcerro
143 42290 jjdelcerro
    public FeatureType getSource() {
144
        return source;
145
    }
146 40435 jjdelcerro
147 42290 jjdelcerro
    public FeatureType getNotEditableCopy() {
148
        this.fixAll();
149
        DefaultFeatureType copy = new DefaultFeatureType(this, false);
150
        Iterator iter = this.iterator();
151
        DefaultFeatureAttributeDescriptor attr;
152
        while (iter.hasNext()) {
153
            attr = (DefaultFeatureAttributeDescriptor) iter.next();
154 44259 jjdelcerro
            DefaultFeatureAttributeDescriptor newattr = new DefaultFeatureAttributeDescriptor(attr);
155
            newattr.setFeatureType(copy);
156
            copy.add(newattr);
157 42027 jjdelcerro
        }
158 42290 jjdelcerro
        return copy;
159
    }
160 42027 jjdelcerro
161 42290 jjdelcerro
    public EditableFeatureAttributeDescriptor add(String name, int type) {
162
        return this.add(name, type, true);
163
    }
164 40435 jjdelcerro
165 44505 jjdelcerro
    @Override
166
    public void removeAll() {
167
        while( !super.isEmpty() ) {
168
            super.remove(0);
169
        }
170
        this.fixAll();
171
    }
172
173
    @Override
174
    public void addAll(FeatureType attributes) {
175
        super.addAll(attributes);
176
        this.fixAll();
177
    }
178
179 44190 jjdelcerro
    private EditableFeatureAttributeDescriptor add(String name, int type, boolean strongChanges) {
180
        DefaultEditableFeatureAttributeDescriptor attr = new DefaultEditableFeatureAttributeDescriptor(this, strongChanges);
181 42290 jjdelcerro
        Iterator iter = this.iterator();
182
        while (iter.hasNext()) {
183
            EditableFeatureAttributeDescriptor descriptor = (EditableFeatureAttributeDescriptor) iter.next();
184
            if (descriptor.getName().equalsIgnoreCase(name)) {
185
                throw new RuntimeException(
186
                        MessageFormat.format("Name descriptor {0} duplicated.", new String[]{name})
187
                );
188
            }
189 40435 jjdelcerro
190 42290 jjdelcerro
        }
191
        attr.setName(name);
192
        switch (type) {
193 44669 jjdelcerro
            case DataTypes.DOUBLE:
194
            case DataTypes.FLOAT:
195
            case DataTypes.DECIMAL:
196 42290 jjdelcerro
            case DataTypes.BOOLEAN:
197
            case DataTypes.BYTE:
198
            case DataTypes.BYTEARRAY:
199
            case DataTypes.CHAR:
200
            case DataTypes.DATE:
201
            case DataTypes.GEOMETRY:
202
            case DataTypes.INT:
203
            case DataTypes.LONG:
204
            case DataTypes.OBJECT:
205 44253 jjdelcerro
            case DataTypes.LIST: // Solo para campos calculados ???
206 42290 jjdelcerro
            case DataTypes.STRING:
207
            case DataTypes.TIME:
208
            case DataTypes.TIMESTAMP:
209 43283 jjdelcerro
            case DataTypes.URI:
210
            case DataTypes.URL:
211
            case DataTypes.FILE:
212 42290 jjdelcerro
                break;
213 40435 jjdelcerro
214 42290 jjdelcerro
            default:
215
                throw new UnsupportedDataTypeException(name, type);
216
        }
217
        attr.setDataType(type);
218
        attr.setIndex(this.size());
219 44190 jjdelcerro
220 44669 jjdelcerro
        this.hasStrongChanges = this.hasStrongChanges || strongChanges;
221 42290 jjdelcerro
        super.add(attr);
222
        this.pk = null;
223
        return attr;
224
    }
225 40435 jjdelcerro
226 42290 jjdelcerro
    public EditableFeatureAttributeDescriptor add(String name, int type,
227
            int size) {
228
        return this.add(name, type, true).setSize(size);
229
    }
230 40435 jjdelcerro
231 42290 jjdelcerro
    public EditableFeatureAttributeDescriptor add(String name, int type,
232
            Evaluator evaluator) {
233
        if (evaluator == null) {
234
            throw new IllegalArgumentException();
235
        }
236
        return this.add(name, type, false).setEvaluator(evaluator);
237
    }
238 41335 jjdelcerro
239 42290 jjdelcerro
    public EditableFeatureAttributeDescriptor add(String name, int type,
240
            FeatureAttributeEmulator emulator) {
241
        if (emulator == null) {
242
            throw new IllegalArgumentException();
243
        }
244
        return this.add(name, type, false).setFeatureAttributeEmulator(emulator);
245
    }
246 42293 jjdelcerro
247
    public EditableFeatureAttributeDescriptor add(String name, String type) {
248
        return this.add(name,ToolsLocator.getDataTypesManager().getType(type));
249
    }
250 40435 jjdelcerro
251 42293 jjdelcerro
    public EditableFeatureAttributeDescriptor add(String name, String type, int size) {
252
        return this.add(name,ToolsLocator.getDataTypesManager().getType(type), size);
253
    }
254 43966 jjdelcerro
255
    public Object removeAttributeDescriptor(String name) {
256
//    Hemos metido los metodos removeAttributeDescriptor ya que existe
257
//    un problema desde python al llamar a los metodos remove. En lugar de llamar
258
//    a estos metodos remove, llama a los de la superclase ArrayList.
259
        return this.remove(name);
260
    }
261 42293 jjdelcerro
262 43966 jjdelcerro
    public boolean removeAttributeDescriptor(EditableFeatureAttributeDescriptor attribute) {
263
        return this.remove(attribute);
264
    }
265 44669 jjdelcerro
266 43966 jjdelcerro
    @Override
267 44669 jjdelcerro
    public FeatureAttributeDescriptor remove(int index) {
268
        FeatureAttributeDescriptor attr = (FeatureAttributeDescriptor) this.get(index);
269 44190 jjdelcerro
        if ( !attr.isComputed() ) {
270 42290 jjdelcerro
            hasStrongChanges = true;
271
        }
272 44669 jjdelcerro
        if( index == this.getDefaultGeometryAttributeIndex() ) {
273
            this.defaultGeometryAttributeIndex  = -1;
274
            this.defaultGeometryAttributeName = null;
275
        } else if( index == this.getDefaultTimeAttributeIndex() ) {
276
            this.defaultTimeAttributeIndex = -1;
277
            this.defaultTimeAttributeName = null;
278
        }
279 42290 jjdelcerro
        super.remove(attr);
280
        this.pk = null;
281
        this.fixAll();
282
        return attr;
283
    }
284 44669 jjdelcerro
285
    @Override
286
    public Object remove(String name) {
287
        FeatureAttributeDescriptor attr = (FeatureAttributeDescriptor) this.get(name);
288
        if (attr == null) {
289
            return null;
290
        }
291
        this.remove(attr.getIndex());
292
        return attr;
293
    }
294 40435 jjdelcerro
295 43610 jjdelcerro
    @Override
296 43966 jjdelcerro
    public boolean remove(EditableFeatureAttributeDescriptor attribute) {
297 44669 jjdelcerro
        this.remove(attribute.getIndex());
298 43966 jjdelcerro
        return true;
299
    }
300
301
    @Override
302 42290 jjdelcerro
    protected void fixAll() {
303 43610 jjdelcerro
        super.fixAll();
304 44582 omartinez
        for (FeatureAttributeDescriptor attr : this) {
305
            if (attr instanceof DefaultEditableFeatureAttributeDescriptor) {
306
                DefaultEditableFeatureAttributeDescriptor attr2 = (DefaultEditableFeatureAttributeDescriptor) attr;
307
                if (attr2.hasStrongChanges()) {
308
                    this.hasStrongChanges = true;
309
                    break;
310
                }
311
            }
312
        }
313 42290 jjdelcerro
    }
314 40435 jjdelcerro
315 42290 jjdelcerro
    public void checkIntegrity() throws DataListException {
316
        Iterator iter = super.iterator();
317
        FeatureTypeIntegrityException ex = null;
318 40435 jjdelcerro
319 42290 jjdelcerro
        while (iter.hasNext()) {
320
            DefaultEditableFeatureAttributeDescriptor attr = (DefaultEditableFeatureAttributeDescriptor) iter
321
                    .next();
322
            try {
323
                attr.checkIntegrity();
324
            } catch (Exception e) {
325
                if (ex == null) {
326
                    ex = new FeatureTypeIntegrityException(this.getId());
327
                }
328
                ex.add(e);
329
            }
330
        }
331
        if (ex != null) {
332
            throw ex;
333
        }
334
    }
335 40435 jjdelcerro
336 43074 jjdelcerro
    @Override
337
    public void setDefaultGeometryType(int type, int subType) {
338
        DefaultEditableFeatureType descr = (DefaultEditableFeatureType)this.getDefaultGeometryAttribute();
339
        if( descr == null ) {
340
            throw new IllegalStateException("Default geometry attribute not set.");
341
        }
342
        descr.setDefaultGeometryType(type, subType);
343
    }
344
345 42290 jjdelcerro
    public void setDefaultGeometryAttributeName(String name) {
346
        if (name == null || name.length() == 0) {
347
            this.defaultGeometryAttributeName = null;
348
            this.defaultGeometryAttributeIndex = -1;
349
            return;
350
        }
351
        DefaultFeatureAttributeDescriptor attr = (DefaultFeatureAttributeDescriptor) this
352
                .get(name);
353
        if (attr == null) {
354
            throw new IllegalArgumentException("Attribute '" + name
355
                    + "' not found.");
356
        }
357 43420 jjdelcerro
//        if (attr.getType() != DataTypes.GEOMETRY) {
358
//            throw new IllegalArgumentException("Attribute '" + name
359
//                    + "' is not a geometry.");
360
//        }
361 42290 jjdelcerro
        this.defaultGeometryAttributeName = name;
362
        this.defaultGeometryAttributeIndex = attr.getIndex();
363
    }
364 40435 jjdelcerro
365 42290 jjdelcerro
    public void setHasOID(boolean hasOID) {
366
        this.hasOID = hasOID;
367
    }
368 40435 jjdelcerro
369 42290 jjdelcerro
    protected Iterator getIterator(Iterator iter) {
370
        return new EditableDelegatedIterator(iter, this);
371
    }
372
373 41272 jjdelcerro
    public EditableFeatureAttributeDescriptor getEditableAttributeDescriptor(String name) {
374
        return (EditableFeatureAttributeDescriptor) this.getAttributeDescriptor(name);
375
    }
376
377
    public EditableFeatureAttributeDescriptor getEditableAttributeDescriptor(int index) {
378 42290 jjdelcerro
        return (EditableFeatureAttributeDescriptor) this.getAttributeDescriptor(index);
379 41272 jjdelcerro
    }
380
381 42290 jjdelcerro
    protected class EditableDelegatedIterator extends DelegatedIterator {
382 40435 jjdelcerro
383 42290 jjdelcerro
        private DefaultEditableFeatureType fType;
384 40435 jjdelcerro
385 42290 jjdelcerro
        public EditableDelegatedIterator(Iterator iter,
386
                DefaultEditableFeatureType fType) {
387
            super(iter);
388
            this.fType = fType;
389
        }
390 40435 jjdelcerro
391 42290 jjdelcerro
        public void remove() {
392
            this.iterator.remove();
393
            this.fType.fixAll();
394
        }
395 40435 jjdelcerro
396 42290 jjdelcerro
    }
397 40435 jjdelcerro
398 42290 jjdelcerro
    protected void setAllowAutomaticValues(boolean value) {
399
        this.allowAtomaticValues = value;
400
    }
401 44582 omartinez
402
    @Override
403
    public void copyFrom(FeatureType other) {
404
        super.copyFrom(other);
405
        if (other instanceof EditableFeatureType) {
406
            DefaultEditableFeatureType other2 = (DefaultEditableFeatureType) other;
407 44587 omartinez
            this.hasStrongChanges = other2.hasStrongChanges();
408 44582 omartinez
            this.source = (DefaultFeatureType) other2.getSource();
409
        } else {
410
            this.hasStrongChanges = false;
411
            this.source = (DefaultFeatureType) other;
412
        }
413
    }
414 40435 jjdelcerro
415
}