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
/**
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
            DefaultFeatureAttributeDescriptor newattr = new DefaultFeatureAttributeDescriptor(attr);
154
            newattr.setFeatureType(copy);
155
            copy.add(newattr);
156
        }
157
        return copy;
158
    }
159

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

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

    
175
        }
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
            case DataTypes.LIST: // Solo para campos calculados ???
190
            case DataTypes.STRING:
191
            case DataTypes.TIME:
192
            case DataTypes.TIMESTAMP:
193
            case DataTypes.URI:
194
            case DataTypes.URL:
195
            case DataTypes.FILE:
196
                break;
197

    
198
            default:
199
                throw new UnsupportedDataTypeException(name, type);
200
        }
201
        attr.setDataType(type);
202
        attr.setIndex(this.size());
203
        
204
        super.add(attr);
205
        this.pk = null;
206
        return attr;
207
    }
208

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

    
214
    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

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

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

    
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
    
245
    public boolean removeAttributeDescriptor(EditableFeatureAttributeDescriptor attribute) {
246
        return this.remove(attribute);
247
    }
248
    
249
    @Override
250
    public Object remove(String name) {
251
        DefaultFeatureAttributeDescriptor attr = (DefaultFeatureAttributeDescriptor) this
252
                .get(name);
253
        if (attr == null) {
254
            return null;
255
        }
256
        if ( !attr.isComputed() ) {
257
            hasStrongChanges = true;
258
        }
259
        super.remove(attr);
260
        this.pk = null;
261
        this.fixAll();
262
        return attr;
263
    }
264

    
265
    @Override
266
    public boolean remove(EditableFeatureAttributeDescriptor attribute) {
267
        if ( !attribute.isComputed() ) {
268
            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
    protected void fixAll() {
280
        super.fixAll();
281
    }
282

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

    
287
        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

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

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

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

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

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

    
349
    protected class EditableDelegatedIterator extends DelegatedIterator {
350

    
351
        private DefaultEditableFeatureType fType;
352

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

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

    
364
    }
365

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

    
370
}