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

History | View | Annotate | Download (38.4 KB)

1 40559 jjdelcerro
/**
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 40435 jjdelcerro
package org.gvsig.fmap.dal.feature.impl;
25
26
import java.lang.ref.WeakReference;
27
import java.util.ArrayList;
28 41342 jjdelcerro
import java.util.Arrays;
29 40435 jjdelcerro
import java.util.Collections;
30 44498 omartinez
import java.util.Date;
31 43981 omartinez
import java.util.HashSet;
32 40435 jjdelcerro
import java.util.Iterator;
33 43998 jjdelcerro
import java.util.LinkedHashSet;
34 40435 jjdelcerro
import java.util.List;
35 43981 omartinez
import java.util.Set;
36 44262 jjdelcerro
import java.util.function.Predicate;
37 43610 jjdelcerro
import java.util.zip.CRC32;
38 44583 omartinez
import org.apache.commons.collections4.ListUtils;
39 43358 jjdelcerro
import org.apache.commons.lang3.ArrayUtils;
40 43377 jjdelcerro
import org.apache.commons.lang3.StringUtils;
41 40435 jjdelcerro
42
import org.cresques.cts.IProjection;
43
44
import org.gvsig.fmap.dal.DataTypes;
45
import org.gvsig.fmap.dal.exception.DataException;
46
import org.gvsig.fmap.dal.feature.EditableFeatureType;
47
import org.gvsig.fmap.dal.feature.Feature;
48
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
49 41342 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator;
50 40435 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureRules;
51 43739 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureStore;
52 40435 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureType;
53 44190 jjdelcerro
import org.gvsig.tools.ToolsLocator;
54 40435 jjdelcerro
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 44253 jjdelcerro
import org.gvsig.tools.dynobject.DynStruct_v2;
61
import org.gvsig.tools.dynobject.Tags;
62 40435 jjdelcerro
import org.gvsig.tools.dynobject.exception.DynMethodException;
63
import org.gvsig.tools.dynobject.exception.DynObjectValidateException;
64 44253 jjdelcerro
import org.gvsig.tools.dynobject.impl.DefaultTags;
65 44498 omartinez
import org.gvsig.tools.i18n.I18nManager;
66 44190 jjdelcerro
import org.gvsig.tools.persistence.PersistenceManager;
67
import org.gvsig.tools.persistence.Persistent;
68
import org.gvsig.tools.persistence.PersistentState;
69
import org.gvsig.tools.persistence.exception.PersistenceException;
70 40435 jjdelcerro
71 44253 jjdelcerro
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 40435 jjdelcerro
80 44253 jjdelcerro
    /**
81
     *
82
     */
83
    private static final long serialVersionUID = -7988721447349282215L;
84 40435 jjdelcerro
85 44262 jjdelcerro
    public static final RecentUsedsAttributesImpl RECENTS_USEDS = new RecentUsedsAttributesImpl();
86
87 44253 jjdelcerro
    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 40435 jjdelcerro
100 44253 jjdelcerro
    private List srsList = null;
101
    private WeakReference storeRef;
102
    private boolean requiredFields;
103 40435 jjdelcerro
104 44253 jjdelcerro
    private String description;
105
    private String label;
106
    private Tags tags;
107 40435 jjdelcerro
108 44253 jjdelcerro
    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 40435 jjdelcerro
123 44253 jjdelcerro
    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 40435 jjdelcerro
132 44253 jjdelcerro
    protected DefaultFeatureType(FeatureStore store) {
133
        this(store, (String) null);
134
    }
135 43555 fdiaz
136 44253 jjdelcerro
    protected DefaultFeatureType(DefaultFeatureType other) {
137
        this(other.getStore(), (String) null);
138 44318 jjdelcerro
        copyFrom(other, true);
139 44253 jjdelcerro
    }
140 40435 jjdelcerro
141 44253 jjdelcerro
    protected DefaultFeatureType(DefaultFeatureType other,
142
            boolean copyAttributes) {
143
        this(other.getStore(), (String) null);
144 44318 jjdelcerro
        this.copyFrom(other, copyAttributes);
145 44253 jjdelcerro
    }
146 40435 jjdelcerro
147 44318 jjdelcerro
    @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 44505 jjdelcerro
    protected void addAll(FeatureType attributes) {
157
        for (FeatureAttributeDescriptor attribute : attributes) {
158
            DefaultFeatureAttributeDescriptor copy = this.getCopyAttributeDescriptor((DefaultFeatureAttributeDescriptor) attribute);
159
            super.add(copy);
160
        }
161
        DefaultFeatureType ft = (DefaultFeatureType) attributes;
162
        this.pk = null;
163
        this.defaultGeometryAttributeName = ft.defaultGeometryAttributeName;
164
        this.defaultTimeAttributeName = ft.defaultTimeAttributeName;
165
        this.defaultGeometryAttributeIndex = ft.defaultGeometryAttributeIndex;
166
        this.defaultTimeAttributeIndex = ft.defaultTimeAttributeIndex;
167
    }
168
169 44318 jjdelcerro
    protected void copyFrom(DefaultFeatureType other, boolean copyAttributes) {
170 44253 jjdelcerro
        this.id = other.getId();
171
        if (copyAttributes) {
172 44505 jjdelcerro
            this.addAll((FeatureType)other);
173 44253 jjdelcerro
        }
174 44506 jjdelcerro
        this.defaultGeometryAttributeName = other.defaultGeometryAttributeName;
175
        this.defaultTimeAttributeName = other.defaultTimeAttributeName;
176
        this.defaultGeometryAttributeIndex = other.defaultGeometryAttributeIndex;
177
        this.defaultTimeAttributeIndex = other.defaultTimeAttributeIndex;
178 44505 jjdelcerro
179 44253 jjdelcerro
        this.hasEvaluators = other.hasEvaluators;
180
        this.hasEmulators = other.hasEmulators;
181
        this.rules = (DefaultFeatureRules) other.rules.getCopy();
182
        this.hasOID = other.hasOID;
183
        this.id = other.id; // XXX ???? copiar o no esto????
184
        this.internalID = other.internalID;
185
        this.label = other.label;
186
        this.description = other.description;
187
        try {
188
            this.tags = (Tags) other.tags.clone();
189
        } catch (CloneNotSupportedException ex) {
190
191
        }
192
    }
