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

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