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

History | View | Annotate | Download (34.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
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.Comparator;
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.lang3.ArrayUtils;
39
import org.apache.commons.lang3.StringUtils;
40

    
41
import org.cresques.cts.IProjection;
42

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

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

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

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

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

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

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

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

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

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

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

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

    
146
    @Override
147
    public void copyFrom(FeatureType other) {
148
        String internalIDSaved = this.internalID;
149
        String idSaved = this.id;
150
        copyFrom((DefaultFeatureType) other, true);
151
        this.id = idSaved;
152
        this.internalID = internalIDSaved;
153
    }
154
    
155
    protected void copyFrom(DefaultFeatureType other, boolean copyAttributes) {
156
        this.id = other.getId();
157
        if (copyAttributes) {
158
            Iterator iter = other.iterator();
159
            DefaultFeatureAttributeDescriptor attr;
160
            while (iter.hasNext()) {
161
                attr = (DefaultFeatureAttributeDescriptor) iter.next();
162
                DefaultFeatureAttributeDescriptor copy = this.getCopyAttributeDescriptor(attr);
163
                super.add(copy);
164
            }
165
            this.pk = null;
166
        }
167
        this.defaultGeometryAttributeName = other.defaultGeometryAttributeName;
168
        this.defaultTimeAttributeName = other.defaultTimeAttributeName;
169
        this.hasEvaluators = other.hasEvaluators;
170
        this.hasEmulators = other.hasEmulators;
171
        this.rules = (DefaultFeatureRules) other.rules.getCopy();
172
        this.defaultGeometryAttributeIndex = other.defaultGeometryAttributeIndex;
173
        this.defaultTimeAttributeIndex = other.defaultTimeAttributeIndex;
174
        this.hasOID = other.hasOID;
175
        this.id = other.id; // XXX ???? copiar o no esto????
176
        this.internalID = other.internalID;
177
        this.label = other.label;
178
        this.description = other.description;
179
        try {
180
            this.tags = (Tags) other.tags.clone();
181
        } catch (CloneNotSupportedException ex) {
182
            
183
        }
184
    }
185
    
186
    protected DefaultFeatureAttributeDescriptor getCopyAttributeDescriptor(DefaultFeatureAttributeDescriptor src) {
187
        DefaultFeatureAttributeDescriptor copy = new DefaultFeatureAttributeDescriptor(src);
188
        copy.setFeatureType(this);
189
        return copy;
190
    }
191

    
192
    @Override
193
    public String getId() {
194
        return this.id;
195
    }
196

    
197
    @Override
198
    public Object get(String name) {
199
        FeatureAttributeDescriptor attr;
200
        Iterator iter = this.iterator();
201
        while (iter.hasNext()) {
202
            attr = (FeatureAttributeDescriptor) iter.next();
203
            if (attr.getName().equalsIgnoreCase(name)) {
204
                return attr;
205
            }
206
        }
207
        return null;
208
    }
209

    
210
    @Override
211
    public FeatureAttributeDescriptor getAttributeDescriptor(String name) {
212
        FeatureAttributeDescriptor attr;
213
        Iterator iter = this.iterator();
214
        while (iter.hasNext()) {
215
            attr = (FeatureAttributeDescriptor) iter.next();
216
            if (attr.getName().equalsIgnoreCase(name)) {
217
                return attr;
218
            }
219
        }
220
        return null;
221
    }
222

    
223
    @Override
224
    public FeatureAttributeDescriptor getAttributeDescriptor(int index) {
225
        return (FeatureAttributeDescriptor) super.get(index);
226
    }
227

    
228
    @Override
229
    public FeatureType getCopy() {
230
        return new DefaultFeatureType(this);
231
    }
232

    
233
    @Override
234
    public Object clone() {
235
        return this.getCopy();
236
    }
237

    
238
    public int getDefaultGeometryAttributeIndex() {
239
        return this.defaultGeometryAttributeIndex;
240
    }
241

    
242
    public String getDefaultGeometryAttributeName() {
243
        return this.defaultGeometryAttributeName;
244
    }
245

    
246
    public int getDefaultTimeAttributeIndex() {
247
        return this.defaultTimeAttributeIndex;
248
    }
249

    
250
    public String getDefaultTimeAttributeName() {
251
        return this.defaultTimeAttributeName;
252
    }
253

    
254
    public EditableFeatureType getEditable() {
255
        return new DefaultEditableFeatureType(this);
256
    }
257

    
258
    public int getIndex(String name) {
259
        FeatureAttributeDescriptor attr;
260
        Iterator iter = this.iterator();
261
        while (iter.hasNext()) {
262
            attr = (FeatureAttributeDescriptor) iter.next();
263
            if (attr.getName().equalsIgnoreCase(name)) {
264
                return attr.getIndex();
265
            }
266
        }
267
        return -1;
268
    }
269

    
270
    public FeatureRules getRules() {
271
        return this.rules;
272
    }
273

    
274
    public boolean hasEvaluators() {
275
        return this.hasEvaluators;
276
    }
277

    
278
    public boolean hasEmulators() {
279
        return this.hasEmulators;
280
    }
281

    
282
    public boolean hasRequiredFields() {
283
        return this.requiredFields;
284
    }
285

    
286
    public List getSRSs() {
287
        if (this.srsList == null) {
288
            ArrayList tmp = new ArrayList();
289
            Iterator iter = iterator();
290
            Iterator tmpIter;
291
            boolean allreadyHave;
292
            IProjection tmpSRS;
293
            FeatureAttributeDescriptor attr;
294
            while (iter.hasNext()) {
295
                attr = (FeatureAttributeDescriptor) iter.next();
296
                if (attr.getDataType().getType() == DataTypes.GEOMETRY
297
                        && attr.getSRS() != null) {
298
                    allreadyHave = false;
299
                    tmpIter = tmp.iterator();
300
                    while (tmpIter.hasNext()) {
301
                        tmpSRS = (IProjection) tmpIter.next();
302
                        if (tmpSRS.getAbrev().equals(attr.getSRS().getAbrev())) {
303
                            allreadyHave = true;
304
                            break;
305
                        }
306
                    }
307
                    if (!allreadyHave) {
308
                        tmp.add(attr.getSRS());
309
                    }
310
                }
311
            }
312
            this.srsList = Collections.unmodifiableList(tmp);
313
        }
314
        return this.srsList;
315
    }
316

    
317
    public IProjection getDefaultSRS() {
318
        if (this.getDefaultGeometryAttributeIndex() < 0) {
319
            return null;
320
        }
321
        return this.getAttributeDescriptor(
322
                this.getDefaultGeometryAttributeIndex()).getSRS();
323
    }
324

    
325
    public void validateFeature(Feature feature, int mode) throws DataException {
326
        DefaultFeatureRules rules = (DefaultFeatureRules) this.getRules();
327
        rules.validate(feature, mode);
328
    }
329

    
330
    public FeatureType getSubtype() throws DataException {
331
        return new SubtypeFeatureType(this, null, null, true);
332
    }
333

    
334
    public FeatureType getSubtype(String[] names) throws DataException {
335
        return this.getSubtype(names, null, true);
336
    }
337

    
338
    public FeatureType getSubtype(String[] names, String[] constantsNames) throws DataException {
339
        return this.getSubtype(names, constantsNames, true);
340
    }
341

    
342
    public FeatureType getSubtype(String[] names, String[] constantsNames, boolean includePk) throws DataException {
343
        if (ArrayUtils.isEmpty(names) && ArrayUtils.isEmpty(constantsNames)) {
344
            return (FeatureType) this.clone();
345
        }
346
        return new SubtypeFeatureType(this, names, constantsNames,includePk);
347
    }
348

    
349
    public boolean isSubtypeOf(FeatureType featureType) {
350
        return false;
351
    }
352

    
353
    @Override
354
    public List<FeatureAttributeDescriptor> toList() {
355
        return Collections.unmodifiableList(this);
356
    }
357

    
358
    @Override
359
    public Tags getTags() {
360
        return this.tags;
361
    }
362

    
363
    @Override
364
    public String getLabel() {
365
        return this.label;
366
    }
367

    
368
    @Override
369
    public void setLabel(String label) {
370
        this.label = label;
371
    }
372

    
373
    @Override
374
    public DynField addDynField(String name, int type) {
375
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
376
    }
377

    
378
    class SubtypeFeatureType extends DefaultFeatureType {
379

    
380
        /**
381
         *
382
         */
383
        private static final long serialVersionUID = 6913732960073922540L;
384
        WeakReference parent;
385

    
386
        SubtypeFeatureType(DefaultFeatureType parent, String[] names, String[] constantsNames, boolean includePk)
387
                throws DataException {
388
            super(parent, false);
389
            DefaultFeatureAttributeDescriptor attrcopy;
390
            Set<String> attrnames = new LinkedHashSet<>();
391
            Set<String> requiredAttrnames = new HashSet<>();
392

    
393
            if (ArrayUtils.isEmpty(names)) {
394
                for (FeatureAttributeDescriptor attrdesc : parent) {
395
                    attrnames.add(attrdesc.getName().toLowerCase());
396
                }
397
            } else { 
398
                attrnames.addAll(Arrays.asList(names));
399
                requiredAttrnames.addAll(Arrays.asList(names));
400
            }
401
            // Add required fields for emulated fields
402
            if (parent.hasEmulators) {
403
                // Ojo, este bucle falla cuando hay un campo calculado que depende
404
                // de otro campo calculado.
405
                for (FeatureAttributeDescriptor attrdesc : parent) {
406
                    FeatureAttributeEmulator emulator = attrdesc.getFeatureAttributeEmulator();
407
                    if (emulator != null && attrnames.contains(attrdesc.getName().toLowerCase())) {
408
                        String theNames[] = emulator.getRequiredFieldNames();
409
                        if (names != null) {
410
                            for (String name : theNames) {
411
                                name = name.toLowerCase();
412
                                attrnames.add(name);
413
                                requiredAttrnames.add(name);
414
                            }
415
                        }
416
                    }
417
                }
418
            }
419
            // Add missing pk fiels
420
            if (includePk && !parent.hasOID()) {
421
                for (FeatureAttributeDescriptor attrdesc : parent) {
422
                    if (attrdesc.isPrimaryKey()) {
423
                        String name = attrdesc.getName().toLowerCase();
424
                        attrnames.add(name);
425
                        requiredAttrnames.add(name);
426
                    }
427
                }
428
            }
429

    
430
            // Copy attributes
431
            int i = 0;
432
            for (String name : attrnames) {
433
                DefaultFeatureAttributeDescriptor attr = (DefaultFeatureAttributeDescriptor) parent.get(name);
434
                if (attr == null) {
435
                    throw new SubtypeFeatureTypeNameException(name, parent.getId());
436
                }
437
                attrcopy = new DefaultFeatureAttributeDescriptor(attr);
438
                this.add(attrcopy);
439
                attrcopy.index = i++;
440
            }
441

    
442
            // Set the constants attributes.
443
            if (!ArrayUtils.isEmpty(constantsNames)) {
444
                for (String name : constantsNames) {
445
                    if (!requiredAttrnames.contains(name)) {
446
                        DefaultFeatureAttributeDescriptor attr = (DefaultFeatureAttributeDescriptor) this.get(name);
447
                        attr.setConstantValue(true);
448
                    }
449
                }
450
            }
451

    
452
            this.defaultGeometryAttributeIndex = this.getIndex(this.defaultGeometryAttributeName);
453
            if (this.defaultGeometryAttributeIndex < 0) {
454
                this.defaultGeometryAttributeName = null;
455
            }
456
            this.defaultTimeAttributeIndex = this.getIndex(this.defaultTimeAttributeName);
457
            if (this.defaultTimeAttributeIndex < 0) {
458
                this.defaultTimeAttributeName = null;
459
            }
460
            this.parent = new WeakReference(parent);
461
        }
462

    
463
        public FeatureType getSubtype(String[] names, boolean includePk) throws DataException {
464
            return new SubtypeFeatureType((DefaultFeatureType) this.parent
465
                    .get(), names, null, includePk);
466
        }
467

    
468
        public boolean isSubtypeOf(FeatureType featureType) {
469
            if (featureType == null) {
470
                return false;
471
            }
472
            FeatureType parent = (FeatureType) this.parent.get();
473
            return featureType.equals(parent);
474
        }
475

    
476
        public EditableFeatureType getEditable() {
477
            throw new UnsupportedOperationException();
478
        }
479
    }
480

    
481
    public class SubtypeFeatureTypeNameException extends DataException {
482

    
483
        /**
484
         *
485
         */
486
        private static final long serialVersionUID = -4414242486723260101L;
487
        private final static String MESSAGE_FORMAT = "Attribute name '%(name)' not found in type (%(type)).";
488
        private final static String MESSAGE_KEY = "_SubtypeFeatureTypeNameException";
489

    
490
        public SubtypeFeatureTypeNameException(String name, String type) {
491
            super(MESSAGE_FORMAT, MESSAGE_KEY, serialVersionUID);
492
            setValue("name", name);
493
            setValue("type", type);
494
        }
495
    }
496

    
497
    public boolean hasOID() {
498
        return hasOID;
499
    }
500

    
501
    public String toString() {
502
        StringBuffer s = new StringBuffer();
503
        s.append(this.getId());
504
        s.append(":[");
505
        String attName;
506
        for (int i = 0; i < size(); i++) {
507
            attName = ((FeatureAttributeDescriptor) get(i)).getName().toString();
508
            s.append(attName);
509
            if (i < size() - 1) {
510
                s.append(',');
511
            }
512
        }
513
        s.append(']');
514
        return s.toString();
515
    }
516

    
517
    public Iterator<FeatureAttributeDescriptor> iterator() {
518
        return getIterator(super.iterator());
519
    }
520

    
521
    protected Iterator getIterator(Iterator iter) {
522
        return new DelegatedIterator(iter);
523
    }
524

    
525
    protected class DelegatedIterator implements Iterator {
526

    
527
        protected Iterator iterator;
528

    
529
        public DelegatedIterator(Iterator iter) {
530
            this.iterator = iter;
531
        }
532

    
533
        public boolean hasNext() {
534
            return iterator.hasNext();
535
        }
536

    
537
        public Object next() {
538
            return iterator.next();
539
        }
540

    
541
        public void remove() {
542
            throw new UnsupportedOperationException();
543
        }
544

    
545
    }
546

    
547
    public boolean allowAutomaticValues() {
548
        return this.allowAtomaticValues;
549
    }
550

    
551
    public FeatureAttributeDescriptor[] getAttributeDescriptors() {
552
        return (FeatureAttributeDescriptor[]) super
553
                .toArray(new FeatureAttributeDescriptor[super.size()]);
554
    }
555

    
556
    public FeatureAttributeDescriptor[] getPrimaryKey() {
557
        if (pk == null) {
558
            List pks = new ArrayList();
559
            Iterator iter = super.iterator();
560
            FeatureAttributeDescriptor attr;
561
            while (iter.hasNext()) {
562
                attr = (FeatureAttributeDescriptor) iter.next();
563
                if (attr.isPrimaryKey()) {
564
                    pks.add(attr);
565
                }
566
            }
567
            if (pks.isEmpty()) {
568
                pk = new FeatureAttributeDescriptor[0];
569
            } else {
570
                pk = (FeatureAttributeDescriptor[]) pks.toArray(new FeatureAttributeDescriptor[pks.size()]);
571
            }
572
        }
573
        return pk;
574
    }
575

    
576
    public FeatureAttributeDescriptor getDefaultGeometryAttribute() {
577
        if (this.defaultGeometryAttributeIndex < 0) {
578
            return null;
579
        }
580
        return (FeatureAttributeDescriptor) super
581
                .get(this.defaultGeometryAttributeIndex);
582
    }
583

    
584
    public boolean equals(Object o) {
585
        if (this == o) {
586
            return true;
587
        }
588
        if (!(o instanceof DefaultFeatureType)) {
589
            return false;
590
        }
591
        DefaultFeatureType other = (DefaultFeatureType) o;
592
        if (!this.id.equals(other.id)) {
593
            return false;
594
        }
595
        if (this.size() != other.size()) {
596
            return false;
597
        }
598
        FeatureAttributeDescriptor attr, attrOther;
599
        Iterator iter, iterOther;
600
        iter = this.iterator();
601
        iterOther = other.iterator();
602
        while (iter.hasNext()) {
603
            attr = (FeatureAttributeDescriptor) iter.next();
604
            attrOther = (FeatureAttributeDescriptor) iterOther.next();
605
            if (!attr.equals(attrOther)) {
606
                return false;
607
            }
608
        }
609

    
610
        if (!StringUtils.equals(defaultGeometryAttributeName, other.defaultGeometryAttributeName)) {
611
            return false;
612
        }
613
        if (!StringUtils.equals(defaultTimeAttributeName, other.defaultTimeAttributeName)) {
614
            return false;
615
        }
616
        return true;
617

    
618
    }
619

    
620
    /**
621
     * Start of DynClass interface implementation READONLY
622
     */
623
    public DynField addDynField(String name) {
624
        throw new UnsupportedOperationException();
625
    }
626

    
627
    public DynField getDeclaredDynField(String name) {
628
        return (DynField) getAttributeDescriptor(name);
629
    }
630

    
631
    public DynField[] getDeclaredDynFields() {
632
        return (DynField[]) getAttributeDescriptors();
633
    }
634

    
635
    public String getDescription() {
636
        return this.description;
637
    }
638

    
639
    public DynField getDynField(String name) {
640
        return (DynField) getAttributeDescriptor(name);
641
    }
642

    
643
    public DynField[] getDynFields() {
644
        return (DynField[]) getAttributeDescriptors();
645
    }
646

    
647
    public String getName() {
648
        return this.id + "_" + internalID;
649
    }
650

    
651
    public void removeDynField(String name) {
652
        throw new UnsupportedOperationException();
653

    
654
    }
655

    
656
    public void addDynMethod(DynMethod dynMethod) {
657
        throw new UnsupportedOperationException();
658

    
659
    }
660

    
661
    public void extend(DynClass dynClass) {
662
        throw new UnsupportedOperationException();
663

    
664
    }
665

    
666
    public void extend(String dynClassName) {
667
        throw new UnsupportedOperationException();
668

    
669
    }
670

    
671
    public void extend(String namespace, String dynClassName) {
672
        throw new UnsupportedOperationException();
673

    
674
    }
675

    
676
    public DynMethod getDeclaredDynMethod(String name)
677
            throws DynMethodException {
678
        return null;
679
    }
680

    
681
    public DynMethod[] getDeclaredDynMethods() throws DynMethodException {
682
        return null;
683
    }
684

    
685
    public DynMethod getDynMethod(String name) throws DynMethodException {
686
        return null;
687
    }
688

    
689
    public DynMethod getDynMethod(int code) throws DynMethodException {
690
        return null;
691
    }
692

    
693
    public DynMethod[] getDynMethods() throws DynMethodException {
694
        return null;
695
    }
696

    
697
    public DynClass[] getSuperDynClasses() {
698
        return null;
699
    }
700

    
701
    public boolean isInstance(DynObject dynObject) {
702
        if (dynObject.getDynClass().getName() == getName()) {
703
            return true;
704
        }
705
        return false;
706
    }
707

    
708
    public DynObject newInstance() {
709

    
710
        throw new UnsupportedOperationException();
711
    }
712

    
713
    public void removeDynMethod(String name) {
714
        throw new UnsupportedOperationException();
715

    
716
    }
717

    
718
    public DynField addDynFieldChoice(String name, int type,
719
            Object defaultValue, DynObjectValueItem[] values,
720
            boolean mandatory, boolean persistent) {
721
        throw new UnsupportedOperationException();
722
    }
723

    
724
    public DynField addDynFieldRange(String name, int type,
725
            Object defaultValue, Object min, Object max, boolean mandatory,
726
            boolean persistent) {
727
        throw new UnsupportedOperationException();
728
    }
729

    
730
    public DynField addDynFieldSingle(String name, int type,
731
            Object defaultValue, boolean mandatory, boolean persistent) {
732
        throw new UnsupportedOperationException();
733
    }
734

    
735
    public void validate(DynObject object) throws DynObjectValidateException {
736
        //FIXME: not sure it's the correct code
737
        if (object instanceof Feature) {
738
            Feature fea = (Feature) object;
739
            if (fea.getType().equals(this)) {
740
                return;
741
            }
742
        }
743
        throw new DynObjectValidateException(this.id);
744
    }
745

    
746
    public DynField addDynFieldLong(String name) {
747
        throw new UnsupportedOperationException();
748
    }
749

    
750
    public DynField addDynFieldChoice(String name, int type,
751
            Object defaultValue, DynObjectValueItem[] values) {
752
        throw new UnsupportedOperationException();
753
    }
754

    
755
    public DynField addDynFieldRange(String name, int type,
756
            Object defaultValue, Object min, Object max) {
757
        throw new UnsupportedOperationException();
758
    }
759

    
760
    public DynField addDynFieldSingle(String name, int type, Object defaultValue) {
761
        throw new UnsupportedOperationException();
762
    }
763

    
764
    public DynField addDynFieldString(String name) {
765
        throw new UnsupportedOperationException();
766
    }
767

    
768
    public DynField addDynFieldInt(String name) {
769
        throw new UnsupportedOperationException();
770
    }
771

    
772
    public DynField addDynFieldDouble(String name) {
773
        throw new UnsupportedOperationException();
774
    }
775

    
776
    public DynField addDynFieldFloat(String name) {
777
        throw new UnsupportedOperationException();
778
    }
779

    
780
    public DynField addDynFieldBoolean(String name) {
781
        throw new UnsupportedOperationException();
782
    }
783

    
784
    public DynField addDynFieldList(String name) {
785
        throw new UnsupportedOperationException();
786
    }
787

    
788
    public DynField addDynFieldMap(String name) {
789
        throw new UnsupportedOperationException();
790
    }
791

    
792
    public DynField addDynFieldObject(String name) {
793
        throw new UnsupportedOperationException();
794
    }
795

    
796
    public DynField addDynFieldSet(String name) {
797
        throw new UnsupportedOperationException();
798
    }
799

    
800
    public DynField addDynFieldArray(String name) {
801
        throw new UnsupportedOperationException();
802
    }
803

    
804
    public DynField addDynFieldDate(String name) {
805
        throw new UnsupportedOperationException();
806
    }
807

    
808
    public void extend(DynStruct struct) {
809
        throw new UnsupportedOperationException();
810
    }
811

    
812
    public String getFullName() {
813
        // TODO: usar el DynClassName
814
        return this.id;
815
    }
816

    
817
    public String getNamespace() {
818
        return "DALFeature";
819
    }
820

    
821
    public DynStruct[] getSuperDynStructs() {
822
        return null;
823
    }
824

    
825
    public void setDescription(String description) {
826
        this.description = description;
827
    }
828

    
829
    public void setNamespace(String namespace) {
830
        throw new UnsupportedOperationException();
831
    }
832

    
833
    public DynField addDynFieldFile(String name) {
834
        throw new UnsupportedOperationException();
835
    }
836

    
837
    public DynField addDynFieldFolder(String name) {
838
        throw new UnsupportedOperationException();
839
    }
840

    
841
    public DynField addDynFieldURL(String name) {
842
        throw new UnsupportedOperationException();
843
    }
844

    
845
    public DynField addDynFieldURI(String name) {
846
        throw new UnsupportedOperationException();
847
    }
848

    
849
    public boolean isExtendable(DynStruct dynStruct) {
850
        return false;
851
    }
852

    
853
    public void extend(DynStruct[] structs) {
854
        // TODO Auto-generated method stub
855

    
856
    }
857

    
858
    public void remove(DynStruct superDynStruct) {
859
        // TODO Auto-generated method stub
860

    
861
    }
862

    
863
    public void removeAll(DynStruct[] superDynStruct) {
864
        // TODO Auto-generated method stub
865

    
866
    }
867

    
868
    public FeatureAttributeDescriptor getDefaultTimeAttribute() {
869
        if (this.defaultTimeAttributeIndex < 0) {
870
            return null;
871
        }
872
        return (FeatureAttributeDescriptor) super
873
                .get(this.defaultTimeAttributeIndex);
874
    }
875

    
876
    public void setDefaultTimeAttributeName(String name) {
877
        if (name == null || name.length() == 0) {
878
            this.defaultTimeAttributeIndex = -1;
879
            return;
880
        }
881
        DefaultFeatureAttributeDescriptor attr = (DefaultFeatureAttributeDescriptor) this.get(name);
882
        if (attr == null) {
883
            throw new IllegalArgumentException("Attribute '" + name + "' not found.");
884
        }
885
        if (attr.getIndex() < 0) {
886
            fixAll();
887
        }
888
        this.defaultTimeAttributeIndex = attr.getIndex();
889
    }
890

    
891
    protected void fixAll() {
892
        int i = 0;
893
        Iterator iter = super.iterator();
894
        DefaultFeatureAttributeDescriptor attr;
895

    
896
        while (iter.hasNext()) {
897
            attr = (DefaultFeatureAttributeDescriptor) iter.next();
898
            if (attr.getOder() < 1) {
899
                attr.setOrder(i * 10);
900
            }
901
            attr.setIndex(i++);
902
            attr.fixAll();
903
            if (attr.getEvaluator() != null) {
904
                this.hasEvaluators = true;
905
            }
906
            if (attr.getFeatureAttributeEmulator() != null) {
907
                this.hasEmulators = true;
908
                String[] x = attr.getFeatureAttributeEmulator().getRequiredFieldNames();
909
                if (!ArrayUtils.isEmpty(x)) {
910
                    this.requiredFields = true;
911
                }
912
            }
913
            switch (attr.getType()) {
914
                case DataTypes.GEOMETRY:
915
                    if (this.defaultGeometryAttributeName == null) {
916
                        this.defaultGeometryAttributeName = attr.getName();
917
                    }
918
                    break;
919
                case DataTypes.INSTANT:
920
                case DataTypes.INTERVAL:
921
                case DataTypes.DATE:
922
                    if (this.defaultTimeAttributeName == null && attr.isTime()) {
923
                        this.defaultTimeAttributeName = attr.getName();
924
                    }
925
                    break;
926
            }
927
        }
928
        if (this.defaultGeometryAttributeName != null) {
929
            this.defaultGeometryAttributeIndex = this.getIndex(this.defaultGeometryAttributeName);
930
        }
931
        if (this.defaultTimeAttributeName != null) {
932
            this.defaultTimeAttributeIndex = this.getIndex(this.defaultTimeAttributeName);
933
        }
934
        this.internalID = Long.toHexString(this.getCRC());
935

    
936
    }
937

    
938
    protected long getCRC() {
939
        StringBuffer buffer = new StringBuffer();
940
        for (int i = 0; i < this.size(); i++) {
941
            FeatureAttributeDescriptor x = this.getAttributeDescriptor(i);
942
            buffer.append(x.getName());
943
            buffer.append(x.getDataTypeName());
944
            buffer.append(x.getSize());
945
        }
946
        CRC32 crc = new CRC32();
947
        byte[] data = buffer.toString().getBytes();
948
        crc.update(data);
949
        return crc.getValue();
950
    }
951

    
952
    @Override
953
    public FeatureStore getStore() {
954
        if (this.storeRef == null) {
955
            return null;
956
        }
957
        return (FeatureStore) this.storeRef.get();
958
    }
959

    
960
    public void setStore(FeatureStore store) {
961
        if (store == null) {
962
            this.storeRef = null;
963
        } else {
964
            this.storeRef = new WeakReference(store);
965
        }
966
    }
967

    
968
    @Override
969
    public List<FeatureAttributeDescriptor> getFilteredAttributes(
970
            Predicate<FeatureAttributeDescriptor> filter,
971
            int max
972
    ) {
973
        List<FeatureAttributeDescriptor> attrs = new ArrayList<>();
974
        for (FeatureAttributeDescriptor attribute : this) {
975
            if (filter.test(attribute)) {
976
                attrs.add(attribute);
977
            }
978
        }
979
        return attrs;
980
    }
981
    
982
    @Override
983
    public List<FeatureAttributeDescriptor> getRecentUseds() {
984
        return RECENTS_USEDS.getAttributes(this);
985
    }
986
    
987
    @Override
988
    public void loadFromState(PersistentState state)
989
            throws PersistenceException {
990

    
991
//        FIXME: rules
992
        hasEvaluators = state.getBoolean("hasEvaluators");
993
        hasEmulators = state.getBoolean("hasEmulators");
994
        defaultGeometryAttributeName = state.getString("defaultGeometryAttributeName");
995
        defaultTimeAttributeName = state.getString("defaultTimeAttributeName");
996
        defaultGeometryAttributeIndex = state.getInt("defaultGeometryAttributeIndex");
997
        defaultTimeAttributeIndex = state.getInt("defaultTimeAttributeIndex");
998
        id = state.getString("id");
999
        hasOID = state.getBoolean("hasOID");
1000
        allowAtomaticValues = state.getBoolean("allowAtomaticValues");
1001

    
1002
        requiredFields = state.getBoolean("requiredFields");
1003
        internalID = state.getString("internalID");
1004
        tags = (Tags) state.get("tags");
1005
        if( tags == null ) {
1006
            this.tags = new DefaultTags();
1007
        }
1008

    
1009
        List<FeatureAttributeDescriptor> elements = state.getList("elements");
1010
        for (FeatureAttributeDescriptor element : elements) {
1011
            ((DefaultFeatureAttributeDescriptor) element).setFeatureType(this);
1012
            super.add(element);
1013
        }
1014
        this.pk = null;
1015
        this.fixAll();
1016
    }
1017

    
1018
    @Override
1019
    public void saveToState(PersistentState state) throws PersistenceException {
1020

    
1021
//        FIXME: rules
1022
        state.set("hasEvaluators", hasEvaluators);
1023
        state.set("hasEmulators", hasEmulators);
1024
        state.set("defaultGeometryAttributeName", defaultGeometryAttributeName);
1025
        state.set("defaultTimeAttributeName", defaultTimeAttributeName);
1026
        state.set("defaultGeometryAttributeIndex", defaultGeometryAttributeIndex);
1027
        state.set("defaultTimeAttributeIndex", defaultTimeAttributeIndex);
1028
        state.set("id", id);
1029
        state.set("hasOID", hasOID);
1030
        state.set("allowAtomaticValues", allowAtomaticValues);
1031

    
1032
        state.set("requiredFields", requiredFields);
1033
        state.set("internalID", internalID);
1034

    
1035
        List<FeatureAttributeDescriptor> elements = new ArrayList<>();
1036
        elements.addAll(this);
1037
        state.set("elements", elements);
1038
        state.set("tags", tags);
1039

    
1040
    }
1041

    
1042
    private static final String FEATTYPE_PERSISTENCE_DEFINITION_NAME = "FeatureType";
1043

    
1044
    public static void registerPersistenceDefinition() {
1045
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
1046

    
1047
        if (manager.getDefinition(FEATTYPE_PERSISTENCE_DEFINITION_NAME)
1048
                == null) {
1049
            DynStruct definition = manager.addDefinition(DefaultFeatureType.class,
1050
                    FEATTYPE_PERSISTENCE_DEFINITION_NAME,
1051
                    FEATTYPE_PERSISTENCE_DEFINITION_NAME
1052
                    + " persistent definition",
1053
                    null,
1054
                    null
1055
            );
1056
//            definition.addDynFieldObject("rules");
1057
            definition.addDynFieldBoolean("hasEvaluators");
1058
            definition.addDynFieldBoolean("hasEmulators");
1059
            definition.addDynFieldString("defaultGeometryAttributeName");
1060
            definition.addDynFieldString("defaultTimeAttributeName");
1061
            definition.addDynFieldInt("defaultGeometryAttributeIndex");
1062
            definition.addDynFieldInt("defaultTimeAttributeIndex");
1063
            definition.addDynFieldString("id");
1064
            definition.addDynFieldBoolean("hasOID");
1065
            definition.addDynFieldBoolean("allowAtomaticValues");
1066

    
1067
            definition.addDynFieldBoolean("requiredFields");
1068
            definition.addDynFieldString("internalID");
1069

    
1070
            definition.addDynFieldList("elements")
1071
                    .setClassOfItems(FeatureAttributeDescriptor.class);
1072

    
1073
            definition.addDynFieldObject("tags")
1074
                    .setClassOfValue(Tags.class);
1075
            
1076
        }
1077
    }
1078

    
1079
}