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