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

History | View | Annotate | Download (12.8 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
import org.gvsig.fmap.dal.DALLocator;
29

    
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
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator;
36
import org.gvsig.fmap.dal.feature.FeatureStore;
37
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
import org.gvsig.tools.ToolsLocator;
41
import org.gvsig.tools.evaluator.Evaluator;
42

    
43
public class DefaultEditableFeatureType extends DefaultFeatureType implements
44
        EditableFeatureType {
45

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

    
51
    private boolean hasStrongChanges;
52
    private DefaultFeatureType source;
53

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

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

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

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

    
76
    @Override
77
    protected DefaultFeatureAttributeDescriptor getCopyAttributeDescriptor(DefaultFeatureAttributeDescriptor src) {
78
        DefaultFeatureAttributeDescriptor copy = new DefaultEditableFeatureAttributeDescriptor(src);
79
        copy.setFeatureType(this);
80
        return copy;
81
    }
82

    
83
    public boolean hasStrongChanges() {
84
        if (hasStrongChanges) {
85
            return true;
86
        }
87
        Iterator iter = this.iterator();
88
        DefaultEditableFeatureAttributeDescriptor attr;
89
        while (iter.hasNext()) {
90
            attr = (DefaultEditableFeatureAttributeDescriptor) iter.next();
91
            if (attr.isComputed()) {
92
                continue;
93
            }
94
            if (attr.hasStrongChanges()) {
95
                return true;
96
            }
97
        }
98
        return false;
99
    }
100

    
101
    public FeatureType getCopy() {
102
        return new DefaultEditableFeatureType(this);
103
    }
104

    
105
    public EditableFeatureType getEditable() {
106
        throw new UnsupportedOperationException();
107
    }
108

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

    
129
    public EditableFeatureAttributeDescriptor addLike(
130
            FeatureAttributeDescriptor other) {
131
        DefaultEditableFeatureAttributeDescriptor editableAttr;
132
        if (other instanceof DefaultEditableFeatureAttributeDescriptor) {
133
            editableAttr = new DefaultEditableFeatureAttributeDescriptor(
134
                    (DefaultFeatureAttributeDescriptor) other);
135
        } else {
136
            editableAttr = new DefaultEditableFeatureAttributeDescriptor(
137
                    (DefaultFeatureAttributeDescriptor) other);
138
        }
139
        super.add(editableAttr);
140
        this.fixAll();
141
        return editableAttr;
142

    
143
    }
144

    
145
    public FeatureType getSource() {
146
        return source;
147
    }
148

    
149
    public FeatureType getNotEditableCopy() {
150
        this.fixAll();
151
        DefaultFeatureType copy = new DefaultFeatureType(this, false);
152
        Iterator iter = this.iterator();
153
        DefaultFeatureAttributeDescriptor attr;
154
        while (iter.hasNext()) {
155
            attr = (DefaultFeatureAttributeDescriptor) iter.next();
156
            DefaultFeatureAttributeDescriptor newattr = new DefaultFeatureAttributeDescriptor(attr);
157
            newattr.setFeatureType(copy);
158
            copy.add(newattr);
159
        }
160
        return copy;
161
    }
162

    
163
    public EditableFeatureAttributeDescriptor add(String name, int type) {
164
        return this.add(name, type, true);
165
    }
166

    
167
    @Override
168
    public void removeAll() {
169
        while( !super.isEmpty() ) {
170
            super.remove(0);
171
        }
172
        this.fixAll();
173
    }
174

    
175
    @Override
176
    public void addAll(FeatureType attributes) {
177
        super.addAll(attributes);
178
        this.fixAll();
179
    }
180
    
181
    private EditableFeatureAttributeDescriptor add(String name, int type, boolean strongChanges) {
182
        DefaultEditableFeatureAttributeDescriptor attr = new DefaultEditableFeatureAttributeDescriptor(this, strongChanges);
183
        Iterator iter = this.iterator();
184
        while (iter.hasNext()) {
185
            EditableFeatureAttributeDescriptor descriptor = (EditableFeatureAttributeDescriptor) iter.next();
186
            if (descriptor.getName().equalsIgnoreCase(name)) {
187
                throw new RuntimeException(
188
                        MessageFormat.format("Name descriptor {0} duplicated.", new String[]{name})
189
                );
190
            }
191

    
192
        }
193
        attr.setName(name);
194
        switch (type) {
195
            case DataTypes.BOOLEAN:
196
            case DataTypes.BYTE:
197
            case DataTypes.BYTEARRAY:
198
            case DataTypes.CHAR:
199
            case DataTypes.DATE:
200
            case DataTypes.DOUBLE:
201
            case DataTypes.FLOAT:
202
            case DataTypes.GEOMETRY:
203
            case DataTypes.INT:
204
            case DataTypes.LONG:
205
            case DataTypes.OBJECT:
206
            case DataTypes.LIST: // Solo para campos calculados ???
207
            case DataTypes.STRING:
208
            case DataTypes.TIME:
209
            case DataTypes.TIMESTAMP:
210
            case DataTypes.URI:
211
            case DataTypes.URL:
212
            case DataTypes.FILE:
213
                break;
214

    
215
            default:
216
                throw new UnsupportedDataTypeException(name, type);
217
        }
218
        attr.setDataType(type);
219
        attr.setIndex(this.size());
220
        attr.setSize(DALLocator.getManager().getDefaultSize(type));
221
        
222
        super.add(attr);
223
        this.pk = null;
224
        return attr;
225
    }
226

    
227
    public EditableFeatureAttributeDescriptor add(String name, int type,
228
            int size) {
229
        return this.add(name, type, true).setSize(size);
230
    }
231

    
232
    public EditableFeatureAttributeDescriptor add(String name, int type,
233
            Evaluator evaluator) {
234
        if (evaluator == null) {
235
            throw new IllegalArgumentException();
236
        }
237
        return this.add(name, type, false).setEvaluator(evaluator);
238
    }
239

    
240
    public EditableFeatureAttributeDescriptor add(String name, int type,
241
            FeatureAttributeEmulator emulator) {
242
        if (emulator == null) {
243
            throw new IllegalArgumentException();
244
        }
245
        return this.add(name, type, false).setFeatureAttributeEmulator(emulator);
246
    }
247
    
248
    public EditableFeatureAttributeDescriptor add(String name, String type) {
249
        return this.add(name,ToolsLocator.getDataTypesManager().getType(type));
250
    }
251

    
252
    public EditableFeatureAttributeDescriptor add(String name, String type, int size) {
253
        return this.add(name,ToolsLocator.getDataTypesManager().getType(type), size);
254
    }
255

    
256
    public Object removeAttributeDescriptor(String name) {
257
//    Hemos metido los metodos removeAttributeDescriptor ya que existe
258
//    un problema desde python al llamar a los metodos remove. En lugar de llamar
259
//    a estos metodos remove, llama a los de la superclase ArrayList.
260
        return this.remove(name);
261
    }
262
    
263
    public boolean removeAttributeDescriptor(EditableFeatureAttributeDescriptor attribute) {
264
        return this.remove(attribute);
265
    }
266
    
267
    @Override
268
    public Object remove(String name) {
269
        DefaultFeatureAttributeDescriptor attr = (DefaultFeatureAttributeDescriptor) this
270
                .get(name);
271
        if (attr == null) {
272
            return null;
273
        }
274
        if ( !attr.isComputed() ) {
275
            hasStrongChanges = true;
276
        }
277
        super.remove(attr);
278
        this.pk = null;
279
        this.fixAll();
280
        return attr;
281
    }
282

    
283
    @Override
284
    public boolean remove(EditableFeatureAttributeDescriptor attribute) {
285
        if ( !attribute.isComputed() ) {
286
            hasStrongChanges = true;
287
        }
288
        if (!super.remove(attribute)) {
289
            return false;
290
        }
291
        this.pk = null;
292
        this.fixAll();
293
        return true;
294
    }
295

    
296
    @Override
297
    protected void fixAll() {
298
        super.fixAll();
299
    }
300

    
301
    public void checkIntegrity() throws DataListException {
302
        Iterator iter = super.iterator();
303
        FeatureTypeIntegrityException ex = null;
304

    
305
        while (iter.hasNext()) {
306
            DefaultEditableFeatureAttributeDescriptor attr = (DefaultEditableFeatureAttributeDescriptor) iter
307
                    .next();
308
            try {
309
                attr.checkIntegrity();
310
            } catch (Exception e) {
311
                if (ex == null) {
312
                    ex = new FeatureTypeIntegrityException(this.getId());
313
                }
314
                ex.add(e);
315
            }
316
        }
317
        if (ex != null) {
318
            throw ex;
319
        }
320
    }
321

    
322
    @Override
323
    public void setDefaultGeometryType(int type, int subType) {
324
        DefaultEditableFeatureType descr = (DefaultEditableFeatureType)this.getDefaultGeometryAttribute();
325
        if( descr == null ) {
326
            throw new IllegalStateException("Default geometry attribute not set.");
327
        }
328
        descr.setDefaultGeometryType(type, subType);
329
    }
330

    
331
    public void setDefaultGeometryAttributeName(String name) {
332
        if (name == null || name.length() == 0) {
333
            this.defaultGeometryAttributeName = null;
334
            this.defaultGeometryAttributeIndex = -1;
335
            return;
336
        }
337
        DefaultFeatureAttributeDescriptor attr = (DefaultFeatureAttributeDescriptor) this
338
                .get(name);
339
        if (attr == null) {
340
            throw new IllegalArgumentException("Attribute '" + name
341
                    + "' not found.");
342
        }
343
//        if (attr.getType() != DataTypes.GEOMETRY) {
344
//            throw new IllegalArgumentException("Attribute '" + name
345
//                    + "' is not a geometry.");
346
//        }
347
        this.defaultGeometryAttributeName = name;
348
        this.defaultGeometryAttributeIndex = attr.getIndex();
349
    }
350

    
351
    public void setHasOID(boolean hasOID) {
352
        this.hasOID = hasOID;
353
    }
354

    
355
    protected Iterator getIterator(Iterator iter) {
356
        return new EditableDelegatedIterator(iter, this);
357
    }
358

    
359
    public EditableFeatureAttributeDescriptor getEditableAttributeDescriptor(String name) {
360
        return (EditableFeatureAttributeDescriptor) this.getAttributeDescriptor(name);
361
    }
362

    
363
    public EditableFeatureAttributeDescriptor getEditableAttributeDescriptor(int index) {
364
        return (EditableFeatureAttributeDescriptor) this.getAttributeDescriptor(index);
365
    }
366

    
367
    protected class EditableDelegatedIterator extends DelegatedIterator {
368

    
369
        private DefaultEditableFeatureType fType;
370

    
371
        public EditableDelegatedIterator(Iterator iter,
372
                DefaultEditableFeatureType fType) {
373
            super(iter);
374
            this.fType = fType;
375
        }
376

    
377
        public void remove() {
378
            this.iterator.remove();
379
            this.fType.fixAll();
380
        }
381

    
382
    }
383

    
384
    protected void setAllowAutomaticValues(boolean value) {
385
        this.allowAtomaticValues = value;
386
    }
387

    
388
}