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 @ 47349
History | View | Annotate | Download (16.2 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 |
import java.util.Iterator; |
27 |
import javax.json.JsonObject; |
28 |
import org.apache.commons.lang3.StringUtils; |
29 |
import org.gvsig.fmap.dal.DataTypeUtils; |
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 |
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator; |
35 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
36 |
import org.gvsig.fmap.dal.feature.FeatureType; |
37 |
import org.gvsig.fmap.dal.feature.exception.FeatureTypeIntegrityException; |
38 |
import org.gvsig.fmap.dal.impl.DefaultDataManager; |
39 |
import org.gvsig.json.Json; |
40 |
import org.gvsig.json.JsonManager; |
41 |
import org.gvsig.json.JsonObjectBuilder; |
42 |
import org.gvsig.json.SupportToJson; |
43 |
import org.gvsig.tools.ToolsLocator; |
44 |
import org.gvsig.tools.evaluator.Evaluator; |
45 |
|
46 |
@SuppressWarnings("UseSpecificCatch") |
47 |
public class DefaultEditableFeatureType extends DefaultFeatureType implements |
48 |
EditableFeatureType { |
49 |
|
50 |
/**
|
51 |
*
|
52 |
*/
|
53 |
private static final long serialVersionUID = -713976880396024389L; |
54 |
|
55 |
private boolean hasStrongChanges; |
56 |
private DefaultFeatureType source;
|
57 |
|
58 |
public DefaultEditableFeatureType(FeatureStore store) {
|
59 |
super(store);
|
60 |
this.hasStrongChanges = false; |
61 |
this.source = null; |
62 |
} |
63 |
|
64 |
public DefaultEditableFeatureType(FeatureStore store, String id) { |
65 |
super(store, id);
|
66 |
this.hasStrongChanges = false; |
67 |
this.source = null; |
68 |
} |
69 |
|
70 |
protected DefaultEditableFeatureType(DefaultEditableFeatureType other) {
|
71 |
super(other);
|
72 |
this.hasStrongChanges = other.hasStrongChanges;
|
73 |
this.source = (DefaultFeatureType) other.getSource();
|
74 |
} |
75 |
|
76 |
protected DefaultEditableFeatureType(DefaultFeatureType other) {
|
77 |
super(other);
|
78 |
this.source = other;
|
79 |
this.fixAll();
|
80 |
} |
81 |
|
82 |
@Override
|
83 |
protected DefaultFeatureAttributeDescriptor getCopyAttributeDescriptor(DefaultFeatureAttributeDescriptor src) {
|
84 |
DefaultFeatureAttributeDescriptor copy = new DefaultEditableFeatureAttributeDescriptor(src);
|
85 |
copy.setFeatureType(this);
|
86 |
return copy;
|
87 |
} |
88 |
|
89 |
@Override
|
90 |
public void forceStrongChanges() { |
91 |
this.hasStrongChanges = true; |
92 |
} |
93 |
|
94 |
public boolean hasStrongChanges() { |
95 |
if (hasStrongChanges) {
|
96 |
return true; |
97 |
} |
98 |
Iterator iter = this.iterator(); |
99 |
DefaultEditableFeatureAttributeDescriptor attr; |
100 |
while (iter.hasNext()) {
|
101 |
attr = (DefaultEditableFeatureAttributeDescriptor) iter.next(); |
102 |
if (attr.isComputed()) {
|
103 |
continue;
|
104 |
} |
105 |
if (attr.hasStrongChanges()) {
|
106 |
return true; |
107 |
} |
108 |
} |
109 |
return false; |
110 |
} |
111 |
|
112 |
@Override
|
113 |
public FeatureType getCopy() {
|
114 |
DefaultEditableFeatureType copy = new DefaultEditableFeatureType(this); |
115 |
copy.fixAll(); |
116 |
return copy;
|
117 |
} |
118 |
|
119 |
@Override
|
120 |
public EditableFeatureType getEditable() {
|
121 |
return (EditableFeatureType) this.getCopy(); |
122 |
} |
123 |
|
124 |
public boolean addAll(DefaultFeatureType other) { |
125 |
Iterator iter = other.iterator();
|
126 |
DefaultFeatureAttributeDescriptor attr; |
127 |
DefaultEditableFeatureAttributeDescriptor editableAttr; |
128 |
while (iter.hasNext()) {
|
129 |
attr = (DefaultFeatureAttributeDescriptor) iter.next(); |
130 |
if (attr instanceof DefaultEditableFeatureAttributeDescriptor) { |
131 |
editableAttr = new DefaultEditableFeatureAttributeDescriptor(
|
132 |
attr); |
133 |
} else {
|
134 |
editableAttr = new DefaultEditableFeatureAttributeDescriptor(
|
135 |
attr); |
136 |
} |
137 |
super.add(editableAttr);
|
138 |
} |
139 |
this.pk = null; |
140 |
this.fixAll();
|
141 |
return true; |
142 |
} |
143 |
|
144 |
@Override
|
145 |
public EditableFeatureAttributeDescriptor addLike(
|
146 |
FeatureAttributeDescriptor other) { |
147 |
DefaultEditableFeatureAttributeDescriptor editableAttr; |
148 |
editableAttr = new DefaultEditableFeatureAttributeDescriptor(
|
149 |
(DefaultFeatureAttributeDescriptor) other); |
150 |
super.add(editableAttr);
|
151 |
if (!editableAttr.isComputed()) {
|
152 |
this.hasStrongChanges = true; |
153 |
} |
154 |
this.fixAll();
|
155 |
return editableAttr;
|
156 |
} |
157 |
|
158 |
@Override
|
159 |
public FeatureType getSource() {
|
160 |
return source;
|
161 |
} |
162 |
|
163 |
@Override
|
164 |
public FeatureType getNotEditableCopy() {
|
165 |
this.fixAll();
|
166 |
DefaultFeatureType copy = new DefaultFeatureType(this, false); |
167 |
Iterator iter = this.iterator(); |
168 |
DefaultFeatureAttributeDescriptor attr; |
169 |
while (iter.hasNext()) {
|
170 |
attr = (DefaultFeatureAttributeDescriptor) iter.next(); |
171 |
DefaultFeatureAttributeDescriptor newattr = new DefaultFeatureAttributeDescriptor(attr);
|
172 |
newattr.setFeatureType(copy); |
173 |
copy.add(newattr); |
174 |
} |
175 |
return copy;
|
176 |
} |
177 |
|
178 |
@Override
|
179 |
public EditableFeatureAttributeDescriptor add(String name, int type) { |
180 |
return this.add(name, type, true); |
181 |
} |
182 |
|
183 |
@Override
|
184 |
public void removeAll() { |
185 |
while (!super.isEmpty()) { |
186 |
super.remove(0); |
187 |
} |
188 |
this.fixAll();
|
189 |
} |
190 |
|
191 |
@Override
|
192 |
public void addAll(FeatureType attributes) { |
193 |
super.addAll(attributes);
|
194 |
this.fixAll();
|
195 |
} |
196 |
|
197 |
private EditableFeatureAttributeDescriptor add(String name, int type, boolean strongChanges) { |
198 |
Iterator iter = this.iterator(); |
199 |
while (iter.hasNext()) {
|
200 |
EditableFeatureAttributeDescriptor descriptor = (EditableFeatureAttributeDescriptor) iter.next(); |
201 |
if (descriptor.getName().equalsIgnoreCase(name)) {
|
202 |
throw new IllegalArgumentException( |
203 |
MessageFormat.format("Duplicated name descriptor {0}.", name) |
204 |
); |
205 |
} |
206 |
|
207 |
} |
208 |
DefaultEditableFeatureAttributeDescriptor attr = DefaultDataManager.createEditableFeatureAttributeDescriptor(this, name, type, strongChanges);
|
209 |
attr.setIndex(this.size());
|
210 |
|
211 |
this.hasStrongChanges = this.hasStrongChanges || strongChanges; |
212 |
super.add(attr);
|
213 |
this.pk = null; |
214 |
return attr;
|
215 |
} |
216 |
|
217 |
@Override
|
218 |
public EditableFeatureAttributeDescriptor add(String name, int type, int size) { |
219 |
return this.add(name, type, true).setSize(size); |
220 |
} |
221 |
|
222 |
@Override
|
223 |
public EditableFeatureAttributeDescriptor add(String name, int type, |
224 |
Evaluator evaluator) { |
225 |
if (evaluator == null) { |
226 |
throw new IllegalArgumentException(); |
227 |
} |
228 |
return this.add(name, type, false).setEvaluator(evaluator); |
229 |
} |
230 |
|
231 |
@Override
|
232 |
public EditableFeatureAttributeDescriptor add(String name, int type, |
233 |
FeatureAttributeEmulator emulator) { |
234 |
if (emulator == null) { |
235 |
throw new IllegalArgumentException(); |
236 |
} |
237 |
return this.add(name, type, false).setFeatureAttributeEmulator(emulator); |
238 |
} |
239 |
|
240 |
@Override
|
241 |
public EditableFeatureAttributeDescriptor add(String name, String type) { |
242 |
return this.add(name, ToolsLocator.getDataTypesManager().getType(type)); |
243 |
} |
244 |
|
245 |
@Override
|
246 |
public EditableFeatureAttributeDescriptor add(String name, String type, int size) { |
247 |
return this.add(name, ToolsLocator.getDataTypesManager().getType(type), size); |
248 |
} |
249 |
|
250 |
public Object removeAttributeDescriptor(String name) { |
251 |
// Hemos metido los metodos removeAttributeDescriptor ya que existe
|
252 |
// un problema desde python al llamar a los metodos remove. En lugar de llamar
|
253 |
// a estos metodos remove, llama a los de la superclase ArrayList.
|
254 |
return this.remove(name); |
255 |
} |
256 |
|
257 |
public boolean removeAttributeDescriptor(EditableFeatureAttributeDescriptor attribute) { |
258 |
return this.remove(attribute); |
259 |
} |
260 |
|
261 |
@Override
|
262 |
public FeatureAttributeDescriptor remove(int index) { |
263 |
FeatureAttributeDescriptor attr = (FeatureAttributeDescriptor) this.get(index);
|
264 |
if (!attr.isComputed()) {
|
265 |
hasStrongChanges = true;
|
266 |
} |
267 |
if (index == this.getDefaultGeometryAttributeIndex()) { |
268 |
this.defaultGeometryAttributeIndex = -1; |
269 |
this.defaultGeometryAttributeName = null; |
270 |
} else if (index == this.getDefaultTimeAttributeIndex()) { |
271 |
this.defaultTimeAttributeIndex = -1; |
272 |
this.defaultTimeAttributeName = null; |
273 |
} |
274 |
super.remove(attr);
|
275 |
this.pk = null; |
276 |
this.fixAll();
|
277 |
return attr;
|
278 |
} |
279 |
|
280 |
@Override
|
281 |
public Object remove(String name) { |
282 |
FeatureAttributeDescriptor attr = (FeatureAttributeDescriptor) this.get(name);
|
283 |
if (attr == null) { |
284 |
return null; |
285 |
} |
286 |
this.remove(attr.getIndex());
|
287 |
return attr;
|
288 |
} |
289 |
|
290 |
@Override
|
291 |
public boolean remove(EditableFeatureAttributeDescriptor attribute) { |
292 |
this.remove(attribute.getIndex());
|
293 |
return true; |
294 |
} |
295 |
|
296 |
@Override
|
297 |
protected void fixAll() { |
298 |
super.fixAll();
|
299 |
for (FeatureAttributeDescriptor attr : this) { |
300 |
if (attr instanceof DefaultEditableFeatureAttributeDescriptor) { |
301 |
DefaultEditableFeatureAttributeDescriptor attr2 = (DefaultEditableFeatureAttributeDescriptor) attr; |
302 |
if (attr2.hasStrongChanges()) {
|
303 |
this.hasStrongChanges = true; |
304 |
break;
|
305 |
} |
306 |
} |
307 |
} |
308 |
} |
309 |
|
310 |
public void checkIntegrity() throws DataListException { |
311 |
Iterator iter = super.iterator(); |
312 |
FeatureTypeIntegrityException ex = null;
|
313 |
|
314 |
while (iter.hasNext()) {
|
315 |
DefaultEditableFeatureAttributeDescriptor attr = (DefaultEditableFeatureAttributeDescriptor) iter |
316 |
.next(); |
317 |
try {
|
318 |
attr.checkIntegrity(); |
319 |
} catch (Exception e) { |
320 |
if (ex == null) { |
321 |
ex = new FeatureTypeIntegrityException(this.getId()); |
322 |
} |
323 |
ex.add(e); |
324 |
} |
325 |
} |
326 |
if (ex != null) { |
327 |
throw ex;
|
328 |
} |
329 |
} |
330 |
|
331 |
@Override
|
332 |
public void setDefaultGeometryType(int type, int subType) { |
333 |
EditableFeatureAttributeDescriptor descr = (EditableFeatureAttributeDescriptor) this.getDefaultGeometryAttribute();
|
334 |
if (descr == null) { |
335 |
throw new IllegalStateException("Default geometry attribute not set."); |
336 |
} |
337 |
descr.setGeometryType(type, subType); |
338 |
} |
339 |
|
340 |
@Override
|
341 |
public void setDefaultGeometryAttributeName(String name) { |
342 |
if (name == null || name.length() == 0) { |
343 |
this.defaultGeometryAttributeName = null; |
344 |
this.defaultGeometryAttributeIndex = -1; |
345 |
return;
|
346 |
} |
347 |
FeatureAttributeDescriptor attr = this.getAttributeDescriptor(name);
|
348 |
if (attr == null) { |
349 |
throw new IllegalArgumentException("Attribute '" + name + "' not found."); |
350 |
} |
351 |
this.defaultGeometryAttributeName = name;
|
352 |
this.defaultGeometryAttributeIndex = attr.getIndex();
|
353 |
} |
354 |
|
355 |
@Override
|
356 |
public int getDefaultGeometryAttributeIndex() { |
357 |
this.fixAll();
|
358 |
return this.defaultGeometryAttributeIndex; |
359 |
} |
360 |
|
361 |
@Override
|
362 |
public String getDefaultGeometryAttributeName() { |
363 |
this.fixAll();
|
364 |
return this.defaultGeometryAttributeName; |
365 |
} |
366 |
|
367 |
@Override
|
368 |
public FeatureAttributeDescriptor getDefaultGeometryAttribute() {
|
369 |
this.fixAll();
|
370 |
return super.getDefaultGeometryAttribute(); |
371 |
} |
372 |
|
373 |
@Override
|
374 |
public int getDefaultTimeAttributeIndex() { |
375 |
this.fixAll();
|
376 |
return this.defaultTimeAttributeIndex; |
377 |
} |
378 |
|
379 |
@Override
|
380 |
public String getDefaultTimeAttributeName() { |
381 |
this.fixAll();
|
382 |
return this.defaultTimeAttributeName; |
383 |
} |
384 |
|
385 |
@Override
|
386 |
public void setHasOID(boolean hasOID) { |
387 |
this.hasOID = hasOID;
|
388 |
} |
389 |
|
390 |
@Override
|
391 |
protected Iterator getIterator(Iterator iter) { |
392 |
return new EditableDelegatedIterator(iter, this); |
393 |
} |
394 |
|
395 |
@Override
|
396 |
public EditableFeatureAttributeDescriptor getEditableAttributeDescriptor(String name) { |
397 |
return (EditableFeatureAttributeDescriptor) this.getAttributeDescriptor(name); |
398 |
} |
399 |
|
400 |
@Override
|
401 |
public EditableFeatureAttributeDescriptor getEditableAttributeDescriptor(int index) { |
402 |
return (EditableFeatureAttributeDescriptor) this.getAttributeDescriptor(index); |
403 |
} |
404 |
|
405 |
@Override
|
406 |
public void setCheckFeaturesAtFinishEditing(boolean check) { |
407 |
this.checkFeaturesAtFinishEditing = check;
|
408 |
} |
409 |
|
410 |
@Override
|
411 |
public void setCheckFeaturesAtInsert(boolean check) { |
412 |
this.checkFeaturesAtInsert = check;
|
413 |
} |
414 |
|
415 |
protected class EditableDelegatedIterator extends DelegatedIterator { |
416 |
|
417 |
private final DefaultEditableFeatureType fType; |
418 |
|
419 |
public EditableDelegatedIterator(Iterator iter, DefaultEditableFeatureType fType) { |
420 |
super(iter);
|
421 |
this.fType = fType;
|
422 |
} |
423 |
|
424 |
@Override
|
425 |
public void remove() { |
426 |
this.iterator.remove();
|
427 |
this.fType.fixAll();
|
428 |
} |
429 |
|
430 |
} |
431 |
|
432 |
protected void setAllowAutomaticValues(boolean value) { |
433 |
this.allowAtomaticValues = value;
|
434 |
} |
435 |
|
436 |
@Override
|
437 |
public void copyFrom(FeatureType other) { |
438 |
super.copyFrom(other);
|
439 |
if (other instanceof EditableFeatureType) { |
440 |
DefaultEditableFeatureType other2 = (DefaultEditableFeatureType) other; |
441 |
this.hasStrongChanges = other2.hasStrongChanges();
|
442 |
this.source = (DefaultFeatureType) other2.getSource();
|
443 |
} else {
|
444 |
this.hasStrongChanges = false; |
445 |
this.source = (DefaultFeatureType) other;
|
446 |
} |
447 |
} |
448 |
|
449 |
public void copyFrom(JsonObject json) { |
450 |
// TODO: falta por implementar copyFrom(json)
|
451 |
} |
452 |
|
453 |
@Override
|
454 |
public void set(String name, String value) { |
455 |
if (StringUtils.isBlank(name)) {
|
456 |
throw new IllegalArgumentException("Name can't be empty"); |
457 |
} |
458 |
switch (name.trim().toLowerCase()) {
|
459 |
case "checkfeaturesatfinishediting": |
460 |
this.setCheckFeaturesAtFinishEditing(DataTypeUtils.toBoolean(value, false)); |
461 |
break;
|
462 |
case "checkfeaturesatinsert": |
463 |
this.setCheckFeaturesAtInsert(DataTypeUtils.toBoolean(value, false)); |
464 |
break;
|
465 |
case "defaultgeometryattributename": |
466 |
case "defaultgeometryname": |
467 |
case "defaultgeometry": |
468 |
this.setDefaultGeometryAttributeName(DataTypeUtils.toString(value, null)); |
469 |
break;
|
470 |
default:
|
471 |
throw new IllegalArgumentException("Name attribute '" + name + "' not valid."); |
472 |
} |
473 |
} |
474 |
|
475 |
private static class TheJsonSerializer implements JsonManager.JsonSerializer { |
476 |
|
477 |
public TheJsonSerializer() {
|
478 |
} |
479 |
|
480 |
@Override
|
481 |
public Class getObjectClass() { |
482 |
return DefaultEditableFeatureType.class;
|
483 |
} |
484 |
|
485 |
@Override
|
486 |
public Object toObject(JsonObject json) { |
487 |
EditableFeatureType o = new DefaultFeatureType().getEditable();
|
488 |
o.fromJson(json); |
489 |
return o;
|
490 |
} |
491 |
|
492 |
@Override
|
493 |
public JsonObjectBuilder toJsonBuilder(Object value) { |
494 |
return ((SupportToJson)value).toJsonBuilder();
|
495 |
} |
496 |
|
497 |
} |
498 |
|
499 |
public static void selfRegister() { |
500 |
Json.registerSerializer(new TheJsonSerializer());
|
501 |
} |
502 |
} |