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

History | View | Annotate | Download (12.2 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.hasStrongChanges()) {
89
                return true;
90
            }
91
        }
92
        return false;
93
    }
94

    
95
    public FeatureType getCopy() {
96
        return new DefaultEditableFeatureType(this);
97
    }
98

    
99
    public EditableFeatureType getEditable() {
100
        throw new UnsupportedOperationException();
101
    }
102

    
103
    public boolean addAll(DefaultFeatureType other) {
104
        Iterator iter = other.iterator();
105
        DefaultFeatureAttributeDescriptor attr;
106
        DefaultEditableFeatureAttributeDescriptor editableAttr;
107
        while (iter.hasNext()) {
108
            attr = (DefaultFeatureAttributeDescriptor) iter.next();
109
            if (attr instanceof DefaultEditableFeatureAttributeDescriptor) {
110
                editableAttr = new DefaultEditableFeatureAttributeDescriptor(
111
                        attr);
112
            } else {
113
                editableAttr = new DefaultEditableFeatureAttributeDescriptor(
114
                        attr);
115
            }
116
            super.add(editableAttr);
117
        }
118
        this.pk = null;
119
        this.fixAll();
120
        return true;
121
    }
122

    
123
    public EditableFeatureAttributeDescriptor addLike(
124
            FeatureAttributeDescriptor other) {
125
        DefaultEditableFeatureAttributeDescriptor editableAttr;
126
        if (other instanceof DefaultEditableFeatureAttributeDescriptor) {
127
            editableAttr = new DefaultEditableFeatureAttributeDescriptor(
128
                    (DefaultFeatureAttributeDescriptor) other);
129
        } else {
130
            editableAttr = new DefaultEditableFeatureAttributeDescriptor(
131
                    (DefaultFeatureAttributeDescriptor) other);
132
        }
133
        super.add(editableAttr);
134
        this.fixAll();
135
        return editableAttr;
136

    
137
    }
138

    
139
    public FeatureType getSource() {
140
        return source;
141
    }
142

    
143
    public FeatureType getNotEditableCopy() {
144
        this.fixAll();
145
        DefaultFeatureType copy = new DefaultFeatureType(this, false);
146
        Iterator iter = this.iterator();
147
        DefaultFeatureAttributeDescriptor attr;
148
        while (iter.hasNext()) {
149
            attr = (DefaultFeatureAttributeDescriptor) iter.next();
150
            copy.add(new DefaultFeatureAttributeDescriptor(attr));
151
        }
152
        return copy;
153
    }
154

    
155
    public EditableFeatureAttributeDescriptor add(String name, int type) {
156
        return this.add(name, type, true);
157
    }
158

    
159
    private EditableFeatureAttributeDescriptor add(String name, int type, boolean updateHasStrongChanges) {
160
        DefaultEditableFeatureAttributeDescriptor attr = new DefaultEditableFeatureAttributeDescriptor(this);
161
        Iterator iter = this.iterator();
162
        while (iter.hasNext()) {
163
            EditableFeatureAttributeDescriptor descriptor = (EditableFeatureAttributeDescriptor) iter.next();
164
            if (descriptor.getName().equalsIgnoreCase(name)) {
165
                throw new RuntimeException(
166
                        MessageFormat.format("Name descriptor {0} duplicated.", new String[]{name})
167
                );
168
            }
169

    
170
        }
171
        attr.setName(name);
172
        switch (type) {
173
            case DataTypes.BOOLEAN:
174
            case DataTypes.BYTE:
175
            case DataTypes.BYTEARRAY:
176
            case DataTypes.CHAR:
177
            case DataTypes.DATE:
178
            case DataTypes.DOUBLE:
179
            case DataTypes.FLOAT:
180
            case DataTypes.GEOMETRY:
181
            case DataTypes.INT:
182
            case DataTypes.LONG:
183
            case DataTypes.OBJECT:
184
            case DataTypes.STRING:
185
            case DataTypes.TIME:
186
            case DataTypes.TIMESTAMP:
187
            case DataTypes.URI:
188
            case DataTypes.URL:
189
            case DataTypes.FILE:
190
                break;
191

    
192
            default:
193
                throw new UnsupportedDataTypeException(name, type);
194
        }
195
        attr.setDataType(type);
196
        attr.setIndex(this.size());
197
        super.add(attr);
198
        if (!hasStrongChanges && updateHasStrongChanges) {
199
            hasStrongChanges = true;
200
        }
201
        this.pk = null;
202
        return attr;
203
    }
204

    
205
    public EditableFeatureAttributeDescriptor add(String name, int type,
206
            int size) {
207
        return this.add(name, type, true).setSize(size);
208
    }
209

    
210
    public EditableFeatureAttributeDescriptor add(String name, int type,
211
            Evaluator evaluator) {
212
        if (evaluator == null) {
213
            throw new IllegalArgumentException();
214
        }
215
        return this.add(name, type, false).setEvaluator(evaluator);
216
    }
217

    
218
    public EditableFeatureAttributeDescriptor add(String name, int type,
219
            FeatureAttributeEmulator emulator) {
220
        if (emulator == null) {
221
            throw new IllegalArgumentException();
222
        }
223
        return this.add(name, type, false).setFeatureAttributeEmulator(emulator);
224
    }
