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

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
            copy.add(new DefaultFeatureAttributeDescriptor(attr));
154 42027 jjdelcerro
        }
155 42290 jjdelcerro
        return copy;
156
    }
157 42027 jjdelcerro
158 42290 jjdelcerro
    public EditableFeatureAttributeDescriptor add(String name, int type) {
159
        return this.add(name, type, true);
160
    }
161 40435 jjdelcerro
162
    private EditableFeatureAttributeDescriptor add(String name, int type, boolean updateHasStrongChanges) {
163 43739 jjdelcerro
        DefaultEditableFeatureAttributeDescriptor attr = new DefaultEditableFeatureAttributeDescriptor(this);
164 42290 jjdelcerro
        Iterator iter = this.iterator();
165
        while (iter.hasNext()) {
166
            EditableFeatureAttributeDescriptor descriptor = (EditableFeatureAttributeDescriptor) iter.next();
167
            if (descriptor.getName().equalsIgnoreCase(name)) {
168
                throw new RuntimeException(
169
                        MessageFormat.format("Name descriptor {0} duplicated.", new String[]{name})
170
                );
171
            }
172 40435 jjdelcerro
173 42290 jjdelcerro
        }
174
        attr.setName(name);
175
        switch (type) {
176
            case DataTypes.BOOLEAN:
177
            case DataTypes.BYTE:
178
            case DataTypes.BYTEARRAY:
179
            case DataTypes.CHAR:
180
            case DataTypes.DATE:
181
            case DataTypes.DOUBLE:
182
            case DataTypes.FLOAT:
183
            case DataTypes.GEOMETRY:
184
            case DataTypes.INT:
185
            case DataTypes.LONG:
186
            case DataTypes.OBJECT:
187
            case DataTypes.STRING:
188
            case DataTypes.TIME:
189
            case DataTypes.TIMESTAMP:
190 43283 jjdelcerro
            case DataTypes.URI:
191
            case DataTypes.URL:
192
            case DataTypes.FILE:
193 42290 jjdelcerro
                break;
194 40435 jjdelcerro
195 42290 jjdelcerro
            default:
196
                throw new UnsupportedDataTypeException(name, type);
197
        }
198
        attr.setDataType(type);
199
        attr.setIndex(this.size());
200
        super.add(attr);
201
        if (!hasStrongChanges && updateHasStrongChanges) {
202
            hasStrongChanges = true;
203
        }
204
        this.pk = null;
205
        return attr;
206
    }
207 40435 jjdelcerro
208 42290 jjdelcerro
    public EditableFeatureAttributeDescriptor add(String name, int type,
209
            int size) {
210
        return this.add(name, type, true).setSize(size);
211
    }
212 40435 jjdelcerro
213 42290 jjdelcerro
    public EditableFeatureAttributeDescriptor add(String name, int type,
214
            Evaluator evaluator) {
215
        if (evaluator == null) {
216
            throw new IllegalArgumentException();
217
        }
218
        return this.add(name, type, false).setEvaluator(evaluator);
219
    }
220 41335 jjdelcerro
221 42290 jjdelcerro
    public EditableFeatureAttributeDescriptor add(String name, int type,
222
            FeatureAttributeEmulator emulator) {
223
        if (emulator == null) {
224
            throw new IllegalArgumentException();
225
        }
226
        return this.add(name, type, false).setFeatureAttributeEmulator(emulator);
227
    }
228 42293 jjdelcerro
229
    public EditableFeatureAttributeDescriptor add(String name, String type) {
230
        return this.add(name,ToolsLocator.getDataTypesManager().getType(type));
231
    }
232 40435 jjdelcerro
233 42293 jjdelcerro
    public EditableFeatureAttributeDescriptor add(String name, String type, int size) {
234
        return this.add(name,ToolsLocator.getDataTypesManager().getType(type), size);
235
    }
236 43966 jjdelcerro
237
    public Object removeAttributeDescriptor(String name) {
238
//    Hemos metido los metodos removeAttributeDescriptor ya que existe
239
//    un problema desde python al llamar a los metodos remove. En lugar de llamar
240
//    a estos metodos remove, llama a los de la superclase ArrayList.
241
        return this.remove(name);
242
    }
243 42293 jjdelcerro
244 43966 jjdelcerro
    public boolean removeAttributeDescriptor(EditableFeatureAttributeDescriptor attribute) {
245
        return this.remove(attribute);
246
    }
247
248
    @Override
249 42290 jjdelcerro
    public Object remove(String name) {
250
        DefaultFeatureAttributeDescriptor attr = (DefaultFeatureAttributeDescriptor) this
251
                .get(name);
252
        if (attr == null) {
253
            return null;
254
        }
255
        if (attr.getEvaluator() == null) {
256
            hasStrongChanges = true;
257
        }
258
        super.remove(attr);
259
        this.pk = null;
260
        this.fixAll();
261
        return attr;
262
    }
