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

History | View | Annotate | Download (10.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
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.fmap.dal.feature.impl;
25

    
26
import java.text.MessageFormat;
27

    
28
import java.util.Iterator;
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.evaluator.Evaluator;
40

    
41
public class DefaultEditableFeatureType extends DefaultFeatureType implements
42
                EditableFeatureType {
43

    
44
        /**
45
         *
46
         */
47
        private static final long serialVersionUID = -713976880396024389L;
48

    
49
        private boolean hasStrongChanges;
50
        private DefaultFeatureType source;
51

    
52
        public DefaultEditableFeatureType() {
53
                super();
54
                this.hasStrongChanges = false;
55
                this.source = null;
56
        }
57

    
58
        public DefaultEditableFeatureType(String id) {
59
                super(id);
60
                this.hasStrongChanges = false;
61
                this.source = null;
62
        }
63

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

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

    
74
        protected void intitalizeAddAttibute(DefaultFeatureAttributeDescriptor attr) {
75
                super.add(new DefaultEditableFeatureAttributeDescriptor(attr));
76
        }
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
        public FeatureType getNotEditableCopy() {
143
                this.fixAll();
144
                DefaultFeatureType copy = new DefaultFeatureType(this, false);
145
                Iterator iter = this.iterator();
146
                DefaultFeatureAttributeDescriptor attr;
147
                while (iter.hasNext()) {
148
                        attr = (DefaultFeatureAttributeDescriptor) iter.next();
149
                        copy.add(new DefaultFeatureAttributeDescriptor(attr));
150
                }
151
                return copy;
152
        }
153

    
154
        public EditableFeatureAttributeDescriptor add(String name, int type) {
155
            return this.add(name,type,true);
156
        }
157
        
158
    private EditableFeatureAttributeDescriptor add(String name, int type, boolean updateHasStrongChanges) {
159
                DefaultEditableFeatureAttributeDescriptor attr = new DefaultEditableFeatureAttributeDescriptor();
160
                Iterator iter = this.iterator();
161
                while (iter.hasNext()) {
162
                        EditableFeatureAttributeDescriptor descriptor = (EditableFeatureAttributeDescriptor) iter.next();
163
                        if(descriptor.getName().equalsIgnoreCase(name)){
164
                                throw new RuntimeException(
165
                                                MessageFormat.format("Name descriptor {0} duplicated.",new String[]{name})
166
                                                );
167
                        }
168
                        
169
                }
170
                attr.setName(name);
171
                switch (type) {
172
                case DataTypes.BOOLEAN:
173
                case DataTypes.BYTE:
174
                case DataTypes.BYTEARRAY:
175
                case DataTypes.CHAR:
176
                case DataTypes.DATE:
177
                case DataTypes.DOUBLE:
178
                case DataTypes.FLOAT:
179
                case DataTypes.GEOMETRY:
180
                case DataTypes.INT:
181
                case DataTypes.LONG:
182
                case DataTypes.OBJECT:
183
                case DataTypes.STRING:
184
                case DataTypes.TIME:
185
                case DataTypes.TIMESTAMP:
186
                        break;
187

    
188
                default:
189
                        throw new UnsupportedDataTypeException(name, type);
190
                }
191
                attr.setDataType(type);
192
                attr.setIndex(this.size());
193
                super.add(attr);
194
                if (!hasStrongChanges && updateHasStrongChanges){
195
                    hasStrongChanges = true;
196
                }
197
                this.pk = null;
198
                return attr;
199
        }
200

    
201
        public EditableFeatureAttributeDescriptor add(String name, int type,
202
                        int size) {
203
                return this.add(name, type,true).setSize(size);
204
        }
205

    
206
        public EditableFeatureAttributeDescriptor add(String name, int type,
207
                        Evaluator evaluator) {
208
            if (evaluator == null){
209
                throw new IllegalArgumentException();
210
            }
211
                return this.add(name, type, false).setEvaluator(evaluator);
212
        }
213

    
214
        public EditableFeatureAttributeDescriptor add(String name, int type,
215
                                 FeatureAttributeEmulator emulator) {
216
            if (emulator == null){
217
                throw new IllegalArgumentException();
218
            }
219
            return this.add(name, type, false).setFeatureAttributeEmulator(emulator);
220
        }
221

    
222
        public Object remove(String name) {
223
                DefaultFeatureAttributeDescriptor attr = (DefaultFeatureAttributeDescriptor) this
224
                                .get(name);
225
                if (attr == null) {
226
                        return null;
227
                }
228
                if (attr.getEvaluator() == null) {
229
                        hasStrongChanges = true;
230
                }
231
                super.remove(attr);
232
                this.pk = null;
233
                this.fixAll();
234
                return attr;
235
        }
236

    
237
        protected void fixAll() {
238
                int i = 0;
239
                Iterator iter = super.iterator();
240
                DefaultFeatureAttributeDescriptor attr;
241

    
242
                while (iter.hasNext()) {
243
                        attr = (DefaultFeatureAttributeDescriptor) iter
244
                                        .next();
245
                        attr.setIndex(i++);
246
                        if (attr instanceof DefaultEditableFeatureAttributeDescriptor) {
247
                                ((DefaultEditableFeatureAttributeDescriptor) attr).fixAll();
248
                        }
249
                        if( attr.getEvaluator()!=null ) {
250
                                this.hasEvaluators = true;
251
                        }
252
                        if( attr.getFeatureAttributeEmulator()!=null ) {
253
                            this.hasEmulators = true;
254
                        }
255
                        if( this.defaultGeometryAttributeName == null && attr.getType() == DataTypes.GEOMETRY ) {
256
                                this.defaultGeometryAttributeName = attr.getName();
257
                        }
258
                }
259
                if (this.defaultGeometryAttributeName != null) {
260
                        this.defaultGeometryAttributeIndex = this.getIndex(this.defaultGeometryAttributeName);
261
                }
262
        }
263

    
264
        public void checkIntegrity() throws DataListException {
265
                Iterator iter = super.iterator();
266
                FeatureTypeIntegrityException ex = null;
267

    
268
                while (iter.hasNext()) {
269
                        DefaultEditableFeatureAttributeDescriptor attr = (DefaultEditableFeatureAttributeDescriptor) iter
270
                                        .next();
271
                        try {
272
                                attr.checkIntegrity();
273
                        } catch (Exception e) {
274
                                if (ex == null) {
275
                                        ex = new FeatureTypeIntegrityException(this.getId());
276
                                }
277
                                ex.add(e);
278
                        }
279
                }
280
                if (ex != null) {
281
                        throw ex;
282
                }
283
        }
284

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

    
296
        public void setDefaultGeometryAttributeName(String name) {
297
                if (name == null || name.length() == 0) {
298
                        this.defaultGeometryAttributeName = null;
299
                        this.defaultGeometryAttributeIndex = -1;
300
                        return;
301
                }
302
                DefaultFeatureAttributeDescriptor attr = (DefaultFeatureAttributeDescriptor) this
303
                                .get(name);
304
                if (attr == null) {
305
                        throw new IllegalArgumentException("Attribute '" + name
306
                                        + "' not found.");
307
                }
308
                if (attr.getType() != DataTypes.GEOMETRY) {
309
                        throw new IllegalArgumentException("Attribute '" + name
310
                                        + "' is not a geometry.");
311
                }
312
                this.defaultGeometryAttributeName = name;
313
                this.defaultGeometryAttributeIndex = attr.getIndex();
314
        }
315

    
316
        public void setHasOID(boolean hasOID) {
317
                this.hasOID = hasOID;
318
        }
319

    
320
        protected Iterator getIterator(Iterator iter) {
321
                return new EditableDelegatedIterator(iter, this);
322
        }
323

    
324
    public EditableFeatureAttributeDescriptor getEditableAttributeDescriptor(String name) {
325
        return (EditableFeatureAttributeDescriptor) this.getAttributeDescriptor(name);
326
    }
327

    
328
    public EditableFeatureAttributeDescriptor getEditableAttributeDescriptor(int index) {
329
       return (EditableFeatureAttributeDescriptor) this.getAttributeDescriptor(index);
330
    }
331

    
332
        protected class EditableDelegatedIterator extends DelegatedIterator {
333

    
334
                private DefaultEditableFeatureType fType;
335

    
336
                public EditableDelegatedIterator(Iterator iter,
337
                                DefaultEditableFeatureType fType) {
338
                        super(iter);
339
                        this.fType = fType;
340
                }
341

    
342
                public void remove() {
343
                        this.iterator.remove();
344
                        this.fType.fixAll();
345
                }
346

    
347
        }
348

    
349
        protected void setAllowAutomaticValues(boolean value) {
350
                this.allowAtomaticValues = value;
351
        }
352

    
353
    public void setDefaultTimeAttributeName(String name) {
354
        if (name == null || name.length() == 0) {
355
            this.defaultTimeAttributeIndex = -1;
356
            return;
357
        }
358
        DefaultFeatureAttributeDescriptor attr = (DefaultFeatureAttributeDescriptor) this
359
                .get(name);
360
        if (attr == null) {
361
            throw new IllegalArgumentException("Attribute '" + name
362
                    + "' not found.");
363
        }
364

    
365
        this.defaultTimeAttributeIndex = attr.getIndex();        
366
    }
367
}