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 / DefaultFeatureType.java @ 46655
History | View | Annotate | Download (45.6 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
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.fmap.dal.feature.impl; |
25 |
|
26 |
import java.io.File; |
27 |
import java.lang.ref.WeakReference; |
28 |
import java.util.ArrayList; |
29 |
import java.util.Collections; |
30 |
import java.util.Date; |
31 |
import java.util.HashSet; |
32 |
import java.util.Iterator; |
33 |
import java.util.LinkedHashSet; |
34 |
import java.util.List; |
35 |
import java.util.Objects; |
36 |
import java.util.Set; |
37 |
import java.util.function.Predicate; |
38 |
import java.util.zip.CRC32; |
39 |
import javax.json.JsonObject; |
40 |
import javax.json.JsonValue; |
41 |
import org.apache.commons.lang3.ArrayUtils; |
42 |
import org.apache.commons.lang3.StringUtils; |
43 |
|
44 |
import org.cresques.cts.IProjection; |
45 |
|
46 |
import org.gvsig.fmap.dal.DataTypes; |
47 |
import org.gvsig.fmap.dal.exception.DataException; |
48 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
49 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
50 |
import org.gvsig.fmap.dal.feature.Feature; |
51 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
52 |
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator; |
53 |
import org.gvsig.fmap.dal.feature.FeatureRules; |
54 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
55 |
import org.gvsig.fmap.dal.feature.FeatureType; |
56 |
import org.gvsig.tools.ToolsLocator; |
57 |
import org.gvsig.tools.dynobject.DynClass; |
58 |
import org.gvsig.tools.dynobject.DynField; |
59 |
import org.gvsig.tools.dynobject.DynMethod; |
60 |
import org.gvsig.tools.dynobject.DynObject; |
61 |
import org.gvsig.tools.dynobject.DynObjectValueItem; |
62 |
import org.gvsig.tools.dynobject.DynStruct; |
63 |
import org.gvsig.tools.dynobject.DynStruct_v2; |
64 |
import org.gvsig.tools.dynobject.Tags; |
65 |
import org.gvsig.tools.dynobject.exception.DynMethodException; |
66 |
import org.gvsig.tools.dynobject.exception.DynObjectValidateException; |
67 |
import org.gvsig.tools.dynobject.impl.DefaultTags; |
68 |
import org.gvsig.tools.i18n.I18nManager; |
69 |
import org.gvsig.tools.persistence.PersistenceManager; |
70 |
import org.gvsig.tools.persistence.Persistent; |
71 |
import org.gvsig.tools.persistence.PersistentState; |
72 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
73 |
import org.gvsig.fmap.dal.feature.FeatureExtraColumns; |
74 |
import org.gvsig.json.Json; |
75 |
import org.gvsig.json.JsonManager; |
76 |
import org.gvsig.json.JsonObjectBuilder; |
77 |
import org.gvsig.json.SupportToJson; |
78 |
|
79 |
@SuppressWarnings("UseSpecificCatch") |
80 |
public class DefaultFeatureType |
81 |
extends ArrayList<FeatureAttributeDescriptor> |
82 |
implements
|
83 |
FeatureType, |
84 |
Persistent, |
85 |
DynClass, |
86 |
DynStruct_v2, |
87 |
org.gvsig.tools.lang.Cloneable { |
88 |
|
89 |
/**
|
90 |
*
|
91 |
*/
|
92 |
private static final long serialVersionUID = -7988721447349282215L; |
93 |
|
94 |
public static final RecentUsedsAttributesImpl RECENTS_USEDS = new RecentUsedsAttributesImpl(); |
95 |
|
96 |
private DefaultFeatureRules rules;
|
97 |
protected boolean hasEvaluators; |
98 |
protected boolean hasEmulators; |
99 |
protected String defaultGeometryAttributeName; |
100 |
protected String defaultTimeAttributeName; |
101 |
protected int defaultGeometryAttributeIndex; |
102 |
protected int defaultTimeAttributeIndex; |
103 |
private String id; |
104 |
protected boolean hasOID; |
105 |
protected boolean allowAtomaticValues; |
106 |
protected FeatureAttributeDescriptor[] pk = null; |
107 |
protected String internalID = null; |
108 |
|
109 |
private List srsList = null; |
110 |
private WeakReference storeRef; |
111 |
private boolean requiredFields; |
112 |
|
113 |
private String description; |
114 |
private String label; |
115 |
private Tags tags;
|
116 |
private FeatureExtraColumns extraColumns;
|
117 |
protected boolean checkFeaturesAtFinishEditing; |
118 |
protected boolean checkFeaturesAtInsert; |
119 |
|
120 |
public DefaultFeatureType() {
|
121 |
// Usado en la persistencia.
|
122 |
this.internalID = Integer.toHexString((int) (Math.random() * 100000)).toUpperCase(); |
123 |
this.id = id = "default"; |
124 |
this.rules = new DefaultFeatureRules(); |
125 |
this.hasEvaluators = false; |
126 |
this.hasEmulators = false; |
127 |
this.defaultGeometryAttributeName = null; |
128 |
this.defaultTimeAttributeName = null; |
129 |
this.defaultGeometryAttributeIndex = -1; |
130 |
this.defaultTimeAttributeIndex = -1; |
131 |
this.allowAtomaticValues = false; |
132 |
this.tags = new DefaultTags(); |
133 |
this.extraColumns = new DefaultFeatureExtraColumns(); |
134 |
this.checkFeaturesAtFinishEditing = false; |
135 |
this.checkFeaturesAtInsert = true; |
136 |
} |
137 |
|
138 |
protected DefaultFeatureType(FeatureStore store, String id) { |
139 |
this();
|
140 |
if (StringUtils.isEmpty(id)) {
|
141 |
id = "default";
|
142 |
} |
143 |
this.id = id;
|
144 |
setStore(store); |
145 |
} |
146 |
|
147 |
protected DefaultFeatureType(FeatureStore store) {
|
148 |
this(store, (String) null); |
149 |
} |
150 |
|
151 |
protected DefaultFeatureType(DefaultFeatureType other) {
|
152 |
this(other.getStore(), (String) null); |
153 |
copyFrom(other, true);
|
154 |
} |
155 |
|
156 |
protected DefaultFeatureType(DefaultFeatureType other,
|
157 |
boolean copyAttributes) {
|
158 |
this(other.getStore(), (String) null); |
159 |
this.copyFrom(other, copyAttributes);
|
160 |
} |
161 |
|
162 |
@Override
|
163 |
public void copyFrom(FeatureType other) { |
164 |
String internalIDSaved = this.internalID; |
165 |
String idSaved = this.id; |
166 |
copyFrom((DefaultFeatureType) other, true);
|
167 |
this.id = idSaved;
|
168 |
this.internalID = internalIDSaved;
|
169 |
} |
170 |
|
171 |
@Override
|
172 |
public boolean isCheckFeaturesAtFinishEditing() { |
173 |
return this.checkFeaturesAtFinishEditing; |
174 |
} |
175 |
|
176 |
@Override
|
177 |
public boolean isCheckFeaturesAtInsert() { |
178 |
return this.checkFeaturesAtInsert; |
179 |
} |
180 |
|
181 |
|
182 |
protected void addAll(FeatureType attributes) { |
183 |
for (FeatureAttributeDescriptor attribute : attributes) {
|
184 |
DefaultFeatureAttributeDescriptor copy = this.getCopyAttributeDescriptor((DefaultFeatureAttributeDescriptor) attribute);
|
185 |
super.add(copy);
|
186 |
} |
187 |
DefaultFeatureType ft = (DefaultFeatureType) attributes; |
188 |
this.pk = null; |
189 |
this.defaultGeometryAttributeName = ft.defaultGeometryAttributeName;
|
190 |
this.defaultTimeAttributeName = ft.defaultTimeAttributeName;
|
191 |
this.defaultGeometryAttributeIndex = ft.defaultGeometryAttributeIndex;
|
192 |
this.defaultTimeAttributeIndex = ft.defaultTimeAttributeIndex;
|
193 |
} |
194 |
|
195 |
protected void copyFrom(DefaultFeatureType other, boolean copyAttributes) { |
196 |
this.id = other.getId();
|
197 |
if (copyAttributes) {
|
198 |
this.addAll((FeatureType)other);
|
199 |
} |
200 |
this.defaultGeometryAttributeName = other.defaultGeometryAttributeName;
|
201 |
this.defaultTimeAttributeName = other.defaultTimeAttributeName;
|
202 |
this.defaultGeometryAttributeIndex = other.defaultGeometryAttributeIndex;
|
203 |
this.defaultTimeAttributeIndex = other.defaultTimeAttributeIndex;
|
204 |
|
205 |
this.hasEvaluators = other.hasEvaluators;
|
206 |
this.hasEmulators = other.hasEmulators;
|
207 |
this.rules = (DefaultFeatureRules) other.rules.getCopy();
|
208 |
this.hasOID = other.hasOID;
|
209 |
this.id = other.id; // XXX ???? copiar o no esto???? |
210 |
this.internalID = other.internalID;
|
211 |
this.label = other.label;
|
212 |
this.description = other.description;
|
213 |
this.extraColumns = other.extraColumns.getCopy();
|
214 |
try {
|
215 |
this.tags = (Tags) other.tags.clone();
|
216 |
} catch (CloneNotSupportedException ex) { |
217 |
|
218 |
} |
219 |
this.checkFeaturesAtFinishEditing = other.checkFeaturesAtFinishEditing;
|
220 |
this.checkFeaturesAtInsert = other.checkFeaturesAtInsert;
|
221 |
} |
222 |
|
223 |
protected DefaultFeatureAttributeDescriptor getCopyAttributeDescriptor(DefaultFeatureAttributeDescriptor src) {
|
224 |
DefaultFeatureAttributeDescriptor copy = new DefaultFeatureAttributeDescriptor(src);
|
225 |
copy.setFeatureType(this);
|
226 |
return copy;
|
227 |
} |
228 |
|
229 |
@Override
|
230 |
public String getId() { |
231 |
return this.id; |
232 |
} |
233 |
|
234 |
@Override
|
235 |
public Object get(String name) { |
236 |
FeatureAttributeDescriptor attr; |
237 |
Iterator iter = this.iterator(); |
238 |
while (iter.hasNext()) {
|
239 |
attr = (FeatureAttributeDescriptor) iter.next(); |
240 |
if (attr.getName().equalsIgnoreCase(name)) {
|
241 |
return attr;
|
242 |
} |
243 |
} |
244 |
return null; |
245 |
} |
246 |
|
247 |
@Override
|
248 |
public FeatureAttributeDescriptor getAttributeDescriptor(String name) { |
249 |
FeatureAttributeDescriptor attr; |
250 |
Iterator iter = this.iterator(); |
251 |
while (iter.hasNext()) {
|
252 |
attr = (FeatureAttributeDescriptor) iter.next(); |
253 |
if (attr.getName().equalsIgnoreCase(name)) {
|
254 |
return attr;
|
255 |
} |
256 |
} |
257 |
return null; |
258 |
} |
259 |
|
260 |
@Override
|
261 |
public FeatureAttributeDescriptor getAttributeDescriptorFromAll(String name) { |
262 |
FeatureAttributeDescriptor attrdesc = this.getAttributeDescriptor(name);
|
263 |
if (attrdesc == null && this.getExtraColumns().get(name) != null) { |
264 |
attrdesc = this.getExtraColumns().get(name);
|
265 |
} |
266 |
return attrdesc;
|
267 |
} |
268 |
|
269 |
@Override
|
270 |
public FeatureAttributeDescriptor getAttributeDescriptor(int index) { |
271 |
return (FeatureAttributeDescriptor) super.get(index); |
272 |
} |
273 |
|
274 |
@Override
|
275 |
public String getAttributeName(int index) { |
276 |
try {
|
277 |
return super.get(index).getName(); |
278 |
} catch(Exception ex) { |
279 |
return null; |
280 |
} |
281 |
} |
282 |
|
283 |
@Override
|
284 |
public FeatureType getCopy() {
|
285 |
return new DefaultFeatureType(this); |
286 |
} |
287 |
|
288 |
@Override
|
289 |
public Object clone() { |
290 |
return this.getCopy(); |
291 |
} |
292 |
|
293 |
@Override
|
294 |
public int getDefaultGeometryAttributeIndex() { |
295 |
return this.defaultGeometryAttributeIndex; |
296 |
} |
297 |
|
298 |
@Override
|
299 |
public String getDefaultGeometryAttributeName() { |
300 |
return this.defaultGeometryAttributeName; |
301 |
} |
302 |
|
303 |
@Override
|
304 |
public int getDefaultTimeAttributeIndex() { |
305 |
return this.defaultTimeAttributeIndex; |
306 |
} |
307 |
|
308 |
@Override
|
309 |
public String getDefaultTimeAttributeName() { |
310 |
return this.defaultTimeAttributeName; |
311 |
} |
312 |
|
313 |
@Override
|
314 |
public EditableFeatureType getEditable() {
|
315 |
return new DefaultEditableFeatureType(this); |
316 |
} |
317 |
|
318 |
@Override
|
319 |
public int getIndex(String name) { |
320 |
FeatureAttributeDescriptor attr; |
321 |
Iterator iter = this.iterator(); |
322 |
while (iter.hasNext()) {
|
323 |
attr = (FeatureAttributeDescriptor) iter.next(); |
324 |
if (attr.getName().equalsIgnoreCase(name)) {
|
325 |
return attr.getIndex();
|
326 |
} |
327 |
} |
328 |
return -1; |
329 |
} |
330 |
|
331 |
@Override
|
332 |
public FeatureRules getRules() {
|
333 |
return this.rules; |
334 |
} |
335 |
|
336 |
@Override
|
337 |
public boolean hasEvaluators() { |
338 |
return this.hasEvaluators; |
339 |
} |
340 |
|
341 |
public boolean hasEmulators() { |
342 |
return this.hasEmulators; |
343 |
} |
344 |
|
345 |
public boolean hasRequiredFields() { |
346 |
return this.requiredFields; |
347 |
} |
348 |
|
349 |
@Override
|
350 |
public List getSRSs() { |
351 |
if (this.srsList == null) { |
352 |
ArrayList tmp = new ArrayList(); |
353 |
Iterator iter = iterator();
|
354 |
Iterator tmpIter;
|
355 |
boolean allreadyHave;
|
356 |
IProjection tmpSRS; |
357 |
FeatureAttributeDescriptor attr; |
358 |
while (iter.hasNext()) {
|
359 |
attr = (FeatureAttributeDescriptor) iter.next(); |
360 |
if (attr.getDataType().getType() == DataTypes.GEOMETRY
|
361 |
&& attr.getSRS() != null) {
|
362 |
allreadyHave = false;
|
363 |
tmpIter = tmp.iterator(); |
364 |
while (tmpIter.hasNext()) {
|
365 |
tmpSRS = (IProjection) tmpIter.next(); |
366 |
if (tmpSRS.getAbrev().equals(attr.getSRS().getAbrev())) {
|
367 |
allreadyHave = true;
|
368 |
break;
|
369 |
} |
370 |
} |
371 |
if (!allreadyHave) {
|
372 |
tmp.add(attr.getSRS()); |
373 |
} |
374 |
} |
375 |
} |
376 |
this.srsList = Collections.unmodifiableList(tmp); |
377 |
} |
378 |
return this.srsList; |
379 |
} |
380 |
|
381 |
@Override
|
382 |
public IProjection getDefaultSRS() {
|
383 |
if (this.getDefaultGeometryAttributeIndex() < 0) { |
384 |
return null; |
385 |
} |
386 |
return this.getAttributeDescriptor( |
387 |
this.getDefaultGeometryAttributeIndex()).getSRS();
|
388 |
} |
389 |
|
390 |
public void validateFeature(EditableFeature feature, int check) throws DataException { |
391 |
DefaultFeatureRules theRules = (DefaultFeatureRules) this.getRules();
|
392 |
theRules.validate(feature, check); |
393 |
} |
394 |
|
395 |
public void validateFeature(Feature feature, int check) throws DataException { |
396 |
DefaultFeatureRules theRules = (DefaultFeatureRules) this.getRules();
|
397 |
theRules.validate(feature, check); |
398 |
} |
399 |
|
400 |
public FeatureType getSubtype() throws DataException { |
401 |
return new SubtypeFeatureType(this, null, null, true); |
402 |
} |
403 |
|
404 |
public FeatureType getSubtype(String[] names) throws DataException { |
405 |
return this.getSubtype(names, null, true); |
406 |
} |
407 |
|
408 |
public FeatureType getSubtype(String[] names, String[] constantsNames) throws DataException { |
409 |
return this.getSubtype(names, constantsNames, true); |
410 |
} |
411 |
|
412 |
public FeatureType getSubtype(String[] names, String[] constantsNames, boolean includePk) throws DataException { |
413 |
if (ArrayUtils.isEmpty(names) && ArrayUtils.isEmpty(constantsNames)) {
|
414 |
return (FeatureType) this.clone(); |
415 |
} |
416 |
return new SubtypeFeatureType(this, names, constantsNames,includePk); |
417 |
} |
418 |
|
419 |
public boolean isSubtypeOf(FeatureType featureType) { |
420 |
return false; |
421 |
} |
422 |
|
423 |
@Override
|
424 |
public List<FeatureAttributeDescriptor> toList() { |
425 |
return Collections.unmodifiableList(this); |
426 |
} |
427 |
|
428 |
@Override
|
429 |
public Tags getTags() {
|
430 |
return this.tags; |
431 |
} |
432 |
|
433 |
@Override
|
434 |
public String getLabel() { |
435 |
return this.label; |
436 |
} |
437 |
|
438 |
@Override
|
439 |
public void setLabel(String label) { |
440 |
this.label = label;
|
441 |
} |
442 |
|
443 |
@Override
|
444 |
public DynField addDynField(String name, int type) { |
445 |
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
446 |
} |
447 |
|
448 |
@Override
|
449 |
public FeatureExtraColumns getExtraColumns() {
|
450 |
return extraColumns;
|
451 |
} |
452 |
|
453 |
public void setExtraColumn(FeatureExtraColumns extraColumn) { //List<FeatureAttributeDescriptor> descriptors) { |
454 |
this.extraColumns = extraColumn;
|
455 |
} |
456 |
|
457 |
@Override
|
458 |
public Iterable<FeatureAttributeDescriptor> getAllAttributeDescriptors() { |
459 |
Set<FeatureAttributeDescriptor> all = new HashSet<FeatureAttributeDescriptor>(); |
460 |
for (FeatureAttributeDescriptor attributeDescriptor : this.getAttributeDescriptors()) { |
461 |
all.add(attributeDescriptor); |
462 |
} |
463 |
for (FeatureAttributeDescriptor extraColumn : this.getExtraColumns().getColumns()) { |
464 |
all.add(extraColumn); |
465 |
} |
466 |
return all;
|
467 |
} |
468 |
|
469 |
class SubtypeFeatureType extends DefaultFeatureType { |
470 |
|
471 |
/**
|
472 |
*
|
473 |
*/
|
474 |
private static final long serialVersionUID = 6913732960073922540L; |
475 |
WeakReference parent;
|
476 |
|
477 |
SubtypeFeatureType(DefaultFeatureType parent, String[] names, String[] constantsNames, boolean includePk) |
478 |
throws DataException {
|
479 |
super(parent, false); |
480 |
DefaultFeatureAttributeDescriptor attrcopy; |
481 |
Set<String> attrnames = new LinkedHashSet<>(); |
482 |
Set<String> requiredAttrnames = new HashSet<>(); |
483 |
|
484 |
if (ArrayUtils.isEmpty(names)) {
|
485 |
for (FeatureAttributeDescriptor attrdesc : parent) {
|
486 |
attrnames.add(attrdesc.getName().toLowerCase()); |
487 |
} |
488 |
} else {
|
489 |
for (String name : names) { |
490 |
name = name.toLowerCase(); |
491 |
attrnames.add(name); |
492 |
requiredAttrnames.add(name); |
493 |
} |
494 |
} |
495 |
// Add required fields for emulated fields
|
496 |
if (parent.hasEmulators) {
|
497 |
// Ojo, este bucle falla cuando hay un campo calculado que depende
|
498 |
// de otro campo calculado.
|
499 |
for (FeatureAttributeDescriptor attrdesc : parent) {
|
500 |
FeatureAttributeEmulator emulator = attrdesc.getFeatureAttributeEmulator(); |
501 |
if (emulator != null && attrnames.contains(attrdesc.getName().toLowerCase())) { |
502 |
String theNames[] = emulator.getRequiredFieldNames(); |
503 |
if (names != null) { |
504 |
for (String name : theNames) { |
505 |
name = name.toLowerCase(); |
506 |
attrnames.add(name); |
507 |
requiredAttrnames.add(name); |
508 |
} |
509 |
} |
510 |
} |
511 |
} |
512 |
} |
513 |
// Add missing pk fiels
|
514 |
if (includePk && !parent.hasOID()) {
|
515 |
for (FeatureAttributeDescriptor attrdesc : parent) {
|
516 |
if (attrdesc.isPrimaryKey()) {
|
517 |
String name = attrdesc.getName().toLowerCase();
|
518 |
attrnames.add(name); |
519 |
requiredAttrnames.add(name); |
520 |
} |
521 |
} |
522 |
} |
523 |
|
524 |
// Copy attributes
|
525 |
int i = 0; |
526 |
for (String name : attrnames) { |
527 |
DefaultFeatureAttributeDescriptor attr = (DefaultFeatureAttributeDescriptor) parent.get(name); |
528 |
if (attr == null) { |
529 |
throw new SubtypeFeatureTypeNameException(name, parent.getId()); |
530 |
} |
531 |
attrcopy = new DefaultFeatureAttributeDescriptor(attr);
|
532 |
this.add(attrcopy);
|
533 |
attrcopy.index = i++; |
534 |
} |
535 |
|
536 |
// Set the constants attributes.
|
537 |
if (!ArrayUtils.isEmpty(constantsNames)) {
|
538 |
for (String name : constantsNames) { |
539 |
if (!requiredAttrnames.contains(name.toLowerCase())) {
|
540 |
DefaultFeatureAttributeDescriptor attr = (DefaultFeatureAttributeDescriptor) this.get(name);
|
541 |
attr.setConstantValue(true);
|
542 |
} |
543 |
} |
544 |
} |
545 |
|
546 |
this.defaultGeometryAttributeIndex = this.getIndex(this.defaultGeometryAttributeName); |
547 |
if (this.defaultGeometryAttributeIndex < 0) { |
548 |
this.defaultGeometryAttributeName = null; |
549 |
} |
550 |
this.defaultTimeAttributeIndex = this.getIndex(this.defaultTimeAttributeName); |
551 |
if (this.defaultTimeAttributeIndex < 0) { |
552 |
this.defaultTimeAttributeName = null; |
553 |
} |
554 |
this.parent = new WeakReference(parent); |
555 |
} |
556 |
|
557 |
public FeatureType getSubtype(String[] names, boolean includePk) throws DataException { |
558 |
return new SubtypeFeatureType((DefaultFeatureType) this.parent |
559 |
.get(), names, null, includePk);
|
560 |
} |
561 |
|
562 |
public boolean isSubtypeOf(FeatureType featureType) { |
563 |
if (featureType == null) { |
564 |
return false; |
565 |
} |
566 |
FeatureType parent = (FeatureType) this.parent.get();
|
567 |
return featureType.equals(parent);
|
568 |
} |
569 |
|
570 |
public EditableFeatureType getEditable() {
|
571 |
throw new UnsupportedOperationException(); |
572 |
} |
573 |
} |
574 |
|
575 |
public class SubtypeFeatureTypeNameException extends DataException { |
576 |
|
577 |
/**
|
578 |
*
|
579 |
*/
|
580 |
private static final long serialVersionUID = -4414242486723260101L; |
581 |
private final static String MESSAGE_FORMAT = "Attribute name '%(name)' not found in type (%(type))."; |
582 |
private final static String MESSAGE_KEY = "_SubtypeFeatureTypeNameException"; |
583 |
|
584 |
public SubtypeFeatureTypeNameException(String name, String type) { |
585 |
super(MESSAGE_FORMAT, MESSAGE_KEY, serialVersionUID);
|
586 |
setValue("name", name);
|
587 |
setValue("type", type);
|
588 |
} |
589 |
} |
590 |
|
591 |
public boolean hasOID() { |
592 |
return hasOID;
|
593 |
} |
594 |
|
595 |
@Override
|
596 |
public String toString() { |
597 |
StringBuffer s = new StringBuffer(); |
598 |
s.append(this.getId());
|
599 |
s.append(":[");
|
600 |
String attName;
|
601 |
for (int i = 0; i < size(); i++) { |
602 |
attName = ((FeatureAttributeDescriptor) get(i)).getName().toString(); |
603 |
s.append(attName); |
604 |
if (i < size() - 1) { |
605 |
s.append(',');
|
606 |
} |
607 |
} |
608 |
s.append(']');
|
609 |
return s.toString();
|
610 |
} |
611 |
|
612 |
@Override
|
613 |
public Iterator<FeatureAttributeDescriptor> iterator() { |
614 |
return getIterator(super.iterator()); |
615 |
} |
616 |
|
617 |
protected Iterator getIterator(Iterator iter) { |
618 |
return new DelegatedIterator(iter); |
619 |
} |
620 |
|
621 |
protected class DelegatedIterator implements Iterator { |
622 |
|
623 |
protected Iterator iterator; |
624 |
|
625 |
public DelegatedIterator(Iterator iter) { |
626 |
this.iterator = iter;
|
627 |
} |
628 |
|
629 |
@Override
|
630 |
public boolean hasNext() { |
631 |
return iterator.hasNext();
|
632 |
} |
633 |
|
634 |
@Override
|
635 |
public Object next() { |
636 |
return iterator.next();
|
637 |
} |
638 |
|
639 |
@Override
|
640 |
public void remove() { |
641 |
throw new UnsupportedOperationException(); |
642 |
} |
643 |
|
644 |
} |
645 |
|
646 |
@Override
|
647 |
public boolean allowAutomaticValues() { |
648 |
return this.allowAtomaticValues; |
649 |
} |
650 |
|
651 |
@Override
|
652 |
public FeatureAttributeDescriptor[] getAttributeDescriptors() { |
653 |
return (FeatureAttributeDescriptor[]) super |
654 |
.toArray(new FeatureAttributeDescriptor[super.size()]); |
655 |
} |
656 |
|
657 |
@Override
|
658 |
public boolean hasPrimaryKey() { |
659 |
if( pk!=null ) { |
660 |
return pk.length>0; |
661 |
} |
662 |
for (FeatureAttributeDescriptor attr : this) { |
663 |
if( attr.isPrimaryKey() ) {
|
664 |
return true; |
665 |
} |
666 |
} |
667 |
return false; |
668 |
} |
669 |
|
670 |
@Override
|
671 |
public boolean supportReferences() { |
672 |
return this.hasOID() || this.hasPrimaryKey(); |
673 |
} |
674 |
|
675 |
@Override
|
676 |
public FeatureAttributeDescriptor[] getPrimaryKey() { |
677 |
if (pk == null) { |
678 |
List pks = new ArrayList(); |
679 |
Iterator iter = super.iterator(); |
680 |
FeatureAttributeDescriptor attr; |
681 |
while (iter.hasNext()) {
|
682 |
attr = (FeatureAttributeDescriptor) iter.next(); |
683 |
if (attr.isPrimaryKey()) {
|
684 |
pks.add(attr); |
685 |
} |
686 |
} |
687 |
if (pks.isEmpty()) {
|
688 |
pk = new FeatureAttributeDescriptor[0]; |
689 |
} else {
|
690 |
pk = (FeatureAttributeDescriptor[]) pks.toArray(new FeatureAttributeDescriptor[pks.size()]); |
691 |
} |
692 |
} |
693 |
return pk;
|
694 |
} |
695 |
|
696 |
public FeatureAttributeDescriptor getDefaultGeometryAttribute() {
|
697 |
if (this.defaultGeometryAttributeIndex < 0) { |
698 |
return null; |
699 |
} |
700 |
return (FeatureAttributeDescriptor) super |
701 |
.get(this.defaultGeometryAttributeIndex);
|
702 |
} |
703 |
|
704 |
@Override
|
705 |
public boolean equals(Object o) { |
706 |
if (this == o) { |
707 |
return true; |
708 |
} |
709 |
if (!(o instanceof DefaultFeatureType)) { |
710 |
return false; |
711 |
} |
712 |
DefaultFeatureType other = (DefaultFeatureType) o; |
713 |
if (!this.id.equals(other.id)) { |
714 |
return false; |
715 |
} |
716 |
if (this.size() != other.size()) { |
717 |
return false; |
718 |
} |
719 |
FeatureAttributeDescriptor attr, attrOther; |
720 |
Iterator iter, iterOther;
|
721 |
iter = this.iterator();
|
722 |
iterOther = other.iterator(); |
723 |
while (iter.hasNext()) {
|
724 |
attr = (FeatureAttributeDescriptor) iter.next(); |
725 |
attrOther = (FeatureAttributeDescriptor) iterOther.next(); |
726 |
if (!attr.equals(attrOther)) {
|
727 |
return false; |
728 |
} |
729 |
} |
730 |
|
731 |
if (!StringUtils.equals(defaultGeometryAttributeName, other.defaultGeometryAttributeName)) {
|
732 |
return false; |
733 |
} |
734 |
if (!StringUtils.equals(defaultTimeAttributeName, other.defaultTimeAttributeName)) {
|
735 |
return false; |
736 |
} |
737 |
return true; |
738 |
|
739 |
} |
740 |
|
741 |
/**
|
742 |
* Start of DynClass interface implementation READONLY
|
743 |
*/
|
744 |
@Override
|
745 |
public DynField addDynField(String name) { |
746 |
throw new UnsupportedOperationException(); |
747 |
} |
748 |
|
749 |
@Override
|
750 |
public DynField getDeclaredDynField(String name) { |
751 |
return (DynField) getAttributeDescriptor(name);
|
752 |
} |
753 |
|
754 |
@Override
|
755 |
public DynField[] getDeclaredDynFields() { |
756 |
return (DynField[]) getAttributeDescriptors(); |
757 |
} |
758 |
|
759 |
@Override
|
760 |
public String getDescription() { |
761 |
return this.description; |
762 |
} |
763 |
|
764 |
@Override
|
765 |
public DynField getDynField(String name) { |
766 |
return (DynField) getAttributeDescriptor(name);
|
767 |
} |
768 |
|
769 |
@Override
|
770 |
public DynField[] getDynFields() { |
771 |
return (DynField[]) getAttributeDescriptors(); |
772 |
} |
773 |
|
774 |
@Override
|
775 |
public String getName() { |
776 |
return this.id + "_" + internalID; |
777 |
} |
778 |
|
779 |
@Override
|
780 |
public void removeDynField(String name) { |
781 |
throw new UnsupportedOperationException(); |
782 |
|
783 |
} |
784 |
|
785 |
@Override
|
786 |
public void addDynMethod(DynMethod dynMethod) { |
787 |
throw new UnsupportedOperationException(); |
788 |
|
789 |
} |
790 |
|
791 |
public void extend(DynClass dynClass) { |
792 |
throw new UnsupportedOperationException(); |
793 |
|
794 |
} |
795 |
|
796 |
@Override
|
797 |
public void extend(String dynClassName) { |
798 |
throw new UnsupportedOperationException(); |
799 |
|
800 |
} |
801 |
|
802 |
@Override
|
803 |
public void extend(String namespace, String dynClassName) { |
804 |
throw new UnsupportedOperationException(); |
805 |
|
806 |
} |
807 |
|
808 |
@Override
|
809 |
public DynMethod getDeclaredDynMethod(String name) |
810 |
throws DynMethodException {
|
811 |
return null; |
812 |
} |
813 |
|
814 |
@Override
|
815 |
public DynMethod[] getDeclaredDynMethods() throws DynMethodException { |
816 |
return null; |
817 |
} |
818 |
|
819 |
@Override
|
820 |
public DynMethod getDynMethod(String name) throws DynMethodException { |
821 |
return null; |
822 |
} |
823 |
|
824 |
@Override
|
825 |
public DynMethod getDynMethod(int code) throws DynMethodException { |
826 |
return null; |
827 |
} |
828 |
|
829 |
@Override
|
830 |
public DynMethod[] getDynMethods() throws DynMethodException { |
831 |
return null; |
832 |
} |
833 |
|
834 |
@Override
|
835 |
public DynClass[] getSuperDynClasses() { |
836 |
return null; |
837 |
} |
838 |
|
839 |
@Override
|
840 |
public boolean isInstance(DynObject dynObject) { |
841 |
if (dynObject.getDynClass().getName() == getName()) {
|
842 |
return true; |
843 |
} |
844 |
return false; |
845 |
} |
846 |
|
847 |
@Override
|
848 |
public DynObject newInstance() {
|
849 |
|
850 |
throw new UnsupportedOperationException(); |
851 |
} |
852 |
|
853 |
@Override
|
854 |
public void removeDynMethod(String name) { |
855 |
throw new UnsupportedOperationException(); |
856 |
|
857 |
} |
858 |
|
859 |
@Override
|
860 |
public DynField addDynFieldChoice(String name, int type, |
861 |
Object defaultValue, DynObjectValueItem[] values, |
862 |
boolean mandatory, boolean persistent) { |
863 |
throw new UnsupportedOperationException(); |
864 |
} |
865 |
|
866 |
@Override
|
867 |
public DynField addDynFieldRange(String name, int type, |
868 |
Object defaultValue, Object min, Object max, boolean mandatory, |
869 |
boolean persistent) {
|
870 |
throw new UnsupportedOperationException(); |
871 |
} |
872 |
|
873 |
@Override
|
874 |
public DynField addDynFieldSingle(String name, int type, |
875 |
Object defaultValue, boolean mandatory, boolean persistent) { |
876 |
throw new UnsupportedOperationException(); |
877 |
} |
878 |
|
879 |
@Override
|
880 |
public void validate(DynObject object) throws DynObjectValidateException { |
881 |
//FIXME: not sure it's the correct code
|
882 |
if (object instanceof Feature) { |
883 |
Feature fea = (Feature) object; |
884 |
if (fea.getType().equals(this)) { |
885 |
return;
|
886 |
} |
887 |
} |
888 |
throw new DynObjectValidateException(this.id); |
889 |
} |
890 |
|
891 |
@Override
|
892 |
public DynField addDynFieldLong(String name) { |
893 |
throw new UnsupportedOperationException(); |
894 |
} |
895 |
|
896 |
@Override
|
897 |
public DynField addDynFieldChoice(String name, int type, |
898 |
Object defaultValue, DynObjectValueItem[] values) { |
899 |
throw new UnsupportedOperationException(); |
900 |
} |
901 |
|
902 |
@Override
|
903 |
public DynField addDynFieldRange(String name, int type, |
904 |
Object defaultValue, Object min, Object max) { |
905 |
throw new UnsupportedOperationException(); |
906 |
} |
907 |
|
908 |
@Override
|
909 |
public DynField addDynFieldSingle(String name, int type, Object defaultValue) { |
910 |
throw new UnsupportedOperationException(); |
911 |
} |
912 |
|
913 |
@Override
|
914 |
public DynField addDynFieldString(String name) { |
915 |
throw new UnsupportedOperationException(); |
916 |
} |
917 |
|
918 |
@Override
|
919 |
public DynField addDynFieldInt(String name) { |
920 |
throw new UnsupportedOperationException(); |
921 |
} |
922 |
|
923 |
@Override
|
924 |
public DynField addDynFieldDouble(String name) { |
925 |
throw new UnsupportedOperationException(); |
926 |
} |
927 |
|
928 |
@Override
|
929 |
public DynField addDynFieldFloat(String name) { |
930 |
throw new UnsupportedOperationException(); |
931 |
} |
932 |
|
933 |
public DynField addDynFieldBoolean(String name) { |
934 |
throw new UnsupportedOperationException(); |
935 |
} |
936 |
|
937 |
@Override
|
938 |
public DynField addDynFieldList(String name) { |
939 |
throw new UnsupportedOperationException(); |
940 |
} |
941 |
|
942 |
@Override
|
943 |
public DynField addDynFieldMap(String name) { |
944 |
throw new UnsupportedOperationException(); |
945 |
} |
946 |
|
947 |
@Override
|
948 |
public DynField addDynFieldObject(String name) { |
949 |
throw new UnsupportedOperationException(); |
950 |
} |
951 |
|
952 |
@Override
|
953 |
public DynField addDynFieldSet(String name) { |
954 |
throw new UnsupportedOperationException(); |
955 |
} |
956 |
|
957 |
@Override
|
958 |
public DynField addDynFieldArray(String name) { |
959 |
throw new UnsupportedOperationException(); |
960 |
} |
961 |
|
962 |
@Override
|
963 |
public DynField addDynFieldDate(String name) { |
964 |
throw new UnsupportedOperationException(); |
965 |
} |
966 |
|
967 |
@Override
|
968 |
public void extend(DynStruct struct) { |
969 |
throw new UnsupportedOperationException(); |
970 |
} |
971 |
|
972 |
@Override
|
973 |
public String getFullName() { |
974 |
// TODO: usar el DynClassName
|
975 |
return this.id; |
976 |
} |
977 |
|
978 |
@Override
|
979 |
public String getNamespace() { |
980 |
return "DALFeature"; |
981 |
} |
982 |
|
983 |
@Override
|
984 |
public DynStruct[] getSuperDynStructs() { |
985 |
return null; |
986 |
} |
987 |
|
988 |
@Override
|
989 |
public void setDescription(String description) { |
990 |
this.description = description;
|
991 |
} |
992 |
|
993 |
@Override
|
994 |
public void setNamespace(String namespace) { |
995 |
throw new UnsupportedOperationException(); |
996 |
} |
997 |
|
998 |
@Override
|
999 |
public DynField addDynFieldFile(String name) { |
1000 |
throw new UnsupportedOperationException(); |
1001 |
} |
1002 |
|
1003 |
@Override
|
1004 |
public DynField addDynFieldFolder(String name) { |
1005 |
throw new UnsupportedOperationException(); |
1006 |
} |
1007 |
|
1008 |
@Override
|
1009 |
public DynField addDynFieldURL(String name) { |
1010 |
throw new UnsupportedOperationException(); |
1011 |
} |
1012 |
|
1013 |
@Override
|
1014 |
public DynField addDynFieldURI(String name) { |
1015 |
throw new UnsupportedOperationException(); |
1016 |
} |
1017 |
|
1018 |
@Override
|
1019 |
public boolean isExtendable(DynStruct dynStruct) { |
1020 |
return false; |
1021 |
} |
1022 |
|
1023 |
public void extend(DynStruct[] structs) { |
1024 |
// TODO Auto-generated method stub
|
1025 |
|
1026 |
} |
1027 |
|
1028 |
@Override
|
1029 |
public void remove(DynStruct superDynStruct) { |
1030 |
// TODO Auto-generated method stub
|
1031 |
|
1032 |
} |
1033 |
|
1034 |
public void removeAll(DynStruct[] superDynStruct) { |
1035 |
// TODO Auto-generated method stub
|
1036 |
|
1037 |
} |
1038 |
|
1039 |
@Override
|
1040 |
public FeatureAttributeDescriptor getDefaultTimeAttribute() {
|
1041 |
if (this.defaultTimeAttributeIndex < 0) { |
1042 |
return null; |
1043 |
} |
1044 |
return (FeatureAttributeDescriptor) super |
1045 |
.get(this.defaultTimeAttributeIndex);
|
1046 |
} |
1047 |
|
1048 |
public void setDefaultTimeAttributeName(String name) { |
1049 |
if (name == null || name.length() == 0) { |
1050 |
this.defaultTimeAttributeIndex = -1; |
1051 |
return;
|
1052 |
} |
1053 |
DefaultFeatureAttributeDescriptor attr = (DefaultFeatureAttributeDescriptor) this.get(name);
|
1054 |
if (attr == null) { |
1055 |
throw new IllegalArgumentException("Attribute '" + name + "' not found."); |
1056 |
} |
1057 |
if (attr.getIndex() < 0) { |
1058 |
fixAll(); |
1059 |
} |
1060 |
this.defaultTimeAttributeIndex = attr.getIndex();
|
1061 |
} |
1062 |
|
1063 |
protected void fixAll() { |
1064 |
int i = 0; |
1065 |
Iterator iter = super.iterator(); |
1066 |
DefaultFeatureAttributeDescriptor attr; |
1067 |
|
1068 |
while (iter.hasNext()) {
|
1069 |
attr = (DefaultFeatureAttributeDescriptor) iter.next(); |
1070 |
if (attr.getOder() < 1) { |
1071 |
attr.setOrder(i * 10);
|
1072 |
} |
1073 |
attr.setIndex(i++); |
1074 |
attr.fixAll(); |
1075 |
if (attr.getEvaluator() != null) { |
1076 |
this.hasEvaluators = true; |
1077 |
} |
1078 |
if (attr.getFeatureAttributeEmulator() != null) { |
1079 |
this.hasEmulators = true; |
1080 |
String[] x = attr.getFeatureAttributeEmulator().getRequiredFieldNames(); |
1081 |
if (!ArrayUtils.isEmpty(x)) {
|
1082 |
this.requiredFields = true; |
1083 |
} |
1084 |
} |
1085 |
switch (attr.getType()) {
|
1086 |
case DataTypes.GEOMETRY:
|
1087 |
if (this.defaultGeometryAttributeName == null |
1088 |
|| !StringUtils.equals(this.defaultGeometryAttributeName, attr.getName())) {
|
1089 |
this.defaultGeometryAttributeName = attr.getName();
|
1090 |
} |
1091 |
break;
|
1092 |
case DataTypes.INSTANT:
|
1093 |
case DataTypes.INTERVAL:
|
1094 |
case DataTypes.DATE:
|
1095 |
if (this.defaultTimeAttributeName == null && attr.isTime()) { |
1096 |
this.defaultTimeAttributeName = attr.getName();
|
1097 |
} |
1098 |
break;
|
1099 |
} |
1100 |
} |
1101 |
if (this.defaultGeometryAttributeName != null) { |
1102 |
this.defaultGeometryAttributeIndex = this.getIndex(this.defaultGeometryAttributeName); |
1103 |
} |
1104 |
if (this.defaultTimeAttributeName != null) { |
1105 |
this.defaultTimeAttributeIndex = this.getIndex(this.defaultTimeAttributeName); |
1106 |
} |
1107 |
this.internalID = Long.toHexString(this.getCRC()); |
1108 |
|
1109 |
} |
1110 |
|
1111 |
protected long getCRC() { |
1112 |
StringBuffer buffer = new StringBuffer(); |
1113 |
for (int i = 0; i < this.size(); i++) { |
1114 |
FeatureAttributeDescriptor x = this.getAttributeDescriptor(i);
|
1115 |
buffer.append(x.getName()); |
1116 |
buffer.append(x.getDataTypeName()); |
1117 |
buffer.append(x.getSize()); |
1118 |
} |
1119 |
CRC32 crc = new CRC32(); |
1120 |
byte[] data = buffer.toString().getBytes(); |
1121 |
crc.update(data); |
1122 |
return crc.getValue();
|
1123 |
} |
1124 |
|
1125 |
@Override
|
1126 |
public FeatureStore getStore() {
|
1127 |
if (this.storeRef == null) { |
1128 |
return null; |
1129 |
} |
1130 |
return (FeatureStore) this.storeRef.get(); |
1131 |
} |
1132 |
|
1133 |
public void setStore(FeatureStore store) { |
1134 |
if (store == null) { |
1135 |
this.storeRef = null; |
1136 |
} else {
|
1137 |
this.storeRef = new WeakReference(store); |
1138 |
} |
1139 |
} |
1140 |
|
1141 |
@Override
|
1142 |
public List<FeatureAttributeDescriptor> getFilteredAttributes( |
1143 |
Predicate<FeatureAttributeDescriptor> filter,
|
1144 |
int max
|
1145 |
) { |
1146 |
List<FeatureAttributeDescriptor> attrs = new ArrayList<>(); |
1147 |
for (FeatureAttributeDescriptor attribute : this) { |
1148 |
if (filter.test(attribute)) {
|
1149 |
attrs.add(attribute); |
1150 |
} |
1151 |
if( max>0 && attrs.size()>= max) { |
1152 |
break;
|
1153 |
} |
1154 |
} |
1155 |
return attrs;
|
1156 |
} |
1157 |
|
1158 |
@Override
|
1159 |
public List<FeatureAttributeDescriptor> getRecentUseds() { |
1160 |
return RECENTS_USEDS.getAttributes(this); |
1161 |
} |
1162 |
|
1163 |
@Override
|
1164 |
public void loadFromState(PersistentState state) |
1165 |
throws PersistenceException {
|
1166 |
|
1167 |
hasEvaluators = state.getBoolean("hasEvaluators");
|
1168 |
hasEmulators = state.getBoolean("hasEmulators");
|
1169 |
defaultGeometryAttributeName = state.getString("defaultGeometryAttributeName");
|
1170 |
defaultTimeAttributeName = state.getString("defaultTimeAttributeName");
|
1171 |
defaultGeometryAttributeIndex = state.getInt("defaultGeometryAttributeIndex");
|
1172 |
defaultTimeAttributeIndex = state.getInt("defaultTimeAttributeIndex");
|
1173 |
id = state.getString("id");
|
1174 |
hasOID = state.getBoolean("hasOID");
|
1175 |
allowAtomaticValues = state.getBoolean("allowAtomaticValues");
|
1176 |
|
1177 |
requiredFields = state.getBoolean("requiredFields");
|
1178 |
internalID = state.getString("internalID");
|
1179 |
tags = (Tags) state.get("tags");
|
1180 |
if( tags == null ) { |
1181 |
this.tags = new DefaultTags(); |
1182 |
} |
1183 |
this.checkFeaturesAtFinishEditing = state.getBoolean("checkFeaturesAtFinishEditing"); |
1184 |
this.checkFeaturesAtInsert = state.getBoolean("checkFeaturesAtInsert"); |
1185 |
|
1186 |
List<FeatureAttributeDescriptor> elements = state.getList("elements"); |
1187 |
for (FeatureAttributeDescriptor element : elements) {
|
1188 |
((DefaultFeatureAttributeDescriptor) element).setFeatureType(this);
|
1189 |
super.add(element);
|
1190 |
} |
1191 |
this.pk = null; |
1192 |
|
1193 |
this.rules = new DefaultFeatureRules(); |
1194 |
List stateRules = state.getList("rules"); |
1195 |
if(stateRules != null) { |
1196 |
this.rules.addAll(state.getList("rules")); |
1197 |
} |
1198 |
this.fixAll();
|
1199 |
} |
1200 |
|
1201 |
@Override
|
1202 |
public void saveToState(PersistentState state) throws PersistenceException { |
1203 |
|
1204 |
// FIXME: rules
|
1205 |
state.set("hasEvaluators", hasEvaluators);
|
1206 |
state.set("hasEmulators", hasEmulators);
|
1207 |
state.set("defaultGeometryAttributeName", defaultGeometryAttributeName);
|
1208 |
state.set("defaultTimeAttributeName", defaultTimeAttributeName);
|
1209 |
state.set("defaultGeometryAttributeIndex", defaultGeometryAttributeIndex);
|
1210 |
state.set("defaultTimeAttributeIndex", defaultTimeAttributeIndex);
|
1211 |
state.set("id", id);
|
1212 |
state.set("hasOID", hasOID);
|
1213 |
state.set("allowAtomaticValues", allowAtomaticValues);
|
1214 |
|
1215 |
state.set("requiredFields", requiredFields);
|
1216 |
state.set("internalID", internalID);
|
1217 |
|
1218 |
List<FeatureAttributeDescriptor> elements = new ArrayList<>(); |
1219 |
elements.addAll(this);
|
1220 |
state.set("elements", elements);
|
1221 |
state.set("tags", tags);
|
1222 |
state.set("checkFeaturesAtFinishEditing",this.checkFeaturesAtFinishEditing); |
1223 |
state.set("checkFeaturesAtInsert",this.checkFeaturesAtInsert); |
1224 |
state.set("rules", this.rules.iterator()); |
1225 |
} |
1226 |
|
1227 |
private static final String FEATTYPE_PERSISTENCE_DEFINITION_NAME = "FeatureType"; |
1228 |
|
1229 |
public static void registerPersistenceDefinition() { |
1230 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
1231 |
|
1232 |
if (manager.getDefinition(FEATTYPE_PERSISTENCE_DEFINITION_NAME)
|
1233 |
== null) {
|
1234 |
DynStruct definition = manager.addDefinition(DefaultFeatureType.class, |
1235 |
FEATTYPE_PERSISTENCE_DEFINITION_NAME, |
1236 |
FEATTYPE_PERSISTENCE_DEFINITION_NAME |
1237 |
+ " persistent definition",
|
1238 |
null,
|
1239 |
null
|
1240 |
); |
1241 |
definition.addDynFieldBoolean("hasEvaluators");
|
1242 |
definition.addDynFieldBoolean("hasEmulators");
|
1243 |
definition.addDynFieldString("defaultGeometryAttributeName");
|
1244 |
definition.addDynFieldString("defaultTimeAttributeName");
|
1245 |
definition.addDynFieldInt("defaultGeometryAttributeIndex");
|
1246 |
definition.addDynFieldInt("defaultTimeAttributeIndex");
|
1247 |
definition.addDynFieldString("id");
|
1248 |
definition.addDynFieldBoolean("hasOID");
|
1249 |
definition.addDynFieldBoolean("allowAtomaticValues");
|
1250 |
|
1251 |
definition.addDynFieldBoolean("requiredFields");
|
1252 |
definition.addDynFieldString("internalID");
|
1253 |
|
1254 |
definition.addDynFieldList("elements")
|
1255 |
.setClassOfItems(FeatureAttributeDescriptor.class); |
1256 |
|
1257 |
definition.addDynFieldObject("tags").setClassOfValue(Tags.class);
|
1258 |
definition.addDynFieldBoolean("checkFeaturesAtFinishEditing")
|
1259 |
.setMandatory(false)
|
1260 |
.setDefaultFieldValue(false);
|
1261 |
definition.addDynFieldBoolean("checkFeaturesAtInsert")
|
1262 |
.setMandatory(false)
|
1263 |
.setDefaultFieldValue(true);
|
1264 |
definition.addDynFieldList("rules")
|
1265 |
.setClassOfItems(FeatureRules.class); |
1266 |
} |
1267 |
} |
1268 |
|
1269 |
@Override
|
1270 |
public FeatureStore getAsFeatureStore() {
|
1271 |
FeatureStore store = FeatureTypeToStoreProviderAdapter.createFeatureStore(this);
|
1272 |
return store;
|
1273 |
} |
1274 |
|
1275 |
|
1276 |
@Override
|
1277 |
public String getNewFieldName() { |
1278 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
1279 |
String prefix = i18n.getTranslation("_Field"); |
1280 |
String fieldName;
|
1281 |
for (int i = 1; i < 1000; i++) { |
1282 |
fieldName = prefix+i; |
1283 |
if( this.get(fieldName)==null ) { |
1284 |
return fieldName;
|
1285 |
} |
1286 |
} |
1287 |
fieldName = prefix + (new Date()).getTime(); |
1288 |
return fieldName;
|
1289 |
} |
1290 |
|
1291 |
@Override
|
1292 |
public FeatureType getOriginalFeatureType() {
|
1293 |
DefaultFeatureStore store = (DefaultFeatureStore) this.getStore();
|
1294 |
if (store==null) { |
1295 |
return null; |
1296 |
} |
1297 |
return store.getOriginalFeatureType(this); |
1298 |
} |
1299 |
@Override
|
1300 |
public boolean hasOnlyMetadataChanges(FeatureType old) { |
1301 |
if( old == null ) { |
1302 |
throw new NullPointerException(); |
1303 |
} |
1304 |
// Si hay campos nuevos -> false
|
1305 |
for (FeatureAttributeDescriptor attr : this) { |
1306 |
if(!attr.isComputed() && old.getAttributeDescriptor(attr.getName())==null) { |
1307 |
return false; |
1308 |
} |
1309 |
} |
1310 |
|
1311 |
// Si se ha eliminado algun campo -> false
|
1312 |
for (FeatureAttributeDescriptor attr : old) {
|
1313 |
if(!attr.isComputed() && this.getAttributeDescriptor(attr.getName())==null ) { |
1314 |
return false; |
1315 |
} |
1316 |
} |
1317 |
|
1318 |
// No hay campos nuevos ni se ha eliminado ninguno, asi que comparamos
|
1319 |
// los campos uno a uno.
|
1320 |
for (FeatureAttributeDescriptor attr : this) { |
1321 |
FeatureAttributeDescriptor attrold = old.getAttributeDescriptor(attr.getName()); |
1322 |
if(!attr.isComputed() && !attr.hasOnlyMetadataChanges(attrold) ) {
|
1323 |
return false; |
1324 |
} |
1325 |
} |
1326 |
return true; |
1327 |
} |
1328 |
|
1329 |
@Override
|
1330 |
public void writeAsDALFile(File file) { |
1331 |
this.writeAsDALFile(file, "bin"); |
1332 |
} |
1333 |
|
1334 |
public void writeAsDALFile(File file, String format) { |
1335 |
try {
|
1336 |
DALFile dalFile = DALFile.getDALFile(); |
1337 |
dalFile.setFeatureType(this);
|
1338 |
if( !dalFile.isEmpty() ) {
|
1339 |
dalFile.write(file,format); |
1340 |
} |
1341 |
} catch (Exception ex) { |
1342 |
throw new RuntimeException("Can't write as DAL file ("+Objects.toString(file)+").", ex); |
1343 |
} |
1344 |
} |
1345 |
|
1346 |
@Override
|
1347 |
public JsonObject toJson() {
|
1348 |
return this.toJsonBuilder().build(); |
1349 |
} |
1350 |
|
1351 |
@Override
|
1352 |
public JsonObjectBuilder toJsonBuilder() {
|
1353 |
JsonObjectBuilder builder = Json.createObjectBuilder(); |
1354 |
builder.add_class(this);
|
1355 |
builder.add("id",id);
|
1356 |
builder.add("internalID",internalID);
|
1357 |
builder.add("label",label);
|
1358 |
builder.add("description",description);
|
1359 |
builder.add("allowAtomaticValues",allowAtomaticValues);
|
1360 |
builder.add("hasOID",hasOID);
|
1361 |
builder.add("defaultGeometryAttributeName",defaultGeometryAttributeName);
|
1362 |
builder.add("defaultTimeAttributeName",defaultTimeAttributeName);
|
1363 |
builder.add("checkFeaturesAtFinishEditing",checkFeaturesAtFinishEditing);
|
1364 |
builder.add("checkFeaturesAtInsert",checkFeaturesAtInsert);
|
1365 |
builder.add("tags", tags);
|
1366 |
builder.add("extraColumns", (SupportToJson)this.extraColumns); |
1367 |
builder.add("descriptors", this.iterator()); |
1368 |
|
1369 |
return builder;
|
1370 |
} |
1371 |
|
1372 |
public void fromJson(JsonObject json) { |
1373 |
this.clear();
|
1374 |
this.id = json.getString("id", null); |
1375 |
this.internalID = json.getString("internalID", null); |
1376 |
this.label = json.getString("label", null); |
1377 |
this.description = json.getString("description",null); |
1378 |
this.allowAtomaticValues = json.getBoolean("allowAtomaticValues",false); |
1379 |
this.checkFeaturesAtFinishEditing = json.getBoolean("checkFeaturesAtFinishEditing",false); |
1380 |
this.checkFeaturesAtInsert = json.getBoolean("checkFeaturesAtInsert",true); |
1381 |
this.hasOID = json.getBoolean("hasOID", false); |
1382 |
this.defaultGeometryAttributeName = json.getString("defaultGeometryAttributeName", null); |
1383 |
this.defaultTimeAttributeName = json.getString("defaultTimeAttributeName",null); |
1384 |
this.tags = (Tags) Json.toObject(json, "tags"); |
1385 |
this.extraColumns = (FeatureExtraColumns) Json.toObject(json, "extraColumns"); |
1386 |
if( json.containsKey("descriptors") ) { |
1387 |
for (JsonValue jsonValue : json.getJsonArray("descriptors")) { |
1388 |
DefaultEditableFeatureAttributeDescriptor attr = new DefaultEditableFeatureAttributeDescriptor(this, false); |
1389 |
attr.fromJson((JsonObject) jsonValue); |
1390 |
this.add(attr);
|
1391 |
} |
1392 |
} |
1393 |
} |
1394 |
|
1395 |
private static class TheJsonSerializer implements JsonManager.JsonSerializer { |
1396 |
|
1397 |
public TheJsonSerializer() {
|
1398 |
} |
1399 |
|
1400 |
@Override
|
1401 |
public Class getObjectClass() { |
1402 |
return DefaultFeatureType.class;
|
1403 |
} |
1404 |
|
1405 |
@Override
|
1406 |
public Object toObject(JsonObject json) { |
1407 |
DefaultFeatureType o = new DefaultFeatureType();
|
1408 |
o.fromJson(json); |
1409 |
return o;
|
1410 |
} |
1411 |
|
1412 |
@Override
|
1413 |
public JsonObjectBuilder toJsonBuilder(Object value) { |
1414 |
return ((SupportToJson)value).toJsonBuilder();
|
1415 |
} |
1416 |
|
1417 |
} |
1418 |
|
1419 |
public static void selfRegister() { |
1420 |
Json.registerSerializer(new TheJsonSerializer());
|
1421 |
} |
1422 |
|
1423 |
} |