193 44318 jjdelcerro
194
    protected DefaultFeatureAttributeDescriptor getCopyAttributeDescriptor(DefaultFeatureAttributeDescriptor src) {
195
        DefaultFeatureAttributeDescriptor copy = new DefaultFeatureAttributeDescriptor(src);
196
        copy.setFeatureType(this);
197
        return copy;
198 44253 jjdelcerro
    }
199 40435 jjdelcerro
200 44318 jjdelcerro
    @Override
201 44253 jjdelcerro
    public String getId() {
202
        return this.id;
203
    }
204 40435 jjdelcerro
205 44318 jjdelcerro
    @Override
206 44253 jjdelcerro
    public Object get(String name) {
207
        FeatureAttributeDescriptor attr;
208
        Iterator iter = this.iterator();
209
        while (iter.hasNext()) {
210
            attr = (FeatureAttributeDescriptor) iter.next();
211
            if (attr.getName().equalsIgnoreCase(name)) {
212
                return attr;
213
            }
214
        }
215
        return null;
216
    }
217 40435 jjdelcerro
218 44318 jjdelcerro
    @Override
219 44253 jjdelcerro
    public FeatureAttributeDescriptor getAttributeDescriptor(String name) {
220
        FeatureAttributeDescriptor attr;
221
        Iterator iter = this.iterator();
222
        while (iter.hasNext()) {
223
            attr = (FeatureAttributeDescriptor) iter.next();
224
            if (attr.getName().equalsIgnoreCase(name)) {
225
                return attr;
226
            }
227 42700 jjdelcerro
        }
228 44253 jjdelcerro
        return null;
229
    }
230 40435 jjdelcerro
231 44318 jjdelcerro
    @Override
232 44253 jjdelcerro
    public FeatureAttributeDescriptor getAttributeDescriptor(int index) {
233
        return (FeatureAttributeDescriptor) super.get(index);
234
    }
235 40435 jjdelcerro
236 44318 jjdelcerro
    @Override
237 44253 jjdelcerro
    public FeatureType getCopy() {
238
        return new DefaultFeatureType(this);
239
    }
240 40435 jjdelcerro
241 44318 jjdelcerro
    @Override
242 44253 jjdelcerro
    public Object clone() {
243
        return this.getCopy();
244
    }
245 44077 jjdelcerro
246 44583 omartinez
    @Override
247 44253 jjdelcerro
    public int getDefaultGeometryAttributeIndex() {
248
        return this.defaultGeometryAttributeIndex;
249
    }
250 44077 jjdelcerro
251 44583 omartinez
    @Override
252 44253 jjdelcerro
    public String getDefaultGeometryAttributeName() {
253
        return this.defaultGeometryAttributeName;
254
    }
255 40435 jjdelcerro
256 44583 omartinez
    @Override
257 44253 jjdelcerro
    public int getDefaultTimeAttributeIndex() {
258
        return this.defaultTimeAttributeIndex;
259
    }
260 40435 jjdelcerro
261 44583 omartinez
    @Override
262 44253 jjdelcerro
    public String getDefaultTimeAttributeName() {
263
        return this.defaultTimeAttributeName;
264
    }
265 40435 jjdelcerro
266 44583 omartinez
    @Override
267 44253 jjdelcerro
    public EditableFeatureType getEditable() {
268
        return new DefaultEditableFeatureType(this);
269
    }
270 40435 jjdelcerro
271 44583 omartinez
    @Override
272 44253 jjdelcerro
    public int getIndex(String name) {
273
        FeatureAttributeDescriptor attr;
274
        Iterator iter = this.iterator();
275
        while (iter.hasNext()) {
276
            attr = (FeatureAttributeDescriptor) iter.next();
277
            if (attr.getName().equalsIgnoreCase(name)) {
278
                return attr.getIndex();
279
            }
280 43998 jjdelcerro
        }
281 44253 jjdelcerro
        return -1;
282
    }
283 41342 jjdelcerro
284 44583 omartinez
    @Override
285 44253 jjdelcerro
    public FeatureRules getRules() {
286
        return this.rules;
287
    }
288 40435 jjdelcerro
289 44583 omartinez
    @Override
290 44253 jjdelcerro
    public boolean hasEvaluators() {
291
        return this.hasEvaluators;
292
    }
293 40435 jjdelcerro
294 44253 jjdelcerro
    public boolean hasEmulators() {
295
        return this.hasEmulators;
296
    }
297 40435 jjdelcerro
298 44253 jjdelcerro
    public boolean hasRequiredFields() {
299
        return this.requiredFields;
300
    }
301
302 44583 omartinez
    @Override
303 44253 jjdelcerro
    public List getSRSs() {
304
        if (this.srsList == null) {
305
            ArrayList tmp = new ArrayList();
306
            Iterator iter = iterator();
307
            Iterator tmpIter;
308
            boolean allreadyHave;
309
            IProjection tmpSRS;
310
            FeatureAttributeDescriptor attr;
311
            while (iter.hasNext()) {
312
                attr = (FeatureAttributeDescriptor) iter.next();
313
                if (attr.getDataType().getType() == DataTypes.GEOMETRY
314
                        && attr.getSRS() != null) {
315
                    allreadyHave = false;
316
                    tmpIter = tmp.iterator();
317
                    while (tmpIter.hasNext()) {
318
                        tmpSRS = (IProjection) tmpIter.next();
319
                        if (tmpSRS.getAbrev().equals(attr.getSRS().getAbrev())) {
320
                            allreadyHave = true;
321
                            break;
322
                        }
323
                    }
324
                    if (!allreadyHave) {
325
                        tmp.add(attr.getSRS());
326
                    }
327 41212 jjdelcerro
                }
328 44253 jjdelcerro
            }
329
            this.srsList = Collections.unmodifiableList(tmp);
330
        }
331
        return this.srsList;
332
    }
333 40435 jjdelcerro
334 44583 omartinez
    @Override
335 44253 jjdelcerro
    public IProjection getDefaultSRS() {
336
        if (this.getDefaultGeometryAttributeIndex() < 0) {
337
            return null;
338
        }
339
        return this.getAttributeDescriptor(
340
                this.getDefaultGeometryAttributeIndex()).getSRS();
341
    }
342
343
    public void validateFeature(Feature feature, int mode) throws DataException {
344
        DefaultFeatureRules rules = (DefaultFeatureRules) this.getRules();
345
        rules.validate(feature, mode);
346
    }
347
348 44374 jjdelcerro
    public FeatureType getSubtype() throws DataException {
349
        return new SubtypeFeatureType(this, null, null, true);
350
    }
351
352 44253 jjdelcerro
    public FeatureType getSubtype(String[] names) throws DataException {
353 44374 jjdelcerro
        return this.getSubtype(names, null, true);
354 44253 jjdelcerro
    }
