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