svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dal / src / org / gvsig / fmap / dal / feature / impl / DefaultFeatureType.java @ 26323
History | View | Annotate | Download (7.12 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.Iterator; |
6 |
import java.util.List; |
7 |
import java.util.UUID; |
8 |
|
9 |
import org.gvsig.fmap.dal.exception.DataException; |
10 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
11 |
import org.gvsig.fmap.dal.feature.Feature; |
12 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
13 |
import org.gvsig.fmap.dal.feature.FeatureRules; |
14 |
import org.gvsig.fmap.dal.feature.FeatureType; |
15 |
import org.gvsig.tools.exception.NotYetImplemented; |
16 |
|
17 |
public class DefaultFeatureType extends ArrayList implements FeatureType { |
18 |
|
19 |
/**
|
20 |
*
|
21 |
*/
|
22 |
private static final long serialVersionUID = -7988721447349282215L; |
23 |
|
24 |
private DefaultFeatureRules rules;
|
25 |
private boolean hasEvaluators; |
26 |
protected String defaultGeometryAttributeName; |
27 |
protected int defaultGeometryAttributeIndex; |
28 |
private String id; |
29 |
protected boolean hasOID; |
30 |
|
31 |
protected DefaultFeatureType() {
|
32 |
this.id = getNewId();
|
33 |
this.rules = new DefaultFeatureRules(); |
34 |
this.hasEvaluators = false; |
35 |
this.defaultGeometryAttributeName = null; |
36 |
this.defaultGeometryAttributeIndex = -1; |
37 |
} |
38 |
|
39 |
protected DefaultFeatureType(DefaultFeatureType other) {
|
40 |
initialize(other, true);
|
41 |
} |
42 |
|
43 |
protected DefaultFeatureType(DefaultFeatureType other,
|
44 |
boolean copyAttributes) {
|
45 |
initialize(other, copyAttributes); |
46 |
} |
47 |
|
48 |
synchronized private String getNewId() { |
49 |
return UUID.randomUUID().toString(); // FIXME: to libCompat |
50 |
} |
51 |
|
52 |
protected void initialize(DefaultFeatureType other, boolean copyAttributes) { |
53 |
this.id = other.getId();
|
54 |
if (copyAttributes) {
|
55 |
Iterator iter = other.iterator();
|
56 |
DefaultFeatureAttributeDescriptor attr; |
57 |
while (iter.hasNext()) {
|
58 |
attr = (DefaultFeatureAttributeDescriptor) iter.next(); |
59 |
this.intitalizeAddAttibute(attr);
|
60 |
} |
61 |
} |
62 |
this.defaultGeometryAttributeName = other.defaultGeometryAttributeName;
|
63 |
this.hasEvaluators = other.hasEvaluators;
|
64 |
this.rules = (DefaultFeatureRules) other.rules.getCopy();
|
65 |
this.defaultGeometryAttributeIndex = other.defaultGeometryAttributeIndex;
|
66 |
this.hasOID = other.hasOID;
|
67 |
this.id = other.id; // XXX ???? copiar o no esto???? |
68 |
} |
69 |
|
70 |
protected void intitalizeAddAttibute(DefaultFeatureAttributeDescriptor attr) { |
71 |
super.add(attr.getCopy());
|
72 |
} |
73 |
|
74 |
public String getId() { |
75 |
return this.id; |
76 |
} |
77 |
|
78 |
public Object get(String name) { |
79 |
FeatureAttributeDescriptor attr; |
80 |
Iterator iter = this.iterator(); |
81 |
while (iter.hasNext()) {
|
82 |
attr = (FeatureAttributeDescriptor) iter.next(); |
83 |
if (attr.getName().equalsIgnoreCase(name)) {
|
84 |
return attr;
|
85 |
} |
86 |
} |
87 |
return null; |
88 |
} |
89 |
|
90 |
public FeatureAttributeDescriptor getAttributeDescriptor(String name) { |
91 |
FeatureAttributeDescriptor attr; |
92 |
Iterator iter = this.iterator(); |
93 |
while (iter.hasNext()) {
|
94 |
attr = (FeatureAttributeDescriptor) iter.next(); |
95 |
if (attr.getName().equalsIgnoreCase(name)) {
|
96 |
return attr;
|
97 |
} |
98 |
} |
99 |
return null; |
100 |
} |
101 |
|
102 |
public FeatureAttributeDescriptor getAttributeDescriptor(int index) { |
103 |
return (FeatureAttributeDescriptor) super.get(index); |
104 |
} |
105 |
|
106 |
public FeatureType getCopy() {
|
107 |
return new DefaultFeatureType(this); |
108 |
} |
109 |
|
110 |
public int getDefaultGeometryAttributeIndex() { |
111 |
return this.defaultGeometryAttributeIndex; |
112 |
} |
113 |
|
114 |
public String getDefaultGeometryAttributeName() { |
115 |
return this.defaultGeometryAttributeName; |
116 |
} |
117 |
|
118 |
public EditableFeatureType getEditable() {
|
119 |
return new DefaultEditableFeatureType(this); |
120 |
} |
121 |
|
122 |
public int getIndex(String name) { |
123 |
FeatureAttributeDescriptor attr; |
124 |
Iterator iter = this.iterator(); |
125 |
while (iter.hasNext()) {
|
126 |
attr = (FeatureAttributeDescriptor) iter.next(); |
127 |
if (attr.getName().equalsIgnoreCase(name)) {
|
128 |
return attr.getIndex();
|
129 |
} |
130 |
} |
131 |
return -1; |
132 |
} |
133 |
|
134 |
public FeatureRules getRules() {
|
135 |
return this.rules; |
136 |
} |
137 |
|
138 |
public boolean hasEvaluators() { |
139 |
return this.hasEvaluators; |
140 |
} |
141 |
|
142 |
public List getSRSs() { |
143 |
// FIXME: Falta por implementar
|
144 |
throw new NotYetImplemented(); |
145 |
} |
146 |
|
147 |
public String getDefaultSRS() { |
148 |
return this.getAttributeDescriptor( |
149 |
this.getDefaultGeometryAttributeIndex()).getSRS();
|
150 |
} |
151 |
|
152 |
public void validateFeature(Feature feature, int mode) { |
153 |
if (Feature.UPDATE == mode){
|
154 |
((DefaultFeatureRules)getRules()).validate(feature); |
155 |
} |
156 |
} |
157 |
|
158 |
public FeatureType getSubtype(String[] names) throws DataException { |
159 |
return new SubtypeFeatureType(this, names); |
160 |
} |
161 |
|
162 |
public boolean isSubtypeOf(FeatureType featureType) { |
163 |
return false; |
164 |
} |
165 |
|
166 |
|
167 |
|
168 |
class SubtypeFeatureType extends DefaultFeatureType { |
169 |
/**
|
170 |
*
|
171 |
*/
|
172 |
private static final long serialVersionUID = 6913732960073922540L; |
173 |
WeakReference parent;
|
174 |
|
175 |
SubtypeFeatureType(DefaultFeatureType parent, String[] names) |
176 |
throws DataException {
|
177 |
super(parent, false); |
178 |
DefaultFeatureAttributeDescriptor attrcopy; |
179 |
DefaultFeatureAttributeDescriptor attr; |
180 |
for (int i = 0; i < names.length; i++) { |
181 |
attr = (DefaultFeatureAttributeDescriptor) parent |
182 |
.getAttributeDescriptor(names[i]); |
183 |
if (attr == null) { |
184 |
throw new SubtypeFeatureTypeNameException(names[i], parent |
185 |
.getId()); |
186 |
} |
187 |
attrcopy = new DefaultFeatureAttributeDescriptor(attr);
|
188 |
this.add(attrcopy);
|
189 |
attrcopy.index = i; |
190 |
} |
191 |
this.defaultGeometryAttributeIndex = this |
192 |
.getIndex(this.defaultGeometryAttributeName);
|
193 |
if (this.defaultGeometryAttributeIndex < 0) { |
194 |
this.defaultGeometryAttributeName = null; |
195 |
} |
196 |
this.parent = new WeakReference(parent); |
197 |
} |
198 |
|
199 |
public FeatureType getSubtype(String[] names) throws DataException { |
200 |
return new SubtypeFeatureType((DefaultFeatureType) this.parent |
201 |
.get(), names); |
202 |
} |
203 |
|
204 |
public boolean isSubtypeOf(FeatureType featureType) { |
205 |
if (featureType == null) { |
206 |
return false; |
207 |
} |
208 |
FeatureType parent = (FeatureType) this.parent.get();
|
209 |
return featureType.equals(parent);
|
210 |
} |
211 |
|
212 |
public EditableFeatureType getEditable() {
|
213 |
throw new UnsupportedOperationException(); |
214 |
} |
215 |
} |
216 |
|
217 |
public class SubtypeFeatureTypeNameException extends DataException { |
218 |
|
219 |
/**
|
220 |
*
|
221 |
*/
|
222 |
private static final long serialVersionUID = -4414242486723260101L; |
223 |
private final static String MESSAGE_FORMAT = "Attribute name '%(name)s' not found in type (%(type)s)."; |
224 |
private final static String MESSAGE_KEY = "_SubtypeFeatureTypeNameException"; |
225 |
|
226 |
public SubtypeFeatureTypeNameException(String name, String type) { |
227 |
super(MESSAGE_FORMAT, MESSAGE_KEY, serialVersionUID);
|
228 |
setValue("name", name);
|
229 |
setValue("type", type);
|
230 |
} |
231 |
} |
232 |
|
233 |
public boolean hasOID() { |
234 |
return hasOID;
|
235 |
} |
236 |
public String toString(){ |
237 |
StringBuffer s = new StringBuffer(); |
238 |
String attName;
|
239 |
for (int i = 0; i < size(); i++) { |
240 |
attName =((FeatureAttributeDescriptor)get(i)).getName().toString(); |
241 |
if (attName.length() > 3) { |
242 |
s.append(attName.substring(0, 3)); |
243 |
} else {
|
244 |
s.append(attName); |
245 |
} |
246 |
s.append(';');
|
247 |
} |
248 |
return s.toString();
|
249 |
} |
250 |
|
251 |
public Iterator iterator() { |
252 |
return getIterator(super.iterator()); |
253 |
} |
254 |
|
255 |
protected Iterator getIterator(Iterator iter) { |
256 |
return new DelegatedIterator(iter); |
257 |
} |
258 |
|
259 |
protected class DelegatedIterator implements Iterator { |
260 |
|
261 |
protected Iterator iterator; |
262 |
|
263 |
public DelegatedIterator(Iterator iter) { |
264 |
this.iterator = iter;
|
265 |
} |
266 |
|
267 |
public boolean hasNext() { |
268 |
return iterator.hasNext();
|
269 |
} |
270 |
|
271 |
public Object next() { |
272 |
return iterator.next();
|
273 |
} |
274 |
|
275 |
public void remove() { |
276 |
throw new UnsupportedOperationException(); |
277 |
} |
278 |
|
279 |
} |
280 |
|
281 |
} |