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

History | View | Annotate | Download (13.6 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 java.util.zip.CRC32;
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.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() {
54
        super();
55
        this.hasStrongChanges = false;
56
        this.source = null;
57
    }
58

    
59
    public DefaultEditableFeatureType(String id) {
60
        super(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
    protected void intitalizeAddAttibute(DefaultFeatureAttributeDescriptor attr) {
76
        super.add(new DefaultEditableFeatureAttributeDescriptor(attr));
77
    }
78

    
79
    public boolean hasStrongChanges() {
80
        if (hasStrongChanges) {
81
            return true;
82
        }
83
        Iterator iter = this.iterator();
84
        DefaultEditableFeatureAttributeDescriptor attr;
85
        while (iter.hasNext()) {
86
            attr = (DefaultEditableFeatureAttributeDescriptor) iter.next();
87
            if (attr.hasStrongChanges()) {
88
                return true;
89
            }
90
        }
91
        return false;
92
    }
93

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

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

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

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

    
136
    }
137

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

    
142
    private long getCRC() {
143
        StringBuffer buffer = new StringBuffer();
144
        for (int i = 0; i < this.size(); i++) {
145
            FeatureAttributeDescriptor x = this.getAttributeDescriptor(i);
146
            buffer.append(x.getName());
147
            buffer.append(x.getDataTypeName());
148
            buffer.append(x.getSize());
149
        }
150
        CRC32 crc = new CRC32();
151
        byte[] data = buffer.toString().getBytes();
152
        crc.update(data);
153
        return crc.getValue();
154
    }
155

    
156
    public FeatureType getNotEditableCopy() {
157
        this.fixAll();
158
        DefaultFeatureType copy = new DefaultFeatureType(this, false);
159
        Iterator iter = this.iterator();
160
        DefaultFeatureAttributeDescriptor attr;
161
        while (iter.hasNext()) {
162
            attr = (DefaultFeatureAttributeDescriptor) iter.next();
163
            copy.add(new DefaultFeatureAttributeDescriptor(attr));
164
        }
165
        return copy;
166
    }
167

    
168
    public EditableFeatureAttributeDescriptor add(String name, int type) {
169
        return this.add(name, type, true);
170
    }
171

    
172
    private EditableFeatureAttributeDescriptor add(String name, int type, boolean updateHasStrongChanges) {
173
        DefaultEditableFeatureAttributeDescriptor attr = new DefaultEditableFeatureAttributeDescriptor();
174
        Iterator iter = this.iterator();
175
        while (iter.hasNext()) {
176
            EditableFeatureAttributeDescriptor descriptor = (EditableFeatureAttributeDescriptor) iter.next();
177
            if (descriptor.getName().equalsIgnoreCase(name)) {
178
                throw new RuntimeException(
179
                        MessageFormat.format("Name descriptor {0} duplicated.", new String[]{name})
180
                );
181
            }
182

    
183
        }
184
        attr.setName(name);
185
        switch (type) {
186
            case DataTypes.BOOLEAN:
187
            case DataTypes.BYTE:
188
            case DataTypes.BYTEARRAY:
189
            case DataTypes.CHAR:
190
            case DataTypes.DATE:
191
            case DataTypes.DOUBLE:
192
            case DataTypes.FLOAT:
193
            case DataTypes.GEOMETRY:
194
            case DataTypes.INT:
195
            case DataTypes.LONG:
196
            case DataTypes.OBJECT:
197
            case DataTypes.STRING:
198
            case DataTypes.TIME:
199
            case DataTypes.TIMESTAMP:
200
            case DataTypes.URI:
201
            case DataTypes.URL:
202
            case DataTypes.FILE:
203
                break;
204

    
205
            default:
206
                throw new UnsupportedDataTypeException(name, type);
207
        }
208
        attr.setDataType(type);
209
        attr.setIndex(this.size());
210
        super.add(attr);
211
        if (!hasStrongChanges && updateHasStrongChanges) {
212
            hasStrongChanges = true;
213
        }
214
        this.pk = null;
215
        return attr;
216
    }
217

    
218
    public EditableFeatureAttributeDescriptor add(String name, int type,
219
            int size) {
220
        return this.add(name, type, true).setSize(size);
221
    }
222

    
223
    public EditableFeatureAttributeDescriptor add(String name, int type,
224
            Evaluator evaluator) {
225
        if (evaluator == null) {
226
            throw new IllegalArgumentException();
227
        }
228
        return this.add(name, type, false).setEvaluator(evaluator);
229
    }
230

    
231
    public EditableFeatureAttributeDescriptor add(String name, int type,
232
            FeatureAttributeEmulator emulator) {
233
        if (emulator == null) {
234
            throw new IllegalArgumentException();
235
        }
236
        return this.add(name, type, false).setFeatureAttributeEmulator(emulator);
237
    }
238
    
239
    public EditableFeatureAttributeDescriptor add(String name, String type) {
240
        return this.add(name,ToolsLocator.getDataTypesManager().getType(type));
241
    }
242

    
243
    public EditableFeatureAttributeDescriptor add(String name, String type, int size) {
244
        return this.add(name,ToolsLocator.getDataTypesManager().getType(type), size);
245
    }
246
    
247
    public Object remove(String name) {
248
        DefaultFeatureAttributeDescriptor attr = (DefaultFeatureAttributeDescriptor) this
249
                .get(name);
250
        if (attr == null) {
251
            return null;
252
        }
253
        if (attr.getEvaluator() == null) {
254
            hasStrongChanges = true;
255
        }
256
        super.remove(attr);
257
        this.pk = null;
258
        this.fixAll();
259
        return attr;
260
    }
261

    
262
    protected void fixAll() {
263
        int i = 0;
264
        Iterator iter = super.iterator();
265
        DefaultFeatureAttributeDescriptor attr;
266

    
267
        while (iter.hasNext()) {
268
            attr = (DefaultFeatureAttributeDescriptor) iter
269
                    .next();
270
            attr.setIndex(i++);
271
            if (attr instanceof DefaultEditableFeatureAttributeDescriptor) {
272
                ((DefaultEditableFeatureAttributeDescriptor) attr).fixAll();
273
            }
274
            if (attr.getEvaluator() != null) {
275
                this.hasEvaluators = true;
276
            }
277
            if (attr.getFeatureAttributeEmulator() != null) {
278
                this.hasEmulators = true;
279
            }
280
            if (this.defaultGeometryAttributeName == null && attr.getType() == DataTypes.GEOMETRY) {
281
                this.defaultGeometryAttributeName = attr.getName();
282
            }
283
        }
284
        if (this.defaultGeometryAttributeName != null) {
285
            this.defaultGeometryAttributeIndex = this.getIndex(this.defaultGeometryAttributeName);
286
        }
287
        this.internalID = Long.toHexString(this.getCRC());
288
    }
289

    
290
    public void checkIntegrity() throws DataListException {
291
        Iterator iter = super.iterator();
292
        FeatureTypeIntegrityException ex = null;
293

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

    
311
    public boolean remove(EditableFeatureAttributeDescriptor attribute) {
312
        if (attribute.getEvaluator() != null) {
313
            hasStrongChanges = true;
314
        }
315
        if (!super.remove(attribute)) {
316
            return false;
317
        }
318
        this.fixAll();
319
        return true;
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
    public void setDefaultTimeAttributeName(String name) {
389
        if (name == null || name.length() == 0) {
390
            this.defaultTimeAttributeIndex = -1;
391
            return;
392
        }
393
        DefaultFeatureAttributeDescriptor attr = (DefaultFeatureAttributeDescriptor) this
394
                .get(name);
395
        if (attr == null) {
396
            throw new IllegalArgumentException("Attribute '" + name
397
                    + "' not found.");
398
        }
399

    
400
        this.defaultTimeAttributeIndex = attr.getIndex();
401
    }
402
}