svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.spi / src / main / java / org / gvsig / fmap / dal / feature / spi / DefaultFeatureProvider.java @ 45155
History | View | Annotate | Download (11.1 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 modify it under
|
7 |
* the terms of the GNU General Public License as published by the Free Software
|
8 |
* Foundation; either version 3 of the License, or (at your option) any later
|
9 |
* version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful, but WITHOUT
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
13 |
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
14 |
* details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License along with
|
17 |
* this program; if not, write to the Free Software Foundation, Inc., 51
|
18 |
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us at info AT
|
21 |
* gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.fmap.dal.feature.spi; |
24 |
|
25 |
import org.apache.commons.lang3.ArrayUtils; |
26 |
import org.apache.commons.lang3.StringUtils; |
27 |
import org.gvsig.fmap.dal.DataTypes; |
28 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
29 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
30 |
import org.gvsig.fmap.dal.feature.FeatureType; |
31 |
import org.gvsig.fmap.geom.Geometry; |
32 |
import org.gvsig.fmap.geom.primitive.Envelope; |
33 |
import org.gvsig.timesupport.Time; |
34 |
import org.gvsig.tools.ToolsLocator; |
35 |
import org.gvsig.tools.dataTypes.Coercion; |
36 |
import org.gvsig.tools.dataTypes.CoercionException; |
37 |
|
38 |
/**
|
39 |
* Default implementation for {@link FeatureProvider}
|
40 |
*
|
41 |
*/
|
42 |
public class DefaultFeatureProvider implements FeatureProvider { |
43 |
|
44 |
protected FeatureType featureType;
|
45 |
protected boolean[] nulls; |
46 |
protected Object[] values; |
47 |
protected Geometry defaultGeometry;
|
48 |
protected Envelope envelope;
|
49 |
private Object oid; |
50 |
private boolean isNew = false; |
51 |
|
52 |
private String[] extraValuesNames; |
53 |
private Object[] extraValues; |
54 |
|
55 |
public DefaultFeatureProvider(FeatureType type) {
|
56 |
if (type instanceof EditableFeatureType) { |
57 |
throw new IllegalArgumentException("type can't by editable."); |
58 |
} |
59 |
this.featureType = type;
|
60 |
this.values = new Object[featureType.size()]; |
61 |
this.nulls = new boolean[featureType.size()]; |
62 |
|
63 |
this.envelope = null; |
64 |
this.defaultGeometry = null; |
65 |
this.oid = null; |
66 |
} |
67 |
|
68 |
public DefaultFeatureProvider(FeatureType type, Object oid) { |
69 |
this(type);
|
70 |
this.oid = oid;
|
71 |
} |
72 |
|
73 |
public DefaultFeatureProvider(FeatureType type, DefaultFeatureProvider data) {
|
74 |
this(type);
|
75 |
this.oid = data.oid;
|
76 |
this.defaultGeometry = data.defaultGeometry;
|
77 |
this.envelope = data.envelope;
|
78 |
this.isNew = data.isNew;
|
79 |
} |
80 |
|
81 |
public boolean isReadOnly(int i) { |
82 |
FeatureAttributeDescriptor attribute = featureType.getAttributeDescriptor(i); |
83 |
if (attribute.getEvaluator() != null) { |
84 |
return true; |
85 |
} |
86 |
return false; |
87 |
} |
88 |
|
89 |
@Override
|
90 |
public void set(int i, Object value) { |
91 |
FeatureAttributeDescriptor attribute = featureType.getAttributeDescriptor(i); |
92 |
// if (this.isReadOnly(i)) {
|
93 |
// return;
|
94 |
// }
|
95 |
if (featureType.getDefaultGeometryAttributeIndex() == i) {
|
96 |
if (value instanceof Geometry) { |
97 |
defaultGeometry = (Geometry) value; |
98 |
} else {
|
99 |
try {
|
100 |
defaultGeometry = (Geometry) ToolsLocator.getDataTypesManager().coerce(DataTypes.GEOMETRY, value); |
101 |
} catch (CoercionException ex) {
|
102 |
|
103 |
} |
104 |
} |
105 |
envelope = null;
|
106 |
} else if(value != null) { |
107 |
Class objectClass = attribute.getObjectClass();
|
108 |
if (objectClass != null) { |
109 |
if (!objectClass.isInstance(value)) {
|
110 |
try {
|
111 |
Coercion coercer = attribute.getDataType().getCoercion(); |
112 |
if (attribute.getType() == DataTypes.STRING && value instanceof Boolean) { |
113 |
value = coercer.coerce(value, attribute.getCoercionContext()); |
114 |
value = StringUtils.left((String) value, attribute.getSize());
|
115 |
} else {
|
116 |
value = coercer.coerce(value, attribute.getCoercionContext()); |
117 |
} |
118 |
} catch (CoercionException e) {
|
119 |
throw new IllegalArgumentException("Can't convert to " |
120 |
+ attribute.getDataType().getName() |
121 |
+ " from '"
|
122 |
+ value.getClass().getName() |
123 |
+ "' with value '"
|
124 |
+ value.toString() |
125 |
+ "' and context '"
|
126 |
+ attribute.getCoercionContext() |
127 |
+ "'.");
|
128 |
} |
129 |
} |
130 |
} |
131 |
} |
132 |
if (value == null) { |
133 |
nulls[i] = true;
|
134 |
values[i] = attribute.getDefaultValue(); |
135 |
} else {
|
136 |
values[i] = value; |
137 |
nulls[i] = false;
|
138 |
} |
139 |
} |
140 |
|
141 |
/*
|
142 |
* (non-Javadoc)
|
143 |
*
|
144 |
* @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#set(java.lang.String,
|
145 |
* java.lang.Object)
|
146 |
*/
|
147 |
public void set(String name, Object value) { |
148 |
set(featureType.getIndex(name), value); |
149 |
} |
150 |
|
151 |
/*
|
152 |
* (non-Javadoc)
|
153 |
*
|
154 |
* @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#get(int)
|
155 |
*/
|
156 |
public Object get(int i) { |
157 |
return values[i];
|
158 |
} |
159 |
|
160 |
/*
|
161 |
* (non-Javadoc)
|
162 |
*
|
163 |
* @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#get(java.lang.String)
|
164 |
*/
|
165 |
public Object get(String name) { |
166 |
FeatureAttributeDescriptor featureAttributeDescriptor = featureType.getAttributeDescriptor(name); |
167 |
return values[featureAttributeDescriptor.getIndex()];
|
168 |
} |
169 |
|
170 |
/*
|
171 |
* (non-Javadoc)
|
172 |
*
|
173 |
* @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#getType()
|
174 |
*/
|
175 |
public FeatureType getType() {
|
176 |
return featureType;
|
177 |
} |
178 |
|
179 |
/*
|
180 |
* (non-Javadoc)
|
181 |
*
|
182 |
* @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#getCopy()
|
183 |
*/
|
184 |
public FeatureProvider getCopy() {
|
185 |
DefaultFeatureProvider data = new DefaultFeatureProvider(
|
186 |
this.getType());
|
187 |
return getCopy(data);
|
188 |
} |
189 |
|
190 |
/**
|
191 |
* Copy values from current instance to <code>data</code>
|
192 |
*/
|
193 |
protected FeatureProvider getCopy(DefaultFeatureProvider data) {
|
194 |
data.oid = this.oid;
|
195 |
System.arraycopy(this.values, 0, data.values, 0, this.values.length); |
196 |
data.defaultGeometry = this.defaultGeometry;
|
197 |
data.envelope = this.envelope;
|
198 |
data.isNew = this.isNew;
|
199 |
if (this.extraValues!=null) { |
200 |
data.extraValues = new Object[this.extraValues.length]; |
201 |
System.arraycopy(this.extraValues, 0, data.extraValues, 0, this.extraValues.length); |
202 |
} else {
|
203 |
data.extraValues = null;
|
204 |
} |
205 |
if (this.extraValuesNames!=null) { |
206 |
data.extraValuesNames = new String[this.extraValuesNames.length]; |
207 |
System.arraycopy(this.extraValuesNames, 0, data.extraValuesNames, 0, this.extraValuesNames.length); |
208 |
} else {
|
209 |
data.extraValuesNames = null;
|
210 |
} |
211 |
return data;
|
212 |
} |
213 |
|
214 |
/*
|
215 |
* (non-Javadoc)
|
216 |
*
|
217 |
* @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#getDefaultEnvelope()
|
218 |
*/
|
219 |
public Envelope getDefaultEnvelope() {
|
220 |
if (envelope == null && getDefaultGeometry() != null) { |
221 |
envelope = getDefaultGeometry().getEnvelope(); |
222 |
} |
223 |
return envelope;
|
224 |
} |
225 |
|
226 |
/*
|
227 |
* (non-Javadoc)
|
228 |
*
|
229 |
* @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#getDefaultGeometry()
|
230 |
*/
|
231 |
public Geometry getDefaultGeometry() {
|
232 |
return this.defaultGeometry; |
233 |
} |
234 |
|
235 |
@Override
|
236 |
public Time getDefaultTime() { |
237 |
int index = featureType.getDefaultTimeAttributeIndex();
|
238 |
Object value = this.get(index); |
239 |
if (! (value instanceof Time) ) { |
240 |
try {
|
241 |
value = (Time) ToolsLocator.getDataTypesManager().coerce(DataTypes.INSTANT, value);
|
242 |
} catch (CoercionException ex) {
|
243 |
|
244 |
} |
245 |
} |
246 |
return (Time) value; |
247 |
} |
248 |
|
249 |
/*
|
250 |
* (non-Javadoc)
|
251 |
*
|
252 |
* @see
|
253 |
* org.gvsig.fmap.dal.feature.spi.FeatureProvider#setDefaultEnvelope(org
|
254 |
* .gvsig.fmap.geom.primitive.Envelope)
|
255 |
*/
|
256 |
public void setDefaultEnvelope(Envelope envelope) { |
257 |
this.envelope = envelope;
|
258 |
} |
259 |
|
260 |
@Override
|
261 |
public void setDefaultGeometry(Geometry geom) { |
262 |
int i = featureType.getDefaultGeometryAttributeIndex();
|
263 |
this.set(i, geom);
|
264 |
} |
265 |
|
266 |
@Override
|
267 |
public void setDefaultTime(Time time) { |
268 |
int i = featureType.getDefaultTimeAttributeIndex();
|
269 |
this.set(i, time);
|
270 |
} |
271 |
|
272 |
|
273 |
public boolean isNull(int i) { |
274 |
return nulls[i];
|
275 |
} |
276 |
|
277 |
/*
|
278 |
* (non-Javadoc)
|
279 |
*
|
280 |
* @see
|
281 |
* org.gvsig.fmap.dal.feature.spi.FeatureProvider#isNull(java.lang.String)
|
282 |
*/
|
283 |
public boolean isNull(String name) { |
284 |
int i = featureType.getIndex(name);
|
285 |
return isNull(i);
|
286 |
} |
287 |
|
288 |
/*
|
289 |
* (non-Javadoc)
|
290 |
*
|
291 |
* @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#getOID()
|
292 |
*/
|
293 |
public Object getOID() { |
294 |
return this.oid; |
295 |
} |
296 |
|
297 |
/*
|
298 |
* (non-Javadoc)
|
299 |
*
|
300 |
* @see
|
301 |
* org.gvsig.fmap.dal.feature.spi.FeatureProvider#setOID(java.lang.Object)
|
302 |
*/
|
303 |
public void setOID(Object oid) { |
304 |
this.oid = oid;
|
305 |
} |
306 |
|
307 |
/*
|
308 |
* (non-Javadoc)
|
309 |
*
|
310 |
* @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#isNew()
|
311 |
*/
|
312 |
public boolean isNew() { |
313 |
return isNew;
|
314 |
} |
315 |
|
316 |
/*
|
317 |
* (non-Javadoc)
|
318 |
*
|
319 |
* @see org.gvsig.fmap.dal.feature.spi.FeatureProvider#setNew(boolean)
|
320 |
*/
|
321 |
public void setNew(boolean isNew) { |
322 |
this.isNew = isNew;
|
323 |
} |
324 |
|
325 |
@Override
|
326 |
public void setExtraValue(int index, Object value) { |
327 |
this.extraValues[index] = value;
|
328 |
} |
329 |
|
330 |
@Override
|
331 |
public Object getExtraValue(int index) { |
332 |
if( this.extraValues == null ) { |
333 |
return null; |
334 |
} |
335 |
return this.extraValues[index]; |
336 |
} |
337 |
|
338 |
@Override
|
339 |
public Object getExtraValue(String name) { |
340 |
if( this.extraValuesNames == null || this.extraValues == null) { |
341 |
return null; |
342 |
} |
343 |
for (int i = 0; i < this.extraValuesNames.length; i++) { |
344 |
if( StringUtils.equalsIgnoreCase(this.extraValuesNames[i], name) ) { |
345 |
return this.extraValues[i]; |
346 |
} |
347 |
} |
348 |
return null; |
349 |
} |
350 |
|
351 |
@Override
|
352 |
public boolean hasExtraValue(String name) { |
353 |
if( this.extraValuesNames == null ) { |
354 |
return false; |
355 |
} |
356 |
for (int i = 0; i < this.extraValuesNames.length; i++) { |
357 |
if( StringUtils.equalsIgnoreCase(this.extraValuesNames[i], name) ) { |
358 |
return true; |
359 |
} |
360 |
} |
361 |
return false; |
362 |
} |
363 |
|
364 |
@Override
|
365 |
public void setExtraValueNames(String[] extraValueNames) { |
366 |
this.extraValuesNames = extraValueNames;
|
367 |
this.extraValues = new Object[this.extraValuesNames.length]; |
368 |
} |
369 |
|
370 |
|
371 |
} |