355 41212 jjdelcerro
356 44253 jjdelcerro
    public FeatureType getSubtype(String[] names, String[] constantsNames) throws DataException {
357 44374 jjdelcerro
        return this.getSubtype(names, constantsNames, true);
358
    }
359
360
    public FeatureType getSubtype(String[] names, String[] constantsNames, boolean includePk) throws DataException {
361 44253 jjdelcerro
        if (ArrayUtils.isEmpty(names) && ArrayUtils.isEmpty(constantsNames)) {
362
            return (FeatureType) this.clone();
363
        }
364 44374 jjdelcerro
        return new SubtypeFeatureType(this, names, constantsNames,includePk);
365 44253 jjdelcerro
    }
366 43998 jjdelcerro
367 44253 jjdelcerro
    public boolean isSubtypeOf(FeatureType featureType) {
368
        return false;
369
    }
370
371 44149 jjdelcerro
    @Override
372
    public List<FeatureAttributeDescriptor> toList() {
373
        return Collections.unmodifiableList(this);
374
    }
375 40435 jjdelcerro
376 44253 jjdelcerro
    @Override
377
    public Tags getTags() {
378
        return this.tags;
379
    }
380 40435 jjdelcerro
381 44253 jjdelcerro
    @Override
382
    public String getLabel() {
383
        return this.label;
384
    }
385
386
    @Override
387
    public void setLabel(String label) {
388
        this.label = label;
389
    }
390
391
    @Override
392
    public DynField addDynField(String name, int type) {
393
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
394
    }
395
396
    class SubtypeFeatureType extends DefaultFeatureType {
397
398
        /**
399
         *
400
         */
401
        private static final long serialVersionUID = 6913732960073922540L;
402
        WeakReference parent;
403
404 44374 jjdelcerro
        SubtypeFeatureType(DefaultFeatureType parent, String[] names, String[] constantsNames, boolean includePk)
405 44253 jjdelcerro
                throws DataException {
406
            super(parent, false);
407
            DefaultFeatureAttributeDescriptor attrcopy;
408
            Set<String> attrnames = new LinkedHashSet<>();
409
            Set<String> requiredAttrnames = new HashSet<>();
410
411
            if (ArrayUtils.isEmpty(names)) {
412
                for (FeatureAttributeDescriptor attrdesc : parent) {
413 44376 jjdelcerro
                    attrnames.add(attrdesc.getName().toLowerCase());
414 44253 jjdelcerro
                }
415 44376 jjdelcerro
            } else {
416 44253 jjdelcerro
                attrnames.addAll(Arrays.asList(names));
417
                requiredAttrnames.addAll(Arrays.asList(names));
418
            }
419
            // Add required fields for emulated fields
420
            if (parent.hasEmulators) {
421 44376 jjdelcerro
                // Ojo, este bucle falla cuando hay un campo calculado que depende
422
                // de otro campo calculado.
423 44253 jjdelcerro
                for (FeatureAttributeDescriptor attrdesc : parent) {
424
                    FeatureAttributeEmulator emulator = attrdesc.getFeatureAttributeEmulator();
425 44376 jjdelcerro
                    if (emulator != null && attrnames.contains(attrdesc.getName().toLowerCase())) {
426
                        String theNames[] = emulator.getRequiredFieldNames();
427
                        if (names != null) {
428
                            for (String name : theNames) {
429
                                name = name.toLowerCase();
430
                                attrnames.add(name);
431
                                requiredAttrnames.add(name);
432
                            }
433 43998 jjdelcerro
                        }
434
                    }
435 44253 jjdelcerro
                }
436
            }
437
            // Add missing pk fiels
438 44374 jjdelcerro
            if (includePk && !parent.hasOID()) {
439 44253 jjdelcerro
                for (FeatureAttributeDescriptor attrdesc : parent) {
440
                    if (attrdesc.isPrimaryKey()) {
441 44376 jjdelcerro
                        String name = attrdesc.getName().toLowerCase();
442
                        attrnames.add(name);
443
                        requiredAttrnames.add(name);
444 43998 jjdelcerro
                    }
445 44253 jjdelcerro
                }
446
            }
447 41212 jjdelcerro
448 44253 jjdelcerro
            // Copy attributes
449
            int i = 0;
450
            for (String name : attrnames) {
451
                DefaultFeatureAttributeDescriptor attr = (DefaultFeatureAttributeDescriptor) parent.get(name);
452
                if (attr == null) {
453
                    throw new SubtypeFeatureTypeNameException(name, parent.getId());
454
                }
455
                attrcopy = new DefaultFeatureAttributeDescriptor(attr);
456
                this.add(attrcopy);
457
                attrcopy.index = i++;
458
            }
459
460 44376 jjdelcerro
            // Set the constants attributes.
461 44253 jjdelcerro
            if (!ArrayUtils.isEmpty(constantsNames)) {
462
                for (String name : constantsNames) {
463 44583 omartinez
                    if (!requiredAttrnames.contains(name.toLowerCase())) {
464 44253 jjdelcerro
                        DefaultFeatureAttributeDescriptor attr = (DefaultFeatureAttributeDescriptor) this.get(name);
465
                        attr.setConstantValue(true);
466 41342 jjdelcerro
                    }
467
                }
468 44253 jjdelcerro
            }
469 40435 jjdelcerro
470 44253 jjdelcerro
            this.defaultGeometryAttributeIndex = this.getIndex(this.defaultGeometryAttributeName);
471
            if (this.defaultGeometryAttributeIndex < 0) {
472
                this.defaultGeometryAttributeName = null;
473
            }
474
            this.defaultTimeAttributeIndex = this.getIndex(this.defaultTimeAttributeName);
475
            if (this.defaultTimeAttributeIndex < 0) {
476
                this.defaultTimeAttributeName = null;
477
            }
478
            this.parent = new WeakReference(parent);
479
        }
480 40435 jjdelcerro
481 44374 jjdelcerro
        public FeatureType getSubtype(String[] names, boolean includePk) throws DataException {
482 44253 jjdelcerro
            return new SubtypeFeatureType((DefaultFeatureType) this.parent
483 44374 jjdelcerro
                    .get(), names, null, includePk);
484 44253 jjdelcerro
        }
485 40435 jjdelcerro
486 44253 jjdelcerro
        public boolean isSubtypeOf(FeatureType featureType) {
487
            if (featureType == null) {
488
                return false;
489
            }
490
            FeatureType parent = (FeatureType) this.parent.get();
491
            return featureType.equals(parent);
492
        }
493 40435 jjdelcerro
494 44253 jjdelcerro
        public EditableFeatureType getEditable() {
495
            throw new UnsupportedOperationException();
496
        }
497
    }
