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

History | View | Annotate | Download (13.7 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.FeatureAttributeEmulator;
43
import org.gvsig.fmap.dal.feature.FeatureType;
44
import org.gvsig.fmap.dal.feature.exception.AttributeFeatureTypeIntegrityException;
45
import org.gvsig.fmap.dal.feature.exception.AttributeFeatureTypeSizeException;
46
import org.gvsig.fmap.geom.Geometry;
47
import org.gvsig.fmap.geom.GeometryUtils;
48
import org.gvsig.fmap.geom.type.GeometryType;
49
import org.gvsig.timesupport.Interval;
50
import org.gvsig.tools.ToolsLocator;
51
import org.gvsig.tools.dataTypes.DataType;
52
import org.gvsig.tools.evaluator.Evaluator;
53

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

    
61
    private final DefaultFeatureAttributeDescriptor source;
62
    private boolean hasStrongChanges;
63
    private String originalName = null;
64

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

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

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

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

    
107
        if (ex.size() > 0) {
108
            throw ex;
109
        }
110
    }
111

    
112
    public EditableFeatureAttributeDescriptor setAllowNull(boolean allowNull) {
113
        updateStrongChanges(this.allowNull, allowNull);
114
        this.allowNull = allowNull;
115
        return this;
116
    }
117

    
118
    public EditableFeatureAttributeDescriptor setDataType(int type) {
119
        updateStrongChanges(this.dataType, type);
120
        this.dataType = ToolsLocator.getDataTypesManager().get(type);
121
        return this;
122
    }
123

    
124
    public EditableFeatureAttributeDescriptor setDefaultValue(
125
        Object defaultValue) {
126
        updateStrongChanges(this.defaultValue, defaultValue);
127
        this.defaultValue = defaultValue;
128
        return this;
129
    }
130

    
131
    public EditableFeatureAttributeDescriptor setEvaluator(Evaluator evaluator) {
132
        updateStrongChanges(this.evaluator, evaluator);
133
        this.evaluator = evaluator;
134
        return this;
135
    }
136

    
137
    @Override
138
    public EditableFeatureAttributeDescriptor setFeatureAttributeEmulator(FeatureAttributeEmulator featureAttributeEmulator) {
139
        this.featureAttributeEmulator = featureAttributeEmulator;
140
        return this;
141
    }
142
    
143
    @Override
144
    public EditableFeatureAttributeDescriptor setFeatureAttributeEmulator(Expression expression) {
145
        if( ExpressionUtils.isPhraseEmpty(expression) ) {
146
            this.setFeatureAttributeEmulator((FeatureAttributeEmulator)null);
147
            return this;
148
        } 
149
        FeatureAttributeEmulatorExpression emulator = DALLocator.getDataManager().createFeatureAttributeEmulatorExpression(
150
                this.getFeatureType(),
151
                expression
152
        );
153
        if( !emulator.isValid() ) {
154
            this.setFeatureAttributeEmulator(emulator);
155
            throw new IllegalArgumentException("Can't create calculated field '"+this.getName()+"', "+emulator.getErrorMessage());
156
        }
157
        return this.setFeatureAttributeEmulator(emulator);
158
    }
159

    
160
    @Override
161
    public EditableFeatureAttributeDescriptor setFeatureAttributeEmulator(String expression) {
162
        if( StringUtils.isBlank(expression) ) {
163
            this.setFeatureAttributeEmulator((FeatureAttributeEmulator)null);
164
            return this;
165
        } 
166
        return this.setFeatureAttributeEmulator(ExpressionUtils.createExpression(expression));
167
    }
168
        
169
    @Override
170
    public EditableFeatureAttributeDescriptor setGeometryType(int type) {
171
        this.geometryType = type;
172
        if( this.geometrySubType == Geometry.SUBTYPES.UNKNOWN ) {
173
            this.geometrySubType = Geometry.SUBTYPES.GEOM2D;
174
        }
175
        this.geomType = null;
176
        return this;
177
    }
178

    
179
    public EditableFeatureAttributeDescriptor setGeometrySubType(int subType) {
180
        this.geometrySubType = subType;
181
        this.geomType = null;
182
        return this;
183
    }
184

    
185
    public EditableFeatureAttributeDescriptor setGeometryType(
186
        GeometryType geometryType) {
187
        updateStrongChanges(this.geomType, geometryType);
188
        this.geomType = geometryType;
189
        this.geometryType = this.geomType.getType();
190
        this.geometrySubType = this.geomType.getSubType();
191
        return this;
192
    }
193

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

    
225
    public EditableFeatureAttributeDescriptor setIsPrimaryKey(
226
        boolean isPrimaryKey) {
227
        updateStrongChanges(this.primaryKey, primaryKey);
228
        this.primaryKey = isPrimaryKey;
229
        return this;
230
    }
231

    
232
    public EditableFeatureAttributeDescriptor setIsReadOnly(boolean isReadOnly) {
233
        updateStrongChanges(this.readOnly, readOnly);
234
        this.readOnly = isReadOnly;
235
        return this;
236
    }
