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

History | View | Annotate | Download (10 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.FeatureType;
36
import org.gvsig.fmap.dal.feature.exception.FeatureTypeIntegrityException;
37
import org.gvsig.fmap.dal.feature.exception.UnsupportedDataTypeException;
38
import org.gvsig.tools.evaluator.Evaluator;
39

    
40
public class DefaultEditableFeatureType extends DefaultFeatureType implements
41
                EditableFeatureType {
42

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

    
48
        private boolean hasStrongChanges;
49
        private DefaultFeatureType source;
50

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

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

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

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

    
73
        protected void intitalizeAddAttibute(DefaultFeatureAttributeDescriptor attr) {
74
                super.add(new DefaultEditableFeatureAttributeDescriptor(attr));
75
        }
76

    
77

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

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

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

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

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

    
135
        }
136

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

    
141
        public FeatureType getNotEditableCopy() {
142
                this.fixAll();
143
                DefaultFeatureType copy = new DefaultFeatureType(this, false);
144
                Iterator iter = this.iterator();
145
                DefaultFeatureAttributeDescriptor attr;
146
                while (iter.hasNext()) {
147
                        attr = (DefaultFeatureAttributeDescriptor) iter.next();
148
                        copy.add(new DefaultFeatureAttributeDescriptor(attr));
149
                }
150
                return copy;
151
        }
152

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

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

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

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

    
213
        public Object remove(String name) {
214
                DefaultFeatureAttributeDescriptor attr = (DefaultFeatureAttributeDescriptor) this
215
                                .get(name);
216
                if (attr == null) {
217
                        return null;
218
                }
219
                if (attr.getEvaluator() == null) {
220
                        hasStrongChanges = true;
221
                }
222
                super.remove(attr);
223
                this.pk = null;
224
                this.fixAll();
225
                return attr;
226
        }
227

    
228
        protected void fixAll() {
229
                int i = 0;
230
                Iterator iter = super.iterator();
231
                DefaultFeatureAttributeDescriptor attr;
232

    
233
                while (iter.hasNext()) {
234
                        attr = (DefaultFeatureAttributeDescriptor) iter
235
                                        .next();
236
                        attr.setIndex(i++);
237
                        if (attr instanceof DefaultEditableFeatureAttributeDescriptor) {
238
                                ((DefaultEditableFeatureAttributeDescriptor) attr).fixAll();
239
                        }
240
                        if( attr.getEvaluator()!=null ) {
241
                                this.hasEvaluators = true;
242
                        }
243
                        if( this.defaultGeometryAttributeName == null && attr.getType() == DataTypes.GEOMETRY ) {
244
                                this.defaultGeometryAttributeName = attr.getName();
245
                        }
246
                }
247
                if (this.defaultGeometryAttributeName != null) {
248
                        this.defaultGeometryAttributeIndex = this.getIndex(this.defaultGeometryAttributeName);
249
                }
250
        }
251

    
252
        public void checkIntegrity() throws DataListException {
253
                Iterator iter = super.iterator();
254
                FeatureTypeIntegrityException ex = null;
255

    
256
                while (iter.hasNext()) {
257
                        DefaultEditableFeatureAttributeDescriptor attr = (DefaultEditableFeatureAttributeDescriptor) iter
258
                                        .next();
259
                        try {
260
                                attr.checkIntegrity();
261
                        } catch (Exception e) {
262
                                if (ex == null) {
263
                                        ex = new FeatureTypeIntegrityException(this.getId());
264
                                }
265
                                ex.add(e);
266
                        }
267
                }
268
                if (ex != null) {
269
                        throw ex;
270
                }
271
        }
272

    
273
        public boolean remove(EditableFeatureAttributeDescriptor attribute) {
274
                if (attribute.getEvaluator() != null) {
275
                        hasStrongChanges = true;
276
                }
277
                if (!super.remove(attribute)) {
278
                        return false;
279
                }
280
                this.fixAll();
281
                return true;
282
        }
283

    
284
        public void setDefaultGeometryAttributeName(String name) {
285
                if (name == null || name.length() == 0) {
286
                        this.defaultGeometryAttributeName = null;
287
                        this.defaultGeometryAttributeIndex = -1;
288
                        return;
289
                }
290
                DefaultFeatureAttributeDescriptor attr = (DefaultFeatureAttributeDescriptor) this
291
                                .get(name);
292
                if (attr == null) {
293
                        throw new IllegalArgumentException("Attribute '" + name
294
                                        + "' not found.");
295
                }
296
                if (attr.getType() != DataTypes.GEOMETRY) {
297
                        throw new IllegalArgumentException("Attribute '" + name
298
                                        + "' is not a geometry.");
299
                }
300
                this.defaultGeometryAttributeName = name;
301
                this.defaultGeometryAttributeIndex = attr.getIndex();
302
        }
303

    
304
        public void setHasOID(boolean hasOID) {
305
                this.hasOID = hasOID;
306
        }
307

    
308
        protected Iterator getIterator(Iterator iter) {
309
                return new EditableDelegatedIterator(iter, this);
310
        }
311

    
312
    public EditableFeatureAttributeDescriptor getEditableAttributeDescriptor(String name) {
313
        return (EditableFeatureAttributeDescriptor) this.getAttributeDescriptor(name);
314
    }
315

    
316
    public EditableFeatureAttributeDescriptor getEditableAttributeDescriptor(int index) {
317
       return (EditableFeatureAttributeDescriptor) this.getAttributeDescriptor(index);
318
    }
319

    
320
        protected class EditableDelegatedIterator extends DelegatedIterator {
321

    
322
                private DefaultEditableFeatureType fType;
323

    
324
                public EditableDelegatedIterator(Iterator iter,
325
                                DefaultEditableFeatureType fType) {
326
                        super(iter);
327
                        this.fType = fType;
328
                }
329

    
330
                public void remove() {
331
                        this.iterator.remove();
332
                        this.fType.fixAll();
333
                }
334

    
335
        }
336

    
337
        protected void setAllowAutomaticValues(boolean value) {
338
                this.allowAtomaticValues = value;
339
        }
340

    
341
    public void setDefaultTimeAttributeName(String name) {
342
        if (name == null || name.length() == 0) {
343
            this.defaultTimeAttributeIndex = -1;
344
            return;
345
        }
346
        DefaultFeatureAttributeDescriptor attr = (DefaultFeatureAttributeDescriptor) this
347
                .get(name);
348
        if (attr == null) {
349
            throw new IllegalArgumentException("Attribute '" + name
350
                    + "' not found.");
351
        }
352

    
353
        this.defaultTimeAttributeIndex = attr.getIndex();        
354
    }
355
}