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

History | View | Annotate | Download (11.6 KB)

1 40559 jjdelcerro
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6 42290 jjdelcerro
 * 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 40559 jjdelcerro
 *
11 42290 jjdelcerro
 * 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 40559 jjdelcerro
 *
16 42290 jjdelcerro
 * 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 40559 jjdelcerro
 *
20 42290 jjdelcerro
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22 40559 jjdelcerro
 */
23 40435 jjdelcerro
package org.gvsig.fmap.dal.feature.impl;
24
25
import java.text.MessageFormat;
26
27
import java.util.Iterator;
28 42027 jjdelcerro
import java.util.zip.CRC32;
29 40435 jjdelcerro
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 41335 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator;
36 40435 jjdelcerro
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 42293 jjdelcerro
import org.gvsig.tools.ToolsLocator;
40 40435 jjdelcerro
import org.gvsig.tools.evaluator.Evaluator;
41
42
public class DefaultEditableFeatureType extends DefaultFeatureType implements
43 42290 jjdelcerro
        EditableFeatureType {
44 40435 jjdelcerro
45 42290 jjdelcerro
    /**
46
     *
47
     */
48
    private static final long serialVersionUID = -713976880396024389L;
49 40435 jjdelcerro
50 42290 jjdelcerro
    private boolean hasStrongChanges;
51
    private DefaultFeatureType source;
52 40435 jjdelcerro
53 42290 jjdelcerro
    public DefaultEditableFeatureType() {
54
        super();
55
        this.hasStrongChanges = false;
56
        this.source = null;
57
    }
58 40435 jjdelcerro
59 42290 jjdelcerro
    public DefaultEditableFeatureType(String id) {
60
        super(id);
61
        this.hasStrongChanges = false;
62
        this.source = null;
63
    }
64 40435 jjdelcerro
65 42290 jjdelcerro
    protected DefaultEditableFeatureType(DefaultEditableFeatureType other) {
66
        super(other);
67
        this.source = (DefaultFeatureType) other.getSource();
68
    }
69 40435 jjdelcerro
70 42290 jjdelcerro
    protected DefaultEditableFeatureType(DefaultFeatureType other) {
71
        super(other);
72
        this.source = other;
73
    }
74 40435 jjdelcerro
75 42290 jjdelcerro
    protected void intitalizeAddAttibute(DefaultFeatureAttributeDescriptor attr) {
76
        super.add(new DefaultEditableFeatureAttributeDescriptor(attr));
77
    }
78 40435 jjdelcerro
79 42290 jjdelcerro
    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 40435 jjdelcerro
94 42290 jjdelcerro
    public FeatureType getCopy() {
95
        return new DefaultEditableFeatureType(this);
96
    }
97 40435 jjdelcerro
98 42290 jjdelcerro
    public EditableFeatureType getEditable() {
99
        throw new UnsupportedOperationException();
100
    }
101 40435 jjdelcerro
102 42290 jjdelcerro
    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 40435 jjdelcerro
122 42290 jjdelcerro
    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 40435 jjdelcerro
136 42290 jjdelcerro
    }
137 40435 jjdelcerro
138 42290 jjdelcerro
    public FeatureType getSource() {
139
        return source;
140
    }
141 40435 jjdelcerro
142 42290 jjdelcerro
    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 42027 jjdelcerro
        }
151 42290 jjdelcerro
        return copy;
152
    }
153 42027 jjdelcerro
154 42290 jjdelcerro
    public EditableFeatureAttributeDescriptor add(String name, int type) {
155
        return this.add(name, type, true);
156
    }
157 40435 jjdelcerro
158
    private EditableFeatureAttributeDescriptor add(String name, int type, boolean updateHasStrongChanges) {
159 42290 jjdelcerro
        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 40435 jjdelcerro
169 42290 jjdelcerro
        }
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 43283 jjdelcerro
            case DataTypes.URI:
187
            case DataTypes.URL:
188
            case DataTypes.FILE:
189 42290 jjdelcerro
                break;
190 40435 jjdelcerro
191 42290 jjdelcerro
            default:
192
                throw new UnsupportedDataTypeException(name, type);
193
        }
194
        attr.setDataType(type);
195
        attr.setIndex(this.size());
196
        super.add(attr);
197
        if (!hasStrongChanges && updateHasStrongChanges) {
198
            hasStrongChanges = true;
199
        }
200
        this.pk = null;
201
        return attr;
202
    }
203 40435 jjdelcerro
204 42290 jjdelcerro
    public EditableFeatureAttributeDescriptor add(String name, int type,
205
            int size) {
206
        return this.add(name, type, true).setSize(size);
207
    }
208 40435 jjdelcerro
209 42290 jjdelcerro
    public EditableFeatureAttributeDescriptor add(String name, int type,
210
            Evaluator evaluator) {
211
        if (evaluator == null) {
212
            throw new IllegalArgumentException();
213
        }
214
        return this.add(name, type, false).setEvaluator(evaluator);
215
    }
216 41335 jjdelcerro
217 42290 jjdelcerro
    public EditableFeatureAttributeDescriptor add(String name, int type,
218
            FeatureAttributeEmulator emulator) {
219
        if (emulator == null) {
220
            throw new IllegalArgumentException();
221
        }
222
        return this.add(name, type, false).setFeatureAttributeEmulator(emulator);
223
    }
