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

History | View | Annotate | Download (11.7 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

    
29
import org.gvsig.fmap.dal.DataTypes;
30
import org.gvsig.fmap.dal.exception.DataListException;
31
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
32
import org.gvsig.fmap.dal.feature.EditableFeatureType;
33
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
34
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator;
35
import org.gvsig.fmap.dal.feature.FeatureStore;
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(FeatureStore store) {
54
        super(store);
55
        this.hasStrongChanges = false;
56
        this.source = null;
57
    }
58

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

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

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

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

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

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

    
137
    }
138

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

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

    
155
    public EditableFeatureAttributeDescriptor add(String name, int type) {
156
        return this.add(name, type, true);
157
    }
158

    
159
    private EditableFeatureAttributeDescriptor add(String name, int type, boolean updateHasStrongChanges) {
160
        DefaultEditableFeatureAttributeDescriptor attr = new DefaultEditableFeatureAttributeDescriptor(this);
161
        Iterator iter = this.iterator();
162
        while (iter.hasNext()) {
163
            EditableFeatureAttributeDescriptor descriptor = (EditableFeatureAttributeDescriptor) iter.next();
164
            if (descriptor.getName().equalsIgnoreCase(name)) {
165
                throw new RuntimeException(
166
                        MessageFormat.format("Name descriptor {0} duplicated.", new String[]{name})
167
                );
168
            }
169

    
170
        }
171
        attr.setName(name);
172
        switch (type) {
173
            case DataTypes.BOOLEAN:
174
            case DataTypes.BYTE:
175
            case DataTypes.BYTEARRAY:
176
            case DataTypes.CHAR:
177
            case DataTypes.DATE:
178
            case DataTypes.DOUBLE:
179
            case DataTypes.FLOAT:
180
            case DataTypes.GEOMETRY:
181
            case DataTypes.INT:
182
            case DataTypes.LONG:
183
            case DataTypes.OBJECT:
184
            case DataTypes.STRING:
185
            case DataTypes.TIME:
186
            case DataTypes.TIMESTAMP:
187
            case DataTypes.URI:
188
            case DataTypes.URL:
189
            case DataTypes.FILE:
190
                break;
191

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

    
205
    public EditableFeatureAttributeDescriptor add(String name, int type,
206
            int size) {
207
        return this.add(name, type, true).setSize(size);
208
    }
209

    
210
    public EditableFeatureAttributeDescriptor add(String name, int type,
211
            Evaluator evaluator) {
212
        if (evaluator == null) {
213
            throw new IllegalArgumentException();
214
        }
215
        return this.add(name, type, false).setEvaluator(evaluator);
216
    }
217

    
218
    public EditableFeatureAttributeDescriptor add(String name, int type,
219
            FeatureAttributeEmulator emulator) {
220
        if (emulator == null) {
221
            throw new IllegalArgumentException();
222
        }
223
        return this.add(name, type, false).setFeatureAttributeEmulator(emulator);
224
    }
225
    
226
    public EditableFeatureAttributeDescriptor add(String name, String type) {
227
        return this.add(name,ToolsLocator.getDataTypesManager().getType(type));
228
    }
229

    
230
    public EditableFeatureAttributeDescriptor add(String name, String type, int size) {
231
        return this.add(name,ToolsLocator.getDataTypesManager().getType(type), size);
232
    }
233
    
234
    public Object remove(String name) {
235
        DefaultFeatureAttributeDescriptor attr = (DefaultFeatureAttributeDescriptor) this
236
                .get(name);
237
        if (attr == null) {
238
            return null;
239
        }
240
        if (attr.getEvaluator() == null) {
241
            hasStrongChanges = true;
242
        }
243
        super.remove(attr);
244
        this.pk = null;
245
        this.fixAll();
246
        return attr;
247
    }
248

    
249
    @Override
250
    protected void fixAll() {
251
        super.fixAll();
252
    }
253

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

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

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

    
286
    @Override
287
    public void setDefaultGeometryType(int type, int subType) {
288
        DefaultEditableFeatureType descr = (DefaultEditableFeatureType)this.getDefaultGeometryAttribute();
289
        if( descr == null ) {
290
            throw new IllegalStateException("Default geometry attribute not set.");
291
        }
292
        descr.setDefaultGeometryType(type, subType);
293
    }
294

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

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

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

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

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

    
331
    protected class EditableDelegatedIterator extends DelegatedIterator {
332

    
333
        private DefaultEditableFeatureType fType;
334

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

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

    
346
    }
347

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

    
352
}