Statistics
| Revision:

gvsig-vectorediting / org.gvsig.vectorediting / trunk / org.gvsig.vectorediting / org.gvsig.vectorediting.lib / org.gvsig.vectorediting.lib.spi / src / main / java / org / gvsig / vectorediting / lib / spi / DefaultEditingServiceParameter.java @ 3838

History | View | Annotate | Download (11.6 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright ? 2007-2014 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 2
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

    
25
package org.gvsig.vectorediting.lib.spi;
26

    
27
import java.util.Arrays;
28
import java.util.HashSet;
29
import java.util.List;
30
import java.util.Map;
31
import java.util.Objects;
32
import java.util.Set;
33
import org.apache.commons.lang3.StringUtils;
34
import org.gvsig.fmap.dal.feature.FeatureSelection;
35
import org.gvsig.fmap.geom.Geometry;
36
import org.gvsig.fmap.geom.primitive.Point;
37
import org.gvsig.timesupport.DataTypes;
38
import org.gvsig.tools.ToolsLocator;
39
import org.gvsig.tools.dataTypes.CoercionException;
40
import org.gvsig.tools.dataTypes.DataType;
41
import org.gvsig.tools.dataTypes.DataTypesManager;
42
import org.gvsig.vectorediting.lib.api.EditingServiceParameter;
43
import org.gvsig.vectorediting.lib.api.EditingServiceParameterOptions;
44
import org.gvsig.vectorediting.lib.api.exceptions.InvalidEntryException;
45

    
46
public class DefaultEditingServiceParameter implements EditingServiceParameter {
47

    
48
    private String name;
49

    
50
    private String description;
51

    
52
    private Set<TYPE> types;
53

    
54
    private int geometryType;
55

    
56
    private Map<String, String> options;
57

    
58
    private EditingServiceParameterOptions options2;
59

    
60
    private Object defaultValue;
61

    
62
    private boolean optional;
63
    
64
    private int dataType;
65
    
66
    private boolean allowNull;
67

    
68
    public DefaultEditingServiceParameter(String theName,
69
        String theDescription, TYPE... theTypes) {
70

    
71
        this(theName, theDescription, Geometry.TYPES.NULL, null, null , null, false, theTypes);
72

    
73
    }
74

    
75
    public DefaultEditingServiceParameter(String theName,
76
        String theDescription, boolean optional, TYPE... theTypes) {
77

    
78
        this(theName, theDescription, Geometry.TYPES.NULL, null, null, null, optional, theTypes);
79
    }
80

    
81
    public DefaultEditingServiceParameter(String theName,
82
        String theDescription, Map<String, String> options, Object defaultValue, boolean optional, TYPE... theTypes) {
83

    
84
        this(theName, theDescription, Geometry.TYPES.NULL, options, defaultValue, optional, theTypes);
85
    }
86

    
87
    public DefaultEditingServiceParameter(String theName,
88
        String theDescription, EditingServiceParameterOptions options, Object defaultValue, boolean optional, TYPE... theTypes) {
89

    
90
        this(theName, theDescription, Geometry.TYPES.NULL, options, defaultValue, optional, theTypes);
91
    }
92

    
93
    public DefaultEditingServiceParameter(String theName,
94
        Object theDefaultValue, String theDescription, TYPE... theTypes) {
95

    
96
        this(theName, theDescription, Geometry.TYPES.NULL, null, null, theDefaultValue, false, theTypes);
97
        
98
    }
99

    
100
    public DefaultEditingServiceParameter(String theName,
101
        String theDescription, int theGeometryType, TYPE... theTypes) {
102

    
103
        this(theName, theDescription, theGeometryType, null, null, null, false, theTypes);
104

    
105
    }
106

    
107
    public DefaultEditingServiceParameter(String theName,
108
        String theDescription, Object theDefaultValue, int theGeometryType,
109
        TYPE... theTypes) {
110

    
111
        this(theName, theDescription, theGeometryType, null, null, theDefaultValue, false, theTypes);
112
        
113
    }
114

    
115
    public DefaultEditingServiceParameter(String theName,
116
        String theDescription, Map<String, String> theOptions, TYPE... theTypes) {
117

    
118
        this(theName, theDescription, Geometry.TYPES.NULL, theOptions, null, false, theTypes);
119
    }
120

    
121
    public DefaultEditingServiceParameter(String theName,
122
        String theDescription, Map<String, String> theOptions, Object theDefaultValue, TYPE... theTypes) {
123

    
124
        this(theName, theDescription, Geometry.TYPES.NULL, theOptions, theDefaultValue, false, theTypes);
125
    }
126

    
127
    public DefaultEditingServiceParameter(String theName,
128
        String theDescription, EditingServiceParameterOptions theOptions, Object theDefaultValue, TYPE... theTypes) {
129

    
130
        this(theName, theDescription, Geometry.TYPES.NULL, null, theOptions, theDefaultValue, false, theTypes);
131
    }
132

    
133
    public DefaultEditingServiceParameter(String theName,
134
        String theDescription, int theGeometryType,
135
        Map<String, String> theOptions, TYPE... theTypes) {
136

    
137
        this(theName, theDescription, theGeometryType, theOptions, null, false, theTypes);
138

    
139
    }
140

    
141
    public DefaultEditingServiceParameter(String theName,
142
        String theDescription, int theGeometryType,
143
        Map<String, String> theOptions, Object theDefaultValue, TYPE... theTypes) {
144

    
145
        this(theName, theDescription, theGeometryType, theOptions, theDefaultValue, false, theTypes);
146
    }
147

    
148
    public DefaultEditingServiceParameter(String theName,
149
        String theDescription, int theGeometryType,
150
        Map<String, String> theOptions, Object theDefaultValue, 
151
        boolean optional, TYPE... theTypes) {
152
        
153
        this(theName, theDescription, theGeometryType, theOptions, null, theDefaultValue, optional, theTypes);
154

    
155
    }
156

    
157
    public DefaultEditingServiceParameter(String theName,
158
        String theDescription, int theGeometryType,
159
        EditingServiceParameterOptions theOptions, Object theDefaultValue, 
160
        boolean optional, TYPE... theTypes) {
161
        
162
        this(theName, theDescription, theGeometryType, null, theOptions, theDefaultValue, optional, theTypes);
163

    
164
    }
165

    
166
    public DefaultEditingServiceParameter(String theName,
167
        String theDescription, int theGeometryType,
168
        Map<String, String> theOptions, EditingServiceParameterOptions theOptions2, Object theDefaultValue, 
169
        boolean optional, TYPE... theTypes) {
170

    
171
        this.name = theName;
172
        this.description = theDescription;
173
        this.geometryType = theGeometryType;
174

    
175
        this.types = new HashSet<>();
176
        this.types.addAll(Arrays.asList(theTypes));
177

    
178
        this.options = theOptions;
179
        this.options2 = theOptions2;
180
        this.defaultValue = theDefaultValue;
181
        this.optional = optional;
182
        this.dataType = DataTypes.STRING;
183
        this.allowNull = true;
184
    }
185

    
186
    @Override
187
    public Set<TYPE> getTypes() {
188
        return types;
189
    }
190

    
191
    @Override
192
    public String getName() {
193
        return name;
194
    }
195

    
196
    @Override
197
    public String getDescription() {
198
        return description;
199
    }
200

    
201
    public void setDescription(String newDescription) {
202
        this.description = newDescription;
203
    }
204

    
205
    @Override
206
    public int getGeometryType() {
207
        return this.geometryType;
208
    }
209

    
210
    @Override
211
    public Map<String, String> getOptions() {
212
        return this.options;
213
    }
214

    
215
    @Override
216
    public EditingServiceParameterOptions getOptions2() {
217
        return this.options2;
218
    }
219

    
220
    @Override
221
    public Object getDefaultValue() {
222
        return this.defaultValue;
223
    }
224

    
225
    @Override
226
    public void setDefaultValue(Object value) {
227
        this.defaultValue = value;
228
    }
229
    
230
    public String getConsoleDefaultValue() {
231
        if(this.defaultValue == null) {
232
            return "";
233
        }
234
        if (defaultValue instanceof Point) {
235
            Point point = (Point) defaultValue;
236
            StringBuilder builder = new StringBuilder();
237
            for (int i = 0; i < point.getDimension(); i++) {
238
                builder.append(point.getCoordinateAt(i));
239
                if (i < point.getDimension() - 1) {
240
                    builder.append(" , ");
241
                }
242
            }
243
            return builder.toString();
244
        }
245

    
246
        if (this.options2 != null) {
247
            return this.options2.getConsoleValue(this.defaultValue);
248
        }
249
        if (this.options != null) {
250
            //FIXME: ????
251
            return this.options.getOrDefault(defaultValue, "");
252
        }
253
        return Objects.toString(defaultValue, "");
254
    }
255

    
256
    @Override
257
    public boolean isOptional() {
258
        return optional;
259
    }
260

    
261
    public void setOptional(boolean optional) {
262
        this.optional = optional;
263
    }
264
    
265
    public DefaultEditingServiceParameter setDataType(int dataType) {
266
        this.dataType = dataType;
267
        return this;
268
    }
269

    
270
    @Override
271
    public int getDataType() {
272
        return this.dataType;
273
    }
274

    
275
    public DefaultEditingServiceParameter setAllowNull(boolean allowNull) {
276
        this.allowNull = allowNull;
277
        return this;
278
    }
279

    
280
    public boolean getAllowNull() {
281
        return allowNull;
282
    }
283
    
284
    @Override
285
    public boolean isValidValue(Object value) {
286
        if(value == null){
287
            return this.allowNull;
288
        }
289
        
290
        for (TYPE type : this.types) {
291
            switch (type){
292
                case GEOMETRY:
293
                    if (value instanceof Geometry){
294
                        return true;
295
                    }
296
                    break;
297
                case POSITION:
298
                    if (value instanceof Point){
299
                        return true;
300
                    }
301
                    break;
302
                case LIST_POSITIONS:
303
                    if (value instanceof Point){
304
                        return true;
305
                    }
306
                    break;
307
                case OPTION:
308
                    try{
309
                        Object v = (value instanceof String && StringUtils.isBlank((String)value))?this.defaultValue:null;
310
                        this.options2.getValue(value, v);
311
                        return true;
312
                    } catch (InvalidEntryException ex){
313
                        //DO NOTHING
314
                    }
315
                    break;
316
                case SELECTION:
317
                    if (value instanceof FeatureSelection) {
318
                        return true;
319
                    }
320
                    break;
321
                case MULTILAYER_SELECTION:
322
                    if (value instanceof Geometry || value instanceof List){
323
                        return true;
324
                    }
325
                    break;
326
                case DISTANCE:
327
                    if (value instanceof Double){
328
                        return true;
329
                    }
330
                    break;
331
                case CLIPBOARD:
332
                    return true;
333
                case VALUE:
334
                    DataTypesManager manager = ToolsLocator.getDataTypesManager();
335
                    DataType dtype = manager.get(dataType);
336
                    if (!dtype.isObject()) {
337
                        try {
338
                            Object v = dtype.coerce(value);
339
                            return true;
340
                        } catch (CoercionException ex) {
341
                            //DO NOTHING;
342
                        }
343
                    }
344
                    break;
345
            }
346
        }
347
        return false;
348
    }
349

    
350
    @Override
351
    public Object coerceValue(Object value) {
352
        DataTypesManager manager = ToolsLocator.getDataTypesManager();
353
        DataType dtype = manager.get(dataType);
354
        try {
355
            Object v = dtype.coerce(value);
356
            return v;
357
        } catch (CoercionException ex) {
358
            throw new IllegalArgumentException("Illegal value '"+Objects.toString(value)+"' for parameter '"+this.getName()+"'", ex);
359
        }
360
    }
361
    
362
    
363
    
364

    
365
}