498 40435 jjdelcerro
499 44253 jjdelcerro
    public class SubtypeFeatureTypeNameException extends DataException {
500 40435 jjdelcerro
501 44253 jjdelcerro
        /**
502
         *
503
         */
504
        private static final long serialVersionUID = -4414242486723260101L;
505
        private final static String MESSAGE_FORMAT = "Attribute name '%(name)' not found in type (%(type)).";
506
        private final static String MESSAGE_KEY = "_SubtypeFeatureTypeNameException";
507 40435 jjdelcerro
508 44253 jjdelcerro
        public SubtypeFeatureTypeNameException(String name, String type) {
509
            super(MESSAGE_FORMAT, MESSAGE_KEY, serialVersionUID);
510
            setValue("name", name);
511
            setValue("type", type);
512
        }
513
    }
514 40435 jjdelcerro
515 44253 jjdelcerro
    public boolean hasOID() {
516
        return hasOID;
517
    }
518 40435 jjdelcerro
519 44583 omartinez
    @Override
520 44253 jjdelcerro
    public String toString() {
521
        StringBuffer s = new StringBuffer();
522
        s.append(this.getId());
523
        s.append(":[");
524
        String attName;
525
        for (int i = 0; i < size(); i++) {
526
            attName = ((FeatureAttributeDescriptor) get(i)).getName().toString();
527
            s.append(attName);
528
            if (i < size() - 1) {
529
                s.append(',');
530
            }
531
        }
532
        s.append(']');
533
        return s.toString();
534
    }
535 40435 jjdelcerro
536 44583 omartinez
    @Override
537 44253 jjdelcerro
    public Iterator<FeatureAttributeDescriptor> iterator() {
538
        return getIterator(super.iterator());
539
    }
540 40435 jjdelcerro
541 44253 jjdelcerro
    protected Iterator getIterator(Iterator iter) {
542
        return new DelegatedIterator(iter);
543
    }
544 40435 jjdelcerro
545 44253 jjdelcerro
    protected class DelegatedIterator implements Iterator {
546 40435 jjdelcerro
547 44253 jjdelcerro
        protected Iterator iterator;
548 40435 jjdelcerro
549 44253 jjdelcerro
        public DelegatedIterator(Iterator iter) {
550
            this.iterator = iter;
551
        }
552 40435 jjdelcerro
553 44583 omartinez
        @Override
554 44253 jjdelcerro
        public boolean hasNext() {
555
            return iterator.hasNext();
556
        }
557 40435 jjdelcerro
558 44583 omartinez
        @Override
559 44253 jjdelcerro
        public Object next() {
560
            return iterator.next();
561
        }
562 40435 jjdelcerro
563 44583 omartinez
        @Override
564 44253 jjdelcerro
        public void remove() {
565
            throw new UnsupportedOperationException();
566
        }
567 40435 jjdelcerro
568 44253 jjdelcerro
    }
569 40435 jjdelcerro
570 44583 omartinez
    @Override
571 44253 jjdelcerro
    public boolean allowAutomaticValues() {
572
        return this.allowAtomaticValues;
573
    }
574
575 44583 omartinez
    @Override
576 44253 jjdelcerro
    public FeatureAttributeDescriptor[] getAttributeDescriptors() {
577
        return (FeatureAttributeDescriptor[]) super
578
                .toArray(new FeatureAttributeDescriptor[super.size()]);
579
    }
580
581 44435 jjdelcerro
    @Override
582
    public boolean hasPrimaryKey() {
583
        if( pk!=null ) {
584
            return pk.length>0;
585
        }
586
        for (FeatureAttributeDescriptor attr : this) {
587
            if( attr.isPrimaryKey() ) {
588
                return true;
589
            }
590
        }
591
        return false;
592
    }
593
594
    @Override
595
    public boolean supportReferences() {
596
        return this.hasOID() || this.hasPrimaryKey();
597
    }
598
599
    @Override
600 44253 jjdelcerro
    public FeatureAttributeDescriptor[] getPrimaryKey() {
601
        if (pk == null) {
602
            List pks = new ArrayList();
603
            Iterator iter = super.iterator();
604
            FeatureAttributeDescriptor attr;
605
            while (iter.hasNext()) {
606
                attr = (FeatureAttributeDescriptor) iter.next();
607
                if (attr.isPrimaryKey()) {
608
                    pks.add(attr);
609
                }
610
            }
611
            if (pks.isEmpty()) {
612 43358 jjdelcerro
                pk = new FeatureAttributeDescriptor[0];
613
            } else {
614 44253 jjdelcerro
                pk = (FeatureAttributeDescriptor[]) pks.toArray(new FeatureAttributeDescriptor[pks.size()]);
615 43358 jjdelcerro
            }
616 44253 jjdelcerro
        }
617
        return pk;
618
    }
619 40435 jjdelcerro
620 44253 jjdelcerro
    public FeatureAttributeDescriptor getDefaultGeometryAttribute() {
621
        if (this.defaultGeometryAttributeIndex < 0) {
622
            return null;
623
        }
624
        return (FeatureAttributeDescriptor) super
625
                .get(this.defaultGeometryAttributeIndex);
626
    }
627 40435 jjdelcerro
628 44583 omartinez
    @Override
629 44253 jjdelcerro
    public boolean equals(Object o) {
630
        if (this == o) {
631
            return true;
632
        }
633
        if (!(o instanceof DefaultFeatureType)) {
634
            return false;
635
        }
636
        DefaultFeatureType other = (DefaultFeatureType) o;
637
        if (!this.id.equals(other.id)) {
638
            return false;
639
        }
640
        if (this.size() != other.size()) {
641
            return false;
642
        }
643
        FeatureAttributeDescriptor attr, attrOther;
644
        Iterator iter, iterOther;
645
        iter = this.iterator();
646
        iterOther = other.iterator();
647
        while (iter.hasNext()) {
648
            attr = (FeatureAttributeDescriptor) iter.next();
649
            attrOther = (FeatureAttributeDescriptor) iterOther.next();
650
            if (!attr.equals(attrOther)) {
651 44077 jjdelcerro
                return false;
652
            }
653 44253 jjdelcerro
        }
654 40435 jjdelcerro
655 44253 jjdelcerro
        if (!StringUtils.equals(defaultGeometryAttributeName, other.defaultGeometryAttributeName)) {
656
            return false;
657
        }
658
        if (!StringUtils.equals(defaultTimeAttributeName, other.defaultTimeAttributeName)) {
659
            return false;
660
        }
661
        return true;
662 40435 jjdelcerro
663 44253 jjdelcerro
    }
