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

History | View | Annotate | Download (14 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 44262 jjdelcerro
import org.gvsig.fmap.dal.feature.EditableForeingKey;
43 41335 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator;
44 43739 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureType;
45 44262 jjdelcerro
import org.gvsig.fmap.dal.feature.ForeingKey;
46 40435 jjdelcerro
import org.gvsig.fmap.dal.feature.exception.AttributeFeatureTypeIntegrityException;
47
import org.gvsig.fmap.dal.feature.exception.AttributeFeatureTypeSizeException;
48 42303 jjdelcerro
import org.gvsig.fmap.geom.Geometry;
49 44253 jjdelcerro
import org.gvsig.fmap.geom.GeometryUtils;
50 40435 jjdelcerro
import org.gvsig.fmap.geom.type.GeometryType;
51 44077 jjdelcerro
import org.gvsig.timesupport.Interval;
52 40435 jjdelcerro
import org.gvsig.tools.ToolsLocator;
53 44189 jjdelcerro
import org.gvsig.tools.dataTypes.DataType;
54 40435 jjdelcerro
import org.gvsig.tools.evaluator.Evaluator;
55
56
public class DefaultEditableFeatureAttributeDescriptor extends
57
    DefaultFeatureAttributeDescriptor implements
58
    EditableFeatureAttributeDescriptor {
59 40964 jldominguez
60
    private static Logger logger = LoggerFactory.getLogger(
61
        DefaultEditableFeatureAttributeDescriptor.class);
62 40435 jjdelcerro
63 44094 jjdelcerro
    private final DefaultFeatureAttributeDescriptor source;
64 40435 jjdelcerro
    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 44190 jjdelcerro
    public DefaultEditableFeatureAttributeDescriptor(FeatureType type, boolean strongChanges) {
82 43739 jjdelcerro
        super(type);
83 40435 jjdelcerro
        this.source = null;
84 44190 jjdelcerro
        hasStrongChanges = strongChanges;
85 40435 jjdelcerro
    }
86
87
    public DefaultFeatureAttributeDescriptor getSource() {
88
        return this.source;
89
    }
90 44094 jjdelcerro
91 40435 jjdelcerro
    public void fixAll() {
92 44094 jjdelcerro
        super.fixAll();
93 40435 jjdelcerro
    }
94 44094 jjdelcerro
95 40435 jjdelcerro
    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 40964 jldominguez
        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 40435 jjdelcerro
109
        if (ex.size() > 0) {
110
            throw ex;
111
        }
112
    }
113
114
    public EditableFeatureAttributeDescriptor setAllowNull(boolean allowNull) {
115 44189 jjdelcerro
        updateStrongChanges(this.allowNull, allowNull);
116 40435 jjdelcerro
        this.allowNull = allowNull;
117
        return this;
118
    }
119 44262 jjdelcerro
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 40435 jjdelcerro
129
    public EditableFeatureAttributeDescriptor setDataType(int type) {
130 44189 jjdelcerro
        updateStrongChanges(this.dataType, type);
131 40435 jjdelcerro
        this.dataType = ToolsLocator.getDataTypesManager().get(type);
132
        return this;
133
    }
134
135
    public EditableFeatureAttributeDescriptor setDefaultValue(
136
        Object defaultValue) {
137 44189 jjdelcerro
        updateStrongChanges(this.defaultValue, defaultValue);
138 40435 jjdelcerro
        this.defaultValue = defaultValue;
139
        return this;
140
    }
141
142
    public EditableFeatureAttributeDescriptor setEvaluator(Evaluator evaluator) {
143 44189 jjdelcerro
        updateStrongChanges(this.evaluator, evaluator);
144 40435 jjdelcerro
        this.evaluator = evaluator;
145
        return this;
146
    }
147
148 44253 jjdelcerro
    @Override
149 41335 jjdelcerro
    public EditableFeatureAttributeDescriptor setFeatureAttributeEmulator(FeatureAttributeEmulator featureAttributeEmulator) {
150
        this.featureAttributeEmulator = featureAttributeEmulator;
151
        return this;
152
    }
153 44253 jjdelcerro
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 41335 jjdelcerro
180 44253 jjdelcerro
    @Override
181 40435 jjdelcerro
    public EditableFeatureAttributeDescriptor setGeometryType(int type) {
182
        this.geometryType = type;
183 42303 jjdelcerro
        if( this.geometrySubType == Geometry.SUBTYPES.UNKNOWN ) {
184
            this.geometrySubType = Geometry.SUBTYPES.GEOM2D;
185
        }
186 40435 jjdelcerro
        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 44189 jjdelcerro
        updateStrongChanges(this.geomType, geometryType);
199 40435 jjdelcerro
        this.geomType = geometryType;
200
        this.geometryType = this.geomType.getType();
201
        this.geometrySubType = this.geomType.getSubType();
202
        return this;
203
    }
204
205 42716 jjdelcerro
    @Override
206 44253 jjdelcerro
    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 42716 jjdelcerro
    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 40435 jjdelcerro
    public EditableFeatureAttributeDescriptor setIsPrimaryKey(
237
        boolean isPrimaryKey) {
238 44189 jjdelcerro
        updateStrongChanges(this.primaryKey, primaryKey);
239 40435 jjdelcerro
        this.primaryKey = isPrimaryKey;
240
        return this;
241
    }
242
243
    public EditableFeatureAttributeDescriptor setIsReadOnly(boolean isReadOnly) {
244 44189 jjdelcerro
        updateStrongChanges(this.readOnly, readOnly);
245 40435 jjdelcerro
        this.readOnly = isReadOnly;
246
        return this;
247
    }
248
249
    public EditableFeatureAttributeDescriptor setMaximumOccurrences(
250
        int maximumOccurrences) {
251 44189 jjdelcerro
        updateStrongChanges(this.maximumOccurrences, maximumOccurrences);
252 40435 jjdelcerro
        this.maximumOccurrences = maximumOccurrences;
253
        return this;
254
    }
255
256
    public EditableFeatureAttributeDescriptor setMinimumOccurrences(
257
        int minimumOccurrences) {
258 44189 jjdelcerro
        updateStrongChanges(this.minimumOccurrences, minimumOccurrences);
259 40435 jjdelcerro
        this.minimumOccurrences = minimumOccurrences;
260
        return this;
261
    }
262
263 43967 jjdelcerro
    @Override
264 40435 jjdelcerro
    public EditableFeatureAttributeDescriptor setName(String name) {
265 44084 jjdelcerro
        if( StringUtils.equalsIgnoreCase(this.name, name) ) {
266
            return this;
267
        }
268 40435 jjdelcerro
        if (originalName == null) {
269
            originalName = this.name;
270 43967 jjdelcerro
            if (!isComputed()) {
271
                hasStrongChanges = true;
272
            }
273 40435 jjdelcerro
        }
274 44259 jjdelcerro
        super.setName(name);
275 43135 jjdelcerro
        if (!isComputed()) {
276 40435 jjdelcerro
            hasStrongChanges = true;
277
        }
278
        return this;
279
    }
280
281
    public String getOriginalName() {
282
        return originalName;
283
    }
284
285
    public EditableFeatureAttributeDescriptor setObjectClass(Class theClass) {
286 44189 jjdelcerro
        updateStrongChanges(this.objectClass, theClass);
287 40435 jjdelcerro
        this.objectClass = theClass;
288
        return this;
289
    }
290
291
    public EditableFeatureAttributeDescriptor setPrecision(int precision) {
292 44189 jjdelcerro
        updateStrongChanges(this.precision, precision);
293 40435 jjdelcerro
        this.precision = precision;
294
        return this;
295
    }
296
297 44253 jjdelcerro
    @Override
298 40435 jjdelcerro
    public EditableFeatureAttributeDescriptor setSRS(IProjection SRS) {
299 44189 jjdelcerro
        updateStrongChanges(this.SRS, SRS);
300 40435 jjdelcerro
        this.SRS = SRS;
301
        return this;
302
    }
303
304 44253 jjdelcerro
    @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 44077 jjdelcerro
    public EditableFeatureAttributeDescriptor setInterval(Interval interval) {
316 44189 jjdelcerro
        updateStrongChanges(this.getInterval(), interval);
317 44094 jjdelcerro
        super.setInterval(interval);
318 44077 jjdelcerro
        return this;
319
    }
320
321 40435 jjdelcerro
    public EditableFeatureAttributeDescriptor setSize(int size) {
322 44189 jjdelcerro
        updateStrongChanges(this.size, size);
323 40435 jjdelcerro
        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 43362 jjdelcerro
        if( isAutomatic ) {
343
            this.setHidden(true);
344
        }
345 40435 jjdelcerro
        return this;
346
    }
347
348 41483 jjdelcerro
    public EditableFeatureAttributeDescriptor setIsTime(boolean isTime) {
349 44189 jjdelcerro
        updateStrongChanges(this.isTime, isTime);
350 43135 jjdelcerro
        this.isTime = isTime;
351
        return this;
352 41483 jjdelcerro
    }
353
354
    public EditableFeatureAttributeDescriptor setDateFormat(DateFormat dateFormat) {
355
        this.dateFormat = dateFormat;
356
        return this;
357
    }
358 41638 jjdelcerro
359
    public EditableFeatureAttributeDescriptor setIsIndexed(boolean isIndexed) {
360 44189 jjdelcerro
        updateStrongChanges(this.indexed, isIndexed);
361 41638 jjdelcerro
        this.indexed = isIndexed;
362
        return this;
363
    }
364 41483 jjdelcerro
365 41638 jjdelcerro
    public EditableFeatureAttributeDescriptor setAllowIndexDuplicateds(boolean allowDuplicateds) {
366 44189 jjdelcerro
        updateStrongChanges(this.allowIndexDuplicateds, allowDuplicateds);
367 41638 jjdelcerro
        this.allowIndexDuplicateds = allowDuplicateds;
368
        return this;
369
    }
370
371
    public EditableFeatureAttributeDescriptor setIsIndexAscending(boolean ascending) {
372 44189 jjdelcerro
        updateStrongChanges(this.isIndexAscending, ascending);
373 41638 jjdelcerro
        this.isIndexAscending = ascending;
374
        return this;
375
    }
376 44128 jjdelcerro
377
    @Override
378
    public EditableFeatureAttributeDescriptor setDataProfileName(String dataProfile) {
379
        super.setDataProfileName(dataProfile);
380
        return this;
381
    }
382 44189 jjdelcerro
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 40435 jjdelcerro
}