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

History | View | Annotate | Download (13.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.hasStrongChanges = other.hasStrongChanges;
69
        this.source = (DefaultFeatureType) other.getSource();
70
    }
71

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

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

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

    
103
    public FeatureType getCopy() {
104
        return new DefaultEditableFeatureType(this);
105
    }
106

    
107
    public EditableFeatureType getEditable() {
108
        throw new UnsupportedOperationException();
109
    }
110

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

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

    
145
    }
146

    
147
    public FeatureType getSource() {
148
        return source;
149
    }
150

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

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

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

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

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

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

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

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

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

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

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

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

    
298
    @Override
299
    protected void fixAll() {
300
        super.fixAll();
301
        for (FeatureAttributeDescriptor attr : this) {
302
            if (attr instanceof DefaultEditableFeatureAttributeDescriptor) {
303
                DefaultEditableFeatureAttributeDescriptor attr2 = (DefaultEditableFeatureAttributeDescriptor) attr;
304
                if (attr2.hasStrongChanges()) {
305
                    this.hasStrongChanges = true;
306
                    break;
307
                }
308
            }
309
        }
310
    }
311

    
312
    public void checkIntegrity() throws DataListException {
313
        Iterator iter = super.iterator();
314
        FeatureTypeIntegrityException ex = null;
315

    
316
        while (iter.hasNext()) {
317
            DefaultEditableFeatureAttributeDescriptor attr = (DefaultEditableFeatureAttributeDescriptor) iter
318
                    .next();
319
            try {
320
                attr.checkIntegrity();
321
            } catch (Exception e) {
322
                if (ex == null) {
323
                    ex = new FeatureTypeIntegrityException(this.getId());
324
                }
325
                ex.add(e);
326
            }
327
        }
328
        if (ex != null) {
329
            throw ex;
330
        }
331
    }
332

    
333
    @Override
334
    public void setDefaultGeometryType(int type, int subType) {
335
        DefaultEditableFeatureType descr = (DefaultEditableFeatureType)this.getDefaultGeometryAttribute();
336
        if( descr == null ) {
337
            throw new IllegalStateException("Default geometry attribute not set.");
338
        }
339
        descr.setDefaultGeometryType(type, subType);
340
    }
341

    
342
    public void setDefaultGeometryAttributeName(String name) {
343
        if (name == null || name.length() == 0) {
344
            this.defaultGeometryAttributeName = null;
345
            this.defaultGeometryAttributeIndex = -1;
346
            return;
347
        }
348
        DefaultFeatureAttributeDescriptor attr = (DefaultFeatureAttributeDescriptor) this
349
                .get(name);
350
        if (attr == null) {
351
            throw new IllegalArgumentException("Attribute '" + name
352
                    + "' not found.");
353
        }
354
//        if (attr.getType() != DataTypes.GEOMETRY) {
355
//            throw new IllegalArgumentException("Attribute '" + name
356
//                    + "' is not a geometry.");
357
//        }
358
        this.defaultGeometryAttributeName = name;
359
        this.defaultGeometryAttributeIndex = attr.getIndex();
360
    }
361

    
362
    public void setHasOID(boolean hasOID) {
363
        this.hasOID = hasOID;
364
    }
365

    
366
    protected Iterator getIterator(Iterator iter) {
367
        return new EditableDelegatedIterator(iter, this);
368
    }
369

    
370
    public EditableFeatureAttributeDescriptor getEditableAttributeDescriptor(String name) {
371
        return (EditableFeatureAttributeDescriptor) this.getAttributeDescriptor(name);
372
    }
373

    
374
    public EditableFeatureAttributeDescriptor getEditableAttributeDescriptor(int index) {
375
        return (EditableFeatureAttributeDescriptor) this.getAttributeDescriptor(index);
376
    }
377

    
378
    protected class EditableDelegatedIterator extends DelegatedIterator {
379

    
380
        private DefaultEditableFeatureType fType;
381

    
382
        public EditableDelegatedIterator(Iterator iter,
383
                DefaultEditableFeatureType fType) {
384
            super(iter);
385
            this.fType = fType;
386
        }
387

    
388
        public void remove() {
389
            this.iterator.remove();
390
            this.fType.fixAll();
391
        }
392

    
393
    }
394

    
395
    protected void setAllowAutomaticValues(boolean value) {
396
        this.allowAtomaticValues = value;
397
    }
398
    
399
    @Override
400
    public void copyFrom(FeatureType other) {
401
        super.copyFrom(other);
402
        if (other instanceof EditableFeatureType) {
403
            DefaultEditableFeatureType other2 = (DefaultEditableFeatureType) other;
404
            this.hasStrongChanges = other2.hasStrongChanges();
405
            this.source = (DefaultFeatureType) other2.getSource();
406
        } else {
407
            this.hasStrongChanges = false;
408
            this.source = (DefaultFeatureType) other;
409
        }
410
    }
411

    
412
}