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 / DefaultEditableFeatureAttributeDescriptor.java @ 46097

History | View | Annotate | Download (25.4 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.fmap.dal.feature.impl;
25

    
26
import java.math.BigDecimal;
27
import java.text.DateFormat;
28
import java.util.HashMap;
29
import java.util.Locale;
30
import java.util.Objects;
31
import javax.json.JsonObject;
32
import org.apache.commons.lang3.StringUtils;
33

    
34
import org.cresques.cts.IProjection;
35
import org.gvsig.expressionevaluator.Expression;
36
import org.gvsig.expressionevaluator.ExpressionUtils;
37
import org.gvsig.fmap.crs.CRSFactory;
38
import org.gvsig.fmap.dal.DALLocator;
39
import org.gvsig.fmap.dal.DataTypeUtils;
40
import org.gvsig.fmap.dal.DataTypes;
41
import org.gvsig.fmap.dal.expressionevaluator.FeatureAttributeEmulatorExpression;
42
import org.slf4j.Logger;
43
import org.slf4j.LoggerFactory;
44

    
45
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
46
import org.gvsig.fmap.dal.feature.EditableForeingKey;
47
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator;
48
import org.gvsig.fmap.dal.feature.FeatureType;
49
import org.gvsig.fmap.dal.feature.exception.AttributeFeatureTypeIntegrityException;
50
import org.gvsig.fmap.dal.feature.exception.AttributeFeatureTypeSizeException;
51
import org.gvsig.fmap.geom.Geometry;
52
import org.gvsig.fmap.geom.GeometryUtils;
53
import org.gvsig.fmap.geom.type.GeometryType;
54
import org.gvsig.json.Json;
55
import org.gvsig.json.JsonManager;
56
import org.gvsig.json.JsonObjectBuilder;
57
import org.gvsig.json.SupportToJson;
58
import org.gvsig.timesupport.Interval;
59
import org.gvsig.tools.ToolsLocator;
60
import org.gvsig.tools.dataTypes.DataType;
61
import org.gvsig.tools.dynobject.DynField;
62
import org.gvsig.tools.evaluator.Evaluator;
63

    
64
public class DefaultEditableFeatureAttributeDescriptor extends
65
        DefaultFeatureAttributeDescriptor implements
66
        EditableFeatureAttributeDescriptor {
67

    
68
    private static Logger logger = LoggerFactory.getLogger(
69
            DefaultEditableFeatureAttributeDescriptor.class);
70

    
71
    private final DefaultFeatureAttributeDescriptor source;
72
    private boolean hasStrongChanges;
73
    private String originalName = null;
74

    
75
    protected DefaultEditableFeatureAttributeDescriptor(
76
            DefaultFeatureAttributeDescriptor other) {
77
        super(other);
78
        if (other instanceof DefaultEditableFeatureAttributeDescriptor) {
79
            DefaultEditableFeatureAttributeDescriptor other_edi
80
                    = (DefaultEditableFeatureAttributeDescriptor) other;
81
            originalName = other_edi.getOriginalName();
82
            this.source = other_edi.getSource();
83
        } else {
84
            this.source = other;
85
        }
86
        hasStrongChanges = false;
87
    }
88

    
89
    public DefaultEditableFeatureAttributeDescriptor(FeatureType type, boolean strongChanges) {
90
        super(type);
91
        this.source = null;
92
        hasStrongChanges = strongChanges;
93
    }
94

    
95
    public DefaultFeatureAttributeDescriptor getSource() {
96
        return this.source;
97
    }
98

    
99
    public void fixAll() {
100
        super.fixAll();
101
    }
102

    
103
    public void checkIntegrity() throws AttributeFeatureTypeIntegrityException {
104
        AttributeFeatureTypeIntegrityException ex
105
                = new AttributeFeatureTypeIntegrityException(getName());
106
        if (this.size < 0) {
107
            ex.add(new AttributeFeatureTypeSizeException(this.size));
108
        }
109

    
110
        if (this.dataType.isObject() && this.objectClass == null) {
111
            logger.warn("Incorrect data type object, objectClass is null.");
112
            ex.add(new AttributeFeatureTypeIntegrityException(this.name));
113
        }
114

    
115
        // TODO: Add other integrity checks...
116
        if (ex.size() > 0) {
117
            throw ex;
118
        }
119
    }
120

    
121
    public EditableFeatureAttributeDescriptor setAllowNull(boolean allowNull) {
122
        updateStrongChanges(this.allowNull, allowNull);
123
        this.allowNull = allowNull;
124
        return this;
125
    }
126

    
127
    @Override
128
    public EditableForeingKey getForeingKey() {
129
        if (this.foreingKey == null) {
130
            this.foreingKey = new DefaultForeingKey();
131
            this.foreingKey.setDescriptor(this);
132
        }
133
        return this.foreingKey;
134
    }
135

    
136
    public EditableFeatureAttributeDescriptor setDataType(DataType dataType) {
137
        updateStrongChanges(this.dataType, dataType);
138
        this.dataType = dataType;
139
        return this;
140
    }
141

    
142
    public EditableFeatureAttributeDescriptor setDataType(int type) {
143
        updateStrongChanges(this.dataType, type);
144
        this.dataType = ToolsLocator.getDataTypesManager().get(type);
145
        return this;
146
    }
147

    
148
    public EditableFeatureAttributeDescriptor setDefaultValue(
149
            Object defaultValue) {
150
        updateStrongChanges(this.defaultValue, defaultValue);
151
        this.defaultValue = defaultValue;
152
        return this;
153
    }
154

    
155
    public EditableFeatureAttributeDescriptor setAvoidCachingAvailableValues(boolean avoidCachingAvailableValues) {
156
        this.avoidCachingAvailableValues = avoidCachingAvailableValues;
157
        return this;
158
    }
159

    
160
    public EditableFeatureAttributeDescriptor setEvaluator(Evaluator evaluator) {
161
        updateStrongChanges(this.evaluator, evaluator);
162
        this.evaluator = evaluator;
163
        return this;
164
    }
165

    
166
    @Override
167
    public EditableFeatureAttributeDescriptor setFeatureAttributeEmulator(FeatureAttributeEmulator featureAttributeEmulator) {
168
        this.featureAttributeEmulator = featureAttributeEmulator;
169
        return this;
170
    }
171

    
172
    @Override
173
    public EditableFeatureAttributeDescriptor setFeatureAttributeEmulator(Expression expression) {
174
        if (ExpressionUtils.isPhraseEmpty(expression)) {
175
            this.setFeatureAttributeEmulator((FeatureAttributeEmulator) null);
176
            return this;
177
        }
178
        FeatureAttributeEmulatorExpression emulator = DALLocator.getDataManager().createFeatureAttributeEmulatorExpression(
179
                this.getFeatureType(),
180
                expression
181
        );
182
        return this.setFeatureAttributeEmulator(emulator);
183
    }
184

    
185
    @Override
186
    public EditableFeatureAttributeDescriptor setFeatureAttributeEmulator(String expression) {
187
        if (StringUtils.isBlank(expression)) {
188
            this.setFeatureAttributeEmulator((FeatureAttributeEmulator) null);
189
            return this;
190
        }
191
        return this.setFeatureAttributeEmulator(ExpressionUtils.createExpression(expression));
192
    }
193

    
194
    @Override
195
    public EditableFeatureAttributeDescriptor setGeometryType(int type) {
196
        this.geometryType = type;
197
        if (this.geometrySubType == Geometry.SUBTYPES.UNKNOWN) {
198
            this.geometrySubType = Geometry.SUBTYPES.GEOM2D;
199
        }
200
        this.geomType = null;
201
        return this;
202
    }
203

    
204
    public EditableFeatureAttributeDescriptor setGeometrySubType(int subType) {
205
        this.geometrySubType = subType;
206
        this.geomType = null;
207
        return this;
208
    }
209

    
210
    public EditableFeatureAttributeDescriptor setGeometryType(
211
            GeometryType geometryType) {
212
        updateStrongChanges(this.geomType, geometryType);
213
        this.geomType = geometryType;
214
        this.geometryType = this.geomType.getType();
215
        this.geometrySubType = this.geomType.getSubType();
216
        return this;
217
    }
218

    
219
    @Override
220
    public EditableFeatureAttributeDescriptor setGeometryType(String geometryType) {
221
        if (StringUtils.isBlank(geometryType)) {
222
            throw new IllegalArgumentException("Invalid geometry type (null)");
223
        }
224
        String separators = ":/-!;#@";
225
        Character sep = null;
226
        for (char ch : separators.toCharArray()) {
227
            if (geometryType.indexOf(ch) >= 0) {
228
                sep = ch;
229
                break;
230
            }
231
        }
232
        if (sep == null) {
233
            throw new IllegalArgumentException("Invalid geometry type (" + geometryType + ") can't find separator, format can be GEOMETRYTYPE[" + separators + "]GEOMETRYSUBTYPE");
234
        }
235
        String[] xx = geometryType.split("[" + sep + "]");
236
        int theGeomType = GeometryUtils.getGeometryType(xx[0]);
237
        int theGeomSubtype = GeometryUtils.getGeometrySubtype(xx[1]);
238
        this.setGeometryType(theGeomType, theGeomSubtype);
239
        return this;
240
    }
241

    
242
    @Override
243
    public EditableFeatureAttributeDescriptor setGeometryType(int type, int subType) {
244
        this.geometryType = type;
245
        this.geometrySubType = subType;
246
        this.geomType = null;
247
        return this;
248
    }
249

    
250
    @Override
251
    public EditableFeatureAttributeDescriptor setIsPrimaryKey(
252
            boolean isPrimaryKey) {
253
        updateStrongChanges(this.primaryKey, isPrimaryKey);
254
        this.primaryKey = isPrimaryKey;
255
        return this;
256
    }
257

    
258
    @Override
259
    public EditableFeatureAttributeDescriptor setIsReadOnly(boolean isReadOnly) {
260
        updateStrongChanges(this.readOnly, isReadOnly);
261
        this.readOnly = isReadOnly;
262
        return this;
263
    }
264

    
265
    public EditableFeatureAttributeDescriptor setMaximumOccurrences(
266
            int maximumOccurrences) {
267
        updateStrongChanges(this.maximumOccurrences, maximumOccurrences);
268
        this.maximumOccurrences = maximumOccurrences;
269
        return this;
270
    }
271

    
272
    public EditableFeatureAttributeDescriptor setMinimumOccurrences(
273
            int minimumOccurrences) {
274
        updateStrongChanges(this.minimumOccurrences, minimumOccurrences);
275
        this.minimumOccurrences = minimumOccurrences;
276
        return this;
277
    }
278

    
279
    @Override
280
    public EditableFeatureAttributeDescriptor setName(String name) {
281
        if (StringUtils.equals(this.name, name)) {
282
            return this;
283
        }
284
        if (originalName == null) {
285
            originalName = this.name;
286
            if (!isComputed()) {
287
                hasStrongChanges = true;
288
            }
289
        }
290
        super.setName(name);
291
        if (!isComputed()) {
292
            hasStrongChanges = true;
293
        }
294
        return this;
295
    }
296

    
297
    public String getOriginalName() {
298
        return originalName;
299
    }
300

    
301
    public EditableFeatureAttributeDescriptor setObjectClass(Class theClass) {
302
        updateStrongChanges(this.objectClass, theClass);
303
        this.objectClass = theClass;
304
        return this;
305
    }
306

    
307
    @Override
308
    public EditableFeatureAttributeDescriptor setPrecision(int precision) {
309
        updateStrongChanges(this.precision, precision);
310
        this.precision = precision;
311
        return this;
312
    }
313

    
314
    @Override
315
    public EditableFeatureAttributeDescriptor setScale(int scale) {
316
        updateStrongChanges(this.scale, scale);
317
        this.scale = scale;
318
        this.coerceContext = null;
319
        this.mathContext = null;
320
        return this;
321
    }
322

    
323
    @Override
324
    public EditableFeatureAttributeDescriptor setSRS(IProjection SRS) {
325
        updateStrongChanges(this.SRS, SRS);
326
        this.SRS = SRS;
327
        return this;
328
    }
329

    
330
    @Override
331
    public EditableFeatureAttributeDescriptor setSRS(String SRS) {
332
        if (StringUtils.isBlank(SRS)) {
333
            this.setSRS((IProjection) null);
334
            return this;
335
        }
336
        SRS = SRS.replace('@', ':');
337
        IProjection proj = CRSFactory.getCRS(SRS);
338
        this.setSRS(proj);
339
        return this;
340
    }
341

    
342
    public EditableFeatureAttributeDescriptor setInterval(Interval interval) {
343
        updateStrongChanges(this.getInterval(), interval);
344
        super.setInterval(interval);
345
        return this;
346
    }
347

    
348
    public EditableFeatureAttributeDescriptor setSize(int size) {
349
        updateStrongChanges(this.size, size);
350
        this.size = size;
351
        return this;
352
    }
353

    
354
    public boolean hasStrongChanges() {
355
        return hasStrongChanges;
356
    }
357

    
358
    @Override
359
    public EditableFeatureAttributeDescriptor setAdditionalInfo(
360
            String infoName, Object value) {
361
        return this.setAdditionalInfo(infoName, Objects.toString(value, ""));
362
    }
363

    
364
    public EditableFeatureAttributeDescriptor setAdditionalInfo(
365
            String infoName, String value) {
366
        if (this.additionalInfo == null) {
367
            this.additionalInfo = new HashMap();
368
        }
369
        this.additionalInfo.put(infoName, value);
370
        return this;
371
    }
372

    
373
    public EditableFeatureAttributeDescriptor setIsAutomatic(boolean isAutomatic) {
374
        this.isAutomatic = isAutomatic;
375
//        if (isAutomatic) {
376
//            this.setReadOnly(true);
377
//        }
378
        return this;
379
    }
380

    
381
    public EditableFeatureAttributeDescriptor setIsTime(boolean isTime) {
382
        updateStrongChanges(this.isTime, isTime);
383
        this.isTime = isTime;
384
        return this;
385
    }
386

    
387
    public EditableFeatureAttributeDescriptor setDateFormat(DateFormat dateFormat) {
388
        this.dateFormat = dateFormat;
389
        return this;
390
    }
391

    
392
    public EditableFeatureAttributeDescriptor setIsIndexed(boolean isIndexed) {
393
        updateStrongChanges(this.indexed, isIndexed);
394
        this.indexed = isIndexed;
395
        return this;
396
    }
397

    
398
    public EditableFeatureAttributeDescriptor setAllowIndexDuplicateds(boolean allowDuplicateds) {
399
        updateStrongChanges(this.allowIndexDuplicateds, allowDuplicateds);
400
        this.allowIndexDuplicateds = allowDuplicateds;
401
        return this;
402
    }
403

    
404
    public EditableFeatureAttributeDescriptor setIsIndexAscending(boolean ascending) {
405
        updateStrongChanges(this.isIndexAscending, ascending);
406
        this.isIndexAscending = ascending;
407
        return this;
408
    }
409

    
410
    @Override
411
    public EditableFeatureAttributeDescriptor setDataProfileName(String dataProfile) {
412
        super.setDataProfileName(dataProfile);
413
        return this;
414
    }
415

    
416
    private void updateStrongChanges(int previous, int newvalue) {
417
        if (isComputed()) {
418
            return;
419
        }
420
        if (previous == newvalue) {
421
            return;
422
        }
423
        this.hasStrongChanges = true;
424
    }
425

    
426
    private void updateStrongChanges(DataType previous, int newvalue) {
427
        if (isComputed()) {
428
            return;
429
        }
430
        if (previous != null) {
431
            if (previous.getType() == newvalue) {
432
                return;
433
            }
434
        }
435
        this.hasStrongChanges = true;
436
    }
437

    
438
    private void updateStrongChanges(boolean previous, boolean newvalue) {
439
        if (isComputed()) {
440
            return;
441
        }
442
        if (previous == newvalue) {
443
            return;
444
        }
445
        this.hasStrongChanges = true;
446
    }
447

    
448
    private void updateStrongChanges(Object previous, Object newvalue) {
449
        if (isComputed()) {
450
            return;
451
        }
452
        if (Objects.equals(newvalue, previous)) {
453
            return;
454
        }
455
        this.hasStrongChanges = true;
456
    }
457

    
458
    @Override
459
    public EditableFeatureAttributeDescriptor setLocale(Locale locale) {
460
        if (locale == null) {
461
            this.locale = Locale.ENGLISH;
462
        } else {
463
            this.locale = locale;
464
        }
465
        this.coerceContext = null;
466
        this.mathContext = null;
467
        return this;
468
    }
469

    
470
    public EditableFeatureAttributeDescriptor setLocale(String locale) {
471
        Locale l;
472
        try {
473
            String s = DataTypeUtils.toString(locale, null);
474
            if (StringUtils.isBlank(s)) {
475
                return this.setLocale((Locale) null);
476
            }
477
            l = new Locale(s);
478
            return this.setLocale(l);
479
        } catch (Exception ex) {
480
            return this.setLocale((Locale) null);
481
        }
482
    }
483

    
484
    @Override
485
    public EditableFeatureAttributeDescriptor setRoundMode(int roundMode) {
486
        switch (roundMode) {
487
            case BigDecimal.ROUND_UP:
488
            case BigDecimal.ROUND_DOWN:
489
            case BigDecimal.ROUND_CEILING:
490
            case BigDecimal.ROUND_FLOOR:
491
            case BigDecimal.ROUND_HALF_UP:
492
            case BigDecimal.ROUND_HALF_DOWN:
493
            case BigDecimal.ROUND_HALF_EVEN:
494
            case BigDecimal.ROUND_UNNECESSARY:
495
                this.roundMode = roundMode;
496
                this.coerceContext = null;
497
                this.mathContext = null;
498
                break;
499
            default:
500
                throw new IllegalArgumentException("round mode '" + roundMode + "' not valid.");
501
        }
502
        return this;
503
    }
504

    
505
    @Override
506
    public EditableFeatureAttributeDescriptor set(String name, Object value) {
507
        if (StringUtils.isBlank(name)) {
508
            throw new IllegalArgumentException("Name can't be empty");
509
        }
510
        String ss;
511
        switch (name.trim().toLowerCase()) {
512
            case "isavoidcachingavailablevalues":
513
            case "avoidcachingavailablevalues":
514
            case "nocachingavailablevalues":
515
                this.setAvoidCachingAvailableValues(DataTypeUtils.toBoolean(value, false));
516
                break;
517
            case "availablevalues":
518
                this.setAvailableValuesExpression(DataTypeUtils.toString(value, null));
519
                break;
520
            case "isreadonly":
521
            case "readonly":
522
                this.setIsReadOnly(DataTypeUtils.toBoolean(value, false));
523
                break;
524
            case "mandatory":
525
                this.setMandatory(DataTypeUtils.toBoolean(value, false));
526
                break;
527
            case "hidden":
528
                this.setHidden(DataTypeUtils.toBoolean(value, false));
529
                break;
530
            case "allownull":
531
                this.setAllowNull(DataTypeUtils.toBoolean(value, false));
532
                break;
533
            case "indexed":
534
            case "isindexed":
535
                this.setIsIndexed(DataTypeUtils.toBoolean(value, false));
536
                break;
537
            case "pk":
538
            case "ispk":
539
            case "primarykey":
540
            case "isprimarykey":
541
                this.setIsPrimaryKey(DataTypeUtils.toBoolean(value, false));
542
                break;
543
            case "allowindexduplicateds":
544
                this.setAllowIndexDuplicateds(DataTypeUtils.toBoolean(value, false));
545
                break;
546
            case "isautomatic":
547
            case "automatic":
548
                this.setIsAutomatic(DataTypeUtils.toBoolean(value, false));
549
                break;
550
            case "time":
551
            case "istime":
552
                this.setIsTime(DataTypeUtils.toBoolean(value, false));
553
                break;
554
            case "profile":
555
                this.setDataProfileName(DataTypeUtils.toString(value, null));
556
                break;
557
            case "group":
558
                this.setGroup(DataTypeUtils.toString(value, null));
559
                break;
560
            case "description":
561
                this.setDescription(DataTypeUtils.toString(value, null));
562
                break;
563
            case "label":
564
                this.setLabel(DataTypeUtils.toString(value, null));
565
                break;
566
            case "shortlabel":
567
                this.setShortLabel(DataTypeUtils.toString(value, null));
568
                break;
569
            case "expression":
570
                this.setFeatureAttributeEmulator(DataTypeUtils.toString(value, null));
571
                break;
572
            case "size":
573
                this.setSize(DataTypeUtils.toInteger(value, 50));
574
                break;
575
            case "precision":
576
                this.setPrecision(DataTypeUtils.toInteger(value, 10));
577
                break;
578
            case "scale":
579
                this.setScale(DataTypeUtils.toInteger(value, 10));
580
                break;
581
            case "roundmode":
582
                this.setRoundMode(DataTypeUtils.toInteger(value, BigDecimal.ROUND_UNNECESSARY));
583
                break;
584
            case "locale":
585
                this.setLocale(DataTypeUtils.toString(value, null));
586
                break;
587
            case "order":
588
                this.setOrder(DataTypeUtils.toInteger(value, 0));
589
                break;
590
            case "fk":
591
            case "foreingkey":
592
                this.getForeingKey().setForeingKey(DataTypeUtils.toBoolean(value, false));
593
                break;
594
            case "fk_code":
595
            case "foreingkey_code":
596
            case "foreingkey.code":
597
                this.getForeingKey().setCodeName(DataTypeUtils.toString(value, ""));
598
                break;
599
            case "fk_label":
600
            case "foreingkey_label":
601
            case "foreingkey.label":
602
                this.getForeingKey().setLabelFormula(DataTypeUtils.toString(value, ""));
603
                break;
604
            case "fk_closed":
605
            case "fk_closedlist":
606
            case "fk.closedlist":
607
            case "foreingkey_closedlist":
608
            case "foreingkey.closedlist":
609
                this.getForeingKey().setClosedList(DataTypeUtils.toBoolean(value, false));
610
                break;
611
            case "fk_table":
612
            case "foreingkey_table":
613
            case "foreingkey.table":
614
                this.getForeingKey().setTableName(DataTypeUtils.toString(value, null));
615
                break;
616
            case "interval":
617
                this.setInterval(DataTypeUtils.toInterval(value, null));
618
                break;
619
            case "geomtype":
620
            case "geometrytype":
621
                this.setGeometryType(DataTypeUtils.toString(value, null));
622
                break;
623
            case "srs":
624
                this.setSRS(DataTypeUtils.toString(value, null));
625
                break;
626
            case "relation":
627
                this.setRelationType(toRelation(value));
628
                break;
629
            case "name":
630
                this.setName(DataTypeUtils.toString(value, null));
631
                break;
632
            case "type":
633
            case "datatype":
634
                ss = DataTypeUtils.toString(value, null);
635
                if (!StringUtils.isBlank(ss)) {
636
                    this.setDataType(ToolsLocator.getDataTypesManager().getType(ss));
637
                }
638
                break;
639
            case "defaultvalue":
640
                this.setDefaultValue(value);
641
                break;
642
            case "availablevaluesfilter":
643
                this.setAvailableValuesFilter(DataTypeUtils.toString(value, null));
644
                break;
645
            default:
646
                throw new IllegalArgumentException("Name attribute '" + name + "' not valid.");
647
        }
648
        return this;
649
    }
650

    
651
    private int toRelation(Object value) {
652
        if (value == null) {
653
            return DynField.RELATION_TYPE_NONE;
654
        }
655
        Integer x = (Integer) DataTypeUtils.coerce(DataTypes.INT, value, null);
656
        if (x != null) {
657
            return x;
658
        }
659
        try {
660
            String s = value.toString().toUpperCase();
661
            switch (s) {
662
                case "NONE":
663
                default:
664
                    return DynField.RELATION_TYPE_NONE;
665
                case "IDENTITY":
666
                    return DynField.RELATION_TYPE_IDENTITY;
667
                case "COLLABORATION":
668
                    return DynField.RELATION_TYPE_COLLABORATION;
669
                case "COMPOSITION":
670
                    return DynField.RELATION_TYPE_COMPOSITION;
671
                case "AGGREGATE":
672
                    return DynField.RELATION_TYPE_AGGREGATE;
673
            }
674
        } catch (Exception ex) {
675
            return DynField.RELATION_TYPE_NONE;
676
        }
677
    }
678

    
679
    @Override
680
    public EditableFeatureAttributeDescriptor setDisplaySize(int size) {
681
        this.displaySize = size;
682
        return this;
683
    }
684

    
685
    @Override
686
    public EditableFeatureAttributeDescriptor setAvailableValuesFilter(Expression filter) {
687
        super.setAvailableValuesFilter(filter);
688
        return this;
689
    }
690

    
691
    @Override
692
    public EditableFeatureAttributeDescriptor setAvailableValuesFilter(String filter) {
693
        super.setAvailableValuesFilter(filter);
694
        return this;
695
    }
696

    
697
    private static class TheJsonSerializer implements JsonManager.JsonSerializer {
698

    
699
        public TheJsonSerializer() {
700
        }
701

    
702
        @Override
703
        public Class getObjectClass() {
704
            return DefaultEditableFeatureAttributeDescriptor.class;
705
        }
706

    
707
        @Override
708
        public Object toObject(JsonObject json) {
709
            DefaultFeatureAttributeDescriptor o = new DefaultFeatureAttributeDescriptor();
710
            o.fromJson(json);
711
            return o;
712
        }
713

    
714
        @Override
715
        public JsonObjectBuilder toJsonBuilder(Object value) {
716
            return ((SupportToJson) value).toJsonBuilder();
717
        }
718

    
719
    }
720

    
721
    @Override
722
    public EditableFeatureAttributeDescriptor setForeingkey(
723
            boolean isForeingkey,
724
            boolean isClosedList,
725
            String tableName,
726
            String codeName,
727
            String labelFormula
728
    ) {
729
        if (isForeingkey) {
730
            EditableForeingKey fk = this.getForeingKey();
731
            fk.setForeingKey(isForeingkey);
732
            fk.setClosedList(isClosedList);
733
            fk.setTableName(tableName);
734
            fk.setCodeName(codeName);
735
            fk.setLabelFormula(labelFormula);
736
        } else {
737
            this.foreingKey = null;
738
        }
739
        return this;
740
    }
741

    
742
    @Override
743
    public EditableFeatureAttributeDescriptor setTag(String name, Object value) {
744
        this.getTags().set(name, value);
745
        return this;
746
    }
747

    
748
    public static void selfRegister() {
749
        Json.registerSerializer(new TheJsonSerializer());
750
    }
751

    
752
    @Override
753
    public EditableFeatureAttributeDescriptor setDefaultFormat(String format) {
754
        this.defaultFormat = format;
755
        return this;
756
    }
757

    
758
    
759
}