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

History | View | Annotate | Download (21.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.math.BigDecimal;
27
import java.text.DateFormat;
28
import java.util.HashMap;
29
import java.util.Locale;
30
import java.util.Objects;
31
import org.apache.commons.lang3.StringUtils;
32

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

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

    
59
public class DefaultEditableFeatureAttributeDescriptor extends
60
    DefaultFeatureAttributeDescriptor implements
61
    EditableFeatureAttributeDescriptor {
62
    
63
    private static Logger logger = LoggerFactory.getLogger(
64
        DefaultEditableFeatureAttributeDescriptor.class);
65

    
66
    private final DefaultFeatureAttributeDescriptor source;
67
    private boolean hasStrongChanges;
68
    private String originalName = null;
69

    
70
    protected DefaultEditableFeatureAttributeDescriptor(
71
        DefaultFeatureAttributeDescriptor other) {
72
        super(other);
73
        if (other instanceof DefaultEditableFeatureAttributeDescriptor) {
74
            DefaultEditableFeatureAttributeDescriptor other_edi =
75
                (DefaultEditableFeatureAttributeDescriptor) other;
76
            originalName = other_edi.getOriginalName();
77
            this.source = other_edi.getSource();
78
        } else {
79
            this.source = other;
80
        }
81
        hasStrongChanges = false;
82
    }
83

    
84
    public DefaultEditableFeatureAttributeDescriptor(FeatureType type, boolean strongChanges) {
85
        super(type);
86
        this.source = null;
87
        hasStrongChanges = strongChanges;
88
    }
89

    
90
    public DefaultFeatureAttributeDescriptor getSource() {
91
        return this.source;
92
    }
93
    
94
    public void fixAll() {
95
        super.fixAll();
96
    }
97
    
98
    public void checkIntegrity() throws AttributeFeatureTypeIntegrityException {
99
        AttributeFeatureTypeIntegrityException ex =
100
            new AttributeFeatureTypeIntegrityException(getName());
101
        if (this.size < 0) {
102
            ex.add(new AttributeFeatureTypeSizeException(this.size));
103
        }
104

    
105
        if( this.dataType.isObject() && this.objectClass == null ) {
106
            logger.warn("Incorrect data type object, objectClass is null.");
107
            ex.add(new AttributeFeatureTypeIntegrityException(this.name));
108
        }
109
        
110
        // TODO: Add other integrity checks...
111

    
112
        if (ex.size() > 0) {
113
            throw ex;
114
        }
115
    }
116

    
117
    public EditableFeatureAttributeDescriptor setAllowNull(boolean allowNull) {
118
        updateStrongChanges(this.allowNull, allowNull);
119
        this.allowNull = allowNull;
120
        return this;
121
    }
122
    
123
    @Override
124
    public EditableForeingKey getForeingKey() {
125
        if( this.foreingKey==null ) {
126
            this.foreingKey = new DefaultForeingKey();
127
            this.foreingKey.setDescriptor(this);
128
        }
129
        return this.foreingKey;
130
    }
131

    
132
    public EditableFeatureAttributeDescriptor setDataType(DataType dataType) {
133
        updateStrongChanges(this.dataType, dataType);
134
        this.dataType = dataType;
135
        return this;
136
    }
137
    
138
    public EditableFeatureAttributeDescriptor setDataType(int type) {
139
        updateStrongChanges(this.dataType, type);
140
        this.dataType = ToolsLocator.getDataTypesManager().get(type);
141
        return this;
142
    }
143

    
144
    public EditableFeatureAttributeDescriptor setDefaultValue(
145
        Object defaultValue) {
146
        updateStrongChanges(this.defaultValue, defaultValue);
147
        this.defaultValue = defaultValue;
148
        return this;
149
    }
150

    
151
    public EditableFeatureAttributeDescriptor setEvaluator(Evaluator evaluator) {
152
        updateStrongChanges(this.evaluator, evaluator);
153
        this.evaluator = evaluator;
154
        return this;
155
    }
156

    
157
    @Override
158
    public EditableFeatureAttributeDescriptor setFeatureAttributeEmulator(FeatureAttributeEmulator featureAttributeEmulator) {
159
        this.featureAttributeEmulator = featureAttributeEmulator;
160
        return this;
161
    }
162
    
163
    @Override
164
    public EditableFeatureAttributeDescriptor setFeatureAttributeEmulator(Expression expression) {
165
        if( ExpressionUtils.isPhraseEmpty(expression) ) {
166
            this.setFeatureAttributeEmulator((FeatureAttributeEmulator)null);
167
            return this;
168
        } 
169
        FeatureAttributeEmulatorExpression emulator = DALLocator.getDataManager().createFeatureAttributeEmulatorExpression(
170
                this.getFeatureType(),
171
                expression
172
        );
173
        return this.setFeatureAttributeEmulator(emulator);
174
    }
175

    
176
    @Override
177
    public EditableFeatureAttributeDescriptor setFeatureAttributeEmulator(String expression) {
178
        if( StringUtils.isBlank(expression) ) {
179
            this.setFeatureAttributeEmulator((FeatureAttributeEmulator)null);
180
            return this;
181
        } 
182
        return this.setFeatureAttributeEmulator(ExpressionUtils.createExpression(expression));
183
    }
184
        
185
    @Override
186
    public EditableFeatureAttributeDescriptor setGeometryType(int type) {
187
        this.geometryType = type;
188
        if( this.geometrySubType == Geometry.SUBTYPES.UNKNOWN ) {
189
            this.geometrySubType = Geometry.SUBTYPES.GEOM2D;
190
        }
191
        this.geomType = null;
192
        return this;
193
    }
194

    
195
    public EditableFeatureAttributeDescriptor setGeometrySubType(int subType) {
196
        this.geometrySubType = subType;
197
        this.geomType = null;
198
        return this;
199
    }
200

    
201
    public EditableFeatureAttributeDescriptor setGeometryType(
202
        GeometryType geometryType) {
203
        updateStrongChanges(this.geomType, geometryType);
204
        this.geomType = geometryType;
205
        this.geometryType = this.geomType.getType();
206
        this.geometrySubType = this.geomType.getSubType();
207
        return this;
208
    }
209

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

    
241
    public EditableFeatureAttributeDescriptor setIsPrimaryKey(
242
        boolean isPrimaryKey) {
243
        updateStrongChanges(this.primaryKey, primaryKey);
244
        this.primaryKey = isPrimaryKey;
245
        return this;
246
    }
247

    
248
    public EditableFeatureAttributeDescriptor setIsReadOnly(boolean isReadOnly) {
249
        updateStrongChanges(this.readOnly, readOnly);
250
        this.readOnly = isReadOnly;
251
        return this;
252
    }
253

    
254
    public EditableFeatureAttributeDescriptor setMaximumOccurrences(
255
        int maximumOccurrences) {
256
        updateStrongChanges(this.maximumOccurrences, maximumOccurrences);
257
        this.maximumOccurrences = maximumOccurrences;
258
        return this;
259
    }
260

    
261
    public EditableFeatureAttributeDescriptor setMinimumOccurrences(
262
        int minimumOccurrences) {
263
        updateStrongChanges(this.minimumOccurrences, minimumOccurrences);
264
        this.minimumOccurrences = minimumOccurrences;
265
        return this;
266
    }
267

    
268
    @Override
269
    public EditableFeatureAttributeDescriptor setName(String name) {
270
        if( StringUtils.equalsIgnoreCase(this.name, name) ) {
271
            return this;
272
        }
273
        if (originalName == null) {
274
            originalName = this.name;
275
            if (!isComputed()) {
276
                hasStrongChanges = true;
277
            }
278
        }
279
        super.setName(name);
280
        if (!isComputed()) {
281
            hasStrongChanges = true;
282
        }
283
        return this;
284
    }
285
    
286
    public String getOriginalName() {
287
        return originalName;
288
    }
289

    
290
    public EditableFeatureAttributeDescriptor setObjectClass(Class theClass) {
291
        updateStrongChanges(this.objectClass, theClass);
292
        this.objectClass = theClass;
293
        return this;
294
    }
295

    
296
    @Override
297
    public EditableFeatureAttributeDescriptor setPrecision(int precision) {
298
        updateStrongChanges(this.precision, precision);
299
        this.precision = precision;
300
        return this;
301
    }
302

    
303
    @Override
304
    public EditableFeatureAttributeDescriptor setScale(int scale) {
305
        updateStrongChanges(this.scale, scale);
306
        this.scale = scale;
307
        this.coerceContext = null;
308
        this.mathContext = null;
309
        return this;
310
    }
311

    
312
    @Override
313
    public EditableFeatureAttributeDescriptor setSRS(IProjection SRS) {
314
        updateStrongChanges(this.SRS, SRS);
315
        this.SRS = SRS;
316
        return this;
317
    }
318

    
319
    @Override
320
    public EditableFeatureAttributeDescriptor setSRS(String SRS) {
321
        if( StringUtils.isBlank(SRS) ) {
322
            this.setSRS((IProjection)null);
323
            return this;
324
        }
325
        IProjection proj = CRSFactory.getCRS(SRS);
326
        this.setSRS(proj);
327
        return this;
328
    }
329
    
330
    public EditableFeatureAttributeDescriptor setInterval(Interval interval) {
331
        updateStrongChanges(this.getInterval(), interval);
332
        super.setInterval(interval);
333
        return this;
334
    }
335

    
336
    public EditableFeatureAttributeDescriptor setSize(int size) {
337
        updateStrongChanges(this.size, size);
338
        this.size = size;
339
        return this;
340
    }
341

    
342
    public boolean hasStrongChanges() {
343
        return hasStrongChanges;
344
    }
345

    
346
    public EditableFeatureAttributeDescriptor setAdditionalInfo(
347
        String infoName, Object value) {
348
        if (this.additionalInfo == null) {
349
            this.additionalInfo = new HashMap();
350
        }
351
        this.additionalInfo.put(infoName, value);
352
        return this;
353
    }
354

    
355
    public EditableFeatureAttributeDescriptor setIsAutomatic(boolean isAutomatic) {
356
        this.isAutomatic = isAutomatic;
357
        if( isAutomatic ) {
358
            this.setReadOnly(true);
359
        }
360
        return this;
361
    }
362
    
363
    public EditableFeatureAttributeDescriptor setIsTime(boolean isTime) {
364
        updateStrongChanges(this.isTime, isTime);
365
        this.isTime = isTime;
366
        return this;
367
    }
368

    
369
    public EditableFeatureAttributeDescriptor setDateFormat(DateFormat dateFormat) {
370
        this.dateFormat = dateFormat;
371
        return this;
372
    }
373

    
374
    public EditableFeatureAttributeDescriptor setIsIndexed(boolean isIndexed) {
375
        updateStrongChanges(this.indexed, isIndexed);
376
        this.indexed = isIndexed;
377
        return this;
378
    }
379
    
380
    public EditableFeatureAttributeDescriptor setAllowIndexDuplicateds(boolean allowDuplicateds) {
381
        updateStrongChanges(this.allowIndexDuplicateds, allowDuplicateds);
382
        this.allowIndexDuplicateds = allowDuplicateds;
383
        return this;
384
    }
385

    
386
    public EditableFeatureAttributeDescriptor setIsIndexAscending(boolean ascending) {
387
        updateStrongChanges(this.isIndexAscending, ascending);
388
        this.isIndexAscending = ascending;
389
        return this;
390
    }
391

    
392
    @Override
393
    public EditableFeatureAttributeDescriptor setDataProfileName(String dataProfile) {
394
        super.setDataProfileName(dataProfile);
395
        return this;
396
    }
397

    
398
    private void updateStrongChanges(int previous, int newvalue) {
399
        if( isComputed() ) {
400
            return;
401
        }
402
        if( previous == newvalue ) {
403
            return;
404
        }
405
        this.hasStrongChanges = true;
406
    }
407

    
408
    private void updateStrongChanges(DataType previous, int newvalue) {
409
        if( isComputed() ) {
410
            return;
411
        }
412
        if( previous!=null ) {
413
            if( previous.getType() == newvalue ) {
414
                return;
415
            }
416
        }
417
        this.hasStrongChanges = true;
418
    }
419

    
420
    private void updateStrongChanges(boolean previous, boolean newvalue) {
421
        if( isComputed() ) {
422
            return;
423
        }
424
        if( previous == newvalue ) {
425
            return;
426
        }
427
        this.hasStrongChanges = true;
428
    }
429

    
430
    private void updateStrongChanges(Object previous, Object newvalue) {
431
        if( isComputed() ) {
432
            return;
433
        }
434
        if( Objects.equals(newvalue, previous) ) {
435
            return;
436
        }
437
        this.hasStrongChanges = true;
438
    }
439

    
440
    @Override
441
    public EditableFeatureAttributeDescriptor setLocale(Locale locale) {
442
      if( locale == null ) {
443
        this.locale = Locale.ENGLISH;
444
      } else {
445
        this.locale = locale;
446
      }
447
      this.coerceContext = null;
448
      this.mathContext = null;
449
      return this;
450
    }
451

    
452
    public EditableFeatureAttributeDescriptor setLocale(String locale) {
453
      Locale l;
454
      try {
455
        String s = DataTypeUtils.toString(locale, null);
456
        if( StringUtils.isBlank(s) ) {
457
          return this.setLocale((Locale)null);
458
        }
459
        l = new Locale(s);
460
        return this.setLocale(l);
461
      } catch(Exception ex) {
462
          return this.setLocale((Locale)null);
463
      }
464
    }
465
    
466
    @Override
467
    public EditableFeatureAttributeDescriptor setRoundMode(int roundMode) {
468
      switch(roundMode) {
469
        case BigDecimal.ROUND_UP:
470
        case BigDecimal.ROUND_DOWN:
471
        case BigDecimal.ROUND_CEILING:
472
        case BigDecimal.ROUND_FLOOR:
473
        case BigDecimal.ROUND_HALF_UP:
474
        case BigDecimal.ROUND_HALF_DOWN:
475
        case BigDecimal.ROUND_HALF_EVEN:
476
        case BigDecimal.ROUND_UNNECESSARY:
477
          this.roundMode = roundMode;
478
          this.coerceContext = null;
479
          this.mathContext = null;
480
          break;
481
        default:
482
          throw new IllegalArgumentException("round mode '"+roundMode+"' not valid.");
483
      }
484
      return this;
485
    }
486

    
487
    @Override
488
    public EditableFeatureAttributeDescriptor set(String name, Object value) {
489
        if( StringUtils.isBlank(name)) {
490
            throw new IllegalArgumentException("Name can't be empty");
491
        }
492
        String ss; 
493
        switch(name.trim().toLowerCase()) {
494
            case "isreadonly":
495
            case "readonly":
496
                this.setIsReadOnly(DataTypeUtils.toBoolean(value, false));
497
                break;
498
            case "hidden":
499
                this.setHidden(DataTypeUtils.toBoolean(value, false));
500
                break;
501
            case "allownull":
502
                this.setAllowNull(DataTypeUtils.toBoolean(value, false));
503
                break;
504
            case "pk":
505
            case "ispk":
506
            case "primarykey":
507
            case "isprimarykey":
508
                this.setIsPrimaryKey(DataTypeUtils.toBoolean(value, false));
509
                break;
510
            case "isautomatic":
511
            case "automatic":
512
                this.setIsAutomatic(DataTypeUtils.toBoolean(value, false));
513
                break;
514
            case "time":
515
            case "istime":
516
                this.setIsTime(DataTypeUtils.toBoolean(value, false));
517
                break;
518
            case "profile":
519
                this.setDataProfileName(DataTypeUtils.toString(value, null));
520
                break;
521
            case "group":
522
                this.setGroup(DataTypeUtils.toString(value, null));
523
                break;
524
            case "description":
525
                this.setDescription(DataTypeUtils.toString(value, null));
526
                break;
527
            case "label":
528
                this.setLabel(DataTypeUtils.toString(value, null));
529
                break;
530
            case "shortlabel":
531
                this.setShortLabel(DataTypeUtils.toString(value, null));
532
                break;
533
            case "expression":
534
                this.setFeatureAttributeEmulator(DataTypeUtils.toString(value, null));
535
                break;
536
            case "size":
537
                this.setSize(DataTypeUtils.toInteger(value, 50));
538
                break;
539
            case "precision":
540
                this.setPrecision(DataTypeUtils.toInteger(value, 10));
541
                break;
542
            case "scale":
543
                this.setScale(DataTypeUtils.toInteger(value, 10));
544
                break;
545
            case "roundmode":
546
              this.setRoundMode(DataTypeUtils.toInteger(value, BigDecimal.ROUND_UNNECESSARY));
547
              break;
548
            case "locale":
549
              this.setLocale(DataTypeUtils.toString(value, null));
550
              break;
551
            case "order":
552
                this.setOrder(DataTypeUtils.toInteger(value, 0));
553
                break;
554
            case "foreingkey":
555
                this.getForeingKey().setForeingKey(DataTypeUtils.toBoolean(value, false));
556
                break;
557
            case "foreingkey.code":
558
                this.getForeingKey().setCodeName(DataTypeUtils.toString(value, ""));
559
                break;
560
            case "foreingkey.label":
561
                this.getForeingKey().setLabelFormula(DataTypeUtils.toString(value, ""));
562
                break;
563
            case "foreingkey.closedlist":
564
                this.getForeingKey().setClosedList(DataTypeUtils.toBoolean(value, false));
565
                break;
566
            case "foreingkey.table":
567
                this.getForeingKey().setTableName(DataTypeUtils.toString(value, null));
568
                break;
569
            case "interval":
570
                this.setInterval(DataTypeUtils.toInterval(value, null));
571
                break;
572
            case "geomtype":
573
            case "geometrytype":
574
                this.setGeometryType(DataTypeUtils.toString(value, null));
575
                break;
576
            case "srs":
577
                this.setSRS(DataTypeUtils.toProjection(value, null));
578
                break;
579
            case "relation":
580
                this.setRelationType(toRelation(value));
581
                break;
582
            case "name":
583
                this.setName(DataTypeUtils.toString(value, null));
584
                break;
585
            case "type":
586
            case "datatype":
587
                ss = DataTypeUtils.toString(value, null);
588
                if( !StringUtils.isBlank(ss) ) {
589
                    this.setDataType(ToolsLocator.getDataTypesManager().getType(ss));
590
                }
591
                break;
592
            default:
593
                throw new IllegalArgumentException("Name attribute '"+name+"' not valid.");
594
        }     
595
        return this;
596
    }
597
    
598
    private int toRelation(Object value) {
599
      if (value == null) {
600
        return DynField.RELATION_TYPE_NONE;
601
      }
602
      Integer x = (Integer) DataTypeUtils.coerce(DataTypes.INT,value, null);
603
      if (x != null) {
604
        return x;
605
      }
606
      try {
607
        String s = value.toString().toUpperCase();
608
        switch (s) {
609
          case "NONE":
610
          default:
611
            return DynField.RELATION_TYPE_NONE;
612
          case "IDENTITY":
613
            return DynField.RELATION_TYPE_IDENTITY;
614
          case "COLLABORATION":
615
            return DynField.RELATION_TYPE_COLLABORATION;
616
          case "COMPOSITION":
617
            return DynField.RELATION_TYPE_COMPOSITION;
618
          case "AGGREGATE":
619
            return DynField.RELATION_TYPE_AGGREGATE;
620
        }
621
      } catch(Exception ex) {
622
        return DynField.RELATION_TYPE_NONE;
623
      }
624
    }
625

    
626
  @Override
627
  public EditableFeatureAttributeDescriptor setDisplaySize(int size) {
628
    this.displaySize = size;
629
    return this;
630
  }
631
}