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