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 / DefaultEditableFeatureType.java @ 42293
History | View | Annotate | Download (13.1 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | *
|
||
4 | * Copyright (C) 2007-2013 gvSIG Association.
|
||
5 | *
|
||
6 | 42290 | jjdelcerro | * 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 | 40559 | jjdelcerro | *
|
11 | 42290 | jjdelcerro | * 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 | 40559 | jjdelcerro | *
|
16 | 42290 | jjdelcerro | * 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 | 40559 | jjdelcerro | *
|
20 | 42290 | jjdelcerro | * For any additional information, do not hesitate to contact us at info AT
|
21 | * gvsig.com, or visit our website www.gvsig.com.
|
||
22 | 40559 | jjdelcerro | */
|
23 | 40435 | jjdelcerro | package org.gvsig.fmap.dal.feature.impl; |
24 | |||
25 | import java.text.MessageFormat; |
||
26 | |||
27 | import java.util.Iterator; |
||
28 | 42027 | jjdelcerro | import java.util.zip.CRC32; |
29 | 40435 | jjdelcerro | |
30 | import org.gvsig.fmap.dal.DataTypes; |
||
31 | import org.gvsig.fmap.dal.exception.DataListException; |
||
32 | import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
||
33 | import org.gvsig.fmap.dal.feature.EditableFeatureType; |
||
34 | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
||
35 | 41335 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator; |
36 | 40435 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureType; |
37 | import org.gvsig.fmap.dal.feature.exception.FeatureTypeIntegrityException; |
||
38 | import org.gvsig.fmap.dal.feature.exception.UnsupportedDataTypeException; |
||
39 | 42293 | jjdelcerro | import org.gvsig.tools.ToolsLocator; |
40 | 40435 | jjdelcerro | import org.gvsig.tools.evaluator.Evaluator; |
41 | |||
42 | public class DefaultEditableFeatureType extends DefaultFeatureType implements |
||
43 | 42290 | jjdelcerro | EditableFeatureType { |
44 | 40435 | jjdelcerro | |
45 | 42290 | jjdelcerro | /**
|
46 | *
|
||
47 | */
|
||
48 | private static final long serialVersionUID = -713976880396024389L; |
||
49 | 40435 | jjdelcerro | |
50 | 42290 | jjdelcerro | private boolean hasStrongChanges; |
51 | private DefaultFeatureType source;
|
||
52 | 40435 | jjdelcerro | |
53 | 42290 | jjdelcerro | public DefaultEditableFeatureType() {
|
54 | super();
|
||
55 | this.hasStrongChanges = false; |
||
56 | this.source = null; |
||
57 | } |
||
58 | 40435 | jjdelcerro | |
59 | 42290 | jjdelcerro | public DefaultEditableFeatureType(String id) { |
60 | super(id);
|
||
61 | this.hasStrongChanges = false; |
||
62 | this.source = null; |
||
63 | } |
||
64 | 40435 | jjdelcerro | |
65 | 42290 | jjdelcerro | protected DefaultEditableFeatureType(DefaultEditableFeatureType other) {
|
66 | super(other);
|
||
67 | this.source = (DefaultFeatureType) other.getSource();
|
||
68 | } |
||
69 | 40435 | jjdelcerro | |
70 | 42290 | jjdelcerro | protected DefaultEditableFeatureType(DefaultFeatureType other) {
|
71 | super(other);
|
||
72 | this.source = other;
|
||
73 | } |
||
74 | 40435 | jjdelcerro | |
75 | 42290 | jjdelcerro | protected void intitalizeAddAttibute(DefaultFeatureAttributeDescriptor attr) { |
76 | super.add(new DefaultEditableFeatureAttributeDescriptor(attr)); |
||
77 | } |
||
78 | 40435 | jjdelcerro | |
79 | 42290 | jjdelcerro | public boolean hasStrongChanges() { |
80 | if (hasStrongChanges) {
|
||
81 | return true; |
||
82 | } |
||
83 | Iterator iter = this.iterator(); |
||
84 | DefaultEditableFeatureAttributeDescriptor attr; |
||
85 | while (iter.hasNext()) {
|
||
86 | attr = (DefaultEditableFeatureAttributeDescriptor) iter.next(); |
||
87 | if (attr.hasStrongChanges()) {
|
||
88 | return true; |
||
89 | } |
||
90 | } |
||
91 | return false; |
||
92 | } |
||
93 | 40435 | jjdelcerro | |
94 | 42290 | jjdelcerro | public FeatureType getCopy() {
|
95 | return new DefaultEditableFeatureType(this); |
||
96 | } |
||
97 | 40435 | jjdelcerro | |
98 | 42290 | jjdelcerro | public EditableFeatureType getEditable() {
|
99 | throw new UnsupportedOperationException(); |
||
100 | } |
||
101 | 40435 | jjdelcerro | |
102 | 42290 | jjdelcerro | public boolean addAll(DefaultFeatureType other) { |
103 | Iterator iter = other.iterator();
|
||
104 | DefaultFeatureAttributeDescriptor attr; |
||
105 | DefaultEditableFeatureAttributeDescriptor editableAttr; |
||
106 | while (iter.hasNext()) {
|
||
107 | attr = (DefaultFeatureAttributeDescriptor) iter.next(); |
||
108 | if (attr instanceof DefaultEditableFeatureAttributeDescriptor) { |
||
109 | editableAttr = new DefaultEditableFeatureAttributeDescriptor(
|
||
110 | attr); |
||
111 | } else {
|
||
112 | editableAttr = new DefaultEditableFeatureAttributeDescriptor(
|
||
113 | attr); |
||
114 | } |
||
115 | super.add(editableAttr);
|
||
116 | } |
||
117 | this.pk = null; |
||
118 | this.fixAll();
|
||
119 | return true; |
||
120 | } |
||
121 | 40435 | jjdelcerro | |
122 | 42290 | jjdelcerro | public EditableFeatureAttributeDescriptor addLike(
|
123 | FeatureAttributeDescriptor other) { |
||
124 | DefaultEditableFeatureAttributeDescriptor editableAttr; |
||
125 | if (other instanceof DefaultEditableFeatureAttributeDescriptor) { |
||
126 | editableAttr = new DefaultEditableFeatureAttributeDescriptor(
|
||
127 | (DefaultFeatureAttributeDescriptor) other); |
||
128 | } else {
|
||
129 | editableAttr = new DefaultEditableFeatureAttributeDescriptor(
|
||
130 | (DefaultFeatureAttributeDescriptor) other); |
||
131 | } |
||
132 | super.add(editableAttr);
|
||
133 | this.fixAll();
|
||
134 | return editableAttr;
|
||
135 | 40435 | jjdelcerro | |
136 | 42290 | jjdelcerro | } |
137 | 40435 | jjdelcerro | |
138 | 42290 | jjdelcerro | public FeatureType getSource() {
|
139 | return source;
|
||
140 | } |
||
141 | 40435 | jjdelcerro | |
142 | 42290 | jjdelcerro | private long getCRC() { |
143 | StringBuffer buffer = new StringBuffer(); |
||
144 | for (int i = 0; i < this.size(); i++) { |
||
145 | FeatureAttributeDescriptor x = this.getAttributeDescriptor(i);
|
||
146 | buffer.append(x.getName()); |
||
147 | buffer.append(x.getDataTypeName()); |
||
148 | buffer.append(x.getSize()); |
||
149 | } |
||
150 | CRC32 crc = new CRC32(); |
||
151 | byte[] data = buffer.toString().getBytes(); |
||
152 | crc.update(data); |
||
153 | return crc.getValue();
|
||
154 | } |
||
155 | 40435 | jjdelcerro | |
156 | 42290 | jjdelcerro | public FeatureType getNotEditableCopy() {
|
157 | this.fixAll();
|
||
158 | DefaultFeatureType copy = new DefaultFeatureType(this, false); |
||
159 | Iterator iter = this.iterator(); |
||
160 | DefaultFeatureAttributeDescriptor attr; |
||
161 | while (iter.hasNext()) {
|
||
162 | attr = (DefaultFeatureAttributeDescriptor) iter.next(); |
||
163 | copy.add(new DefaultFeatureAttributeDescriptor(attr));
|
||
164 | 42027 | jjdelcerro | } |
165 | 42290 | jjdelcerro | return copy;
|
166 | } |
||
167 | 42027 | jjdelcerro | |
168 | 42290 | jjdelcerro | public EditableFeatureAttributeDescriptor add(String name, int type) { |
169 | return this.add(name, type, true); |
||
170 | } |
||
171 | 40435 | jjdelcerro | |
172 | private EditableFeatureAttributeDescriptor add(String name, int type, boolean updateHasStrongChanges) { |
||
173 | 42290 | jjdelcerro | DefaultEditableFeatureAttributeDescriptor attr = new DefaultEditableFeatureAttributeDescriptor();
|
174 | Iterator iter = this.iterator(); |
||
175 | while (iter.hasNext()) {
|
||
176 | EditableFeatureAttributeDescriptor descriptor = (EditableFeatureAttributeDescriptor) iter.next(); |
||
177 | if (descriptor.getName().equalsIgnoreCase(name)) {
|
||
178 | throw new RuntimeException( |
||
179 | MessageFormat.format("Name descriptor {0} duplicated.", new String[]{name}) |
||
180 | ); |
||
181 | } |
||
182 | 40435 | jjdelcerro | |
183 | 42290 | jjdelcerro | } |
184 | attr.setName(name); |
||
185 | switch (type) {
|
||
186 | case DataTypes.BOOLEAN:
|
||
187 | case DataTypes.BYTE:
|
||
188 | case DataTypes.BYTEARRAY:
|
||
189 | case DataTypes.CHAR:
|
||
190 | case DataTypes.DATE:
|
||
191 | case DataTypes.DOUBLE:
|
||
192 | case DataTypes.FLOAT:
|
||
193 | case DataTypes.GEOMETRY:
|
||
194 | case DataTypes.INT:
|
||
195 | case DataTypes.LONG:
|
||
196 | case DataTypes.OBJECT:
|
||
197 | case DataTypes.STRING:
|
||
198 | case DataTypes.TIME:
|
||
199 | case DataTypes.TIMESTAMP:
|
||
200 | break;
|
||
201 | 40435 | jjdelcerro | |
202 | 42290 | jjdelcerro | default:
|
203 | throw new UnsupportedDataTypeException(name, type); |
||
204 | } |
||
205 | attr.setDataType(type); |
||
206 | attr.setIndex(this.size());
|
||
207 | super.add(attr);
|
||
208 | if (!hasStrongChanges && updateHasStrongChanges) {
|
||
209 | hasStrongChanges = true;
|
||
210 | } |
||
211 | this.pk = null; |
||
212 | return attr;
|
||
213 | } |
||
214 | 40435 | jjdelcerro | |
215 | 42290 | jjdelcerro | public EditableFeatureAttributeDescriptor add(String name, int type, |
216 | int size) {
|
||
217 | return this.add(name, type, true).setSize(size); |
||
218 | } |
||
219 | 40435 | jjdelcerro | |
220 | 42290 | jjdelcerro | public EditableFeatureAttributeDescriptor add(String name, int type, |
221 | Evaluator evaluator) { |
||
222 | if (evaluator == null) { |
||
223 | throw new IllegalArgumentException(); |
||
224 | } |
||
225 | return this.add(name, type, false).setEvaluator(evaluator); |
||
226 | } |
||
227 | 41335 | jjdelcerro | |
228 | 42290 | jjdelcerro | public EditableFeatureAttributeDescriptor add(String name, int type, |
229 | FeatureAttributeEmulator emulator) { |
||
230 | if (emulator == null) { |
||
231 | throw new IllegalArgumentException(); |
||
232 | } |
||
233 | return this.add(name, type, false).setFeatureAttributeEmulator(emulator); |
||
234 | } |
||
235 | 42293 | jjdelcerro | |
236 | public EditableFeatureAttributeDescriptor add(String name, String type) { |
||
237 | return this.add(name,ToolsLocator.getDataTypesManager().getType(type)); |
||
238 | } |
||
239 | 40435 | jjdelcerro | |
240 | 42293 | jjdelcerro | public EditableFeatureAttributeDescriptor add(String name, String type, int size) { |
241 | return this.add(name,ToolsLocator.getDataTypesManager().getType(type), size); |
||
242 | } |
||
243 | |||
244 | 42290 | jjdelcerro | public Object remove(String name) { |
245 | DefaultFeatureAttributeDescriptor attr = (DefaultFeatureAttributeDescriptor) this
|
||
246 | .get(name); |
||
247 | if (attr == null) { |
||
248 | return null; |
||
249 | } |
||
250 | if (attr.getEvaluator() == null) { |
||
251 | hasStrongChanges = true;
|
||
252 | } |
||
253 | super.remove(attr);
|
||
254 | this.pk = null; |
||
255 | this.fixAll();
|
||
256 | return attr;
|
||
257 | } |
||
258 | 40435 | jjdelcerro | |
259 | 42290 | jjdelcerro | protected void fixAll() { |
260 | int i = 0; |
||
261 | Iterator iter = super.iterator(); |
||
262 | DefaultFeatureAttributeDescriptor attr; |
||
263 | 40435 | jjdelcerro | |
264 | 42290 | jjdelcerro | while (iter.hasNext()) {
|
265 | attr = (DefaultFeatureAttributeDescriptor) iter |
||
266 | .next(); |
||
267 | attr.setIndex(i++); |
||
268 | if (attr instanceof DefaultEditableFeatureAttributeDescriptor) { |
||
269 | ((DefaultEditableFeatureAttributeDescriptor) attr).fixAll(); |
||
270 | } |
||
271 | if (attr.getEvaluator() != null) { |
||
272 | this.hasEvaluators = true; |
||
273 | } |
||
274 | if (attr.getFeatureAttributeEmulator() != null) { |
||
275 | this.hasEmulators = true; |
||
276 | } |
||
277 | if (this.defaultGeometryAttributeName == null && attr.getType() == DataTypes.GEOMETRY) { |
||
278 | this.defaultGeometryAttributeName = attr.getName();
|
||
279 | } |
||
280 | } |
||
281 | if (this.defaultGeometryAttributeName != null) { |
||
282 | this.defaultGeometryAttributeIndex = this.getIndex(this.defaultGeometryAttributeName); |
||
283 | } |
||
284 | this.internalID = Long.toHexString(this.getCRC()); |
||
285 | } |
||
286 | 40435 | jjdelcerro | |
287 | 42290 | jjdelcerro | public void checkIntegrity() throws DataListException { |
288 | Iterator iter = super.iterator(); |
||
289 | FeatureTypeIntegrityException ex = null;
|
||
290 | 40435 | jjdelcerro | |
291 | 42290 | jjdelcerro | while (iter.hasNext()) {
|
292 | DefaultEditableFeatureAttributeDescriptor attr = (DefaultEditableFeatureAttributeDescriptor) iter |
||
293 | .next(); |
||
294 | try {
|
||
295 | attr.checkIntegrity(); |
||
296 | } catch (Exception e) { |
||
297 | if (ex == null) { |
||
298 | ex = new FeatureTypeIntegrityException(this.getId()); |
||
299 | } |
||
300 | ex.add(e); |
||
301 | } |
||
302 | } |
||
303 | if (ex != null) { |
||
304 | throw ex;
|
||
305 | } |
||
306 | } |
||
307 | 40435 | jjdelcerro | |
308 | 42290 | jjdelcerro | public boolean remove(EditableFeatureAttributeDescriptor attribute) { |
309 | if (attribute.getEvaluator() != null) { |
||
310 | hasStrongChanges = true;
|
||
311 | } |
||
312 | if (!super.remove(attribute)) { |
||
313 | return false; |
||
314 | } |
||
315 | this.fixAll();
|
||
316 | return true; |
||
317 | } |
||
318 | 40435 | jjdelcerro | |
319 | 42290 | jjdelcerro | public void setDefaultGeometryAttributeName(String name) { |
320 | if (name == null || name.length() == 0) { |
||
321 | this.defaultGeometryAttributeName = null; |
||
322 | this.defaultGeometryAttributeIndex = -1; |
||
323 | return;
|
||
324 | } |
||
325 | DefaultFeatureAttributeDescriptor attr = (DefaultFeatureAttributeDescriptor) this
|
||
326 | .get(name); |
||
327 | if (attr == null) { |
||
328 | throw new IllegalArgumentException("Attribute '" + name |
||
329 | + "' not found.");
|
||
330 | } |
||
331 | if (attr.getType() != DataTypes.GEOMETRY) {
|
||
332 | throw new IllegalArgumentException("Attribute '" + name |
||
333 | + "' is not a geometry.");
|
||
334 | } |
||
335 | this.defaultGeometryAttributeName = name;
|
||
336 | this.defaultGeometryAttributeIndex = attr.getIndex();
|
||
337 | } |
||
338 | 40435 | jjdelcerro | |
339 | 42290 | jjdelcerro | public void setHasOID(boolean hasOID) { |
340 | this.hasOID = hasOID;
|
||
341 | } |
||
342 | 40435 | jjdelcerro | |
343 | 42290 | jjdelcerro | protected Iterator getIterator(Iterator iter) { |
344 | return new EditableDelegatedIterator(iter, this); |
||
345 | } |
||
346 | |||
347 | 41272 | jjdelcerro | public EditableFeatureAttributeDescriptor getEditableAttributeDescriptor(String name) { |
348 | return (EditableFeatureAttributeDescriptor) this.getAttributeDescriptor(name); |
||
349 | } |
||
350 | |||
351 | public EditableFeatureAttributeDescriptor getEditableAttributeDescriptor(int index) { |
||
352 | 42290 | jjdelcerro | return (EditableFeatureAttributeDescriptor) this.getAttributeDescriptor(index); |
353 | 41272 | jjdelcerro | } |
354 | |||
355 | 42290 | jjdelcerro | protected class EditableDelegatedIterator extends DelegatedIterator { |
356 | 40435 | jjdelcerro | |
357 | 42290 | jjdelcerro | private DefaultEditableFeatureType fType;
|
358 | 40435 | jjdelcerro | |
359 | 42290 | jjdelcerro | public EditableDelegatedIterator(Iterator iter, |
360 | DefaultEditableFeatureType fType) { |
||
361 | super(iter);
|
||
362 | this.fType = fType;
|
||
363 | } |
||
364 | 40435 | jjdelcerro | |
365 | 42290 | jjdelcerro | public void remove() { |
366 | this.iterator.remove();
|
||
367 | this.fType.fixAll();
|
||
368 | } |
||
369 | 40435 | jjdelcerro | |
370 | 42290 | jjdelcerro | } |
371 | 40435 | jjdelcerro | |
372 | 42290 | jjdelcerro | protected void setAllowAutomaticValues(boolean value) { |
373 | this.allowAtomaticValues = value;
|
||
374 | } |
||
375 | 40435 | jjdelcerro | |
376 | public void setDefaultTimeAttributeName(String name) { |
||
377 | if (name == null || name.length() == 0) { |
||
378 | this.defaultTimeAttributeIndex = -1; |
||
379 | return;
|
||
380 | } |
||
381 | DefaultFeatureAttributeDescriptor attr = (DefaultFeatureAttributeDescriptor) this
|
||
382 | .get(name); |
||
383 | if (attr == null) { |
||
384 | throw new IllegalArgumentException("Attribute '" + name |
||
385 | + "' not found.");
|
||
386 | } |
||
387 | |||
388 | 42290 | jjdelcerro | this.defaultTimeAttributeIndex = attr.getIndex();
|
389 | 40435 | jjdelcerro | } |
390 | } |