root / branches / v2_0_0_prep / libraries / libFMap_dal / src / org / gvsig / fmap / dal / feature / impl / DefaultFeatureType.java @ 30187
History | View | Annotate | Download (13.6 KB)
1 |
package org.gvsig.fmap.dal.feature.impl; |
---|---|
2 |
|
3 |
import java.lang.ref.WeakReference; |
4 |
import java.util.ArrayList; |
5 |
import java.util.Collections; |
6 |
import java.util.Iterator; |
7 |
import java.util.List; |
8 |
|
9 |
import org.cresques.cts.IProjection; |
10 |
import org.gvsig.fmap.dal.DataTypes; |
11 |
import org.gvsig.fmap.dal.exception.DataException; |
12 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
13 |
import org.gvsig.fmap.dal.feature.Feature; |
14 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
15 |
import org.gvsig.fmap.dal.feature.FeatureRules; |
16 |
import org.gvsig.fmap.dal.feature.FeatureType; |
17 |
import org.gvsig.tools.dynobject.DynClass; |
18 |
import org.gvsig.tools.dynobject.DynField; |
19 |
import org.gvsig.tools.dynobject.DynMethod; |
20 |
import org.gvsig.tools.dynobject.DynObject; |
21 |
import org.gvsig.tools.dynobject.DynObjectValueItem; |
22 |
import org.gvsig.tools.dynobject.exception.DynMethodException; |
23 |
import org.gvsig.tools.dynobject.exception.DynObjectValidateException; |
24 |
|
25 |
public class DefaultFeatureType extends ArrayList implements FeatureType { |
26 |
|
27 |
/**
|
28 |
*
|
29 |
*/
|
30 |
private static final long serialVersionUID = -7988721447349282215L; |
31 |
|
32 |
private DefaultFeatureRules rules;
|
33 |
private boolean hasEvaluators; |
34 |
protected String defaultGeometryAttributeName; |
35 |
protected int defaultGeometryAttributeIndex; |
36 |
private String id; |
37 |
protected boolean hasOID; |
38 |
protected boolean allowAtomaticValues; |
39 |
protected FeatureAttributeDescriptor[] pk = null; |
40 |
|
41 |
private List srsList = null; |
42 |
|
43 |
protected DefaultFeatureType(String id) { |
44 |
this.id = id;
|
45 |
this.rules = new DefaultFeatureRules(); |
46 |
this.hasEvaluators = false; |
47 |
this.defaultGeometryAttributeName = null; |
48 |
this.defaultGeometryAttributeIndex = -1; |
49 |
this.allowAtomaticValues = false; |
50 |
} |
51 |
|
52 |
protected DefaultFeatureType() {
|
53 |
this("default"); |
54 |
} |
55 |
|
56 |
protected DefaultFeatureType(DefaultFeatureType other) {
|
57 |
initialize(other, true);
|
58 |
} |
59 |
|
60 |
protected DefaultFeatureType(DefaultFeatureType other,
|
61 |
boolean copyAttributes) {
|
62 |
initialize(other, copyAttributes); |
63 |
} |
64 |
|
65 |
protected void initialize(DefaultFeatureType other, boolean copyAttributes) { |
66 |
this.id = other.getId();
|
67 |
if (copyAttributes) {
|
68 |
Iterator iter = other.iterator();
|
69 |
DefaultFeatureAttributeDescriptor attr; |
70 |
while (iter.hasNext()) {
|
71 |
attr = (DefaultFeatureAttributeDescriptor) iter.next(); |
72 |
this.intitalizeAddAttibute(attr);
|
73 |
} |
74 |
} |
75 |
this.defaultGeometryAttributeName = other.defaultGeometryAttributeName;
|
76 |
this.hasEvaluators = other.hasEvaluators;
|
77 |
this.rules = (DefaultFeatureRules) other.rules.getCopy();
|
78 |
this.defaultGeometryAttributeIndex = other.defaultGeometryAttributeIndex;
|
79 |
this.hasOID = other.hasOID;
|
80 |
this.id = other.id; // XXX ???? copiar o no esto???? |
81 |
} |
82 |
|
83 |
protected void intitalizeAddAttibute(DefaultFeatureAttributeDescriptor attr) { |
84 |
super.add(attr.getCopy());
|
85 |
} |
86 |
|
87 |
public String getId() { |
88 |
return this.id; |
89 |
} |
90 |
|
91 |
public Object get(String name) { |
92 |
FeatureAttributeDescriptor attr; |
93 |
Iterator iter = this.iterator(); |
94 |
while (iter.hasNext()) {
|
95 |
attr = (FeatureAttributeDescriptor) iter.next(); |
96 |
if (attr.getName().equalsIgnoreCase(name)) {
|
97 |
return attr;
|
98 |
} |
99 |
} |
100 |
return null; |
101 |
} |
102 |
|
103 |
public FeatureAttributeDescriptor getAttributeDescriptor(String name) { |
104 |
FeatureAttributeDescriptor attr; |
105 |
Iterator iter = this.iterator(); |
106 |
while (iter.hasNext()) {
|
107 |
attr = (FeatureAttributeDescriptor) iter.next(); |
108 |
if (attr.getName().equalsIgnoreCase(name)) {
|
109 |
return attr;
|
110 |
} |
111 |
} |
112 |
return null; |
113 |
} |
114 |
|
115 |
public FeatureAttributeDescriptor getAttributeDescriptor(int index) { |
116 |
return (FeatureAttributeDescriptor) super.get(index); |
117 |
} |
118 |
|
119 |
public FeatureType getCopy() {
|
120 |
return new DefaultFeatureType(this); |
121 |
} |
122 |
|
123 |
public int getDefaultGeometryAttributeIndex() { |
124 |
return this.defaultGeometryAttributeIndex; |
125 |
} |
126 |
|
127 |
public String getDefaultGeometryAttributeName() { |
128 |
return this.defaultGeometryAttributeName; |
129 |
} |
130 |
|
131 |
public EditableFeatureType getEditable() {
|
132 |
return new DefaultEditableFeatureType(this); |
133 |
} |
134 |
|
135 |
public int getIndex(String name) { |
136 |
FeatureAttributeDescriptor attr; |
137 |
Iterator iter = this.iterator(); |
138 |
while (iter.hasNext()) {
|
139 |
attr = (FeatureAttributeDescriptor) iter.next(); |
140 |
if (attr.getName().equalsIgnoreCase(name)) {
|
141 |
return attr.getIndex();
|
142 |
} |
143 |
} |
144 |
return -1; |
145 |
} |
146 |
|
147 |
public FeatureRules getRules() {
|
148 |
return this.rules; |
149 |
} |
150 |
|
151 |
public boolean hasEvaluators() { |
152 |
return this.hasEvaluators; |
153 |
} |
154 |
|
155 |
public List getSRSs() { |
156 |
if (this.srsList == null) { |
157 |
ArrayList tmp = new ArrayList(); |
158 |
Iterator iter = iterator();
|
159 |
Iterator tmpIter;
|
160 |
boolean allreadyHave;
|
161 |
IProjection tmpSRS; |
162 |
FeatureAttributeDescriptor attr; |
163 |
while (iter.hasNext()){
|
164 |
attr = (FeatureAttributeDescriptor) iter.next(); |
165 |
if (attr.getDataType() == DataTypes.GEOMETRY
|
166 |
&& attr.getSRS() != null) {
|
167 |
allreadyHave = false;
|
168 |
tmpIter = tmp.iterator(); |
169 |
while (tmpIter.hasNext()) {
|
170 |
tmpSRS = (IProjection) tmpIter.next(); |
171 |
if (tmpSRS.getAbrev().equals(attr.getSRS().getAbrev())) {
|
172 |
allreadyHave = true;
|
173 |
break;
|
174 |
} |
175 |
} |
176 |
if (!allreadyHave) {
|
177 |
tmp.add(attr.getSRS()); |
178 |
} |
179 |
} |
180 |
} |
181 |
this.srsList = Collections.unmodifiableList(tmp); |
182 |
} |
183 |
return this.srsList; |
184 |
} |
185 |
|
186 |
public IProjection getDefaultSRS() {
|
187 |
if (this.getDefaultGeometryAttributeIndex() < 0) { |
188 |
return null; |
189 |
} |
190 |
return this.getAttributeDescriptor( |
191 |
this.getDefaultGeometryAttributeIndex()).getSRS();
|
192 |
} |
193 |
|
194 |
public void validateFeature(Feature feature, int mode) { |
195 |
if (Feature.UPDATE == mode){
|
196 |
((DefaultFeatureRules)getRules()).validate(feature); |
197 |
} |
198 |
} |
199 |
|
200 |
public FeatureType getSubtype(String[] names) throws DataException { |
201 |
return new SubtypeFeatureType(this, names); |
202 |
} |
203 |
|
204 |
public boolean isSubtypeOf(FeatureType featureType) { |
205 |
return false; |
206 |
} |
207 |
|
208 |
|
209 |
|
210 |
class SubtypeFeatureType extends DefaultFeatureType { |
211 |
/**
|
212 |
*
|
213 |
*/
|
214 |
private static final long serialVersionUID = 6913732960073922540L; |
215 |
WeakReference parent;
|
216 |
|
217 |
SubtypeFeatureType(DefaultFeatureType parent, String[] names) |
218 |
throws DataException {
|
219 |
super(parent, false); |
220 |
DefaultFeatureAttributeDescriptor attrcopy; |
221 |
DefaultFeatureAttributeDescriptor attr; |
222 |
// Copy attributes
|
223 |
for (int i = 0; i < names.length; i++) { |
224 |
attr = (DefaultFeatureAttributeDescriptor) parent |
225 |
.getAttributeDescriptor(names[i]); |
226 |
if (attr == null) { |
227 |
throw new SubtypeFeatureTypeNameException(names[i], parent |
228 |
.getId()); |
229 |
} |
230 |
attrcopy = new DefaultFeatureAttributeDescriptor(attr);
|
231 |
this.add(attrcopy);
|
232 |
attrcopy.index = i; |
233 |
} |
234 |
|
235 |
// Add missing pk fiels if any
|
236 |
if (!parent.hasOID()) {
|
237 |
Iterator iter = parent.iterator();
|
238 |
while (iter.hasNext()) {
|
239 |
attr = (DefaultFeatureAttributeDescriptor) iter.next(); |
240 |
if (attr.isPrimaryKey()
|
241 |
&& this.getIndex(attr.getName()) < 0) { |
242 |
attrcopy = new DefaultFeatureAttributeDescriptor(attr);
|
243 |
this.add(attrcopy);
|
244 |
attrcopy.index = this.size() - 1; |
245 |
} |
246 |
} |
247 |
} |
248 |
|
249 |
this.defaultGeometryAttributeIndex = this |
250 |
.getIndex(this.defaultGeometryAttributeName);
|
251 |
if (this.defaultGeometryAttributeIndex < 0) { |
252 |
this.defaultGeometryAttributeName = null; |
253 |
} |
254 |
this.parent = new WeakReference(parent); |
255 |
} |
256 |
|
257 |
public FeatureType getSubtype(String[] names) throws DataException { |
258 |
return new SubtypeFeatureType((DefaultFeatureType) this.parent |
259 |
.get(), names); |
260 |
} |
261 |
|
262 |
public boolean isSubtypeOf(FeatureType featureType) { |
263 |
if (featureType == null) { |
264 |
return false; |
265 |
} |
266 |
FeatureType parent = (FeatureType) this.parent.get();
|
267 |
return featureType.equals(parent);
|
268 |
} |
269 |
|
270 |
public EditableFeatureType getEditable() {
|
271 |
throw new UnsupportedOperationException(); |
272 |
} |
273 |
} |
274 |
|
275 |
public class SubtypeFeatureTypeNameException extends DataException { |
276 |
|
277 |
/**
|
278 |
*
|
279 |
*/
|
280 |
private static final long serialVersionUID = -4414242486723260101L; |
281 |
private final static String MESSAGE_FORMAT = "Attribute name '%(name)s' not found in type (%(type)s)."; |
282 |
private final static String MESSAGE_KEY = "_SubtypeFeatureTypeNameException"; |
283 |
|
284 |
public SubtypeFeatureTypeNameException(String name, String type) { |
285 |
super(MESSAGE_FORMAT, MESSAGE_KEY, serialVersionUID);
|
286 |
setValue("name", name);
|
287 |
setValue("type", type);
|
288 |
} |
289 |
} |
290 |
|
291 |
public boolean hasOID() { |
292 |
return hasOID;
|
293 |
} |
294 |
public String toString(){ |
295 |
StringBuffer s = new StringBuffer(); |
296 |
s.append(this.getId());
|
297 |
s.append(":[");
|
298 |
String attName;
|
299 |
for (int i = 0; i < size(); i++) { |
300 |
attName =((FeatureAttributeDescriptor)get(i)).getName().toString(); |
301 |
s.append(attName); |
302 |
if (i < size() - 1) { |
303 |
s.append(',');
|
304 |
} |
305 |
} |
306 |
s.append(']');
|
307 |
return s.toString();
|
308 |
} |
309 |
|
310 |
public Iterator iterator() { |
311 |
return getIterator(super.iterator()); |
312 |
} |
313 |
|
314 |
protected Iterator getIterator(Iterator iter) { |
315 |
return new DelegatedIterator(iter); |
316 |
} |
317 |
|
318 |
protected class DelegatedIterator implements Iterator { |
319 |
|
320 |
protected Iterator iterator; |
321 |
|
322 |
public DelegatedIterator(Iterator iter) { |
323 |
this.iterator = iter;
|
324 |
} |
325 |
|
326 |
public boolean hasNext() { |
327 |
return iterator.hasNext();
|
328 |
} |
329 |
|
330 |
public Object next() { |
331 |
return iterator.next();
|
332 |
} |
333 |
|
334 |
public void remove() { |
335 |
throw new UnsupportedOperationException(); |
336 |
} |
337 |
|
338 |
} |
339 |
|
340 |
public boolean allowAutomaticValues() { |
341 |
return this.allowAtomaticValues; |
342 |
} |
343 |
|
344 |
public FeatureAttributeDescriptor[] getAttributeDescriptors() { |
345 |
return (FeatureAttributeDescriptor[]) super |
346 |
.toArray(new FeatureAttributeDescriptor[super.size()]); |
347 |
} |
348 |
|
349 |
public FeatureAttributeDescriptor[] getPrimaryKey() { |
350 |
if (pk == null) { |
351 |
List pkList = new ArrayList(); |
352 |
Iterator iter = super.iterator(); |
353 |
FeatureAttributeDescriptor attr; |
354 |
while (iter.hasNext()){
|
355 |
attr = (FeatureAttributeDescriptor) iter.next(); |
356 |
if (attr.isPrimaryKey()){
|
357 |
pkList.add(attr); |
358 |
} |
359 |
} |
360 |
pk = (FeatureAttributeDescriptor[]) pkList
|
361 |
.toArray(new FeatureAttributeDescriptor[pkList.size()]);
|
362 |
} |
363 |
return pk;
|
364 |
} |
365 |
|
366 |
public FeatureAttributeDescriptor getDefaultGeometryAttribute() {
|
367 |
if (this.defaultGeometryAttributeIndex < 0) { |
368 |
return null; |
369 |
} |
370 |
return (FeatureAttributeDescriptor) super |
371 |
.get(this.defaultGeometryAttributeIndex);
|
372 |
} |
373 |
|
374 |
|
375 |
|
376 |
public boolean equals(Object other) { |
377 |
if (this == other) { |
378 |
return true; |
379 |
} |
380 |
if (!(other instanceof DefaultFeatureType)) { |
381 |
return false; |
382 |
} |
383 |
DefaultFeatureType otherType = (DefaultFeatureType) other; |
384 |
if (!this.id.equals(otherType.id)) { |
385 |
return false; |
386 |
} |
387 |
if (this.size() != otherType.size()) { |
388 |
return false; |
389 |
} |
390 |
FeatureAttributeDescriptor attr,attrOther; |
391 |
Iterator iter,iterOther;
|
392 |
iter = this.iterator();
|
393 |
iterOther = otherType.iterator(); |
394 |
while (iter.hasNext()) {
|
395 |
attr = (FeatureAttributeDescriptor) iter.next(); |
396 |
attrOther = (FeatureAttributeDescriptor) iterOther.next(); |
397 |
if (!attr.equals(attrOther)) {
|
398 |
return false; |
399 |
} |
400 |
} |
401 |
|
402 |
if (defaultGeometryAttributeName != otherType.defaultGeometryAttributeName) {
|
403 |
if (defaultGeometryAttributeName == null) { |
404 |
return false; |
405 |
} |
406 |
return defaultGeometryAttributeName
|
407 |
.equals(otherType.defaultGeometryAttributeName); |
408 |
|
409 |
} |
410 |
return true; |
411 |
|
412 |
} |
413 |
|
414 |
/**
|
415 |
* Start of DynClass interface implementation
|
416 |
* READONLY
|
417 |
*/
|
418 |
|
419 |
public DynField addDynField(String name) { |
420 |
throw new UnsupportedOperationException(); |
421 |
} |
422 |
|
423 |
public DynField getDeclaredDynField(String name) { |
424 |
return getAttributeDescriptor(name);
|
425 |
} |
426 |
|
427 |
public DynField[] getDeclaredDynFields() { |
428 |
return getAttributeDescriptors();
|
429 |
} |
430 |
|
431 |
public String getDescription() { |
432 |
// TODO Auto-generated method stub
|
433 |
return null; |
434 |
} |
435 |
|
436 |
public DynField getDynField(String name) { |
437 |
return getAttributeDescriptor(name);
|
438 |
} |
439 |
|
440 |
public DynField[] getDynFields() { |
441 |
return getAttributeDescriptors();
|
442 |
} |
443 |
|
444 |
public String getName() { |
445 |
return Long.toString(serialVersionUID); |
446 |
} |
447 |
|
448 |
public void removeDynField(String name) { |
449 |
throw new UnsupportedOperationException(); |
450 |
|
451 |
} |
452 |
|
453 |
public void addDynMethod(DynMethod dynMethod) { |
454 |
throw new UnsupportedOperationException(); |
455 |
|
456 |
} |
457 |
|
458 |
public void extend(DynClass dynClass) { |
459 |
throw new UnsupportedOperationException(); |
460 |
|
461 |
} |
462 |
|
463 |
public void extend(String dynClassName) { |
464 |
throw new UnsupportedOperationException(); |
465 |
|
466 |
} |
467 |
|
468 |
public DynMethod getDeclaredDynMethod(String name) |
469 |
throws DynMethodException {
|
470 |
// TODO Auto-generated method stub
|
471 |
return null; |
472 |
} |
473 |
|
474 |
public DynMethod[] getDeclaredDynMethods() throws DynMethodException { |
475 |
// TODO Auto-generated method stub
|
476 |
return null; |
477 |
} |
478 |
|
479 |
public DynMethod getDynMethod(String name) throws DynMethodException { |
480 |
// TODO Auto-generated method stub
|
481 |
return null; |
482 |
} |
483 |
|
484 |
public DynMethod getDynMethod(int code) throws DynMethodException { |
485 |
// TODO Auto-generated method stub
|
486 |
return null; |
487 |
} |
488 |
|
489 |
public DynMethod[] getDynMethods() throws DynMethodException { |
490 |
// TODO Auto-generated method stub
|
491 |
return null; |
492 |
} |
493 |
|
494 |
public DynClass[] getSuperDynClasses() { |
495 |
// TODO Auto-generated method stub
|
496 |
return null; |
497 |
} |
498 |
|
499 |
public boolean isInstance(DynObject dynObject) { |
500 |
if (dynObject.getDynClass().getName() == getName())
|
501 |
return true; |
502 |
return false; |
503 |
} |
504 |
|
505 |
public DynObject newInstance() {
|
506 |
|
507 |
throw new UnsupportedOperationException(); |
508 |
} |
509 |
|
510 |
public void removeDynMethod(String name) { |
511 |
throw new UnsupportedOperationException(); |
512 |
|
513 |
} |
514 |
|
515 |
public DynField addDynFieldChoice(String name, int type, |
516 |
Object defaultValue, DynObjectValueItem[] values, |
517 |
boolean mandatory, boolean persistent) { |
518 |
throw new UnsupportedOperationException(); |
519 |
} |
520 |
|
521 |
public DynField addDynFieldRange(String name, int type, |
522 |
Object defaultValue, Object min, Object max, boolean mandatory, |
523 |
boolean persistent) {
|
524 |
throw new UnsupportedOperationException(); |
525 |
} |
526 |
|
527 |
public DynField addDynFieldSingle(String name, int type, |
528 |
Object defaultValue, boolean mandatory, boolean persistent) { |
529 |
throw new UnsupportedOperationException(); |
530 |
} |
531 |
|
532 |
public void validate(DynObject object) throws DynObjectValidateException { |
533 |
//FIXME: not sure it's the correct code
|
534 |
if (object instanceof Feature) { |
535 |
Feature fea = (Feature) object; |
536 |
if (fea.getType().equals(this)) |
537 |
return;
|
538 |
} |
539 |
throw new DynObjectValidateException( |
540 |
"The object is not a Feature or is not this FeatureType",
|
541 |
"NOT_THIS_FEATURE_TYPE", -1); |
542 |
} |
543 |
|
544 |
|
545 |
|
546 |
} |