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

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