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

History | View | Annotate | Download (17.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
import java.util.Iterator;
27
import javax.json.JsonObject;
28
import org.apache.commons.lang3.StringUtils;
29
import org.gvsig.fmap.dal.DataTypeUtils;
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 static org.gvsig.fmap.dal.feature.impl.DefaultFeatureAttributeDescriptor.NOTIFICATION_FIXED_CHANGED;
39
import org.gvsig.fmap.dal.impl.DefaultDataManager;
40
import org.gvsig.json.Json;
41
import org.gvsig.json.JsonManager;
42
import org.gvsig.json.JsonObjectBuilder;
43
import org.gvsig.json.SupportToJson;
44
import org.gvsig.tools.ToolsLocator;
45
import org.gvsig.tools.evaluator.Evaluator;
46
import org.gvsig.tools.observer.Notification;
47
import org.gvsig.tools.observer.Observable;
48
import org.gvsig.tools.observer.Observer;
49

    
50
@SuppressWarnings("UseSpecificCatch")
51
public class DefaultEditableFeatureType extends DefaultFeatureType implements
52
        EditableFeatureType, Observer {
53

    
54
    /**
55
     *
56
     */
57
    private static final long serialVersionUID = -713976880396024389L;
58

    
59
    private boolean hasStrongChanges;
60
    private DefaultFeatureType source;
61

    
62
    public DefaultEditableFeatureType(FeatureStore store) {
63
        super(store);
64
        this.hasStrongChanges = false;
65
        this.source = null;
66
    }
67

    
68
    public DefaultEditableFeatureType(FeatureStore store, String id) {
69
        super(store, id);
70
        this.hasStrongChanges = false;
71
        this.source = null;
72
    }
73

    
74
    protected DefaultEditableFeatureType(DefaultEditableFeatureType other) {
75
        super(other);
76
        this.hasStrongChanges = other.hasStrongChanges;
77
        this.source = (DefaultFeatureType) other.getSource();
78
    }
79

    
80
    protected DefaultEditableFeatureType(DefaultFeatureType other) {
81
        super(other);
82
        this.source = other;
83
        this.fixAll();
84
    }
85

    
86
    @Override
87
    protected DefaultFeatureAttributeDescriptor getCopyAttributeDescriptor(DefaultFeatureAttributeDescriptor src) {
88
        DefaultFeatureAttributeDescriptor copy = new DefaultEditableFeatureAttributeDescriptor(src);
89
        copy.setFeatureType(this);
90
        return copy;
91
    }
92
    
93
    @Override
94
    public void forceStrongChanges() {
95
        this.hasStrongChanges = true;
96
    }
97

    
98
    public boolean hasStrongChanges() {
99
        if (hasStrongChanges) {
100
            return true;
101
        }
102
        Iterator iter = this.iterator();
103
        DefaultEditableFeatureAttributeDescriptor attr;
104
        while (iter.hasNext()) {
105
            attr = (DefaultEditableFeatureAttributeDescriptor) iter.next();
106
            if (attr.isComputed()) {
107
                continue;
108
            }
109
            if (attr.hasStrongChanges()) {
110
                return true;
111
            }
112
        }
113
        return false;
114
    }
115

    
116
    @Override
117
    public FeatureType getCopy() {
118
        DefaultEditableFeatureType copy = new DefaultEditableFeatureType(this);
119
        copy.fixAll();
120
        return copy;
121
    }
122

    
123
    @Override
124
    public EditableFeatureType getEditable() {
125
        return (EditableFeatureType) this.getCopy();
126
    }
127

    
128
    public boolean addAll(DefaultFeatureType other) {
129
        Iterator iter = other.iterator();
130
        DefaultFeatureAttributeDescriptor attr;
131
        DefaultEditableFeatureAttributeDescriptor editableAttr;
132
        while (iter.hasNext()) {
133
            attr = (DefaultFeatureAttributeDescriptor) iter.next();
134
            if (attr instanceof DefaultEditableFeatureAttributeDescriptor) {
135
                editableAttr = new DefaultEditableFeatureAttributeDescriptor(
136
                        attr);
137
            } else {
138
                editableAttr = new DefaultEditableFeatureAttributeDescriptor(
139
                        attr);
140
            }
141
            editableAttr.addObserver(this);
142
            super.add(editableAttr);
143
        }
144
        this.pk = null;
145
        this.fixAll();
146
        return true;
147
    }
148

    
149
    @Override
150
    public EditableFeatureAttributeDescriptor addLike(
151
            FeatureAttributeDescriptor other) {
152
        DefaultEditableFeatureAttributeDescriptor editableAttr;
153
        editableAttr = new DefaultEditableFeatureAttributeDescriptor(
154
                (DefaultFeatureAttributeDescriptor) other);
155
        super.add(editableAttr);
156
        if (!editableAttr.isComputed()) {
157
            this.hasStrongChanges = true;
158
        }
159
        editableAttr.addObserver(this);
160
        this.fixAll();
161
        return editableAttr;
162
    }
163

    
164
    @Override
165
    public FeatureType getSource() {
166
        return source;
167
    }
168

    
169
    @Override
170
    public FeatureType getNotEditableCopy() {
171
        this.fixAll();
172
        DefaultFeatureType copy = new DefaultFeatureType(this, false);
173
        Iterator iter = this.iterator();
174
        DefaultFeatureAttributeDescriptor attr;
175
        while (iter.hasNext()) {
176
            attr = (DefaultFeatureAttributeDescriptor) iter.next();
177
            DefaultFeatureAttributeDescriptor newattr = new DefaultFeatureAttributeDescriptor(attr);
178
            newattr.setFeatureType(copy);
179
            copy.add(newattr);
180
        }
181
        return copy;
182
    }
183

    
184
    @Override
185
    public EditableFeatureAttributeDescriptor add(String name, int type) {
186
        return this.add(name, type, true);
187
    }
188

    
189
    @Override
190
    public void removeAll() {
191
        while (!super.isEmpty()) {
192
            FeatureAttributeDescriptor attr = this.get(0);
193
            if(attr instanceof Observable){
194
                ((Observable) attr).deleteObserver(this);
195
            }
196
            super.remove(0);
197
        }
198
        this.fixAll();
199
    }
200

    
201
    @Override
202
    public void addAll(FeatureType attributes) {
203
        super.addAll(attributes);
204
        this.fixAll();
205
    }
206

    
207
    private EditableFeatureAttributeDescriptor add(String name, int type, boolean strongChanges) {
208
        Iterator iter = this.iterator();
209
        while (iter.hasNext()) {
210
            EditableFeatureAttributeDescriptor descriptor = (EditableFeatureAttributeDescriptor) iter.next();
211
            if (descriptor.getName().equalsIgnoreCase(name)) {
212
                throw new IllegalArgumentException(
213
                        MessageFormat.format("Duplicated name descriptor {0}.", name)
214
                );
215
            }
216

    
217
        }
218
        DefaultEditableFeatureAttributeDescriptor attr = DefaultDataManager.createEditableFeatureAttributeDescriptor(this, name, type, strongChanges);
219
        attr.setIndex(this.size());
220

    
221
        this.hasStrongChanges = this.hasStrongChanges || strongChanges;
222
        super.add(attr);
223
        this.pk = null;
224
        attr.addObserver(this);
225
        this.fixed = false;
226
        return attr;
227
    }
228

    
229
    @Override
230
    public EditableFeatureAttributeDescriptor add(String name, int type, int size) {
231
        return this.add(name, type, true).setSize(size);
232
    }
233

    
234
    @Override
235
    public EditableFeatureAttributeDescriptor add(String name, int type,
236
            Evaluator evaluator) {
237
        if (evaluator == null) {
238
            throw new IllegalArgumentException();
239
        }
240
        return this.add(name, type, false).setEvaluator(evaluator);
241
    }
242

    
243
    @Override
244
    public EditableFeatureAttributeDescriptor add(String name, int type,
245
            FeatureAttributeEmulator emulator) {
246
        if (emulator == null) {
247
            throw new IllegalArgumentException();
248
        }
249
        return this.add(name, type, false).setFeatureAttributeEmulator(emulator);
250
    }
251

    
252
    @Override
253
    public EditableFeatureAttributeDescriptor add(String name, String type) {
254
        return this.add(name, ToolsLocator.getDataTypesManager().getType(type));
255
    }
256

    
257
    @Override
258
    public EditableFeatureAttributeDescriptor add(String name, String type, int size) {
259
        return this.add(name, ToolsLocator.getDataTypesManager().getType(type), size);
260
    }
261

    
262
    public Object removeAttributeDescriptor(String name) {
263
//    Hemos metido los metodos removeAttributeDescriptor ya que existe
264
//    un problema desde python al llamar a los metodos remove. En lugar de llamar
265
//    a estos metodos remove, llama a los de la superclase ArrayList.
266
        return this.remove(name);
267
    }
268

    
269
    public boolean removeAttributeDescriptor(EditableFeatureAttributeDescriptor attribute) {
270
        return this.remove(attribute);
271
    }
272

    
273
    @Override
274
    public FeatureAttributeDescriptor remove(int index) {
275
        FeatureAttributeDescriptor attr = (FeatureAttributeDescriptor) this.get(index);
276
        if (!attr.isComputed()) {
277
            hasStrongChanges = true;
278
        }
279
        if (index == this.getDefaultGeometryAttributeIndex()) {
280
            this.defaultGeometryAttributeIndex = -1;
281
            this.defaultGeometryAttributeName = null;
282
        } else if (index == this.getDefaultTimeAttributeIndex()) {
283
            this.defaultTimeAttributeIndex = -1;
284
            this.defaultTimeAttributeName = null;
285
        }
286
        super.remove(attr);
287
        this.pk = null;
288
        if(attr instanceof Observable){
289
            ((Observable) attr).deleteObserver(this);
290
        }
291
        this.fixAll();
292
        return attr;
293
    }
294

    
295
    @Override
296
    public Object remove(String name) {
297
        FeatureAttributeDescriptor attr = (FeatureAttributeDescriptor) this.get(name);
298
        if (attr == null) {
299
            return null;
300
        }
301
        this.remove(attr.getIndex());
302
        return attr;
303
    }
304

    
305
    @Override
306
    public boolean remove(EditableFeatureAttributeDescriptor attribute) {
307
        this.remove(attribute.getIndex());
308
        return true;
309
    }
310

    
311
    @Override
312
    protected void fixAll() {
313
        if(this.fixed){
314
            return;
315
        }
316
        super.fixAll();
317
        for (FeatureAttributeDescriptor attr : this) {
318
            if (attr instanceof DefaultEditableFeatureAttributeDescriptor) {
319
                DefaultEditableFeatureAttributeDescriptor attr2 = (DefaultEditableFeatureAttributeDescriptor) attr;
320
                if (attr2.hasStrongChanges()) {
321
                    this.hasStrongChanges = true;
322
                    break;
323
                }
324
            }
325
        }
326
        this.fixed = true;
327
    }
328

    
329
    public void checkIntegrity() throws DataListException {
330
        Iterator iter = super.iterator();
331
        FeatureTypeIntegrityException ex = null;
332

    
333
        while (iter.hasNext()) {
334
            DefaultEditableFeatureAttributeDescriptor attr = (DefaultEditableFeatureAttributeDescriptor) iter
335
                    .next();
336
            try {
337
                attr.checkIntegrity();
338
            } catch (Exception e) {
339
                if (ex == null) {
340
                    ex = new FeatureTypeIntegrityException(this.getId());
341
                }
342
                ex.add(e);
343
            }
344
        }
345
        if (ex != null) {
346
            throw ex;
347
        }
348
    }
349

    
350
    @Override
351
    public void setDefaultGeometryType(int type, int subType) {
352
        EditableFeatureAttributeDescriptor descr = (EditableFeatureAttributeDescriptor) this.getDefaultGeometryAttribute();
353
        if (descr == null) {
354
            throw new IllegalStateException("Default geometry attribute not set.");
355
        }
356
        descr.setGeometryType(type, subType);
357
        this.fixed = false;
358
    }
359

    
360
    @Override
361
    public void setDefaultGeometryAttributeName(String name) {
362
        if (name == null || name.length() == 0) {
363
            this.defaultGeometryAttributeName = null;
364
            this.defaultGeometryAttributeIndex = -1;
365
            return;
366
        }
367
        FeatureAttributeDescriptor attr = this.getAttributeDescriptor(name);
368
        if (attr == null) {
369
            throw new IllegalArgumentException("Attribute '" + name + "' not found.");
370
        }
371
        this.defaultGeometryAttributeName = name;
372
        this.defaultGeometryAttributeIndex = attr.getIndex();
373
        this.fixed = false;
374
    }
375

    
376
    @Override
377
    public int getDefaultGeometryAttributeIndex() {
378
        this.fixAll();
379
        return this.defaultGeometryAttributeIndex;
380
    }
381

    
382
    @Override
383
    public String getDefaultGeometryAttributeName() {
384
        this.fixAll();
385
        return this.defaultGeometryAttributeName;
386
    }
387

    
388
    @Override
389
    public FeatureAttributeDescriptor getDefaultGeometryAttribute() {
390
        this.fixAll();
391
        return super.getDefaultGeometryAttribute();
392
    }
393

    
394
    @Override
395
    public int getDefaultTimeAttributeIndex() {
396
        this.fixAll();
397
        return this.defaultTimeAttributeIndex;
398
    }
399

    
400
    @Override
401
    public String getDefaultTimeAttributeName() {
402
        this.fixAll();
403
        return this.defaultTimeAttributeName;
404
    }
405

    
406
    @Override
407
    public void setHasOID(boolean hasOID) {
408
        this.hasOID = hasOID;
409
        this.fixed = false;
410
    }
411

    
412
    @Override
413
    protected Iterator getIterator(Iterator iter) {
414
        return new EditableDelegatedIterator(iter, this);
415
    }
416

    
417
    @Override
418
    public EditableFeatureAttributeDescriptor getEditableAttributeDescriptor(String name) {
419
        return (EditableFeatureAttributeDescriptor) this.getAttributeDescriptor(name);
420
    }
421

    
422
    @Override
423
    public EditableFeatureAttributeDescriptor getEditableAttributeDescriptor(int index) {
424
        return (EditableFeatureAttributeDescriptor) this.getAttributeDescriptor(index);
425
    }
426

    
427
    @Override
428
    public void setCheckFeaturesAtFinishEditing(boolean check) {
429
        this.checkFeaturesAtFinishEditing = check;
430
        this.fixed = false;
431
    }
432

    
433
    @Override
434
    public void setCheckFeaturesAtInsert(boolean check) {
435
        this.checkFeaturesAtInsert = check;
436
        this.fixed = false;
437
    }
438

    
439
    protected class EditableDelegatedIterator extends DelegatedIterator {
440

    
441
        private final DefaultEditableFeatureType fType;
442

    
443
        public EditableDelegatedIterator(Iterator iter, DefaultEditableFeatureType fType) {
444
            super(iter);
445
            this.fType = fType;
446
        }
447

    
448
        @Override
449
        public void remove() {
450
            this.iterator.remove();
451
            this.fType.fixAll();
452
        }
453

    
454
    }
455

    
456
    protected void setAllowAutomaticValues(boolean value) {
457
        this.allowAtomaticValues = value;
458
        this.fixed = false;
459
    }
460

    
461
    @Override
462
    public void copyFrom(FeatureType other) {
463
        super.copyFrom(other);
464
        if (other instanceof EditableFeatureType) {
465
            DefaultEditableFeatureType other2 = (DefaultEditableFeatureType) other;
466
            this.hasStrongChanges = other2.hasStrongChanges();
467
            this.source = (DefaultFeatureType) other2.getSource();
468
        } else {
469
            this.hasStrongChanges = false;
470
            this.source = (DefaultFeatureType) other;
471
        }
472
        this.fixed = false;
473
    }
474

    
475
    public void copyFrom(JsonObject json) {
476
        // TODO: falta por implementar copyFrom(json)
477
        this.fixed = false;
478
    }
479
    
480
    @Override
481
    public void set(String name, String value) {
482
        if (StringUtils.isBlank(name)) {
483
            throw new IllegalArgumentException("Name can't be empty");
484
        }
485
        switch (name.trim().toLowerCase()) {
486
            case "checkfeaturesatfinishediting":
487
                this.setCheckFeaturesAtFinishEditing(DataTypeUtils.toBoolean(value, false));
488
                break;
489
            case "checkfeaturesatinsert":
490
                this.setCheckFeaturesAtInsert(DataTypeUtils.toBoolean(value, false));
491
                break;
492
            case "defaultgeometryattributename":
493
            case "defaultgeometryname":
494
            case "defaultgeometry":
495
                this.setDefaultGeometryAttributeName(DataTypeUtils.toString(value, null));
496
                break;
497
            default:
498
                throw new IllegalArgumentException("Name attribute '" + name + "' not valid.");
499
        }
500
        this.fixed = false;
501
    }
502
    
503
    private static class TheJsonSerializer implements JsonManager.JsonSerializer {
504
        
505
        public TheJsonSerializer() {            
506
        }
507

    
508
        @Override
509
        public Class getObjectClass() {
510
            return DefaultEditableFeatureType.class;
511
        }
512

    
513
        @Override
514
        public Object toObject(JsonObject json) {
515
            EditableFeatureType o = new DefaultFeatureType().getEditable();
516
            o.fromJson(json);
517
            return o;
518
        }
519

    
520
        @Override
521
        public JsonObjectBuilder toJsonBuilder(Object value) {
522
            return ((SupportToJson)value).toJsonBuilder();
523
        }
524
        
525
    }
526

    
527
    public static void selfRegister() {
528
        Json.registerSerializer(new TheJsonSerializer());
529
    }
530
    
531
    @Override
532
    public void update(Observable observable, Object notification) {
533
        if (notification instanceof Notification && observable instanceof DefaultFeatureAttributeDescriptor){
534
            DefaultFeatureAttributeDescriptor attr = (DefaultFeatureAttributeDescriptor) observable;
535
            Notification n = (Notification) notification;
536
            if(n.isOfType(NOTIFICATION_FIXED_CHANGED)){
537
                if(!attr.isFixed()) {
538
                    this.fixed = false;
539
                }
540
            }
541
        }
542
    }
543

    
544
    
545
}