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

History | View | Annotate | Download (35.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.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.fmap.dal.feature.impl.editing.memory.FeatureTypeManager;
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.persistence.PersistenceManager;
66
import org.gvsig.tools.persistence.Persistent;
67
import org.gvsig.tools.persistence.PersistentState;
68
import org.gvsig.tools.persistence.exception.PersistenceException;
69
import org.gvsig.tools.util.ChainedIterator;
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 copyFrom(DefaultFeatureType other, boolean copyAttributes) {
157
        this.id = other.getId();
158
        if (copyAttributes) {
159
            Iterator iter = other.iterator();
160
            DefaultFeatureAttributeDescriptor attr;
161
            while (iter.hasNext()) {
162
                attr = (DefaultFeatureAttributeDescriptor) iter.next();
163
                DefaultFeatureAttributeDescriptor copy = this.getCopyAttributeDescriptor(attr);
164
                super.add(copy);
165
            }
166
            this.pk = null;
167
        }
168
        this.defaultGeometryAttributeName = other.defaultGeometryAttributeName;
169
        this.defaultTimeAttributeName = other.defaultTimeAttributeName;
170
        this.hasEvaluators = other.hasEvaluators;
171
        this.hasEmulators = other.hasEmulators;
172
        this.rules = (DefaultFeatureRules) other.rules.getCopy();
173
        this.defaultGeometryAttributeIndex = other.defaultGeometryAttributeIndex;
174
        this.defaultTimeAttributeIndex = other.defaultTimeAttributeIndex;
175
        this.hasOID = other.hasOID;
176
        this.id = other.id; // XXX ???? copiar o no esto????
177
        this.internalID = other.internalID;
178
        this.label = other.label;
179
        this.description = other.description;
180
        try {
181
            this.tags = (Tags) other.tags.clone();
182
        } catch (CloneNotSupportedException ex) {
183
            
184
        }
185
    }
186
    
187
    protected DefaultFeatureAttributeDescriptor getCopyAttributeDescriptor(DefaultFeatureAttributeDescriptor src) {
188
        DefaultFeatureAttributeDescriptor copy = new DefaultFeatureAttributeDescriptor(src);
189
        copy.setFeatureType(this);
190
        return copy;
191
    }
192

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
379
    class SubtypeFeatureType extends DefaultFeatureType {
380

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

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

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

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

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

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

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

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

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

    
482
    public class SubtypeFeatureTypeNameException extends DataException {
483

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

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

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

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

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

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

    
526
    protected class DelegatedIterator implements Iterator {
527

    
528
        protected Iterator iterator;
529

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

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

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

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

    
546
    }
547

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

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

    
557
    @Override
558
    public boolean hasPrimaryKey() {
559
        if( pk!=null ) {
560
            return pk.length>0;
561
        }
562
        for (FeatureAttributeDescriptor attr : this) {
563
            if( attr.isPrimaryKey() ) {
564
                return true;
565
            }
566
        }
567
        return false;
568
    }
569

    
570
    @Override
571
    public boolean supportReferences() {
572
        return this.hasOID() || this.hasPrimaryKey();
573
    }
574
    
575
    @Override
576
    public FeatureAttributeDescriptor[] getPrimaryKey() {
577
        if (pk == null) {
578
            List pks = new ArrayList();
579
            Iterator iter = super.iterator();
580
            FeatureAttributeDescriptor attr;
581
            while (iter.hasNext()) {
582
                attr = (FeatureAttributeDescriptor) iter.next();
583
                if (attr.isPrimaryKey()) {
584
                    pks.add(attr);
585
                }
586
            }
587
            if (pks.isEmpty()) {
588
                pk = new FeatureAttributeDescriptor[0];
589
            } else {
590
                pk = (FeatureAttributeDescriptor[]) pks.toArray(new FeatureAttributeDescriptor[pks.size()]);
591
            }
592
        }
593
        return pk;
594
    }
595

    
596
    public FeatureAttributeDescriptor getDefaultGeometryAttribute() {
597
        if (this.defaultGeometryAttributeIndex < 0) {
598
            return null;
599
        }
600
        return (FeatureAttributeDescriptor) super
601
                .get(this.defaultGeometryAttributeIndex);
602
    }
603

    
604
    public boolean equals(Object o) {
605
        if (this == o) {
606
            return true;
607
        }
608
        if (!(o instanceof DefaultFeatureType)) {
609
            return false;
610
        }
611
        DefaultFeatureType other = (DefaultFeatureType) o;
612
        if (!this.id.equals(other.id)) {
613
            return false;
614
        }
615
        if (this.size() != other.size()) {
616
            return false;
617
        }
618
        FeatureAttributeDescriptor attr, attrOther;
619
        Iterator iter, iterOther;
620
        iter = this.iterator();
621
        iterOther = other.iterator();
622
        while (iter.hasNext()) {
623
            attr = (FeatureAttributeDescriptor) iter.next();
624
            attrOther = (FeatureAttributeDescriptor) iterOther.next();
625
            if (!attr.equals(attrOther)) {
626
                return false;
627
            }
628
        }
629

    
630
        if (!StringUtils.equals(defaultGeometryAttributeName, other.defaultGeometryAttributeName)) {
631
            return false;
632
        }
633
        if (!StringUtils.equals(defaultTimeAttributeName, other.defaultTimeAttributeName)) {
634
            return false;
635
        }
636
        return true;
637

    
638
    }
639

    
640
    /**
641
     * Start of DynClass interface implementation READONLY
642
     */
643
    public DynField addDynField(String name) {
644
        throw new UnsupportedOperationException();
645
    }
646

    
647
    public DynField getDeclaredDynField(String name) {
648
        return (DynField) getAttributeDescriptor(name);
649
    }
650

    
651
    public DynField[] getDeclaredDynFields() {
652
        return (DynField[]) getAttributeDescriptors();
653
    }
654

    
655
    public String getDescription() {
656
        return this.description;
657
    }
658

    
659
    public DynField getDynField(String name) {
660
        return (DynField) getAttributeDescriptor(name);
661
    }
662

    
663
    public DynField[] getDynFields() {
664
        return (DynField[]) getAttributeDescriptors();
665
    }
666

    
667
    public String getName() {
668
        return this.id + "_" + internalID;
669
    }
670

    
671
    public void removeDynField(String name) {
672
        throw new UnsupportedOperationException();
673

    
674
    }
675

    
676
    public void addDynMethod(DynMethod dynMethod) {
677
        throw new UnsupportedOperationException();
678

    
679
    }
680

    
681
    public void extend(DynClass dynClass) {
682
        throw new UnsupportedOperationException();
683

    
684
    }
685

    
686
    public void extend(String dynClassName) {
687
        throw new UnsupportedOperationException();
688

    
689
    }
690

    
691
    public void extend(String namespace, String dynClassName) {
692
        throw new UnsupportedOperationException();
693

    
694
    }
695

    
696
    public DynMethod getDeclaredDynMethod(String name)
697
            throws DynMethodException {
698
        return null;
699
    }
700

    
701
    public DynMethod[] getDeclaredDynMethods() throws DynMethodException {
702
        return null;
703
    }
704

    
705
    public DynMethod getDynMethod(String name) throws DynMethodException {
706
        return null;
707
    }
708

    
709
    public DynMethod getDynMethod(int code) throws DynMethodException {
710
        return null;
711
    }
712

    
713
    public DynMethod[] getDynMethods() throws DynMethodException {
714
        return null;
715
    }
716

    
717
    public DynClass[] getSuperDynClasses() {
718
        return null;
719
    }
720

    
721
    public boolean isInstance(DynObject dynObject) {
722
        if (dynObject.getDynClass().getName() == getName()) {
723
            return true;
724
        }
725
        return false;
726
    }
727

    
728
    public DynObject newInstance() {
729

    
730
        throw new UnsupportedOperationException();
731
    }
732

    
733
    public void removeDynMethod(String name) {
734
        throw new UnsupportedOperationException();
735

    
736
    }
737

    
738
    public DynField addDynFieldChoice(String name, int type,
739
            Object defaultValue, DynObjectValueItem[] values,
740
            boolean mandatory, boolean persistent) {
741
        throw new UnsupportedOperationException();
742
    }
743

    
744
    public DynField addDynFieldRange(String name, int type,
745
            Object defaultValue, Object min, Object max, boolean mandatory,
746
            boolean persistent) {
747
        throw new UnsupportedOperationException();
748
    }
749

    
750
    public DynField addDynFieldSingle(String name, int type,
751
            Object defaultValue, boolean mandatory, boolean persistent) {
752
        throw new UnsupportedOperationException();
753
    }
754

    
755
    public void validate(DynObject object) throws DynObjectValidateException {
756
        //FIXME: not sure it's the correct code
757
        if (object instanceof Feature) {
758
            Feature fea = (Feature) object;
759
            if (fea.getType().equals(this)) {
760
                return;
761
            }
762
        }
763
        throw new DynObjectValidateException(this.id);
764
    }
765

    
766
    public DynField addDynFieldLong(String name) {
767
        throw new UnsupportedOperationException();
768
    }
769

    
770
    public DynField addDynFieldChoice(String name, int type,
771
            Object defaultValue, DynObjectValueItem[] values) {
772
        throw new UnsupportedOperationException();
773
    }
774

    
775
    public DynField addDynFieldRange(String name, int type,
776
            Object defaultValue, Object min, Object max) {
777
        throw new UnsupportedOperationException();
778
    }
779

    
780
    public DynField addDynFieldSingle(String name, int type, Object defaultValue) {
781
        throw new UnsupportedOperationException();
782
    }
783

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

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

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

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

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

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

    
808
    public DynField addDynFieldMap(String name) {
809
        throw new UnsupportedOperationException();
810
    }
811

    
812
    public DynField addDynFieldObject(String name) {
813
        throw new UnsupportedOperationException();
814
    }
815

    
816
    public DynField addDynFieldSet(String name) {
817
        throw new UnsupportedOperationException();
818
    }
819

    
820
    public DynField addDynFieldArray(String name) {
821
        throw new UnsupportedOperationException();
822
    }
823

    
824
    public DynField addDynFieldDate(String name) {
825
        throw new UnsupportedOperationException();
826
    }
827

    
828
    public void extend(DynStruct struct) {
829
        throw new UnsupportedOperationException();
830
    }
831

    
832
    public String getFullName() {
833
        // TODO: usar el DynClassName
834
        return this.id;
835
    }
836

    
837
    public String getNamespace() {
838
        return "DALFeature";
839
    }
840

    
841
    public DynStruct[] getSuperDynStructs() {
842
        return null;
843
    }
844

    
845
    public void setDescription(String description) {
846
        this.description = description;
847
    }
848

    
849
    public void setNamespace(String namespace) {
850
        throw new UnsupportedOperationException();
851
    }
852

    
853
    public DynField addDynFieldFile(String name) {
854
        throw new UnsupportedOperationException();
855
    }
856

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

    
861
    public DynField addDynFieldURL(String name) {
862
        throw new UnsupportedOperationException();
863
    }
864

    
865
    public DynField addDynFieldURI(String name) {
866
        throw new UnsupportedOperationException();
867
    }
868

    
869
    public boolean isExtendable(DynStruct dynStruct) {
870
        return false;
871
    }
872

    
873
    public void extend(DynStruct[] structs) {
874
        // TODO Auto-generated method stub
875

    
876
    }
877

    
878
    public void remove(DynStruct superDynStruct) {
879
        // TODO Auto-generated method stub
880

    
881
    }
882

    
883
    public void removeAll(DynStruct[] superDynStruct) {
884
        // TODO Auto-generated method stub
885

    
886
    }
887

    
888
    public FeatureAttributeDescriptor getDefaultTimeAttribute() {
889
        if (this.defaultTimeAttributeIndex < 0) {
890
            return null;
891
        }
892
        return (FeatureAttributeDescriptor) super
893
                .get(this.defaultTimeAttributeIndex);
894
    }
895

    
896
    public void setDefaultTimeAttributeName(String name) {
897
        if (name == null || name.length() == 0) {
898
            this.defaultTimeAttributeIndex = -1;
899
            return;
900
        }
901
        DefaultFeatureAttributeDescriptor attr = (DefaultFeatureAttributeDescriptor) this.get(name);
902
        if (attr == null) {
903
            throw new IllegalArgumentException("Attribute '" + name + "' not found.");
904
        }
905
        if (attr.getIndex() < 0) {
906
            fixAll();
907
        }
908
        this.defaultTimeAttributeIndex = attr.getIndex();
909
    }
910

    
911
    protected void fixAll() {
912
        int i = 0;
913
        Iterator iter = super.iterator();
914
        DefaultFeatureAttributeDescriptor attr;
915

    
916
        while (iter.hasNext()) {
917
            attr = (DefaultFeatureAttributeDescriptor) iter.next();
918
            if (attr.getOder() < 1) {
919
                attr.setOrder(i * 10);
920
            }
921
            attr.setIndex(i++);
922
            attr.fixAll();
923
            if (attr.getEvaluator() != null) {
924
                this.hasEvaluators = true;
925
            }
926
            if (attr.getFeatureAttributeEmulator() != null) {
927
                this.hasEmulators = true;
928
                String[] x = attr.getFeatureAttributeEmulator().getRequiredFieldNames();
929
                if (!ArrayUtils.isEmpty(x)) {
930
                    this.requiredFields = true;
931
                }
932
            }
933
            switch (attr.getType()) {
934
                case DataTypes.GEOMETRY:
935
                    if (this.defaultGeometryAttributeName == null) {
936
                        this.defaultGeometryAttributeName = attr.getName();
937
                    }
938
                    break;
939
                case DataTypes.INSTANT:
940
                case DataTypes.INTERVAL:
941
                case DataTypes.DATE:
942
                    if (this.defaultTimeAttributeName == null && attr.isTime()) {
943
                        this.defaultTimeAttributeName = attr.getName();
944
                    }
945
                    break;
946
            }
947
        }
948
        if (this.defaultGeometryAttributeName != null) {
949
            this.defaultGeometryAttributeIndex = this.getIndex(this.defaultGeometryAttributeName);
950
        }
951
        if (this.defaultTimeAttributeName != null) {
952
            this.defaultTimeAttributeIndex = this.getIndex(this.defaultTimeAttributeName);
953
        }
954
        this.internalID = Long.toHexString(this.getCRC());
955

    
956
    }
957

    
958
    protected long getCRC() {
959
        StringBuffer buffer = new StringBuffer();
960
        for (int i = 0; i < this.size(); i++) {
961
            FeatureAttributeDescriptor x = this.getAttributeDescriptor(i);
962
            buffer.append(x.getName());
963
            buffer.append(x.getDataTypeName());
964
            buffer.append(x.getSize());
965
        }
966
        CRC32 crc = new CRC32();
967
        byte[] data = buffer.toString().getBytes();
968
        crc.update(data);
969
        return crc.getValue();
970
    }
971

    
972
    @Override
973
    public FeatureStore getStore() {
974
        if (this.storeRef == null) {
975
            return null;
976
        }
977
        return (FeatureStore) this.storeRef.get();
978
    }
979

    
980
    public void setStore(FeatureStore store) {
981
        if (store == null) {
982
            this.storeRef = null;
983
        } else {
984
            this.storeRef = new WeakReference(store);
985
        }
986
    }
987

    
988
    @Override
989
    public List<FeatureAttributeDescriptor> getFilteredAttributes(
990
            Predicate<FeatureAttributeDescriptor> filter,
991
            int max
992
    ) {
993
        List<FeatureAttributeDescriptor> attrs = new ArrayList<>();
994
        for (FeatureAttributeDescriptor attribute : this) {
995
            if (filter.test(attribute)) {
996
                attrs.add(attribute);
997
            }
998
        }
999
        return attrs;
1000
    }
1001
    
1002
    @Override
1003
    public List<FeatureAttributeDescriptor> getRecentUseds() {
1004
        return RECENTS_USEDS.getAttributes(this);
1005
    }
1006
    
1007
    @Override
1008
    public void loadFromState(PersistentState state)
1009
            throws PersistenceException {
1010

    
1011
//        FIXME: rules
1012
        hasEvaluators = state.getBoolean("hasEvaluators");
1013
        hasEmulators = state.getBoolean("hasEmulators");
1014
        defaultGeometryAttributeName = state.getString("defaultGeometryAttributeName");
1015
        defaultTimeAttributeName = state.getString("defaultTimeAttributeName");
1016
        defaultGeometryAttributeIndex = state.getInt("defaultGeometryAttributeIndex");
1017
        defaultTimeAttributeIndex = state.getInt("defaultTimeAttributeIndex");
1018
        id = state.getString("id");
1019
        hasOID = state.getBoolean("hasOID");
1020
        allowAtomaticValues = state.getBoolean("allowAtomaticValues");
1021

    
1022
        requiredFields = state.getBoolean("requiredFields");
1023
        internalID = state.getString("internalID");
1024
        tags = (Tags) state.get("tags");
1025
        if( tags == null ) {
1026
            this.tags = new DefaultTags();
1027
        }
1028

    
1029
        List<FeatureAttributeDescriptor> elements = state.getList("elements");
1030
        for (FeatureAttributeDescriptor element : elements) {
1031
            ((DefaultFeatureAttributeDescriptor) element).setFeatureType(this);
1032
            super.add(element);
1033
        }
1034
        this.pk = null;
1035
        this.fixAll();
1036
    }
1037

    
1038
    @Override
1039
    public void saveToState(PersistentState state) throws PersistenceException {
1040

    
1041
//        FIXME: rules
1042
        state.set("hasEvaluators", hasEvaluators);
1043
        state.set("hasEmulators", hasEmulators);
1044
        state.set("defaultGeometryAttributeName", defaultGeometryAttributeName);
1045
        state.set("defaultTimeAttributeName", defaultTimeAttributeName);
1046
        state.set("defaultGeometryAttributeIndex", defaultGeometryAttributeIndex);
1047
        state.set("defaultTimeAttributeIndex", defaultTimeAttributeIndex);
1048
        state.set("id", id);
1049
        state.set("hasOID", hasOID);
1050
        state.set("allowAtomaticValues", allowAtomaticValues);
1051

    
1052
        state.set("requiredFields", requiredFields);
1053
        state.set("internalID", internalID);
1054

    
1055
        List<FeatureAttributeDescriptor> elements = new ArrayList<>();
1056
        elements.addAll(this);
1057
        state.set("elements", elements);
1058
        state.set("tags", tags);
1059

    
1060
    }
1061

    
1062
    private static final String FEATTYPE_PERSISTENCE_DEFINITION_NAME = "FeatureType";
1063

    
1064
    public static void registerPersistenceDefinition() {
1065
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
1066

    
1067
        if (manager.getDefinition(FEATTYPE_PERSISTENCE_DEFINITION_NAME)
1068
                == null) {
1069
            DynStruct definition = manager.addDefinition(DefaultFeatureType.class,
1070
                    FEATTYPE_PERSISTENCE_DEFINITION_NAME,
1071
                    FEATTYPE_PERSISTENCE_DEFINITION_NAME
1072
                    + " persistent definition",
1073
                    null,
1074
                    null
1075
            );
1076
//            definition.addDynFieldObject("rules");
1077
            definition.addDynFieldBoolean("hasEvaluators");
1078
            definition.addDynFieldBoolean("hasEmulators");
1079
            definition.addDynFieldString("defaultGeometryAttributeName");
1080
            definition.addDynFieldString("defaultTimeAttributeName");
1081
            definition.addDynFieldInt("defaultGeometryAttributeIndex");
1082
            definition.addDynFieldInt("defaultTimeAttributeIndex");
1083
            definition.addDynFieldString("id");
1084
            definition.addDynFieldBoolean("hasOID");
1085
            definition.addDynFieldBoolean("allowAtomaticValues");
1086

    
1087
            definition.addDynFieldBoolean("requiredFields");
1088
            definition.addDynFieldString("internalID");
1089

    
1090
            definition.addDynFieldList("elements")
1091
                    .setClassOfItems(FeatureAttributeDescriptor.class);
1092

    
1093
            definition.addDynFieldObject("tags")
1094
                    .setClassOfValue(Tags.class);
1095
            
1096
        }
1097
    }
1098

    
1099
    @Override
1100
    public FeatureStore getAsFeatureStore() {
1101
        FeatureStore store = FeatureTypeToStoreProviderAdapter.createFeatureStore(this);
1102
        return store;
1103
    }
1104

    
1105
}