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

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

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

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

    
56
public class DefaultEditableFeatureAttributeDescriptor extends
57
    DefaultFeatureAttributeDescriptor implements
58
    EditableFeatureAttributeDescriptor {
59
    
60
    private static Logger logger = LoggerFactory.getLogger(
61
        DefaultEditableFeatureAttributeDescriptor.class);
62

    
63
    private final DefaultFeatureAttributeDescriptor source;
64
    private boolean hasStrongChanges;
65
    private String originalName = null;
66

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

    
81
    public DefaultEditableFeatureAttributeDescriptor(FeatureType type, boolean strongChanges) {
82
        super(type);
83
        this.source = null;
84
        hasStrongChanges = strongChanges;
85
    }
86

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

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

    
109
        if (ex.size() > 0) {
110
            throw ex;
111
        }
112
    }
113

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

    
129
    public EditableFeatureAttributeDescriptor setDataType(int type) {
130
        updateStrongChanges(this.dataType, type);
131
        this.dataType = ToolsLocator.getDataTypesManager().get(type);
132
        return this;
133
    }
134

    
135
    public EditableFeatureAttributeDescriptor setDefaultValue(
136
        Object defaultValue) {
137
        updateStrongChanges(this.defaultValue, defaultValue);
138
        this.defaultValue = defaultValue;
139
        return this;
140
    }
141

    
142
    public EditableFeatureAttributeDescriptor setEvaluator(Evaluator evaluator) {
143
        updateStrongChanges(this.evaluator, evaluator);
144
        this.evaluator = evaluator;
145
        return this;
146
    }
147

    
148
    @Override
149
    public EditableFeatureAttributeDescriptor setFeatureAttributeEmulator(FeatureAttributeEmulator featureAttributeEmulator) {
150
        this.featureAttributeEmulator = featureAttributeEmulator;
151
        return this;
152
    }
153
    
154
    @Override
155
    public EditableFeatureAttributeDescriptor setFeatureAttributeEmulator(Expression expression) {
156
        if( ExpressionUtils.isPhraseEmpty(expression) ) {
157
            this.setFeatureAttributeEmulator((FeatureAttributeEmulator)null);
158
            return this;
159
        } 
160
        FeatureAttributeEmulatorExpression emulator = DALLocator.getDataManager().createFeatureAttributeEmulatorExpression(
161
                this.getFeatureType(),
162
                expression
163
        );
164
        if( !emulator.isValid() ) {
165
            this.setFeatureAttributeEmulator(emulator);
166
            throw new IllegalArgumentException("Can't create calculated field '"+this.getName()+"', "+emulator.getErrorMessage());
167
        }
168
        return this.setFeatureAttributeEmulator(emulator);
169
    }
170

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

    
190
    public EditableFeatureAttributeDescriptor setGeometrySubType(int subType) {
191
        this.geometrySubType = subType;
192
        this.geomType = null;
193
        return this;
194
    }
195

    
196
    public EditableFeatureAttributeDescriptor setGeometryType(
197
        GeometryType geometryType) {
198
        updateStrongChanges(this.geomType, geometryType);
199
        this.geomType = geometryType;
200
        this.geometryType = this.geomType.getType();
201
        this.geometrySubType = this.geomType.getSubType();
202
        return this;
203
    }
204

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

    
236
    public EditableFeatureAttributeDescriptor setIsPrimaryKey(
237
        boolean isPrimaryKey) {
238
        updateStrongChanges(this.primaryKey, primaryKey);
239
        this.primaryKey = isPrimaryKey;
240
        return this;
241
    }
242

    
243
    public EditableFeatureAttributeDescriptor setIsReadOnly(boolean isReadOnly) {
244
        updateStrongChanges(this.readOnly, readOnly);
245
        this.readOnly = isReadOnly;
246
        return this;
247
    }
248

    
249
    public EditableFeatureAttributeDescriptor setMaximumOccurrences(
250
        int maximumOccurrences) {
251
        updateStrongChanges(this.maximumOccurrences, maximumOccurrences);
252
        this.maximumOccurrences = maximumOccurrences;
253
        return this;
254
    }
255

    
256
    public EditableFeatureAttributeDescriptor setMinimumOccurrences(
257
        int minimumOccurrences) {
258
        updateStrongChanges(this.minimumOccurrences, minimumOccurrences);
259
        this.minimumOccurrences = minimumOccurrences;
260
        return this;
261
    }
