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 / DefaultFeatureType.java @ 44583

History | View | Annotate | Download (38.4 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.lang.ref.WeakReference;
27
import java.util.ArrayList;
28
import java.util.Arrays;
29
import java.util.Collections;
30
import java.util.Date;
31
import java.util.HashSet;
32
import java.util.Iterator;
33
import java.util.LinkedHashSet;
34
import java.util.List;
35
import java.util.Set;
36
import java.util.function.Predicate;
37
import java.util.zip.CRC32;
38
import org.apache.commons.collections4.ListUtils;
39
import org.apache.commons.lang3.ArrayUtils;
40
import org.apache.commons.lang3.StringUtils;
41

    
42
import org.cresques.cts.IProjection;
43

    
44
import org.gvsig.fmap.dal.DataTypes;
45
import org.gvsig.fmap.dal.exception.DataException;
46
import org.gvsig.fmap.dal.feature.EditableFeatureType;
47
import org.gvsig.fmap.dal.feature.Feature;
48
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
49
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator;
50
import org.gvsig.fmap.dal.feature.FeatureRules;
51
import org.gvsig.fmap.dal.feature.FeatureStore;
52
import org.gvsig.fmap.dal.feature.FeatureType;
53
import org.gvsig.tools.ToolsLocator;
54
import org.gvsig.tools.dynobject.DynClass;
55
import org.gvsig.tools.dynobject.DynField;
56
import org.gvsig.tools.dynobject.DynMethod;
57
import org.gvsig.tools.dynobject.DynObject;
58
import org.gvsig.tools.dynobject.DynObjectValueItem;
59
import org.gvsig.tools.dynobject.DynStruct;
60
import org.gvsig.tools.dynobject.DynStruct_v2;
61
import org.gvsig.tools.dynobject.Tags;
62
import org.gvsig.tools.dynobject.exception.DynMethodException;
63
import org.gvsig.tools.dynobject.exception.DynObjectValidateException;
64
import org.gvsig.tools.dynobject.impl.DefaultTags;
65
import org.gvsig.tools.i18n.I18nManager;
66
import org.gvsig.tools.persistence.PersistenceManager;
67
import org.gvsig.tools.persistence.Persistent;
68
import org.gvsig.tools.persistence.PersistentState;
69
import org.gvsig.tools.persistence.exception.PersistenceException;
70

    
71
public class DefaultFeatureType
72
        extends ArrayList<FeatureAttributeDescriptor>
73
        implements
74
        FeatureType,
75
        Persistent,
76
        DynClass,
77
        DynStruct_v2,
78
        org.gvsig.tools.lang.Cloneable {
79

    
80
    /**
81
     *
82
     */
83
    private static final long serialVersionUID = -7988721447349282215L;
84

    
85
    public static final RecentUsedsAttributesImpl RECENTS_USEDS = new RecentUsedsAttributesImpl();
86

    
87
    private DefaultFeatureRules rules;
88
    protected boolean hasEvaluators;
89
    protected boolean hasEmulators;
90
    protected String defaultGeometryAttributeName;
91
    protected String defaultTimeAttributeName;
92
    protected int defaultGeometryAttributeIndex;
93
    protected int defaultTimeAttributeIndex;
94
    private String id;
95
    protected boolean hasOID;
96
    protected boolean allowAtomaticValues;
97
    protected FeatureAttributeDescriptor[] pk = null;
98
    protected String internalID = null;
99

    
100
    private List srsList = null;
101
    private WeakReference storeRef;
102
    private boolean requiredFields;
103

    
104
    private String description;
105
    private String label;
106
    private Tags tags;
107

    
108
    public DefaultFeatureType() {
109
        // Usado en la persistencia.
110
        this.internalID = Integer.toHexString((int) (Math.random() * 100000)).toUpperCase();
111
        this.id = id = "default";
112
        this.rules = new DefaultFeatureRules();
113
        this.hasEvaluators = false;
114
        this.hasEmulators = false;
115
        this.defaultGeometryAttributeName = null;
116
        this.defaultTimeAttributeName = null;
117
        this.defaultGeometryAttributeIndex = -1;
118
        this.defaultTimeAttributeIndex = -1;
119
        this.allowAtomaticValues = false;
120
        this.tags = new DefaultTags();
121
    }
122

    
123
    protected DefaultFeatureType(FeatureStore store, String id) {
124
        this();
125
        if (StringUtils.isEmpty(id)) {
126
            id = "default";
127
        }
128
        this.id = id;
129
        setStore(store);
130
    }
131

    
132
    protected DefaultFeatureType(FeatureStore store) {
133
        this(store, (String) null);
134
    }
135

    
136
    protected DefaultFeatureType(DefaultFeatureType other) {
137
        this(other.getStore(), (String) null);
138
        copyFrom(other, true);
139
    }
140

    
141
    protected DefaultFeatureType(DefaultFeatureType other,
142
            boolean copyAttributes) {
143
        this(other.getStore(), (String) null);
144
        this.copyFrom(other, copyAttributes);
145
    }
146

    
147
    @Override
148
    public void copyFrom(FeatureType other) {
149
        String internalIDSaved = this.internalID;
150
        String idSaved = this.id;
151
        copyFrom((DefaultFeatureType) other, true);
152
        this.id = idSaved;
153
        this.internalID = internalIDSaved;
154
    }
155
    
156
    protected void addAll(FeatureType attributes) {
157
        for (FeatureAttributeDescriptor attribute : attributes) {
158
            DefaultFeatureAttributeDescriptor copy = this.getCopyAttributeDescriptor((DefaultFeatureAttributeDescriptor) attribute);
159
            super.add(copy);
160
        }
161
        DefaultFeatureType ft = (DefaultFeatureType) attributes; 
162
        this.pk = null;
163
        this.defaultGeometryAttributeName = ft.defaultGeometryAttributeName;
164
        this.defaultTimeAttributeName = ft.defaultTimeAttributeName;
165
        this.defaultGeometryAttributeIndex = ft.defaultGeometryAttributeIndex;
166
        this.defaultTimeAttributeIndex = ft.defaultTimeAttributeIndex;
167
    }
168
    
169
    protected void copyFrom(DefaultFeatureType other, boolean copyAttributes) {
170
        this.id = other.getId();
171
        if (copyAttributes) {
172
            this.addAll((FeatureType)other);
173
        }
174
        this.defaultGeometryAttributeName = other.defaultGeometryAttributeName;
175
        this.defaultTimeAttributeName = other.defaultTimeAttributeName;
176
        this.defaultGeometryAttributeIndex = other.defaultGeometryAttributeIndex;
177
        this.defaultTimeAttributeIndex = other.defaultTimeAttributeIndex;
178

    
179
        this.hasEvaluators = other.hasEvaluators;
180
        this.hasEmulators = other.hasEmulators;
181
        this.rules = (DefaultFeatureRules) other.rules.getCopy();
182
        this.hasOID = other.hasOID;
183
        this.id = other.id; // XXX ???? copiar o no esto????
184
        this.internalID = other.internalID;
185
        this.label = other.label;
186
        this.description = other.description;
187
        try {
188
            this.tags = (Tags) other.tags.clone();
189
        } catch (CloneNotSupportedException ex) {
190
            
191
        }
192
    }
193
    
194
    protected DefaultFeatureAttributeDescriptor getCopyAttributeDescriptor(DefaultFeatureAttributeDescriptor src) {
195
        DefaultFeatureAttributeDescriptor copy = new DefaultFeatureAttributeDescriptor(src);
196
        copy.setFeatureType(this);
197
        return copy;
198
    }
199

    
200
    @Override
201
    public String getId() {
202
        return this.id;
203
    }
204

    
205
    @Override
206
    public Object get(String name) {
207
        FeatureAttributeDescriptor attr;
208
        Iterator iter = this.iterator();
209
        while (iter.hasNext()) {
210
            attr = (FeatureAttributeDescriptor) iter.next();
211
            if (attr.getName().equalsIgnoreCase(name)) {
212
                return attr;
213
            }
214
        }
215
        return null;
216
    }
217

    
218
    @Override
219
    public FeatureAttributeDescriptor getAttributeDescriptor(String name) {
220
        FeatureAttributeDescriptor attr;
221
        Iterator iter = this.iterator();
222
        while (iter.hasNext()) {
223
            attr = (FeatureAttributeDescriptor) iter.next();
224
            if (attr.getName().equalsIgnoreCase(name)) {
225
                return attr;
226
            }
227
        }
228
        return null;
229
    }
230

    
231
    @Override
232
    public FeatureAttributeDescriptor getAttributeDescriptor(int index) {
233
        return (FeatureAttributeDescriptor) super.get(index);
234
    }
235

    
236
    @Override
237
    public FeatureType getCopy() {
238
        return new DefaultFeatureType(this);
239
    }
240

    
241
    @Override
242
    public Object clone() {
243
        return this.getCopy();
244
    }
245

    
246
    @Override
247
    public int getDefaultGeometryAttributeIndex() {
248
        return this.defaultGeometryAttributeIndex;
249
    }
250

    
251
    @Override
252
    public String getDefaultGeometryAttributeName() {
253
        return this.defaultGeometryAttributeName;
254
    }
255

    
256
    @Override
257
    public int getDefaultTimeAttributeIndex() {
258
        return this.defaultTimeAttributeIndex;
259
    }
260

    
261
    @Override
262
    public String getDefaultTimeAttributeName() {
263
        return this.defaultTimeAttributeName;
264
    }
265

    
266
    @Override
267
    public EditableFeatureType getEditable() {
268
        return new DefaultEditableFeatureType(this);
269
    }
270

    
271
    @Override
272
    public int getIndex(String name) {
273
        FeatureAttributeDescriptor attr;
274
        Iterator iter = this.iterator();
275
        while (iter.hasNext()) {
276
            attr = (FeatureAttributeDescriptor) iter.next();
277
            if (attr.getName().equalsIgnoreCase(name)) {
278
                return attr.getIndex();
279
            }
280
        }
281
        return -1;
282
    }
283

    
284
    @Override
285
    public FeatureRules getRules() {
286
        return this.rules;
287
    }
288

    
289
    @Override
290
    public boolean hasEvaluators() {
291
        return this.hasEvaluators;
292
    }
293

    
294
    public boolean hasEmulators() {
295
        return this.hasEmulators;
296
    }
297

    
298
    public boolean hasRequiredFields() {
299
        return this.requiredFields;
300
    }
301

    
302
    @Override
303
    public List getSRSs() {
304
        if (this.srsList == null) {
305
            ArrayList tmp = new ArrayList();
306
            Iterator iter = iterator();
307
            Iterator tmpIter;
308
            boolean allreadyHave;
309
            IProjection tmpSRS;
310
            FeatureAttributeDescriptor attr;
311
            while (iter.hasNext()) {
312
                attr = (FeatureAttributeDescriptor) iter.next();
313
                if (attr.getDataType().getType() == DataTypes.GEOMETRY
314
                        && attr.getSRS() != null) {
315
                    allreadyHave = false;
316
                    tmpIter = tmp.iterator();
317
                    while (tmpIter.hasNext()) {
318
                        tmpSRS = (IProjection) tmpIter.next();
319
                        if (tmpSRS.getAbrev().equals(attr.getSRS().getAbrev())) {
320
                            allreadyHave = true;
321
                            break;
322
                        }
323
                    }
324
                    if (!allreadyHave) {
325
                        tmp.add(attr.getSRS());
326
                    }
327
                }
328
            }
329
            this.srsList = Collections.unmodifiableList(tmp);
330
        }
331
        return this.srsList;
332
    }
333

    
334
    @Override
335
    public IProjection getDefaultSRS() {
336
        if (this.getDefaultGeometryAttributeIndex() < 0) {
337
            return null;
338
        }
339
        return this.getAttributeDescriptor(
340
                this.getDefaultGeometryAttributeIndex()).getSRS();
341
    }
342

    
343
    public void validateFeature(Feature feature, int mode) throws DataException {
344
        DefaultFeatureRules rules = (DefaultFeatureRules) this.getRules();
345
        rules.validate(feature, mode);
346
    }
347

    
348
    public FeatureType getSubtype() throws DataException {
349
        return new SubtypeFeatureType(this, null, null, true);
350
    }
351

    
352
    public FeatureType getSubtype(String[] names) throws DataException {
353
        return this.getSubtype(names, null, true);
354
    }
355

    
356
    public FeatureType getSubtype(String[] names, String[] constantsNames) throws DataException {
357
        return this.getSubtype(names, constantsNames, true);
358
    }
359

    
360
    public FeatureType getSubtype(String[] names, String[] constantsNames, boolean includePk) throws DataException {
361
        if (ArrayUtils.isEmpty(names) && ArrayUtils.isEmpty(constantsNames)) {
362
            return (FeatureType) this.clone();
363
        }
364
        return new SubtypeFeatureType(this, names, constantsNames,includePk);
365
    }
366

    
367
    public boolean isSubtypeOf(FeatureType featureType) {
368
        return false;
369
    }
370

    
371
    @Override
372
    public List<FeatureAttributeDescriptor> toList() {
373
        return Collections.unmodifiableList(this);
374
    }
375

    
376
    @Override
377
    public Tags getTags() {
378
        return this.tags;
379
    }
380

    
381
    @Override
382
    public String getLabel() {
383
        return this.label;
384
    }
385

    
386
    @Override
387
    public void setLabel(String label) {
388
        this.label = label;
389
    }
390

    
391
    @Override
392
    public DynField addDynField(String name, int type) {
393
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
394
    }
395

    
396
    class SubtypeFeatureType extends DefaultFeatureType {
397

    
398
        /**
399
         *
400
         */
401
        private static final long serialVersionUID = 6913732960073922540L;
402
        WeakReference parent;
403

    
404
        SubtypeFeatureType(DefaultFeatureType parent, String[] names, String[] constantsNames, boolean includePk)
405
                throws DataException {
406
            super(parent, false);
407
            DefaultFeatureAttributeDescriptor attrcopy;
408
            Set<String> attrnames = new LinkedHashSet<>();
409
            Set<String> requiredAttrnames = new HashSet<>();
410

    
411
            if (ArrayUtils.isEmpty(names)) {
412
                for (FeatureAttributeDescriptor attrdesc : parent) {
413
                    attrnames.add(attrdesc.getName().toLowerCase());
414
                }
415
            } else { 
416
                attrnames.addAll(Arrays.asList(names));
417
                requiredAttrnames.addAll(Arrays.asList(names));
418
            }
419
            // Add required fields for emulated fields
420
            if (parent.hasEmulators) {
421
                // Ojo, este bucle falla cuando hay un campo calculado que depende
422
                // de otro campo calculado.
423
                for (FeatureAttributeDescriptor attrdesc : parent) {
424
                    FeatureAttributeEmulator emulator = attrdesc.getFeatureAttributeEmulator();
425
                    if (emulator != null && attrnames.contains(attrdesc.getName().toLowerCase())) {
426
                        String theNames[] = emulator.getRequiredFieldNames();
427
                        if (names != null) {
428
                            for (String name : theNames) {
429
                                name = name.toLowerCase();
430
                                attrnames.add(name);
431
                                requiredAttrnames.add(name);
432
                            }
433
                        }
434
                    }
435
                }
436
            }
437
            // Add missing pk fiels
438
            if (includePk && !parent.hasOID()) {
439
                for (FeatureAttributeDescriptor attrdesc : parent) {
440
                    if (attrdesc.isPrimaryKey()) {
441
                        String name = attrdesc.getName().toLowerCase();
442
                        attrnames.add(name);
443
                        requiredAttrnames.add(name);
444
                    }
445
                }
446
            }
447

    
448
            // Copy attributes
449
            int i = 0;
450
            for (String name : attrnames) {
451
                DefaultFeatureAttributeDescriptor attr = (DefaultFeatureAttributeDescriptor) parent.get(name);
452
                if (attr == null) {
453
                    throw new SubtypeFeatureTypeNameException(name, parent.getId());
454
                }
455
                attrcopy = new DefaultFeatureAttributeDescriptor(attr);
456
                this.add(attrcopy);
457
                attrcopy.index = i++;
458
            }
459

    
460
            // Set the constants attributes.
461
            if (!ArrayUtils.isEmpty(constantsNames)) {
462
                for (String name : constantsNames) {
463
                    if (!requiredAttrnames.contains(name.toLowerCase())) {
464
                        DefaultFeatureAttributeDescriptor attr = (DefaultFeatureAttributeDescriptor) this.get(name);
465
                        attr.setConstantValue(true);
466
                    }
467
                }
468
            }
469

    
470
            this.defaultGeometryAttributeIndex = this.getIndex(this.defaultGeometryAttributeName);
471
            if (this.defaultGeometryAttributeIndex < 0) {
472
                this.defaultGeometryAttributeName = null;
473
            }
474
            this.defaultTimeAttributeIndex = this.getIndex(this.defaultTimeAttributeName);
475
            if (this.defaultTimeAttributeIndex < 0) {
476
                this.defaultTimeAttributeName = null;
477
            }
478
            this.parent = new WeakReference(parent);
479
        }
480

    
481
        public FeatureType getSubtype(String[] names, boolean includePk) throws DataException {
482
            return new SubtypeFeatureType((DefaultFeatureType) this.parent
483
                    .get(), names, null, includePk);
484
        }
485

    
486
        public boolean isSubtypeOf(FeatureType featureType) {
487
            if (featureType == null) {
488
                return false;
489
            }
490
            FeatureType parent = (FeatureType) this.parent.get();
491
            return featureType.equals(parent);
492
        }
493

    
494
        public EditableFeatureType getEditable() {
495
            throw new UnsupportedOperationException();
496
        }
497
    }
498

    
499
    public class SubtypeFeatureTypeNameException extends DataException {
500

    
501
        /**
502
         *
503
         */
504
        private static final long serialVersionUID = -4414242486723260101L;
505
        private final static String MESSAGE_FORMAT = "Attribute name '%(name)' not found in type (%(type)).";
506
        private final static String MESSAGE_KEY = "_SubtypeFeatureTypeNameException";
507

    
508
        public SubtypeFeatureTypeNameException(String name, String type) {
509
            super(MESSAGE_FORMAT, MESSAGE_KEY, serialVersionUID);
510
            setValue("name", name);
511
            setValue("type", type);
512
        }
513
    }
514

    
515
    public boolean hasOID() {
516
        return hasOID;
517
    }
518

    
519
    @Override
520
    public String toString() {
521
        StringBuffer s = new StringBuffer();
522
        s.append(this.getId());
523
        s.append(":[");
524
        String attName;
525
        for (int i = 0; i < size(); i++) {
526
            attName = ((FeatureAttributeDescriptor) get(i)).getName().toString();
527
            s.append(attName);
528
            if (i < size() - 1) {
529
                s.append(',');
530
            }
531
        }
532
        s.append(']');
533
        return s.toString();
534
    }
535

    
536
    @Override
537
    public Iterator<FeatureAttributeDescriptor> iterator() {
538
        return getIterator(super.iterator());
539
    }
540

    
541
    protected Iterator getIterator(Iterator iter) {
542
        return new DelegatedIterator(iter);
543
    }
544

    
545
    protected class DelegatedIterator implements Iterator {
546

    
547
        protected Iterator iterator;
548

    
549
        public DelegatedIterator(Iterator iter) {
550
            this.iterator = iter;
551
        }
552

    
553
        @Override
554
        public boolean hasNext() {
555
            return iterator.hasNext();
556
        }
557

    
558
        @Override
559
        public Object next() {
560
            return iterator.next();
561
        }
562

    
563
        @Override
564
        public void remove() {
565
            throw new UnsupportedOperationException();
566
        }
567

    
568
    }
569

    
570
    @Override
571
    public boolean allowAutomaticValues() {
572
        return this.allowAtomaticValues;
573
    }
574

    
575
    @Override
576
    public FeatureAttributeDescriptor[] getAttributeDescriptors() {
577
        return (FeatureAttributeDescriptor[]) super
578
                .toArray(new FeatureAttributeDescriptor[super.size()]);
579
    }
580

    
581
    @Override
582
    public boolean hasPrimaryKey() {
583
        if( pk!=null ) {
584
            return pk.length>0;
585
        }
586
        for (FeatureAttributeDescriptor attr : this) {
587
            if( attr.isPrimaryKey() ) {
588
                return true;
589
            }
590
        }
591
        return false;
592
    }
593

    
594
    @Override
595
    public boolean supportReferences() {
596
        return this.hasOID() || this.hasPrimaryKey();
597
    }
598
    
599
    @Override
600
    public FeatureAttributeDescriptor[] getPrimaryKey() {
601
        if (pk == null) {
602
            List pks = new ArrayList();
603
            Iterator iter = super.iterator();
604
            FeatureAttributeDescriptor attr;
605
            while (iter.hasNext()) {
606
                attr = (FeatureAttributeDescriptor) iter.next();
607
                if (attr.isPrimaryKey()) {
608
                    pks.add(attr);
609
                }
610
            }
611
            if (pks.isEmpty()) {
612
                pk = new FeatureAttributeDescriptor[0];
613
            } else {
614
                pk = (FeatureAttributeDescriptor[]) pks.toArray(new FeatureAttributeDescriptor[pks.size()]);
615
            }
616
        }
617
        return pk;
618
    }
619

    
620
    public FeatureAttributeDescriptor getDefaultGeometryAttribute() {
621
        if (this.defaultGeometryAttributeIndex < 0) {
622
            return null;
623
        }
624
        return (FeatureAttributeDescriptor) super
625
                .get(this.defaultGeometryAttributeIndex);
626
    }
627

    
628
    @Override
629
    public boolean equals(Object o) {
630
        if (this == o) {
631
            return true;
632
        }
633
        if (!(o instanceof DefaultFeatureType)) {
634
            return false;
635
        }
636
        DefaultFeatureType other = (DefaultFeatureType) o;
637
        if (!this.id.equals(other.id)) {
638
            return false;
639
        }
640
        if (this.size() != other.size()) {
641
            return false;
642
        }
643
        FeatureAttributeDescriptor attr, attrOther;
644
        Iterator iter, iterOther;
645
        iter = this.iterator();
646
        iterOther = other.iterator();
647
        while (iter.hasNext()) {
648
            attr = (FeatureAttributeDescriptor) iter.next();
649
            attrOther = (FeatureAttributeDescriptor) iterOther.next();
650
            if (!attr.equals(attrOther)) {
651
                return false;
652
            }
653
        }
654

    
655
        if (!StringUtils.equals(defaultGeometryAttributeName, other.defaultGeometryAttributeName)) {
656
            return false;
657
        }
658
        if (!StringUtils.equals(defaultTimeAttributeName, other.defaultTimeAttributeName)) {
659
            return false;
660
        }
661
        return true;
662

    
663
    }
664

    
665
    /**
666
     * Start of DynClass interface implementation READONLY
667
     */
668
    @Override
669
    public DynField addDynField(String name) {
670
        throw new UnsupportedOperationException();
671
    }
672

    
673
    @Override
674
    public DynField getDeclaredDynField(String name) {
675
        return (DynField) getAttributeDescriptor(name);
676
    }
677

    
678
    @Override
679
    public DynField[] getDeclaredDynFields() {
680
        return (DynField[]) getAttributeDescriptors();
681
    }
682

    
683
    @Override
684
    public String getDescription() {
685
        return this.description;
686
    }
687

    
688
    @Override
689
    public DynField getDynField(String name) {
690
        return (DynField) getAttributeDescriptor(name);
691
    }
692

    
693
    @Override
694
    public DynField[] getDynFields() {
695
        return (DynField[]) getAttributeDescriptors();
696
    }
697

    
698
    @Override
699
    public String getName() {
700
        return this.id + "_" + internalID;
701
    }
702

    
703
    @Override
704
    public void removeDynField(String name) {
705
        throw new UnsupportedOperationException();
706

    
707
    }
708

    
709
    @Override
710
    public void addDynMethod(DynMethod dynMethod) {
711
        throw new UnsupportedOperationException();
712

    
713
    }
714

    
715
    public void extend(DynClass dynClass) {
716
        throw new UnsupportedOperationException();
717

    
718
    }
719

    
720
    @Override
721
    public void extend(String dynClassName) {
722
        throw new UnsupportedOperationException();
723

    
724
    }
725

    
726
    @Override
727
    public void extend(String namespace, String dynClassName) {
728
        throw new UnsupportedOperationException();
729

    
730
    }
731

    
732
    @Override
733
    public DynMethod getDeclaredDynMethod(String name)
734
            throws DynMethodException {
735
        return null;
736
    }
737

    
738
    @Override
739
    public DynMethod[] getDeclaredDynMethods() throws DynMethodException {
740
        return null;
741
    }
742

    
743
    @Override
744
    public DynMethod getDynMethod(String name) throws DynMethodException {
745
        return null;
746
    }
747

    
748
    @Override
749
    public DynMethod getDynMethod(int code) throws DynMethodException {
750
        return null;
751
    }
752

    
753
    @Override
754
    public DynMethod[] getDynMethods() throws DynMethodException {
755
        return null;
756
    }
757

    
758
    @Override
759
    public DynClass[] getSuperDynClasses() {
760
        return null;
761
    }
762

    
763
    @Override
764
    public boolean isInstance(DynObject dynObject) {
765
        if (dynObject.getDynClass().getName() == getName()) {
766
            return true;
767
        }
768
        return false;
769
    }
770

    
771
    @Override
772
    public DynObject newInstance() {
773

    
774
        throw new UnsupportedOperationException();
775
    }
776

    
777
    @Override
778
    public void removeDynMethod(String name) {
779
        throw new UnsupportedOperationException();
780

    
781
    }
782

    
783
    @Override
784
    public DynField addDynFieldChoice(String name, int type,
785
            Object defaultValue, DynObjectValueItem[] values,
786
            boolean mandatory, boolean persistent) {
787
        throw new UnsupportedOperationException();
788
    }
789

    
790
    @Override
791
    public DynField addDynFieldRange(String name, int type,
792
            Object defaultValue, Object min, Object max, boolean mandatory,
793
            boolean persistent) {
794
        throw new UnsupportedOperationException();
795
    }
796

    
797
    @Override
798
    public DynField addDynFieldSingle(String name, int type,
799
            Object defaultValue, boolean mandatory, boolean persistent) {
800
        throw new UnsupportedOperationException();
801
    }
802

    
803
    @Override
804
    public void validate(DynObject object) throws DynObjectValidateException {
805
        //FIXME: not sure it's the correct code
806
        if (object instanceof Feature) {
807
            Feature fea = (Feature) object;
808
            if (fea.getType().equals(this)) {
809
                return;
810
            }
811
        }
812
        throw new DynObjectValidateException(this.id);
813
    }
814

    
815
    @Override
816
    public DynField addDynFieldLong(String name) {
817
        throw new UnsupportedOperationException();
818
    }
819

    
820
    @Override
821
    public DynField addDynFieldChoice(String name, int type,
822
            Object defaultValue, DynObjectValueItem[] values) {
823
        throw new UnsupportedOperationException();
824
    }
825

    
826
    @Override
827
    public DynField addDynFieldRange(String name, int type,
828
            Object defaultValue, Object min, Object max) {
829
        throw new UnsupportedOperationException();
830
    }
831

    
832
    @Override
833
    public DynField addDynFieldSingle(String name, int type, Object defaultValue) {
834
        throw new UnsupportedOperationException();
835
    }
836

    
837
    @Override
838
    public DynField addDynFieldString(String name) {
839
        throw new UnsupportedOperationException();
840
    }
841

    
842
    @Override
843
    public DynField addDynFieldInt(String name) {
844
        throw new UnsupportedOperationException();
845
    }
846

    
847
    @Override
848
    public DynField addDynFieldDouble(String name) {
849
        throw new UnsupportedOperationException();
850
    }
851

    
852
    @Override
853
    public DynField addDynFieldFloat(String name) {
854
        throw new UnsupportedOperationException();
855
    }
856

    
857
    public DynField addDynFieldBoolean(String name) {
858
        throw new UnsupportedOperationException();
859
    }
860

    
861
    @Override
862
    public DynField addDynFieldList(String name) {
863
        throw new UnsupportedOperationException();
864
    }
865

    
866
    @Override
867
    public DynField addDynFieldMap(String name) {
868
        throw new UnsupportedOperationException();
869
    }
870

    
871
    @Override
872
    public DynField addDynFieldObject(String name) {
873
        throw new UnsupportedOperationException();
874
    }
875

    
876
    @Override
877
    public DynField addDynFieldSet(String name) {
878
        throw new UnsupportedOperationException();
879
    }
880

    
881
    @Override
882
    public DynField addDynFieldArray(String name) {
883
        throw new UnsupportedOperationException();
884
    }
885

    
886
    @Override
887
    public DynField addDynFieldDate(String name) {
888
        throw new UnsupportedOperationException();
889
    }
890

    
891
    @Override
892
    public void extend(DynStruct struct) {
893
        throw new UnsupportedOperationException();
894
    }
895

    
896
    @Override
897
    public String getFullName() {
898
        // TODO: usar el DynClassName
899
        return this.id;
900
    }
901

    
902
    @Override
903
    public String getNamespace() {
904
        return "DALFeature";
905
    }
906

    
907
    @Override
908
    public DynStruct[] getSuperDynStructs() {
909
        return null;
910
    }
911

    
912
    @Override
913
    public void setDescription(String description) {
914
        this.description = description;
915
    }
916

    
917
    @Override
918
    public void setNamespace(String namespace) {
919
        throw new UnsupportedOperationException();
920
    }
921

    
922
    @Override
923
    public DynField addDynFieldFile(String name) {
924
        throw new UnsupportedOperationException();
925
    }
926

    
927
    @Override
928
    public DynField addDynFieldFolder(String name) {
929
        throw new UnsupportedOperationException();
930
    }
931

    
932
    @Override
933
    public DynField addDynFieldURL(String name) {
934
        throw new UnsupportedOperationException();
935
    }
936

    
937
    @Override
938
    public DynField addDynFieldURI(String name) {
939
        throw new UnsupportedOperationException();
940
    }
941

    
942
    @Override
943
    public boolean isExtendable(DynStruct dynStruct) {
944
        return false;
945
    }
946

    
947
    public void extend(DynStruct[] structs) {
948
        // TODO Auto-generated method stub
949

    
950
    }
951

    
952
    @Override
953
    public void remove(DynStruct superDynStruct) {
954
        // TODO Auto-generated method stub
955

    
956
    }
957

    
958
    public void removeAll(DynStruct[] superDynStruct) {
959
        // TODO Auto-generated method stub
960

    
961
    }
962

    
963
    @Override
964
    public FeatureAttributeDescriptor getDefaultTimeAttribute() {
965
        if (this.defaultTimeAttributeIndex < 0) {
966
            return null;
967
        }
968
        return (FeatureAttributeDescriptor) super
969
                .get(this.defaultTimeAttributeIndex);
970
    }
971

    
972
    public void setDefaultTimeAttributeName(String name) {
973
        if (name == null || name.length() == 0) {
974
            this.defaultTimeAttributeIndex = -1;
975
            return;
976
        }
977
        DefaultFeatureAttributeDescriptor attr = (DefaultFeatureAttributeDescriptor) this.get(name);
978
        if (attr == null) {
979
            throw new IllegalArgumentException("Attribute '" + name + "' not found.");
980
        }
981
        if (attr.getIndex() < 0) {
982
            fixAll();
983
        }
984
        this.defaultTimeAttributeIndex = attr.getIndex();
985
    }
986

    
987
    protected void fixAll() {
988
        int i = 0;
989
        Iterator iter = super.iterator();
990
        DefaultFeatureAttributeDescriptor attr;
991

    
992
        while (iter.hasNext()) {
993
            attr = (DefaultFeatureAttributeDescriptor) iter.next();
994
            if (attr.getOder() < 1) {
995
                attr.setOrder(i * 10);
996
            }
997
            attr.setIndex(i++);
998
            attr.fixAll();
999
            if (attr.getEvaluator() != null) {
1000
                this.hasEvaluators = true;
1001
            }
1002
            if (attr.getFeatureAttributeEmulator() != null) {
1003
                this.hasEmulators = true;
1004
                String[] x = attr.getFeatureAttributeEmulator().getRequiredFieldNames();
1005
                if (!ArrayUtils.isEmpty(x)) {
1006
                    this.requiredFields = true;
1007
                }
1008
            }
1009
            switch (attr.getType()) {
1010
                case DataTypes.GEOMETRY:
1011
                    if (this.defaultGeometryAttributeName == null) {
1012
                        this.defaultGeometryAttributeName = attr.getName();
1013
                    }
1014
                    break;
1015
                case DataTypes.INSTANT:
1016
                case DataTypes.INTERVAL:
1017
                case DataTypes.DATE:
1018
                    if (this.defaultTimeAttributeName == null && attr.isTime()) {
1019
                        this.defaultTimeAttributeName = attr.getName();
1020
                    }
1021
                    break;
1022
            }
1023
        }
1024
        if (this.defaultGeometryAttributeName != null) {
1025
            this.defaultGeometryAttributeIndex = this.getIndex(this.defaultGeometryAttributeName);
1026
        }
1027
        if (this.defaultTimeAttributeName != null) {
1028
            this.defaultTimeAttributeIndex = this.getIndex(this.defaultTimeAttributeName);
1029
        }
1030
        this.internalID = Long.toHexString(this.getCRC());
1031

    
1032
    }
1033

    
1034
    protected long getCRC() {
1035
        StringBuffer buffer = new StringBuffer();
1036
        for (int i = 0; i < this.size(); i++) {
1037
            FeatureAttributeDescriptor x = this.getAttributeDescriptor(i);
1038
            buffer.append(x.getName());
1039
            buffer.append(x.getDataTypeName());
1040
            buffer.append(x.getSize());
1041
        }
1042
        CRC32 crc = new CRC32();
1043
        byte[] data = buffer.toString().getBytes();
1044
        crc.update(data);
1045
        return crc.getValue();
1046
    }
1047

    
1048
    @Override
1049
    public FeatureStore getStore() {
1050
        if (this.storeRef == null) {
1051
            return null;
1052
        }
1053
        return (FeatureStore) this.storeRef.get();
1054
    }
1055

    
1056
    public void setStore(FeatureStore store) {
1057
        if (store == null) {
1058
            this.storeRef = null;
1059
        } else {
1060
            this.storeRef = new WeakReference(store);
1061
        }
1062
    }
1063

    
1064
    @Override
1065
    public List<FeatureAttributeDescriptor> getFilteredAttributes(
1066
            Predicate<FeatureAttributeDescriptor> filter,
1067
            int max
1068
    ) {
1069
        List<FeatureAttributeDescriptor> attrs = new ArrayList<>();
1070
        for (FeatureAttributeDescriptor attribute : this) {
1071
            if (filter.test(attribute)) {
1072
                attrs.add(attribute);
1073
            }
1074
        }
1075
        return attrs;
1076
    }
1077
    
1078
    @Override
1079
    public List<FeatureAttributeDescriptor> getRecentUseds() {
1080
        return RECENTS_USEDS.getAttributes(this);
1081
    }
1082
    
1083
    @Override
1084
    public void loadFromState(PersistentState state)
1085
            throws PersistenceException {
1086

    
1087
//        FIXME: rules
1088
        hasEvaluators = state.getBoolean("hasEvaluators");
1089
        hasEmulators = state.getBoolean("hasEmulators");
1090
        defaultGeometryAttributeName = state.getString("defaultGeometryAttributeName");
1091
        defaultTimeAttributeName = state.getString("defaultTimeAttributeName");
1092
        defaultGeometryAttributeIndex = state.getInt("defaultGeometryAttributeIndex");
1093
        defaultTimeAttributeIndex = state.getInt("defaultTimeAttributeIndex");
1094
        id = state.getString("id");
1095
        hasOID = state.getBoolean("hasOID");
1096
        allowAtomaticValues = state.getBoolean("allowAtomaticValues");
1097

    
1098
        requiredFields = state.getBoolean("requiredFields");
1099
        internalID = state.getString("internalID");
1100
        tags = (Tags) state.get("tags");
1101
        if( tags == null ) {
1102
            this.tags = new DefaultTags();
1103
        }
1104

    
1105
        List<FeatureAttributeDescriptor> elements = state.getList("elements");
1106
        for (FeatureAttributeDescriptor element : elements) {
1107
            ((DefaultFeatureAttributeDescriptor) element).setFeatureType(this);
1108
            super.add(element);
1109
        }
1110
        this.pk = null;
1111
        this.fixAll();
1112
    }
1113

    
1114
    @Override
1115
    public void saveToState(PersistentState state) throws PersistenceException {
1116

    
1117
//        FIXME: rules
1118
        state.set("hasEvaluators", hasEvaluators);
1119
        state.set("hasEmulators", hasEmulators);
1120
        state.set("defaultGeometryAttributeName", defaultGeometryAttributeName);
1121
        state.set("defaultTimeAttributeName", defaultTimeAttributeName);
1122
        state.set("defaultGeometryAttributeIndex", defaultGeometryAttributeIndex);
1123
        state.set("defaultTimeAttributeIndex", defaultTimeAttributeIndex);
1124
        state.set("id", id);
1125
        state.set("hasOID", hasOID);
1126
        state.set("allowAtomaticValues", allowAtomaticValues);
1127

    
1128
        state.set("requiredFields", requiredFields);
1129
        state.set("internalID", internalID);
1130

    
1131
        List<FeatureAttributeDescriptor> elements = new ArrayList<>();
1132
        elements.addAll(this);
1133
        state.set("elements", elements);
1134
        state.set("tags", tags);
1135

    
1136
    }
1137

    
1138
    private static final String FEATTYPE_PERSISTENCE_DEFINITION_NAME = "FeatureType";
1139

    
1140
    public static void registerPersistenceDefinition() {
1141
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
1142

    
1143
        if (manager.getDefinition(FEATTYPE_PERSISTENCE_DEFINITION_NAME)
1144
                == null) {
1145
            DynStruct definition = manager.addDefinition(DefaultFeatureType.class,
1146
                    FEATTYPE_PERSISTENCE_DEFINITION_NAME,
1147
                    FEATTYPE_PERSISTENCE_DEFINITION_NAME
1148
                    + " persistent definition",
1149
                    null,
1150
                    null
1151
            );
1152
//            definition.addDynFieldObject("rules");
1153
            definition.addDynFieldBoolean("hasEvaluators");
1154
            definition.addDynFieldBoolean("hasEmulators");
1155
            definition.addDynFieldString("defaultGeometryAttributeName");
1156
            definition.addDynFieldString("defaultTimeAttributeName");
1157
            definition.addDynFieldInt("defaultGeometryAttributeIndex");
1158
            definition.addDynFieldInt("defaultTimeAttributeIndex");
1159
            definition.addDynFieldString("id");
1160
            definition.addDynFieldBoolean("hasOID");
1161
            definition.addDynFieldBoolean("allowAtomaticValues");
1162

    
1163
            definition.addDynFieldBoolean("requiredFields");
1164
            definition.addDynFieldString("internalID");
1165

    
1166
            definition.addDynFieldList("elements")
1167
                    .setClassOfItems(FeatureAttributeDescriptor.class);
1168

    
1169
            definition.addDynFieldObject("tags")
1170
                    .setClassOfValue(Tags.class);
1171
            
1172
        }
1173
    }
1174

    
1175
    @Override
1176
    public FeatureStore getAsFeatureStore() {
1177
        FeatureStore store = FeatureTypeToStoreProviderAdapter.createFeatureStore(this);
1178
        return store;
1179
    }
1180
    
1181
                
1182
    @Override
1183
    public String getNewFieldName() {
1184
        I18nManager i18n = ToolsLocator.getI18nManager();
1185
        String prefix = i18n.getTranslation("_Field");
1186
        String fieldName;
1187
        for (int i = 1; i < 1000; i++) {
1188
            fieldName = prefix+i;
1189
            if( this.get(fieldName)==null ) {
1190
                return fieldName;
1191
            }
1192
        }
1193
        fieldName = prefix + (new Date()).getTime();
1194
        return fieldName;
1195
    }
1196
    
1197
    @Override
1198
   public FeatureType getOriginalFeatureType()  {
1199
        DefaultFeatureStore store = (DefaultFeatureStore) this.getStore();
1200
        if (store==null) {
1201
            return null;
1202
        }
1203
        return store.getOriginalFeatureType(this);
1204
    }
1205
    @Override
1206
    public boolean hasOnlyMetadataChanges(FeatureType old) {
1207
        if( old == null ) {
1208
            throw new NullPointerException();
1209
        }
1210
        // Si hay campos nuevos -> false
1211
        for (FeatureAttributeDescriptor attr : this) {
1212
            if(!attr.isComputed() && old.getAttributeDescriptor(attr.getName())==null) {
1213
                return false;
1214
            }
1215
        }
1216
        
1217
        // Si se ha eliminado algun campo -> false
1218
        for (FeatureAttributeDescriptor attr : old) {
1219
            if(!attr.isComputed() && this.getAttributeDescriptor(attr.getName())==null ) {
1220
                return false;
1221
            }
1222
        }
1223
        
1224
        // No hay campos nuevos ni se ha eliminado ninguno, asi que comparamos
1225
        // los campos uno a uno.
1226
        for (FeatureAttributeDescriptor attr : this) {
1227
            FeatureAttributeDescriptor attrold = old.getAttributeDescriptor(attr.getName());
1228
            if(!attr.isComputed() && !attr.hasOnlyMetadataChanges(attrold) ) {
1229
                return false;
1230
            }
1231
        }
1232
        return true;
1233
    }
1234
}