225
    
226
    public EditableFeatureAttributeDescriptor add(String name, String type) {
227
        return this.add(name,ToolsLocator.getDataTypesManager().getType(type));
228
    }
229

    
230
    public EditableFeatureAttributeDescriptor add(String name, String type, int size) {
231
        return this.add(name,ToolsLocator.getDataTypesManager().getType(type), size);
232
    }
233

    
234
    public Object removeAttributeDescriptor(String name) {
235
//    Hemos metido los metodos removeAttributeDescriptor ya que existe
236
//    un problema desde python al llamar a los metodos remove. En lugar de llamar
237
//    a estos metodos remove, llama a los de la superclase ArrayList.
238
        return this.remove(name);
239
    }
240
    
241
    public boolean removeAttributeDescriptor(EditableFeatureAttributeDescriptor attribute) {
242
        return this.remove(attribute);
243
    }
244
    
245
    @Override
246
    public Object remove(String name) {
247
        DefaultFeatureAttributeDescriptor attr = (DefaultFeatureAttributeDescriptor) this
248
                .get(name);
249
        if (attr == null) {
250
            return null;
251
        }
252
        if (attr.getEvaluator() == null) {
253
            hasStrongChanges = true;
254
        }
255
        super.remove(attr);
256
        this.pk = null;
257
        this.fixAll();
258
        return attr;
259
    }
260

    
261
    @Override
262
    public boolean remove(EditableFeatureAttributeDescriptor attribute) {
263
        if (attribute.getEvaluator() != null) {
264
            hasStrongChanges = true;
265
        }
266
        if (!super.remove(attribute)) {
267
            return false;
268
        }
269
        this.pk = null;
270
        this.fixAll();
271
        return true;
272
    }
273

    
274
    @Override
275
    protected void fixAll() {
276
        super.fixAll();
277
    }
278

    
279
    public void checkIntegrity() throws DataListException {
280
        Iterator iter = super.iterator();
281
        FeatureTypeIntegrityException ex = null;
282

    
283
        while (iter.hasNext()) {
284
            DefaultEditableFeatureAttributeDescriptor attr = (DefaultEditableFeatureAttributeDescriptor) iter
285
                    .next();
286
            try {
287
                attr.checkIntegrity();
288
            } catch (Exception e) {
289
                if (ex == null) {
290
                    ex = new FeatureTypeIntegrityException(this.getId());
291
                }
292
                ex.add(e);
293
            }
294
        }
295
        if (ex != null) {
296
            throw ex;
297
        }
298
    }
299

    
300
    @Override
301
    public void setDefaultGeometryType(int type, int subType) {
302
        DefaultEditableFeatureType descr = (DefaultEditableFeatureType)this.getDefaultGeometryAttribute();
303
        if( descr == null ) {
304
            throw new IllegalStateException("Default geometry attribute not set.");
305
        }
306
        descr.setDefaultGeometryType(type, subType);
307
    }
308

    
309
    public void setDefaultGeometryAttributeName(String name) {
310
        if (name == null || name.length() == 0) {
311
            this.defaultGeometryAttributeName = null;
312
            this.defaultGeometryAttributeIndex = -1;
313
            return;
314
        }
315
        DefaultFeatureAttributeDescriptor attr = (DefaultFeatureAttributeDescriptor) this
316
                .get(name);
317
        if (attr == null) {
318
            throw new IllegalArgumentException("Attribute '" + name
319
                    + "' not found.");
320
        }
321
//        if (attr.getType() != DataTypes.GEOMETRY) {
322
//            throw new IllegalArgumentException("Attribute '" + name
323
//                    + "' is not a geometry.");
324
//        }
325
        this.defaultGeometryAttributeName = name;
326
        this.defaultGeometryAttributeIndex = attr.getIndex();
327
    }
328

    
329
    public void setHasOID(boolean hasOID) {
330
        this.hasOID = hasOID;
331
    }
332

    
333
    protected Iterator getIterator(Iterator iter) {
334
        return new EditableDelegatedIterator(iter, this);
335
    }
336

    
337
    public EditableFeatureAttributeDescriptor getEditableAttributeDescriptor(String name) {
338
        return (EditableFeatureAttributeDescriptor) this.getAttributeDescriptor(name);
339
    }
340

    
341
    public EditableFeatureAttributeDescriptor getEditableAttributeDescriptor(int index) {
342
        return (EditableFeatureAttributeDescriptor) this.getAttributeDescriptor(index);
343
    }
344

    
345
    protected class EditableDelegatedIterator extends DelegatedIterator {
346

    
347
        private DefaultEditableFeatureType fType;
348

    
349
        public EditableDelegatedIterator(Iterator iter,
350
                DefaultEditableFeatureType fType) {
351
            super(iter);
352
            this.fType = fType;
353
        }
354

    
355
        public void remove() {
356
            this.iterator.remove();
357
            this.fType.fixAll();
358
        }
359

    
360
    }
361

    
362
    protected void setAllowAutomaticValues(boolean value) {
363
        this.allowAtomaticValues = value;
364
    }
365

    
366
}