664 40435 jjdelcerro
665 44253 jjdelcerro
    /**
666
     * Start of DynClass interface implementation READONLY
667
     */
668 44583 omartinez
    @Override
669 44253 jjdelcerro
    public DynField addDynField(String name) {
670
        throw new UnsupportedOperationException();
671
    }
672 40435 jjdelcerro
673 44583 omartinez
    @Override
674 44253 jjdelcerro
    public DynField getDeclaredDynField(String name) {
675
        return (DynField) getAttributeDescriptor(name);
676
    }
677 40435 jjdelcerro
678 44583 omartinez
    @Override
679 44253 jjdelcerro
    public DynField[] getDeclaredDynFields() {
680
        return (DynField[]) getAttributeDescriptors();
681
    }
682 40435 jjdelcerro
683 44583 omartinez
    @Override
684 44253 jjdelcerro
    public String getDescription() {
685
        return this.description;
686
    }
687 40435 jjdelcerro
688 44583 omartinez
    @Override
689 44253 jjdelcerro
    public DynField getDynField(String name) {
690
        return (DynField) getAttributeDescriptor(name);
691
    }
692 40435 jjdelcerro
693 44583 omartinez
    @Override
694 44253 jjdelcerro
    public DynField[] getDynFields() {
695
        return (DynField[]) getAttributeDescriptors();
696
    }
697 40435 jjdelcerro
698 44583 omartinez
    @Override
699 44253 jjdelcerro
    public String getName() {
700
        return this.id + "_" + internalID;
701
    }
702 40435 jjdelcerro
703 44583 omartinez
    @Override
704 44253 jjdelcerro
    public void removeDynField(String name) {
705
        throw new UnsupportedOperationException();
706 40435 jjdelcerro
707 44253 jjdelcerro
    }
708 40435 jjdelcerro
709 44583 omartinez
    @Override
710 44253 jjdelcerro
    public void addDynMethod(DynMethod dynMethod) {
711
        throw new UnsupportedOperationException();
712 40435 jjdelcerro
713 44253 jjdelcerro
    }
714 40435 jjdelcerro
715 44253 jjdelcerro
    public void extend(DynClass dynClass) {
716
        throw new UnsupportedOperationException();
717 40435 jjdelcerro
718 44253 jjdelcerro
    }
719 40435 jjdelcerro
720 44583 omartinez
    @Override
721 44253 jjdelcerro
    public void extend(String dynClassName) {
722
        throw new UnsupportedOperationException();
723 40435 jjdelcerro
724 44253 jjdelcerro
    }
725 40435 jjdelcerro
726 44583 omartinez
    @Override
727 44253 jjdelcerro
    public void extend(String namespace, String dynClassName) {
728
        throw new UnsupportedOperationException();
729 40435 jjdelcerro
730 44253 jjdelcerro
    }
731 40435 jjdelcerro
732 44583 omartinez
    @Override
733 44253 jjdelcerro
    public DynMethod getDeclaredDynMethod(String name)
734
            throws DynMethodException {
735
        return null;
736
    }
737 40435 jjdelcerro
738 44583 omartinez
    @Override
739 44253 jjdelcerro
    public DynMethod[] getDeclaredDynMethods() throws DynMethodException {
740
        return null;
741
    }
742 40435 jjdelcerro
743 44583 omartinez
    @Override
744 44253 jjdelcerro
    public DynMethod getDynMethod(String name) throws DynMethodException {
745
        return null;
746
    }
747 40435 jjdelcerro
748 44583 omartinez
    @Override
749 44253 jjdelcerro
    public DynMethod getDynMethod(int code) throws DynMethodException {
750
        return null;
751
    }
752 40435 jjdelcerro
753 44583 omartinez
    @Override
754 44253 jjdelcerro
    public DynMethod[] getDynMethods() throws DynMethodException {
755
        return null;
756
    }
757 40435 jjdelcerro
758 44583 omartinez
    @Override
759 44253 jjdelcerro
    public DynClass[] getSuperDynClasses() {
760
        return null;
761
    }
762 40435 jjdelcerro
763 44583 omartinez
    @Override
764 44253 jjdelcerro
    public boolean isInstance(DynObject dynObject) {
765
        if (dynObject.getDynClass().getName() == getName()) {
766
            return true;
767
        }
768
        return false;
769
    }
770 40435 jjdelcerro
771 44583 omartinez
    @Override
772 44253 jjdelcerro
    public DynObject newInstance() {
773 40435 jjdelcerro
774 44253 jjdelcerro
        throw new UnsupportedOperationException();
775
    }
776 40435 jjdelcerro
777 44583 omartinez
    @Override
778 44253 jjdelcerro
    public void removeDynMethod(String name) {
779
        throw new UnsupportedOperationException();
780 40435 jjdelcerro
781 44253 jjdelcerro
    }
782 40435 jjdelcerro
783 44583 omartinez
    @Override
784 44253 jjdelcerro
    public DynField addDynFieldChoice(String name, int type,
785
            Object defaultValue, DynObjectValueItem[] values,
786
            boolean mandatory, boolean persistent) {
787
        throw new UnsupportedOperationException();
788
    }
789 40435 jjdelcerro
790 44583 omartinez
    @Override
791 44253 jjdelcerro
    public DynField addDynFieldRange(String name, int type,
792
            Object defaultValue, Object min, Object max, boolean mandatory,
793
            boolean persistent) {
794
        throw new UnsupportedOperationException();
795
    }
796 40435 jjdelcerro
797 44583 omartinez
    @Override
798 44253 jjdelcerro
    public DynField addDynFieldSingle(String name, int type,
799
            Object defaultValue, boolean mandatory, boolean persistent) {
800
        throw new UnsupportedOperationException();
801
    }
802 40435 jjdelcerro
803 44583 omartinez
    @Override
804 44253 jjdelcerro
    public void validate(DynObject object) throws DynObjectValidateException {
805
        //FIXME: not sure it's the correct code
806
        if (object instanceof Feature) {
807
            Feature fea = (Feature) object;
808
            if (fea.getType().equals(this)) {
809
                return;
810
            }
811
        }
812
        throw new DynObjectValidateException(this.id);
813
    }