237

    
238
    public EditableFeatureAttributeDescriptor setMaximumOccurrences(
239
        int maximumOccurrences) {
240
        updateStrongChanges(this.maximumOccurrences, maximumOccurrences);
241
        this.maximumOccurrences = maximumOccurrences;
242
        return this;
243
    }
244

    
245
    public EditableFeatureAttributeDescriptor setMinimumOccurrences(
246
        int minimumOccurrences) {
247
        updateStrongChanges(this.minimumOccurrences, minimumOccurrences);
248
        this.minimumOccurrences = minimumOccurrences;
249
        return this;
250
    }
251

    
252
    @Override
253
    public EditableFeatureAttributeDescriptor setName(String name) {
254
        if( StringUtils.equalsIgnoreCase(this.name, name) ) {
255
            return this;
256
        }
257
        if (originalName == null) {
258
            originalName = this.name;
259
            if (!isComputed()) {
260
                hasStrongChanges = true;
261
            }
262
        }
263
        super.setName(name);
264
        if (!isComputed()) {
265
            hasStrongChanges = true;
266
        }
267
        return this;
268
    }
269
    
270
    public String getOriginalName() {
271
        return originalName;
272
    }
273

    
274
    public EditableFeatureAttributeDescriptor setObjectClass(Class theClass) {
275
        updateStrongChanges(this.objectClass, theClass);
276
        this.objectClass = theClass;
277
        return this;
278
    }
279

    
280
    public EditableFeatureAttributeDescriptor setPrecision(int precision) {
281
        updateStrongChanges(this.precision, precision);
282
        this.precision = precision;
283
        return this;
284
    }
285

    
286
    @Override
287
    public EditableFeatureAttributeDescriptor setSRS(IProjection SRS) {
288
        updateStrongChanges(this.SRS, SRS);
289
        this.SRS = SRS;
290
        return this;
291
    }
292

    
293
    @Override
294
    public EditableFeatureAttributeDescriptor setSRS(String SRS) {
295
        if( StringUtils.isBlank(SRS) ) {
296
            this.setSRS((IProjection)null);
297
            return this;
298
        }
299
        IProjection proj = CRSFactory.getCRS(SRS);
300
        this.setSRS(proj);
301
        return this;
302
    }
303
    
304
    public EditableFeatureAttributeDescriptor setInterval(Interval interval) {
305
        updateStrongChanges(this.getInterval(), interval);
306
        super.setInterval(interval);
307
        return this;
308
    }
309

    
310
    public EditableFeatureAttributeDescriptor setSize(int size) {
311
        updateStrongChanges(this.size, size);
312
        this.size = size;
313
        return this;
314
    }
315

    
316
    public boolean hasStrongChanges() {
317
        return hasStrongChanges;
318
    }
319

    
320
    public EditableFeatureAttributeDescriptor setAdditionalInfo(
321
        String infoName, Object value) {
322
        if (this.additionalInfo == null) {
323
            this.additionalInfo = new HashMap();
324
        }
325
        this.additionalInfo.put(infoName, value);
326
        return this;
327
    }
328

    
329
    public EditableFeatureAttributeDescriptor setIsAutomatic(boolean isAutomatic) {
330
        this.isAutomatic = isAutomatic;
331
        if( isAutomatic ) {
332
            this.setHidden(true);
333
        }
334
        return this;
335
    }
336
    
337
    public EditableFeatureAttributeDescriptor setIsTime(boolean isTime) {
338
        updateStrongChanges(this.isTime, isTime);
339
        this.isTime = isTime;
340
        return this;
341
    }
342

    
343
    public EditableFeatureAttributeDescriptor setDateFormat(DateFormat dateFormat) {
344
        this.dateFormat = dateFormat;
345
        return this;
346
    }
347

    
348
    public EditableFeatureAttributeDescriptor setIsIndexed(boolean isIndexed) {
349
        updateStrongChanges(this.indexed, isIndexed);
350
        this.indexed = isIndexed;
351
        return this;
352
    }
353
    
354
    public EditableFeatureAttributeDescriptor setAllowIndexDuplicateds(boolean allowDuplicateds) {
355
        updateStrongChanges(this.allowIndexDuplicateds, allowDuplicateds);
356
        this.allowIndexDuplicateds = allowDuplicateds;
357
        return this;
358
    }
359

    
360
    public EditableFeatureAttributeDescriptor setIsIndexAscending(boolean ascending) {
361
        updateStrongChanges(this.isIndexAscending, ascending);
362
        this.isIndexAscending = ascending;
363
        return this;
364
    }
365

    
366
    @Override
367
    public EditableFeatureAttributeDescriptor setDataProfileName(String dataProfile) {
368
        super.setDataProfileName(dataProfile);
369
        return this;
370
    }
371

    
372
    private void updateStrongChanges(int previous, int newvalue) {
373
        if( isComputed() ) {
374
            return;
375
        }
376
        if( previous == newvalue ) {
377
            return;
378
        }
379
        this.hasStrongChanges = true;
380
    }
381

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

    
394
    private void updateStrongChanges(boolean previous, boolean newvalue) {
395
        if( isComputed() ) {
396
            return;
397
        }
398
        if( previous == newvalue ) {
399
            return;
400
        }
401
        this.hasStrongChanges = true;
402
    }
403

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