224 42293 jjdelcerro
225
    public EditableFeatureAttributeDescriptor add(String name, String type) {
226
        return this.add(name,ToolsLocator.getDataTypesManager().getType(type));
227
    }
228 40435 jjdelcerro
229 42293 jjdelcerro
    public EditableFeatureAttributeDescriptor add(String name, String type, int size) {
230
        return this.add(name,ToolsLocator.getDataTypesManager().getType(type), size);
231
    }
232
233 42290 jjdelcerro
    public Object remove(String name) {
234
        DefaultFeatureAttributeDescriptor attr = (DefaultFeatureAttributeDescriptor) this
235
                .get(name);
236
        if (attr == null) {
237
            return null;
238
        }
239
        if (attr.getEvaluator() == null) {
240
            hasStrongChanges = true;
241
        }
242
        super.remove(attr);
243
        this.pk = null;
244
        this.fixAll();
245
        return attr;
246
    }
247 40435 jjdelcerro
248 43610 jjdelcerro
    @Override
249 42290 jjdelcerro
    protected void fixAll() {
250 43610 jjdelcerro
        super.fixAll();
251 42290 jjdelcerro
    }
252 40435 jjdelcerro
253 42290 jjdelcerro
    public void checkIntegrity() throws DataListException {
254
        Iterator iter = super.iterator();
255
        FeatureTypeIntegrityException ex = null;
256 40435 jjdelcerro
257 42290 jjdelcerro
        while (iter.hasNext()) {
258
            DefaultEditableFeatureAttributeDescriptor attr = (DefaultEditableFeatureAttributeDescriptor) iter
259
                    .next();
260
            try {
261
                attr.checkIntegrity();
262
            } catch (Exception e) {
263
                if (ex == null) {
264
                    ex = new FeatureTypeIntegrityException(this.getId());
265
                }
266
                ex.add(e);
267
            }
268
        }
269
        if (ex != null) {
270
            throw ex;
271
        }
272
    }
273 40435 jjdelcerro
274 42290 jjdelcerro
    public boolean remove(EditableFeatureAttributeDescriptor attribute) {
275
        if (attribute.getEvaluator() != null) {
276
            hasStrongChanges = true;
277
        }
278
        if (!super.remove(attribute)) {
279
            return false;
280
        }
281
        this.fixAll();
282
        return true;
283
    }
284 40435 jjdelcerro
285 43074 jjdelcerro
    @Override
286
    public void setDefaultGeometryType(int type, int subType) {
287
        DefaultEditableFeatureType descr = (DefaultEditableFeatureType)this.getDefaultGeometryAttribute();
288
        if( descr == null ) {
289
            throw new IllegalStateException("Default geometry attribute not set.");
290
        }
291
        descr.setDefaultGeometryType(type, subType);
292
    }
293
294 42290 jjdelcerro
    public void setDefaultGeometryAttributeName(String name) {
295
        if (name == null || name.length() == 0) {
296
            this.defaultGeometryAttributeName = null;
297
            this.defaultGeometryAttributeIndex = -1;
298
            return;
299
        }
300
        DefaultFeatureAttributeDescriptor attr = (DefaultFeatureAttributeDescriptor) this
301
                .get(name);
302
        if (attr == null) {
303
            throw new IllegalArgumentException("Attribute '" + name
304
                    + "' not found.");
305
        }
306 43420 jjdelcerro
//        if (attr.getType() != DataTypes.GEOMETRY) {
307
//            throw new IllegalArgumentException("Attribute '" + name
308
//                    + "' is not a geometry.");
309
//        }
310 42290 jjdelcerro
        this.defaultGeometryAttributeName = name;
311
        this.defaultGeometryAttributeIndex = attr.getIndex();
312
    }
313 40435 jjdelcerro
314 42290 jjdelcerro
    public void setHasOID(boolean hasOID) {
315
        this.hasOID = hasOID;
316
    }
317 40435 jjdelcerro
318 42290 jjdelcerro
    protected Iterator getIterator(Iterator iter) {
319
        return new EditableDelegatedIterator(iter, this);
320
    }
321
322 41272 jjdelcerro
    public EditableFeatureAttributeDescriptor getEditableAttributeDescriptor(String name) {
323
        return (EditableFeatureAttributeDescriptor) this.getAttributeDescriptor(name);
324
    }
325
326
    public EditableFeatureAttributeDescriptor getEditableAttributeDescriptor(int index) {
327 42290 jjdelcerro
        return (EditableFeatureAttributeDescriptor) this.getAttributeDescriptor(index);
328 41272 jjdelcerro
    }
329
330 42290 jjdelcerro
    protected class EditableDelegatedIterator extends DelegatedIterator {
331 40435 jjdelcerro
332 42290 jjdelcerro
        private DefaultEditableFeatureType fType;
333 40435 jjdelcerro
334 42290 jjdelcerro
        public EditableDelegatedIterator(Iterator iter,
335
                DefaultEditableFeatureType fType) {
336
            super(iter);
337
            this.fType = fType;
338
        }
339 40435 jjdelcerro
340 42290 jjdelcerro
        public void remove() {
341
            this.iterator.remove();
342
            this.fType.fixAll();
343
        }
344 40435 jjdelcerro
345 42290 jjdelcerro
    }
346 40435 jjdelcerro
347 42290 jjdelcerro
    protected void setAllowAutomaticValues(boolean value) {
348
        this.allowAtomaticValues = value;
349
    }
350 40435 jjdelcerro
351
}