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 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 41483 jjdelcerro
import java.text.DateFormat;
27 40435 jjdelcerro
import java.util.HashMap;
28 44189 jjdelcerro
import java.util.Objects;
29 44084 jjdelcerro
import org.apache.commons.lang3.StringUtils;
30 40435 jjdelcerro
31
import org.cresques.cts.IProjection;
32 44253 jjdelcerro
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 40964 jldominguez
import org.slf4j.Logger;
39
import org.slf4j.LoggerFactory;
40 40435 jjdelcerro
41
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
42 41335 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator;
43 43739 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureType;
44 40435 jjdelcerro
import org.gvsig.fmap.dal.feature.exception.AttributeFeatureTypeIntegrityException;
45
import org.gvsig.fmap.dal.feature.exception.AttributeFeatureTypeSizeException;
46 42303 jjdelcerro
import org.gvsig.fmap.geom.Geometry;
47 44253 jjdelcerro
import org.gvsig.fmap.geom.GeometryUtils;
48 40435 jjdelcerro
import org.gvsig.fmap.geom.type.GeometryType;
49 44077 jjdelcerro
import org.gvsig.timesupport.Interval;
50 40435 jjdelcerro
import org.gvsig.tools.ToolsLocator;
51 44189 jjdelcerro
import org.gvsig.tools.dataTypes.DataType;
52 40435 jjdelcerro
import org.gvsig.tools.evaluator.Evaluator;
53
54
public class DefaultEditableFeatureAttributeDescriptor extends
55
    DefaultFeatureAttributeDescriptor implements
56
    EditableFeatureAttributeDescriptor {
57 40964 jldominguez
58
    private static Logger logger = LoggerFactory.getLogger(
59
        DefaultEditableFeatureAttributeDescriptor.class);
60 40435 jjdelcerro
61 44094 jjdelcerro
    private final DefaultFeatureAttributeDescriptor source;
62 40435 jjdelcerro
    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 44190 jjdelcerro
    public DefaultEditableFeatureAttributeDescriptor(FeatureType type, boolean strongChanges) {
80 43739 jjdelcerro
        super(type);
81 40435 jjdelcerro
        this.source = null;
82 44190 jjdelcerro
        hasStrongChanges = strongChanges;
83 40435 jjdelcerro
    }
84
85
    public DefaultFeatureAttributeDescriptor getSource() {
86
        return this.source;
87
    }
88 44094 jjdelcerro
89 40435 jjdelcerro
    public void fixAll() {
90 44094 jjdelcerro
        super.fixAll();
91 40435 jjdelcerro
    }
92 44094 jjdelcerro
93 40435 jjdelcerro
    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 40964 jldominguez
        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 40435 jjdelcerro
107
        if (ex.size() > 0) {
108
            throw ex;
109
        }
110
    }
111
112
    public EditableFeatureAttributeDescriptor setAllowNull(boolean allowNull) {
113 44189 jjdelcerro
        updateStrongChanges(this.allowNull, allowNull);
114 40435 jjdelcerro
        this.allowNull = allowNull;
115
        return this;
116
    }
117
118
    public EditableFeatureAttributeDescriptor setDataType(int type) {
119 44189 jjdelcerro
        updateStrongChanges(this.dataType, type);
120 40435 jjdelcerro
        this.dataType = ToolsLocator.getDataTypesManager().get(type);
121
        return this;
122
    }
123
124
    public EditableFeatureAttributeDescriptor setDefaultValue(
125
        Object defaultValue) {
126 44189 jjdelcerro
        updateStrongChanges(this.defaultValue, defaultValue);
127 40435 jjdelcerro
        this.defaultValue = defaultValue;
128
        return this;
129
    }
130
131
    public EditableFeatureAttributeDescriptor setEvaluator(Evaluator evaluator) {
132 44189 jjdelcerro
        updateStrongChanges(this.evaluator, evaluator);
133 40435 jjdelcerro
        this.evaluator = evaluator;
134
        return this;
135
    }
136
137 44253 jjdelcerro
    @Override
138 41335 jjdelcerro
    public EditableFeatureAttributeDescriptor setFeatureAttributeEmulator(FeatureAttributeEmulator featureAttributeEmulator) {
139
        this.featureAttributeEmulator = featureAttributeEmulator;
140
        return this;
141
    }
142 44253 jjdelcerro
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 41335 jjdelcerro
169 44253 jjdelcerro
    @Override
170 40435 jjdelcerro
    public EditableFeatureAttributeDescriptor setGeometryType(int type) {
171
        this.geometryType = type;
172 42303 jjdelcerro
        if( this.geometrySubType == Geometry.SUBTYPES.UNKNOWN ) {
173
            this.geometrySubType = Geometry.SUBTYPES.GEOM2D;
174
        }
175 40435 jjdelcerro
        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 44189 jjdelcerro
        updateStrongChanges(this.geomType, geometryType);
188 40435 jjdelcerro
        this.geomType = geometryType;
189
        this.geometryType = this.geomType.getType();
190
        this.geometrySubType = this.geomType.getSubType();
191
        return this;
192
    }
193
194 42716 jjdelcerro
    @Override
195 44253 jjdelcerro
    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 42716 jjdelcerro
    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 40435 jjdelcerro
    public EditableFeatureAttributeDescriptor setIsPrimaryKey(
226
        boolean isPrimaryKey) {
227 44189 jjdelcerro
        updateStrongChanges(this.primaryKey, primaryKey);
228 40435 jjdelcerro
        this.primaryKey = isPrimaryKey;
229
        return this;
230
    }
