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

History | View | Annotate | Download (12.5 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
    private EditableFeatureAttributeDescriptor add(String name, int type, boolean strongChanges) {
168
        DefaultEditableFeatureAttributeDescriptor attr = new DefaultEditableFeatureAttributeDescriptor(this, strongChanges);
169
        Iterator iter = this.iterator();
170
        while (iter.hasNext()) {
171
            EditableFeatureAttributeDescriptor descriptor = (EditableFeatureAttributeDescriptor) iter.next();
172
            if (descriptor.getName().equalsIgnoreCase(name)) {
173
                throw new RuntimeException(
174
                        MessageFormat.format("Name descriptor {0} duplicated.", new String[]{name})
175
                );
176
            }
177

    
178
        }
179
        attr.setName(name);
180
        switch (type) {
181
            case DataTypes.BOOLEAN:
182
            case DataTypes.BYTE:
183
            case DataTypes.BYTEARRAY:
184
            case DataTypes.CHAR:
185
            case DataTypes.DATE:
186
            case DataTypes.DOUBLE:
187
            case DataTypes.FLOAT:
188
            case DataTypes.GEOMETRY:
189
            case DataTypes.INT:
190
            case DataTypes.LONG:
191
            case DataTypes.OBJECT:
192
            case DataTypes.LIST: // Solo para campos calculados ???
193
            case DataTypes.STRING:
194
            case DataTypes.TIME:
195
            case DataTypes.TIMESTAMP:
196
            case DataTypes.URI:
197
            case DataTypes.URL:
198
            case DataTypes.FILE:
199
                break;
200

    
201
            default:
202
                throw new UnsupportedDataTypeException(name, type);
203
        }
204
        attr.setDataType(type);
205
        attr.setIndex(this.size());
206
        attr.setSize(DALLocator.getManager().getDefaultSize(type));
207
        
208
        super.add(attr);
209
        this.pk = null;
210
        return attr;
211
    }
212

    
213
    public EditableFeatureAttributeDescriptor add(String name, int type,
214
            int size) {
215
        return this.add(name, type, true).setSize(size);
216
    }
217

    
218
    public EditableFeatureAttributeDescriptor add(String name, int type,
219
            Evaluator evaluator) {
220
        if (evaluator == null) {
221
            throw new IllegalArgumentException();
222
        }
223
        return this.add(name, type, false).setEvaluator(evaluator);
224
    }
225

    
226
    public EditableFeatureAttributeDescriptor add(String name, int type,
227
            FeatureAttributeEmulator emulator) {
228
        if (emulator == null) {
229
            throw new IllegalArgumentException();
230
        }
231
        return this.add(name, type, false).setFeatureAttributeEmulator(emulator);
232
    }
233
    
234
    public EditableFeatureAttributeDescriptor add(String name, String type) {
235
        return this.add(name,ToolsLocator.getDataTypesManager().getType(type));
236
    }
237

    
238
    public EditableFeatureAttributeDescriptor add(String name, String type, int size) {
239
        return this.add(name,ToolsLocator.getDataTypesManager().getType(type), size);
240
    }
241

    
242
    public Object removeAttributeDescriptor(String name) {
243
//    Hemos metido los metodos removeAttributeDescriptor ya que existe
244
//    un problema desde python al llamar a los metodos remove. En lugar de llamar
245
//    a estos metodos remove, llama a los de la superclase ArrayList.
246
        return this.remove(name);
247
    }
248
    
249
    public boolean removeAttributeDescriptor(EditableFeatureAttributeDescriptor attribute) {
250
        return this.remove(attribute);
251
    }
252
    
253
    @Override
254
    public Object remove(String name) {
255
        DefaultFeatureAttributeDescriptor attr = (DefaultFeatureAttributeDescriptor) this
256
                .get(name);
257
        if (attr == null) {
258
            return null;
259
        }
260
        if ( !attr.isComputed() ) {
261
            hasStrongChanges = true;
262
        }
263
        super.remove(attr);
264
        this.pk = null;
265
        this.fixAll();
266
        return attr;
267
    }
268

    
269
    @Override
270
    public boolean remove(EditableFeatureAttributeDescriptor attribute) {
271
        if ( !attribute.isComputed() ) {
272
            hasStrongChanges = true;
273
        }
274
        if (!super.remove(attribute)) {
275
            return false;
276
        }
277
        this.pk = null;
278
        this.fixAll();
279
        return true;
280
    }
281

    
282
    @Override
283
    protected void fixAll() {
284
        super.fixAll();
285
    }
286

    
287
    public void checkIntegrity() throws DataListException {
288
        Iterator iter = super.iterator();
289
        FeatureTypeIntegrityException ex = null;
290

    
291
        while (iter.hasNext()) {
292
            DefaultEditableFeatureAttributeDescriptor attr = (DefaultEditableFeatureAttributeDescriptor) iter
293
                    .next();
294
            try {
295
                attr.checkIntegrity();
296
            } catch (Exception e) {
297
                if (ex == null) {
298
                    ex = new FeatureTypeIntegrityException(this.getId());
299
                }
300
                ex.add(e);
301
            }
302
        }
303
        if (ex != null) {
304
            throw ex;
305
        }
306
    }
307

    
308
    @Override
309
    public void setDefaultGeometryType(int type, int subType) {
310
        DefaultEditableFeatureType descr = (DefaultEditableFeatureType)this.getDefaultGeometryAttribute();
311
        if( descr == null ) {
312
            throw new IllegalStateException("Default geometry attribute not set.");
313
        }
314
        descr.setDefaultGeometryType(type, subType);
315
    }
316

    
317
    public void setDefaultGeometryAttributeName(String name) {
318
        if (name == null || name.length() == 0) {
319
            this.defaultGeometryAttributeName = null;
320
            this.defaultGeometryAttributeIndex = -1;
321
            return;
322
        }
323
        DefaultFeatureAttributeDescriptor attr = (DefaultFeatureAttributeDescriptor) this
324
                .get(name);
325
        if (attr == null) {
326
            throw new IllegalArgumentException("Attribute '" + name
327
                    + "' not found.");
328
        }
329
//        if (attr.getType() != DataTypes.GEOMETRY) {
330
//            throw new IllegalArgumentException("Attribute '" + name
331
//                    + "' is not a geometry.");
332
//        }
333
        this.defaultGeometryAttributeName = name;
334
        this.defaultGeometryAttributeIndex = attr.getIndex();
335
    }
336

    
337
    public void setHasOID(boolean hasOID) {
338
        this.hasOID = hasOID;
339
    }
340

    
341
    protected Iterator getIterator(Iterator iter) {
342
        return new EditableDelegatedIterator(iter, this);
343
    }
344

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

    
349
    public EditableFeatureAttributeDescriptor getEditableAttributeDescriptor(int index) {
350
        return (EditableFeatureAttributeDescriptor) this.getAttributeDescriptor(index);
351
    }
352

    
353
    protected class EditableDelegatedIterator extends DelegatedIterator {
354

    
355
        private DefaultEditableFeatureType fType;
356

    
357
        public EditableDelegatedIterator(Iterator iter,
358
                DefaultEditableFeatureType fType) {
359
            super(iter);
360
            this.fType = fType;
361
        }
362

    
363
        public void remove() {
364
            this.iterator.remove();
365
            this.fType.fixAll();
366
        }
367

    
368
    }
369

    
370
    protected void setAllowAutomaticValues(boolean value) {
371
        this.allowAtomaticValues = value;
372
    }
373

    
374
}