814 40435 jjdelcerro
815 44583 omartinez
    @Override
816 44253 jjdelcerro
    public DynField addDynFieldLong(String name) {
817
        throw new UnsupportedOperationException();
818
    }
819 40435 jjdelcerro
820 44583 omartinez
    @Override
821 44253 jjdelcerro
    public DynField addDynFieldChoice(String name, int type,
822
            Object defaultValue, DynObjectValueItem[] values) {
823
        throw new UnsupportedOperationException();
824
    }
825 40435 jjdelcerro
826 44583 omartinez
    @Override
827 44253 jjdelcerro
    public DynField addDynFieldRange(String name, int type,
828
            Object defaultValue, Object min, Object max) {
829
        throw new UnsupportedOperationException();
830
    }
831 40435 jjdelcerro
832 44583 omartinez
    @Override
833 44253 jjdelcerro
    public DynField addDynFieldSingle(String name, int type, Object defaultValue) {
834
        throw new UnsupportedOperationException();
835
    }
836 40435 jjdelcerro
837 44583 omartinez
    @Override
838 44253 jjdelcerro
    public DynField addDynFieldString(String name) {
839
        throw new UnsupportedOperationException();
840
    }
841 43555 fdiaz
842 44583 omartinez
    @Override
843 44253 jjdelcerro
    public DynField addDynFieldInt(String name) {
844
        throw new UnsupportedOperationException();
845
    }
846 43555 fdiaz
847 44583 omartinez
    @Override
848 44253 jjdelcerro
    public DynField addDynFieldDouble(String name) {
849
        throw new UnsupportedOperationException();
850
    }
851 43555 fdiaz
852 44583 omartinez
    @Override
853 44253 jjdelcerro
    public DynField addDynFieldFloat(String name) {
854
        throw new UnsupportedOperationException();
855
    }
856 40435 jjdelcerro
857 44253 jjdelcerro
    public DynField addDynFieldBoolean(String name) {
858
        throw new UnsupportedOperationException();
859
    }
860 40435 jjdelcerro
861 44583 omartinez
    @Override
862 44253 jjdelcerro
    public DynField addDynFieldList(String name) {
863
        throw new UnsupportedOperationException();
864
    }
865 40435 jjdelcerro
866 44583 omartinez
    @Override
867 44253 jjdelcerro
    public DynField addDynFieldMap(String name) {
868
        throw new UnsupportedOperationException();
869
    }
870 40435 jjdelcerro
871 44583 omartinez
    @Override
872 44253 jjdelcerro
    public DynField addDynFieldObject(String name) {
873
        throw new UnsupportedOperationException();
874
    }
875 40435 jjdelcerro
876 44583 omartinez
    @Override
877 44253 jjdelcerro
    public DynField addDynFieldSet(String name) {
878
        throw new UnsupportedOperationException();
879
    }
880 40435 jjdelcerro
881 44583 omartinez
    @Override
882 44253 jjdelcerro
    public DynField addDynFieldArray(String name) {
883
        throw new UnsupportedOperationException();
884
    }
885 40435 jjdelcerro
886 44583 omartinez
    @Override
887 44253 jjdelcerro
    public DynField addDynFieldDate(String name) {
888
        throw new UnsupportedOperationException();
889
    }
890 40435 jjdelcerro
891 44583 omartinez
    @Override
892 44253 jjdelcerro
    public void extend(DynStruct struct) {
893
        throw new UnsupportedOperationException();
894
    }
895 40435 jjdelcerro
896 44583 omartinez
    @Override
897 44253 jjdelcerro
    public String getFullName() {
898 40435 jjdelcerro
        // TODO: usar el DynClassName
899 44253 jjdelcerro
        return this.id;
900
    }
901 40435 jjdelcerro
902 44583 omartinez
    @Override
903 44253 jjdelcerro
    public String getNamespace() {
904
        return "DALFeature";
905
    }
906 40435 jjdelcerro
907 44583 omartinez
    @Override
908 44253 jjdelcerro
    public DynStruct[] getSuperDynStructs() {
909
        return null;
910
    }
911 40435 jjdelcerro
912 44583 omartinez
    @Override
913 44253 jjdelcerro
    public void setDescription(String description) {
914
        this.description = description;
915
    }
916 40435 jjdelcerro
917 44583 omartinez
    @Override
918 44253 jjdelcerro
    public void setNamespace(String namespace) {
919
        throw new UnsupportedOperationException();
920
    }
921 40435 jjdelcerro
922 44583 omartinez
    @Override
923 44253 jjdelcerro
    public DynField addDynFieldFile(String name) {
924
        throw new UnsupportedOperationException();
925
    }
926 40435 jjdelcerro
927 44583 omartinez
    @Override
928 44253 jjdelcerro
    public DynField addDynFieldFolder(String name) {
929
        throw new UnsupportedOperationException();
930
    }
931 40435 jjdelcerro
932 44583 omartinez
    @Override
933 44253 jjdelcerro
    public DynField addDynFieldURL(String name) {
934
        throw new UnsupportedOperationException();
935
    }
936 40435 jjdelcerro
937 44583 omartinez
    @Override
938 44253 jjdelcerro
    public DynField addDynFieldURI(String name) {
939
        throw new UnsupportedOperationException();
940
    }
941 40435 jjdelcerro
942 44583 omartinez
    @Override
943 40435 jjdelcerro
    public boolean isExtendable(DynStruct dynStruct) {
944
        return false;
945
    }
946
947 44253 jjdelcerro
    public void extend(DynStruct[] structs) {
948
        // TODO Auto-generated method stub
949 43555 fdiaz
950 44253 jjdelcerro
    }
951 40435 jjdelcerro
952 44583 omartinez
    @Override
953 44253 jjdelcerro
    public void remove(DynStruct superDynStruct) {
954
        // TODO Auto-generated method stub
955 43555 fdiaz
956 44253 jjdelcerro
    }
957 40435 jjdelcerro
958 44253 jjdelcerro
    public void removeAll(DynStruct[] superDynStruct) {
959
        // TODO Auto-generated method stub
960 43555 fdiaz
961 44253 jjdelcerro
    }
962 40435 jjdelcerro
963 44583 omartinez
    @Override
964 44253 jjdelcerro
    public FeatureAttributeDescriptor getDefaultTimeAttribute() {
965
        if (this.defaultTimeAttributeIndex < 0) {
966
            return null;
967
        }
968
        return (FeatureAttributeDescriptor) super
969
                .get(this.defaultTimeAttributeIndex);
970
    }
