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

History | View | Annotate | Download (12.3 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * 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
 *
11
 * 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
 *
16
 * 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
 *
20
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22
 */
23
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
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator;
35
import org.gvsig.fmap.dal.feature.FeatureStore;
36
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
import org.gvsig.tools.ToolsLocator;
40
import org.gvsig.tools.evaluator.Evaluator;
41

    
42
public class DefaultEditableFeatureType extends DefaultFeatureType implements
43
        EditableFeatureType {
44

    
45
    /**
46
     *
47
     */
48
    private static final long serialVersionUID = -713976880396024389L;
49

    
50
    private boolean hasStrongChanges;
51
    private DefaultFeatureType source;
52

    
53
    public DefaultEditableFeatureType(FeatureStore store) {
54
        super(store);
55
        this.hasStrongChanges = false;
56
        this.source = null;
57
    }
58

    
59
    public DefaultEditableFeatureType(FeatureStore store, String id) {
60
        super(store, id);
61
        this.hasStrongChanges = false;
62
        this.source = null;
63
    }
64

    
65
    protected DefaultEditableFeatureType(DefaultEditableFeatureType other) {
66
        super(other);
67
        this.source = (DefaultFeatureType) other.getSource();
68
    }
69

    
70
    protected DefaultEditableFeatureType(DefaultFeatureType other) {
71
        super(other);
72
        this.source = other;
73
    }
74

    
75
    @Override
76
    protected void intitalizeAddAttibute(DefaultFeatureAttributeDescriptor attr) {
77
        super.add(new DefaultEditableFeatureAttributeDescriptor(attr));
78
    }
79

    
80
    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
            if (attr.isComputed()) {
89
                continue;
90
            }
91
            if (attr.hasStrongChanges()) {
92
                return true;
93
            }
94
        }
95
        return false;
96
    }
97

    
98
    public FeatureType getCopy() {
99
        return new DefaultEditableFeatureType(this);
100
    }
101

    
102
    public EditableFeatureType getEditable() {
103
        throw new UnsupportedOperationException();
104
    }
105

    
106
    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

    
126
    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

    
140
    }
141

    
142
    public FeatureType getSource() {
143
        return source;
144
    }
145

    
146
    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
        }
155
        return copy;
156
    }
157

    
158
    public EditableFeatureAttributeDescriptor add(String name, int type) {
159
        return this.add(name, type, true);
160
    }
161

    
162
    private EditableFeatureAttributeDescriptor add(String name, int type, boolean updateHasStrongChanges) {
163
        DefaultEditableFeatureAttributeDescriptor attr = new DefaultEditableFeatureAttributeDescriptor(this);
164
        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

    
173
        }
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
            case DataTypes.URI:
191
            case DataTypes.URL:
192
            case DataTypes.FILE:
193
                break;
194

    
195
            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

    
208
    public EditableFeatureAttributeDescriptor add(String name, int type,
209
            int size) {
210
        return this.add(name, type, true).setSize(size);
211
    }
212

    
213
    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

    
221
    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
    
229
    public EditableFeatureAttributeDescriptor add(String name, String type) {
230
        return this.add(name,ToolsLocator.getDataTypesManager().getType(type));
231
    }
232

    
233
    public EditableFeatureAttributeDescriptor add(String name, String type, int size) {
234
        return this.add(name,ToolsLocator.getDataTypesManager().getType(type), size);
235
    }
236

    
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
    
244
    public boolean removeAttributeDescriptor(EditableFeatureAttributeDescriptor attribute) {
245
        return this.remove(attribute);
246
    }
247
    
248
    @Override
249
    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

    
264
    @Override
265
    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
    protected void fixAll() {
279
        super.fixAll();
280
    }
281

    
282
    public void checkIntegrity() throws DataListException {
283
        Iterator iter = super.iterator();
284
        FeatureTypeIntegrityException ex = null;
285

    
286
        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

    
303
    @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
    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
//        if (attr.getType() != DataTypes.GEOMETRY) {
325
//            throw new IllegalArgumentException("Attribute '" + name
326
//                    + "' is not a geometry.");
327
//        }
328
        this.defaultGeometryAttributeName = name;
329
        this.defaultGeometryAttributeIndex = attr.getIndex();
330
    }
331

    
332
    public void setHasOID(boolean hasOID) {
333
        this.hasOID = hasOID;
334
    }
335

    
336
    protected Iterator getIterator(Iterator iter) {
337
        return new EditableDelegatedIterator(iter, this);
338
    }
339

    
340
    public EditableFeatureAttributeDescriptor getEditableAttributeDescriptor(String name) {
341
        return (EditableFeatureAttributeDescriptor) this.getAttributeDescriptor(name);
342
    }
343

    
344
    public EditableFeatureAttributeDescriptor getEditableAttributeDescriptor(int index) {
345
        return (EditableFeatureAttributeDescriptor) this.getAttributeDescriptor(index);
346
    }
347

    
348
    protected class EditableDelegatedIterator extends DelegatedIterator {
349

    
350
        private DefaultEditableFeatureType fType;
351

    
352
        public EditableDelegatedIterator(Iterator iter,
353
                DefaultEditableFeatureType fType) {
354
            super(iter);
355
            this.fType = fType;
356
        }
357

    
358
        public void remove() {
359
            this.iterator.remove();
360
            this.fType.fixAll();
361
        }
362

    
363
    }
364

    
365
    protected void setAllowAutomaticValues(boolean value) {
366
        this.allowAtomaticValues = value;
367
    }
368

    
369
}