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

History | View | Annotate | Download (10.9 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.slf4j.Logger;
33
import org.slf4j.LoggerFactory;
34

    
35
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
36
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator;
37
import org.gvsig.fmap.dal.feature.FeatureType;
38
import org.gvsig.fmap.dal.feature.exception.AttributeFeatureTypeIntegrityException;
39
import org.gvsig.fmap.dal.feature.exception.AttributeFeatureTypeSizeException;
40
import org.gvsig.fmap.geom.Geometry;
41
import org.gvsig.fmap.geom.type.GeometryType;
42
import org.gvsig.timesupport.Interval;
43
import org.gvsig.tools.ToolsLocator;
44
import org.gvsig.tools.dataTypes.DataType;
45
import org.gvsig.tools.evaluator.Evaluator;
46

    
47
public class DefaultEditableFeatureAttributeDescriptor extends
48
    DefaultFeatureAttributeDescriptor implements
49
    EditableFeatureAttributeDescriptor {
50
    
51
    private static Logger logger = LoggerFactory.getLogger(
52
        DefaultEditableFeatureAttributeDescriptor.class);
53

    
54
    private final DefaultFeatureAttributeDescriptor source;
55
    private boolean hasStrongChanges;
56
    private String originalName = null;
57

    
58
    protected DefaultEditableFeatureAttributeDescriptor(
59
        DefaultFeatureAttributeDescriptor other) {
60
        super(other);
61
        if (other instanceof DefaultEditableFeatureAttributeDescriptor) {
62
            DefaultEditableFeatureAttributeDescriptor other_edi =
63
                (DefaultEditableFeatureAttributeDescriptor) other;
64
            originalName = other_edi.getOriginalName();
65
            this.source = other_edi.getSource();
66
        } else {
67
            this.source = other;
68
        }
69
        hasStrongChanges = false;
70
    }
71

    
72
    public DefaultEditableFeatureAttributeDescriptor(FeatureType type) {
73
        super(type);
74
        this.source = null;
75
        hasStrongChanges = false;
76
    }
77

    
78
    public DefaultFeatureAttributeDescriptor getSource() {
79
        return this.source;
80
    }
81
    
82
    public void fixAll() {
83
        super.fixAll();
84
    }
85
    
86
    public void checkIntegrity() throws AttributeFeatureTypeIntegrityException {
87
        AttributeFeatureTypeIntegrityException ex =
88
            new AttributeFeatureTypeIntegrityException(getName());
89
        if (this.size < 0) {
90
            ex.add(new AttributeFeatureTypeSizeException(this.size));
91
        }
92

    
93
        if( this.dataType.isObject() && this.objectClass == null ) {
94
            logger.warn("Incorrect data type object, objectClass is null.");
95
            ex.add(new AttributeFeatureTypeIntegrityException(this.name));
96
        }
97
        
98
        // TODO: Add other integrity checks...
99

    
100
        if (ex.size() > 0) {
101
            throw ex;
102
        }
103
    }
104

    
105
    public EditableFeatureAttributeDescriptor setAllowNull(boolean allowNull) {
106
        updateStrongChanges(this.allowNull, allowNull);
107
        this.allowNull = allowNull;
108
        return this;
109
    }
110

    
111
    public EditableFeatureAttributeDescriptor setDataType(int type) {
112
        updateStrongChanges(this.dataType, type);
113
        this.dataType = ToolsLocator.getDataTypesManager().get(type);
114
        return this;
115
    }
116

    
117
    public EditableFeatureAttributeDescriptor setDefaultValue(
118
        Object defaultValue) {
119
        updateStrongChanges(this.defaultValue, defaultValue);
120
        this.defaultValue = defaultValue;
121
        return this;
122
    }
123

    
124
    public EditableFeatureAttributeDescriptor setEvaluator(Evaluator evaluator) {
125
        updateStrongChanges(this.evaluator, evaluator);
126
        this.evaluator = evaluator;
127
        return this;
128
    }
129

    
130
    public EditableFeatureAttributeDescriptor setFeatureAttributeEmulator(FeatureAttributeEmulator featureAttributeEmulator) {
131
        this.featureAttributeEmulator = featureAttributeEmulator;
132
        return this;
133
    }
134
        
135
    public EditableFeatureAttributeDescriptor setGeometryType(int type) {
136
        this.geometryType = type;
137
        if( this.geometrySubType == Geometry.SUBTYPES.UNKNOWN ) {
138
            this.geometrySubType = Geometry.SUBTYPES.GEOM2D;
139
        }
140
        this.geomType = null;
141
        return this;
142
    }
143

    
144
    public EditableFeatureAttributeDescriptor setGeometrySubType(int subType) {
145
        this.geometrySubType = subType;
146
        this.geomType = null;
147
        return this;
148
    }
149

    
150
    public EditableFeatureAttributeDescriptor setGeometryType(
151
        GeometryType geometryType) {
152
        updateStrongChanges(this.geomType, geometryType);
153
        this.geomType = geometryType;
154
        this.geometryType = this.geomType.getType();
155
        this.geometrySubType = this.geomType.getSubType();
156
        return this;
157
    }
158

    
159
    @Override
160
    public EditableFeatureAttributeDescriptor setGeometryType(int type, int subType) {
161
        this.geometryType = type;
162
        this.geometrySubType = subType;
163
        this.geomType = null;
164
        return this;
165
    }
166

    
167
    public EditableFeatureAttributeDescriptor setIsPrimaryKey(
168
        boolean isPrimaryKey) {
169
        updateStrongChanges(this.primaryKey, primaryKey);
170
        this.primaryKey = isPrimaryKey;
171
        return this;
172
    }
173

    
174
    public EditableFeatureAttributeDescriptor setIsReadOnly(boolean isReadOnly) {
175
        updateStrongChanges(this.readOnly, readOnly);
176
        this.readOnly = isReadOnly;
177
        return this;
178
    }
179

    
180
    public EditableFeatureAttributeDescriptor setMaximumOccurrences(
181
        int maximumOccurrences) {
182
        updateStrongChanges(this.maximumOccurrences, maximumOccurrences);
183
        this.maximumOccurrences = maximumOccurrences;
184
        return this;
185
    }
186

    
187
    public EditableFeatureAttributeDescriptor setMinimumOccurrences(
188
        int minimumOccurrences) {
189
        updateStrongChanges(this.minimumOccurrences, minimumOccurrences);
190
        this.minimumOccurrences = minimumOccurrences;
191
        return this;
192
    }
193

    
194
    @Override
195
    public EditableFeatureAttributeDescriptor setName(String name) {
196
        if( StringUtils.equalsIgnoreCase(this.name, name) ) {
197
            return this;
198
        }
199
        if (originalName == null) {
200
            originalName = this.name;
201
            if (!isComputed()) {
202
                hasStrongChanges = true;
203
            }
204
        }
205
        this.name = name;
206
        if (!isComputed()) {
207
            hasStrongChanges = true;
208
        }
209
        return this;
210
    }
211
    
212
    public String getOriginalName() {
213
        return originalName;
214
    }
215

    
216
    public EditableFeatureAttributeDescriptor setObjectClass(Class theClass) {
217
        updateStrongChanges(this.objectClass, theClass);
218
        this.objectClass = theClass;
219
        return this;
220
    }
221

    
222
    public EditableFeatureAttributeDescriptor setPrecision(int precision) {
223
        updateStrongChanges(this.precision, precision);
224
        this.precision = precision;
225
        return this;
226
    }
227

    
228
    public EditableFeatureAttributeDescriptor setSRS(IProjection SRS) {
229
        updateStrongChanges(this.SRS, SRS);
230
        this.SRS = SRS;
231
        return this;
232
    }
233

    
234
    public EditableFeatureAttributeDescriptor setInterval(Interval interval) {
235
        updateStrongChanges(this.getInterval(), interval);
236
        super.setInterval(interval);
237
        return this;
238
    }
239

    
240
    public EditableFeatureAttributeDescriptor setSize(int size) {
241
        updateStrongChanges(this.size, size);
242
        this.size = size;
243
        return this;
244
    }
245

    
246
    public boolean hasStrongChanges() {
247
        return hasStrongChanges;
248
    }
249

    
250
    public EditableFeatureAttributeDescriptor setAdditionalInfo(
251
        String infoName, Object value) {
252
        if (this.additionalInfo == null) {
253
            this.additionalInfo = new HashMap();
254
        }
255
        this.additionalInfo.put(infoName, value);
256
        return this;
257
    }
258

    
259
    public EditableFeatureAttributeDescriptor setIsAutomatic(boolean isAutomatic) {
260
        this.isAutomatic = isAutomatic;
261
        if( isAutomatic ) {
262
            this.setHidden(true);
263
        }
264
        return this;
265
    }
266
    
267
    public EditableFeatureAttributeDescriptor setIsTime(boolean isTime) {
268
        updateStrongChanges(this.isTime, isTime);
269
        this.isTime = isTime;
270
        return this;
271
    }
272

    
273
    public EditableFeatureAttributeDescriptor setDateFormat(DateFormat dateFormat) {
274
        this.dateFormat = dateFormat;
275
        return this;
276
    }
277

    
278
    public EditableFeatureAttributeDescriptor setIsIndexed(boolean isIndexed) {
279
        updateStrongChanges(this.indexed, isIndexed);
280
        this.indexed = isIndexed;
281
        return this;
282
    }
283
    
284
    public EditableFeatureAttributeDescriptor setAllowIndexDuplicateds(boolean allowDuplicateds) {
285
        updateStrongChanges(this.allowIndexDuplicateds, allowDuplicateds);
286
        this.allowIndexDuplicateds = allowDuplicateds;
287
        return this;
288
    }
289

    
290
    public EditableFeatureAttributeDescriptor setIsIndexAscending(boolean ascending) {
291
        updateStrongChanges(this.isIndexAscending, ascending);
292
        this.isIndexAscending = ascending;
293
        return this;
294
    }
295

    
296
    @Override
297
    public EditableFeatureAttributeDescriptor setDataProfileName(String dataProfile) {
298
        super.setDataProfileName(dataProfile);
299
        return this;
300
    }
301

    
302
    private void updateStrongChanges(int previous, int newvalue) {
303
        if( isComputed() ) {
304
            return;
305
        }
306
        if( previous == newvalue ) {
307
            return;
308
        }
309
        this.hasStrongChanges = true;
310
    }
311

    
312
    private void updateStrongChanges(DataType previous, int newvalue) {
313
        if( isComputed() ) {
314
            return;
315
        }
316
        if( previous!=null ) {
317
            if( previous.getType() == newvalue ) {
318
                return;
319
            }
320
        }
321
        this.hasStrongChanges = true;
322
    }
323

    
324
    private void updateStrongChanges(boolean previous, boolean newvalue) {
325
        if( isComputed() ) {
326
            return;
327
        }
328
        if( previous == newvalue ) {
329
            return;
330
        }
331
        this.hasStrongChanges = true;
332
    }
333

    
334
    private void updateStrongChanges(Object previous, Object newvalue) {
335
        if( isComputed() ) {
336
            return;
337
        }
338
        if( Objects.equals(newvalue, previous) ) {
339
            return;
340
        }
341
        this.hasStrongChanges = true;
342
    }
343
}