971 43610 jjdelcerro
972
    public void setDefaultTimeAttributeName(String name) {
973
        if (name == null || name.length() == 0) {
974
            this.defaultTimeAttributeIndex = -1;
975
            return;
976
        }
977
        DefaultFeatureAttributeDescriptor attr = (DefaultFeatureAttributeDescriptor) this.get(name);
978
        if (attr == null) {
979
            throw new IllegalArgumentException("Attribute '" + name + "' not found.");
980
        }
981 44253 jjdelcerro
        if (attr.getIndex() < 0) {
982 43610 jjdelcerro
            fixAll();
983
        }
984
        this.defaultTimeAttributeIndex = attr.getIndex();
985
    }
986 44253 jjdelcerro
987 43610 jjdelcerro
    protected void fixAll() {
988
        int i = 0;
989
        Iterator iter = super.iterator();
990
        DefaultFeatureAttributeDescriptor attr;
991
992
        while (iter.hasNext()) {
993
            attr = (DefaultFeatureAttributeDescriptor) iter.next();
994 44253 jjdelcerro
            if (attr.getOder() < 1) {
995
                attr.setOrder(i * 10);
996 44202 jjdelcerro
            }
997 43610 jjdelcerro
            attr.setIndex(i++);
998 44094 jjdelcerro
            attr.fixAll();
999 43610 jjdelcerro
            if (attr.getEvaluator() != null) {
1000
                this.hasEvaluators = true;
1001
            }
1002
            if (attr.getFeatureAttributeEmulator() != null) {
1003
                this.hasEmulators = true;
1004 43998 jjdelcerro
                String[] x = attr.getFeatureAttributeEmulator().getRequiredFieldNames();
1005 44253 jjdelcerro
                if (!ArrayUtils.isEmpty(x)) {
1006 43998 jjdelcerro
                    this.requiredFields = true;
1007
                }
1008 43610 jjdelcerro
            }
1009 44253 jjdelcerro
            switch (attr.getType()) {
1010 44090 jjdelcerro
                case DataTypes.GEOMETRY:
1011 44253 jjdelcerro
                    if (this.defaultGeometryAttributeName == null) {
1012 44090 jjdelcerro
                        this.defaultGeometryAttributeName = attr.getName();
1013
                    }
1014
                    break;
1015
                case DataTypes.INSTANT:
1016
                case DataTypes.INTERVAL:
1017
                case DataTypes.DATE:
1018 44253 jjdelcerro
                    if (this.defaultTimeAttributeName == null && attr.isTime()) {
1019 44090 jjdelcerro
                        this.defaultTimeAttributeName = attr.getName();
1020
                    }
1021
                    break;
1022 43610 jjdelcerro
            }
1023
        }
1024
        if (this.defaultGeometryAttributeName != null) {
1025
            this.defaultGeometryAttributeIndex = this.getIndex(this.defaultGeometryAttributeName);
1026
        }
1027 44077 jjdelcerro
        if (this.defaultTimeAttributeName != null) {
1028
            this.defaultTimeAttributeIndex = this.getIndex(this.defaultTimeAttributeName);
1029
        }
1030 43610 jjdelcerro
        this.internalID = Long.toHexString(this.getCRC());
1031 44253 jjdelcerro
1032 43610 jjdelcerro
    }
1033 44253 jjdelcerro
1034 43610 jjdelcerro
    protected long getCRC() {
1035
        StringBuffer buffer = new StringBuffer();
1036
        for (int i = 0; i < this.size(); i++) {
1037
            FeatureAttributeDescriptor x = this.getAttributeDescriptor(i);
1038
            buffer.append(x.getName());
1039
            buffer.append(x.getDataTypeName());
1040
            buffer.append(x.getSize());
1041
        }
1042
        CRC32 crc = new CRC32();
1043
        byte[] data = buffer.toString().getBytes();
1044
        crc.update(data);
1045
        return crc.getValue();
1046
    }
1047 43739 jjdelcerro
1048
    @Override
1049
    public FeatureStore getStore() {
1050 44253 jjdelcerro
        if (this.storeRef == null) {
1051 43739 jjdelcerro
            return null;
1052
        }
1053
        return (FeatureStore) this.storeRef.get();
1054
    }
1055 44253 jjdelcerro
1056 44190 jjdelcerro
    public void setStore(FeatureStore store) {
1057 44253 jjdelcerro
        if (store == null) {
1058
            this.storeRef = null;
1059 44190 jjdelcerro
        } else {
1060
            this.storeRef = new WeakReference(store);
1061
        }
1062
    }
1063 43739 jjdelcerro
1064 44190 jjdelcerro
    @Override
1065 44262 jjdelcerro
    public List<FeatureAttributeDescriptor> getFilteredAttributes(
1066
            Predicate<FeatureAttributeDescriptor> filter,
1067
            int max
1068
    ) {
1069
        List<FeatureAttributeDescriptor> attrs = new ArrayList<>();
1070
        for (FeatureAttributeDescriptor attribute : this) {
1071
            if (filter.test(attribute)) {
1072
                attrs.add(attribute);
1073
            }
1074
        }
1075
        return attrs;
1076
    }
1077
1078
    @Override
1079
    public List<FeatureAttributeDescriptor> getRecentUseds() {
1080
        return RECENTS_USEDS.getAttributes(this);
1081
    }
1082
1083
    @Override
1084 44190 jjdelcerro
    public void loadFromState(PersistentState state)
1085
            throws PersistenceException {
1086
1087
//        FIXME: rules
1088
        hasEvaluators = state.getBoolean("hasEvaluators");
1089
        hasEmulators = state.getBoolean("hasEmulators");
1090
        defaultGeometryAttributeName = state.getString("defaultGeometryAttributeName");
1091
        defaultTimeAttributeName = state.getString("defaultTimeAttributeName");
1092
        defaultGeometryAttributeIndex = state.getInt("defaultGeometryAttributeIndex");
1093
        defaultTimeAttributeIndex = state.getInt("defaultTimeAttributeIndex");
1094
        id = state.getString("id");
1095
        hasOID = state.getBoolean("hasOID");
1096
        allowAtomaticValues = state.getBoolean("allowAtomaticValues");
1097
1098
        requiredFields = state.getBoolean("requiredFields");
1099
        internalID = state.getString("internalID");
1100 44262 jjdelcerro
        tags = (Tags) state.get("tags");
1101
        if( tags == null ) {
1102
            this.tags = new DefaultTags();
1103
        }
1104 44253 jjdelcerro
1105 44190 jjdelcerro
        List<FeatureAttributeDescriptor> elements = state.getList("elements");
1106
        for (FeatureAttributeDescriptor element : elements) {
1107 44253 jjdelcerro
            ((DefaultFeatureAttributeDescriptor) element).setFeatureType(this);
1108 44190 jjdelcerro
            super.add(element);
1109
        }
1110 44261 jjdelcerro
        this.pk = null;
1111 44190 jjdelcerro
        this.fixAll();
1112
    }