263 40435 jjdelcerro
264 43610 jjdelcerro
    @Override
265 43966 jjdelcerro
    public boolean remove(EditableFeatureAttributeDescriptor attribute) {
266
        if (attribute.getEvaluator() != null) {
267
            hasStrongChanges = true;
268
        }
269
        if (!super.remove(attribute)) {
270
            return false;
271
        }
272
        this.pk = null;
273
        this.fixAll();
274
        return true;
275
    }
276
277
    @Override
278 42290 jjdelcerro
    protected void fixAll() {
279 43610 jjdelcerro
        super.fixAll();
280 42290 jjdelcerro
    }
281 40435 jjdelcerro
282 42290 jjdelcerro
    public void checkIntegrity() throws DataListException {
283
        Iterator iter = super.iterator();
284
        FeatureTypeIntegrityException ex = null;
285 40435 jjdelcerro
286 42290 jjdelcerro
        while (iter.hasNext()) {
287
            DefaultEditableFeatureAttributeDescriptor attr = (DefaultEditableFeatureAttributeDescriptor) iter
288
                    .next();
289
            try {
290
                attr.checkIntegrity();
291
            } catch (Exception e) {
292
                if (ex == null) {
293
                    ex = new FeatureTypeIntegrityException(this.getId());
294
                }
295
                ex.add(e);
296
            }
297
        }
298
        if (ex != null) {
299
            throw ex;
300
        }
301
    }
302 40435 jjdelcerro
303 43074 jjdelcerro
    @Override
304
    public void setDefaultGeometryType(int type, int subType) {
305
        DefaultEditableFeatureType descr = (DefaultEditableFeatureType)this.getDefaultGeometryAttribute();
306
        if( descr == null ) {
307
            throw new IllegalStateException("Default geometry attribute not set.");
308
        }
309
        descr.setDefaultGeometryType(type, subType);
310
    }
311
312 42290 jjdelcerro
    public void setDefaultGeometryAttributeName(String name) {
313
        if (name == null || name.length() == 0) {
314
            this.defaultGeometryAttributeName = null;
315
            this.defaultGeometryAttributeIndex = -1;
316
            return;
317
        }
318
        DefaultFeatureAttributeDescriptor attr = (DefaultFeatureAttributeDescriptor) this
319
                .get(name);
320
        if (attr == null) {
321
            throw new IllegalArgumentException("Attribute '" + name
322
                    + "' not found.");
323
        }
324 43420 jjdelcerro
//        if (attr.getType() != DataTypes.GEOMETRY) {
325
//            throw new IllegalArgumentException("Attribute '" + name
326
//                    + "' is not a geometry.");
327
//        }
328 42290 jjdelcerro
        this.defaultGeometryAttributeName = name;
329
        this.defaultGeometryAttributeIndex = attr.getIndex();
330
    }
331 40435 jjdelcerro
332 42290 jjdelcerro
    public void setHasOID(boolean hasOID) {
333
        this.hasOID = hasOID;
334
    }
335 40435 jjdelcerro
336 42290 jjdelcerro
    protected Iterator getIterator(Iterator iter) {
337
        return new EditableDelegatedIterator(iter, this);
338
    }
339
340 41272 jjdelcerro
    public EditableFeatureAttributeDescriptor getEditableAttributeDescriptor(String name) {
341
        return (EditableFeatureAttributeDescriptor) this.getAttributeDescriptor(name);
342
    }
343
344
    public EditableFeatureAttributeDescriptor getEditableAttributeDescriptor(int index) {
345 42290 jjdelcerro
        return (EditableFeatureAttributeDescriptor) this.getAttributeDescriptor(index);
346 41272 jjdelcerro
    }
347
348 42290 jjdelcerro
    protected class EditableDelegatedIterator extends DelegatedIterator {
349 40435 jjdelcerro
350 42290 jjdelcerro
        private DefaultEditableFeatureType fType;
351 40435 jjdelcerro
352 42290 jjdelcerro
        public EditableDelegatedIterator(Iterator iter,
353
                DefaultEditableFeatureType fType) {
354
            super(iter);
355
            this.fType = fType;
356
        }
357 40435 jjdelcerro
358 42290 jjdelcerro
        public void remove() {
359
            this.iterator.remove();
360
            this.fType.fixAll();
361
        }
362 40435 jjdelcerro
363 42290 jjdelcerro
    }
364 40435 jjdelcerro
365 42290 jjdelcerro
    protected void setAllowAutomaticValues(boolean value) {
366
        this.allowAtomaticValues = value;
367
    }
368 40435 jjdelcerro
369
}