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