1113
1114
    @Override
1115
    public void saveToState(PersistentState state) throws PersistenceException {
1116
1117
//        FIXME: rules
1118
        state.set("hasEvaluators", hasEvaluators);
1119
        state.set("hasEmulators", hasEmulators);
1120
        state.set("defaultGeometryAttributeName", defaultGeometryAttributeName);
1121
        state.set("defaultTimeAttributeName", defaultTimeAttributeName);
1122
        state.set("defaultGeometryAttributeIndex", defaultGeometryAttributeIndex);
1123
        state.set("defaultTimeAttributeIndex", defaultTimeAttributeIndex);
1124
        state.set("id", id);
1125
        state.set("hasOID", hasOID);
1126
        state.set("allowAtomaticValues", allowAtomaticValues);
1127
1128
        state.set("requiredFields", requiredFields);
1129
        state.set("internalID", internalID);
1130 44253 jjdelcerro
1131 44190 jjdelcerro
        List<FeatureAttributeDescriptor> elements = new ArrayList<>();
1132
        elements.addAll(this);
1133
        state.set("elements", elements);
1134 44262 jjdelcerro
        state.set("tags", tags);
1135 44253 jjdelcerro
1136 44190 jjdelcerro
    }
1137 44253 jjdelcerro
1138 44190 jjdelcerro
    private static final String FEATTYPE_PERSISTENCE_DEFINITION_NAME = "FeatureType";
1139
1140
    public static void registerPersistenceDefinition() {
1141
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
1142 44253 jjdelcerro
1143 44190 jjdelcerro
        if (manager.getDefinition(FEATTYPE_PERSISTENCE_DEFINITION_NAME)
1144
                == null) {
1145
            DynStruct definition = manager.addDefinition(DefaultFeatureType.class,
1146
                    FEATTYPE_PERSISTENCE_DEFINITION_NAME,
1147
                    FEATTYPE_PERSISTENCE_DEFINITION_NAME
1148 44253 jjdelcerro
                    + " persistent definition",
1149 44190 jjdelcerro
                    null,
1150
                    null
1151
            );
1152
//            definition.addDynFieldObject("rules");
1153
            definition.addDynFieldBoolean("hasEvaluators");
1154
            definition.addDynFieldBoolean("hasEmulators");
1155
            definition.addDynFieldString("defaultGeometryAttributeName");
1156
            definition.addDynFieldString("defaultTimeAttributeName");
1157
            definition.addDynFieldInt("defaultGeometryAttributeIndex");
1158
            definition.addDynFieldInt("defaultTimeAttributeIndex");
1159
            definition.addDynFieldString("id");
1160
            definition.addDynFieldBoolean("hasOID");
1161
            definition.addDynFieldBoolean("allowAtomaticValues");
1162 44253 jjdelcerro
1163 44190 jjdelcerro
            definition.addDynFieldBoolean("requiredFields");
1164 44253 jjdelcerro
            definition.addDynFieldString("internalID");
1165 44190 jjdelcerro
1166
            definition.addDynFieldList("elements")
1167 44253 jjdelcerro
                    .setClassOfItems(FeatureAttributeDescriptor.class);
1168 44262 jjdelcerro
1169
            definition.addDynFieldObject("tags")
1170
                    .setClassOfValue(Tags.class);
1171
1172 44190 jjdelcerro
        }
1173
    }
1174
1175 44439 jjdelcerro
    @Override
1176
    public FeatureStore getAsFeatureStore() {
1177
        FeatureStore store = FeatureTypeToStoreProviderAdapter.createFeatureStore(this);
1178
        return store;
1179
    }
1180 44498 omartinez
1181
1182 44583 omartinez
    @Override
1183 44498 omartinez
    public String getNewFieldName() {
1184
        I18nManager i18n = ToolsLocator.getI18nManager();
1185
        String prefix = i18n.getTranslation("_Field");
1186
        String fieldName;
1187
        for (int i = 1; i < 1000; i++) {
1188 44583 omartinez
            fieldName = prefix+i;
1189 44498 omartinez
            if( this.get(fieldName)==null ) {
1190
                return fieldName;
1191
            }
1192
        }
1193 44583 omartinez
        fieldName = prefix + (new Date()).getTime();
1194 44498 omartinez
        return fieldName;
1195
    }
1196 44500 omartinez
1197 44583 omartinez
    @Override
1198 44500 omartinez
   public FeatureType getOriginalFeatureType()  {
1199
        DefaultFeatureStore store = (DefaultFeatureStore) this.getStore();
1200
        if (store==null) {
1201
            return null;
1202
        }
1203
        return store.getOriginalFeatureType(this);
1204
    }
1205 44501 jjdelcerro
    @Override
1206
    public boolean hasOnlyMetadataChanges(FeatureType old) {
1207
        if( old == null ) {
1208
            throw new NullPointerException();
1209
        }
1210
        // Si hay campos nuevos -> false
1211
        for (FeatureAttributeDescriptor attr : this) {
1212 44507 omartinez
            if(!attr.isComputed() && old.getAttributeDescriptor(attr.getName())==null) {
1213 44501 jjdelcerro
                return false;
1214
            }
1215
        }
1216
1217
        // Si se ha eliminado algun campo -> false
1218
        for (FeatureAttributeDescriptor attr : old) {
1219 44507 omartinez
            if(!attr.isComputed() && this.getAttributeDescriptor(attr.getName())==null ) {
1220 44501 jjdelcerro
                return false;
1221
            }
1222
        }
1223
1224
        // No hay campos nuevos ni se ha eliminado ninguno, asi que comparamos
1225
        // los campos uno a uno.
1226
        for (FeatureAttributeDescriptor attr : this) {
1227
            FeatureAttributeDescriptor attrold = old.getAttributeDescriptor(attr.getName());
1228 44507 omartinez
            if(!attr.isComputed() && !attr.hasOnlyMetadataChanges(attrold) ) {
1229 44501 jjdelcerro
                return false;
1230
            }
1231
        }
1232
        return true;
1233
    }
1234 44253 jjdelcerro
}