262

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

    
285
    public EditableFeatureAttributeDescriptor setObjectClass(Class theClass) {
286
        updateStrongChanges(this.objectClass, theClass);
287
        this.objectClass = theClass;
288
        return this;
289
    }
290

    
291
    public EditableFeatureAttributeDescriptor setPrecision(int precision) {
292
        updateStrongChanges(this.precision, precision);
293
        this.precision = precision;
294
        return this;
295
    }
296

    
297
    @Override
298
    public EditableFeatureAttributeDescriptor setSRS(IProjection SRS) {
299
        updateStrongChanges(this.SRS, SRS);
300
        this.SRS = SRS;
301
        return this;
302
    }
303

    
304
    @Override
305
    public EditableFeatureAttributeDescriptor setSRS(String SRS) {
306
        if( StringUtils.isBlank(SRS) ) {
307
            this.setSRS((IProjection)null);
308
            return this;
309
        }
310
        IProjection proj = CRSFactory.getCRS(SRS);
311
        this.setSRS(proj);
312
        return this;
313
    }
314
    
315
    public EditableFeatureAttributeDescriptor setInterval(Interval interval) {
316
        updateStrongChanges(this.getInterval(), interval);
317
        super.setInterval(interval);
318
        return this;
319
    }
320

    
321
    public EditableFeatureAttributeDescriptor setSize(int size) {
322
        updateStrongChanges(this.size, size);
323
        this.size = size;
324
        return this;
325
    }
326

    
327
    public boolean hasStrongChanges() {
328
        return hasStrongChanges;
329
    }
330

    
331
    public EditableFeatureAttributeDescriptor setAdditionalInfo(
332
        String infoName, Object value) {
333
        if (this.additionalInfo == null) {
334
            this.additionalInfo = new HashMap();
335
        }
336
        this.additionalInfo.put(infoName, value);
337
        return this;
338
    }
339

    
340
    public EditableFeatureAttributeDescriptor setIsAutomatic(boolean isAutomatic) {
341
        this.isAutomatic = isAutomatic;
342
        if( isAutomatic ) {
343
            this.setHidden(true);
344
        }
345
        return this;
346
    }
347
    
348
    public EditableFeatureAttributeDescriptor setIsTime(boolean isTime) {
349
        updateStrongChanges(this.isTime, isTime);
350
        this.isTime = isTime;
351
        return this;
352
    }
353

    
354
    public EditableFeatureAttributeDescriptor setDateFormat(DateFormat dateFormat) {
355
        this.dateFormat = dateFormat;
356
        return this;
357
    }
358

    
359
    public EditableFeatureAttributeDescriptor setIsIndexed(boolean isIndexed) {
360
        updateStrongChanges(this.indexed, isIndexed);
361
        this.indexed = isIndexed;
362
        return this;
363
    }
364
    
365
    public EditableFeatureAttributeDescriptor setAllowIndexDuplicateds(boolean allowDuplicateds) {
366
        updateStrongChanges(this.allowIndexDuplicateds, allowDuplicateds);
367
        this.allowIndexDuplicateds = allowDuplicateds;
368
        return this;
369
    }
370

    
371
    public EditableFeatureAttributeDescriptor setIsIndexAscending(boolean ascending) {
372
        updateStrongChanges(this.isIndexAscending, ascending);
373
        this.isIndexAscending = ascending;
374
        return this;
375
    }
376

    
377
    @Override
378
    public EditableFeatureAttributeDescriptor setDataProfileName(String dataProfile) {
379
        super.setDataProfileName(dataProfile);
380
        return this;
381
    }
382

    
383
    private void updateStrongChanges(int previous, int newvalue) {
384
        if( isComputed() ) {
385
            return;
386
        }
387
        if( previous == newvalue ) {
388
            return;
389
        }
390
        this.hasStrongChanges = true;
391
    }
392

    
393
    private void updateStrongChanges(DataType previous, int newvalue) {
394
        if( isComputed() ) {
395
            return;
396
        }
397
        if( previous!=null ) {
398
            if( previous.getType() == newvalue ) {
399
                return;
400
            }
401
        }
402
        this.hasStrongChanges = true;
403
    }
404

    
405
    private void updateStrongChanges(boolean previous, boolean newvalue) {
406
        if( isComputed() ) {
407
            return;
408
        }
409
        if( previous == newvalue ) {
410
            return;
411
        }
412
        this.hasStrongChanges = true;
413
    }
414

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