231
232
    public EditableFeatureAttributeDescriptor setIsReadOnly(boolean isReadOnly) {
233 44189 jjdelcerro
        updateStrongChanges(this.readOnly, readOnly);
234 40435 jjdelcerro
        this.readOnly = isReadOnly;
235
        return this;
236
    }
237
238
    public EditableFeatureAttributeDescriptor setMaximumOccurrences(
239
        int maximumOccurrences) {
240 44189 jjdelcerro
        updateStrongChanges(this.maximumOccurrences, maximumOccurrences);
241 40435 jjdelcerro
        this.maximumOccurrences = maximumOccurrences;
242
        return this;
243
    }
244
245
    public EditableFeatureAttributeDescriptor setMinimumOccurrences(
246
        int minimumOccurrences) {
247 44189 jjdelcerro
        updateStrongChanges(this.minimumOccurrences, minimumOccurrences);
248 40435 jjdelcerro
        this.minimumOccurrences = minimumOccurrences;
249
        return this;
250
    }
251
252 43967 jjdelcerro
    @Override
253 40435 jjdelcerro
    public EditableFeatureAttributeDescriptor setName(String name) {
254 44084 jjdelcerro
        if( StringUtils.equalsIgnoreCase(this.name, name) ) {
255
            return this;
256
        }
257 40435 jjdelcerro
        if (originalName == null) {
258
            originalName = this.name;
259 43967 jjdelcerro
            if (!isComputed()) {
260
                hasStrongChanges = true;
261
            }
262 40435 jjdelcerro
        }
263 44259 jjdelcerro
        super.setName(name);
264 43135 jjdelcerro
        if (!isComputed()) {
265 40435 jjdelcerro
            hasStrongChanges = true;
266
        }
267
        return this;
268
    }
269
270
    public String getOriginalName() {
271
        return originalName;
272
    }
273
274
    public EditableFeatureAttributeDescriptor setObjectClass(Class theClass) {
275 44189 jjdelcerro
        updateStrongChanges(this.objectClass, theClass);
276 40435 jjdelcerro
        this.objectClass = theClass;
277
        return this;
278
    }
279
280
    public EditableFeatureAttributeDescriptor setPrecision(int precision) {
281 44189 jjdelcerro
        updateStrongChanges(this.precision, precision);
282 40435 jjdelcerro
        this.precision = precision;
283
        return this;
284
    }
285
286 44253 jjdelcerro
    @Override
287 40435 jjdelcerro
    public EditableFeatureAttributeDescriptor setSRS(IProjection SRS) {
288 44189 jjdelcerro
        updateStrongChanges(this.SRS, SRS);
289 40435 jjdelcerro
        this.SRS = SRS;
290
        return this;
291
    }
292
293 44253 jjdelcerro
    @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 44077 jjdelcerro
    public EditableFeatureAttributeDescriptor setInterval(Interval interval) {
305 44189 jjdelcerro
        updateStrongChanges(this.getInterval(), interval);
306 44094 jjdelcerro
        super.setInterval(interval);
307 44077 jjdelcerro
        return this;
308
    }
309
310 40435 jjdelcerro
    public EditableFeatureAttributeDescriptor setSize(int size) {
311 44189 jjdelcerro
        updateStrongChanges(this.size, size);
312 40435 jjdelcerro
        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 43362 jjdelcerro
        if( isAutomatic ) {
332
            this.setHidden(true);
333
        }
334 40435 jjdelcerro
        return this;
335
    }
336
337 41483 jjdelcerro
    public EditableFeatureAttributeDescriptor setIsTime(boolean isTime) {
338 44189 jjdelcerro
        updateStrongChanges(this.isTime, isTime);
339 43135 jjdelcerro
        this.isTime = isTime;
340
        return this;
341 41483 jjdelcerro
    }
342
343
    public EditableFeatureAttributeDescriptor setDateFormat(DateFormat dateFormat) {
344
        this.dateFormat = dateFormat;
345
        return this;
346
    }
347 41638 jjdelcerro
348
    public EditableFeatureAttributeDescriptor setIsIndexed(boolean isIndexed) {
349 44189 jjdelcerro
        updateStrongChanges(this.indexed, isIndexed);
350 41638 jjdelcerro
        this.indexed = isIndexed;
351
        return this;
352
    }
353 41483 jjdelcerro
354 41638 jjdelcerro
    public EditableFeatureAttributeDescriptor setAllowIndexDuplicateds(boolean allowDuplicateds) {
355 44189 jjdelcerro
        updateStrongChanges(this.allowIndexDuplicateds, allowDuplicateds);
356 41638 jjdelcerro
        this.allowIndexDuplicateds = allowDuplicateds;
357
        return this;
358
    }
359
360
    public EditableFeatureAttributeDescriptor setIsIndexAscending(boolean ascending) {
361 44189 jjdelcerro
        updateStrongChanges(this.isIndexAscending, ascending);
362 41638 jjdelcerro
        this.isIndexAscending = ascending;
363
        return this;
364
    }
365 44128 jjdelcerro
366
    @Override
367
    public EditableFeatureAttributeDescriptor setDataProfileName(String dataProfile) {
368
        super.setDataProfileName(dataProfile);
369
        return this;
370
    }
371 44189 jjdelcerro
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 40435 jjdelcerro
}