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

History | View | Annotate | Download (35.1 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
    @Override
557
    public boolean hasPrimaryKey() {
558
        if( pk!=null ) {
559
            return pk.length>0;
560
        }
561
        for (FeatureAttributeDescriptor attr : this) {
562
            if( attr.isPrimaryKey() ) {
563
                return true;
564
            }
565
        }
566
        return false;
567
    }
568

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

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

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

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

    
637
    }
638

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

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

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

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

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

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

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

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

    
673
    }
674

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

    
678
    }
679

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

    
683
    }
684

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

    
688
    }
689

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

    
693
    }
694

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

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

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

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

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

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

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

    
727
    public DynObject newInstance() {
728

    
729
        throw new UnsupportedOperationException();
730
    }
731

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

    
735
    }
736

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
875
    }
876

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

    
880
    }
881

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

    
885
    }
886

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

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

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

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

    
955
    }
956

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

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

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

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

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

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

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

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

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

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

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

    
1059
    }
1060

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

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

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

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

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

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

    
1098
}