svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dal / src / org / gvsig / fmap / dal / feature / impl / DefaultEditableFeatureType.java @ 39080
History | View | Annotate | Download (8.49 KB)
1 |
package org.gvsig.fmap.dal.feature.impl; |
---|---|
2 |
|
3 |
import java.text.MessageFormat; |
4 |
|
5 |
import java.util.Iterator; |
6 |
|
7 |
import org.gvsig.fmap.dal.DataTypes; |
8 |
import org.gvsig.fmap.dal.exception.DataListException; |
9 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
10 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
11 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
12 |
import org.gvsig.fmap.dal.feature.FeatureType; |
13 |
import org.gvsig.fmap.dal.feature.exception.FeatureTypeIntegrityException; |
14 |
import org.gvsig.fmap.dal.feature.exception.UnsupportedDataTypeException; |
15 |
import org.gvsig.tools.evaluator.Evaluator; |
16 |
|
17 |
public class DefaultEditableFeatureType extends DefaultFeatureType implements |
18 |
EditableFeatureType { |
19 |
|
20 |
/**
|
21 |
*
|
22 |
*/
|
23 |
private static final long serialVersionUID = -713976880396024389L; |
24 |
|
25 |
private boolean hasStrongChanges; |
26 |
private DefaultFeatureType source;
|
27 |
|
28 |
public DefaultEditableFeatureType() {
|
29 |
super();
|
30 |
this.hasStrongChanges = false; |
31 |
this.source = null; |
32 |
} |
33 |
|
34 |
public DefaultEditableFeatureType(String id) { |
35 |
super(id);
|
36 |
this.hasStrongChanges = false; |
37 |
this.source = null; |
38 |
} |
39 |
|
40 |
protected DefaultEditableFeatureType(DefaultEditableFeatureType other) {
|
41 |
super(other);
|
42 |
this.source = (DefaultFeatureType) other.getSource();
|
43 |
} |
44 |
|
45 |
protected DefaultEditableFeatureType(DefaultFeatureType other) {
|
46 |
super(other);
|
47 |
this.source = other;
|
48 |
} |
49 |
|
50 |
protected void intitalizeAddAttibute(DefaultFeatureAttributeDescriptor attr) { |
51 |
super.add(new DefaultEditableFeatureAttributeDescriptor(attr)); |
52 |
} |
53 |
|
54 |
|
55 |
public boolean hasStrongChanges() { |
56 |
if (hasStrongChanges) {
|
57 |
return true; |
58 |
} |
59 |
Iterator iter = this.iterator(); |
60 |
DefaultEditableFeatureAttributeDescriptor attr; |
61 |
while (iter.hasNext()) {
|
62 |
attr = (DefaultEditableFeatureAttributeDescriptor) iter.next(); |
63 |
if (attr.hasStrongChanges()) {
|
64 |
return true; |
65 |
} |
66 |
} |
67 |
return false; |
68 |
} |
69 |
|
70 |
public FeatureType getCopy() {
|
71 |
return new DefaultEditableFeatureType(this); |
72 |
} |
73 |
|
74 |
public EditableFeatureType getEditable() {
|
75 |
throw new UnsupportedOperationException(); |
76 |
} |
77 |
|
78 |
public boolean addAll(DefaultFeatureType other) { |
79 |
Iterator iter = other.iterator();
|
80 |
DefaultFeatureAttributeDescriptor attr; |
81 |
DefaultEditableFeatureAttributeDescriptor editableAttr; |
82 |
while (iter.hasNext()) {
|
83 |
attr = (DefaultFeatureAttributeDescriptor) iter.next(); |
84 |
if (attr instanceof DefaultEditableFeatureAttributeDescriptor) { |
85 |
editableAttr = new DefaultEditableFeatureAttributeDescriptor(
|
86 |
attr); |
87 |
} else {
|
88 |
editableAttr = new DefaultEditableFeatureAttributeDescriptor(
|
89 |
attr); |
90 |
} |
91 |
super.add(editableAttr);
|
92 |
} |
93 |
this.pk = null; |
94 |
this.fixAll();
|
95 |
return true; |
96 |
} |
97 |
|
98 |
public EditableFeatureAttributeDescriptor addLike(
|
99 |
FeatureAttributeDescriptor other) { |
100 |
DefaultEditableFeatureAttributeDescriptor editableAttr; |
101 |
if (other instanceof DefaultEditableFeatureAttributeDescriptor) { |
102 |
editableAttr = new DefaultEditableFeatureAttributeDescriptor(
|
103 |
(DefaultFeatureAttributeDescriptor) other); |
104 |
} else {
|
105 |
editableAttr = new DefaultEditableFeatureAttributeDescriptor(
|
106 |
(DefaultFeatureAttributeDescriptor) other); |
107 |
} |
108 |
super.add(editableAttr);
|
109 |
this.fixAll();
|
110 |
return editableAttr;
|
111 |
|
112 |
} |
113 |
|
114 |
public FeatureType getSource() {
|
115 |
return source;
|
116 |
} |
117 |
|
118 |
public FeatureType getNotEditableCopy() {
|
119 |
DefaultFeatureType copy = new DefaultFeatureType(this, false); |
120 |
Iterator iter = this.iterator(); |
121 |
DefaultFeatureAttributeDescriptor attr; |
122 |
while (iter.hasNext()) {
|
123 |
attr = (DefaultFeatureAttributeDescriptor) iter.next(); |
124 |
copy.add(new DefaultFeatureAttributeDescriptor(attr));
|
125 |
} |
126 |
return copy;
|
127 |
} |
128 |
|
129 |
public EditableFeatureAttributeDescriptor add(String name, int type) { |
130 |
return this.add(name,type,true); |
131 |
} |
132 |
|
133 |
private EditableFeatureAttributeDescriptor add(String name, int type, boolean updateHasStrongChanges) { |
134 |
DefaultEditableFeatureAttributeDescriptor attr = new DefaultEditableFeatureAttributeDescriptor();
|
135 |
Iterator iter = this.iterator(); |
136 |
while (iter.hasNext()) {
|
137 |
EditableFeatureAttributeDescriptor descriptor = (EditableFeatureAttributeDescriptor) iter.next(); |
138 |
if(descriptor.getName().equalsIgnoreCase(name)){
|
139 |
throw new RuntimeException( |
140 |
MessageFormat.format("Name descriptor {0} duplicated.",new String[]{name}) |
141 |
); |
142 |
} |
143 |
|
144 |
} |
145 |
attr.setName(name); |
146 |
switch (type) {
|
147 |
case DataTypes.BOOLEAN:
|
148 |
case DataTypes.BYTE:
|
149 |
case DataTypes.BYTEARRAY:
|
150 |
case DataTypes.CHAR:
|
151 |
case DataTypes.DATE:
|
152 |
case DataTypes.DOUBLE:
|
153 |
case DataTypes.FLOAT:
|
154 |
case DataTypes.GEOMETRY:
|
155 |
case DataTypes.INT:
|
156 |
case DataTypes.LONG:
|
157 |
case DataTypes.OBJECT:
|
158 |
case DataTypes.STRING:
|
159 |
case DataTypes.TIME:
|
160 |
case DataTypes.TIMESTAMP:
|
161 |
break;
|
162 |
|
163 |
default:
|
164 |
throw new UnsupportedDataTypeException(name, type); |
165 |
} |
166 |
attr.setDataType(type); |
167 |
attr.setIndex(this.size());
|
168 |
super.add(attr);
|
169 |
if (!hasStrongChanges && updateHasStrongChanges){
|
170 |
hasStrongChanges = true;
|
171 |
} |
172 |
this.pk = null; |
173 |
return attr;
|
174 |
} |
175 |
|
176 |
public EditableFeatureAttributeDescriptor add(String name, int type, |
177 |
int size) {
|
178 |
return this.add(name, type,true).setSize(size); |
179 |
} |
180 |
|
181 |
public EditableFeatureAttributeDescriptor add(String name, int type, |
182 |
Evaluator evaluator) { |
183 |
if (evaluator == null){ |
184 |
throw new IllegalArgumentException(); |
185 |
} |
186 |
return this.add(name, type, false).setEvaluator(evaluator); |
187 |
} |
188 |
|
189 |
public Object remove(String name) { |
190 |
DefaultFeatureAttributeDescriptor attr = (DefaultFeatureAttributeDescriptor) this
|
191 |
.get(name); |
192 |
if (attr == null) { |
193 |
return null; |
194 |
} |
195 |
if (attr.getEvaluator() == null) { |
196 |
hasStrongChanges = true;
|
197 |
} |
198 |
super.remove(attr);
|
199 |
this.pk = null; |
200 |
this.fixAll();
|
201 |
return attr;
|
202 |
} |
203 |
|
204 |
protected void fixAll() { |
205 |
int i = 0; |
206 |
Iterator iter = super.iterator(); |
207 |
DefaultFeatureAttributeDescriptor attr; |
208 |
|
209 |
while (iter.hasNext()) {
|
210 |
attr = (DefaultFeatureAttributeDescriptor) iter |
211 |
.next(); |
212 |
attr.setIndex(i++); |
213 |
if (attr instanceof DefaultEditableFeatureAttributeDescriptor) { |
214 |
((DefaultEditableFeatureAttributeDescriptor) attr).fixAll(); |
215 |
} |
216 |
} |
217 |
if (this.defaultGeometryAttributeName == null) { |
218 |
return;
|
219 |
} |
220 |
this.defaultGeometryAttributeIndex = this |
221 |
.getIndex(this.defaultGeometryAttributeName);
|
222 |
} |
223 |
|
224 |
public void checkIntegrity() throws DataListException { |
225 |
Iterator iter = super.iterator(); |
226 |
FeatureTypeIntegrityException ex = null;
|
227 |
|
228 |
while (iter.hasNext()) {
|
229 |
DefaultEditableFeatureAttributeDescriptor attr = (DefaultEditableFeatureAttributeDescriptor) iter |
230 |
.next(); |
231 |
try {
|
232 |
attr.checkIntegrity(); |
233 |
} catch (Exception e) { |
234 |
if (ex == null) { |
235 |
ex = new FeatureTypeIntegrityException(this.getId()); |
236 |
} |
237 |
ex.add(e); |
238 |
} |
239 |
} |
240 |
if (ex != null) { |
241 |
throw ex;
|
242 |
} |
243 |
} |
244 |
|
245 |
public boolean remove(EditableFeatureAttributeDescriptor attribute) { |
246 |
if (attribute.getEvaluator() != null) { |
247 |
hasStrongChanges = true;
|
248 |
} |
249 |
if (!super.remove(attribute)) { |
250 |
return false; |
251 |
} |
252 |
this.fixAll();
|
253 |
return true; |
254 |
} |
255 |
|
256 |
public void setDefaultGeometryAttributeName(String name) { |
257 |
if (name == null || name.length() == 0) { |
258 |
this.defaultGeometryAttributeName = null; |
259 |
this.defaultGeometryAttributeIndex = -1; |
260 |
return;
|
261 |
} |
262 |
DefaultFeatureAttributeDescriptor attr = (DefaultFeatureAttributeDescriptor) this
|
263 |
.get(name); |
264 |
if (attr == null) { |
265 |
throw new IllegalArgumentException("Attribute '" + name |
266 |
+ "' not found.");
|
267 |
} |
268 |
if (attr.getType() != DataTypes.GEOMETRY) {
|
269 |
throw new IllegalArgumentException("Attribute '" + name |
270 |
+ "' is not a geometry.");
|
271 |
} |
272 |
this.defaultGeometryAttributeName = name;
|
273 |
this.defaultGeometryAttributeIndex = attr.getIndex();
|
274 |
} |
275 |
|
276 |
public void setHasOID(boolean hasOID) { |
277 |
this.hasOID = hasOID;
|
278 |
} |
279 |
|
280 |
protected Iterator getIterator(Iterator iter) { |
281 |
return new EditableDelegatedIterator(iter, this); |
282 |
} |
283 |
|
284 |
protected class EditableDelegatedIterator extends DelegatedIterator { |
285 |
|
286 |
private DefaultEditableFeatureType fType;
|
287 |
|
288 |
public EditableDelegatedIterator(Iterator iter, |
289 |
DefaultEditableFeatureType fType) { |
290 |
super(iter);
|
291 |
this.fType = fType;
|
292 |
} |
293 |
|
294 |
public void remove() { |
295 |
this.iterator.remove();
|
296 |
this.fType.fixAll();
|
297 |
} |
298 |
|
299 |
} |
300 |
|
301 |
protected void setAllowAutomaticValues(boolean value) { |
302 |
this.allowAtomaticValues = value;
|
303 |
} |
304 |
|
305 |
public void setDefaultTimeAttributeName(String name) { |
306 |
if (name == null || name.length() == 0) { |
307 |
this.defaultTimeAttributeIndex = -1; |
308 |
return;
|
309 |
} |
310 |
DefaultFeatureAttributeDescriptor attr = (DefaultFeatureAttributeDescriptor) this
|
311 |
.get(name); |
312 |
if (attr == null) { |
313 |
throw new IllegalArgumentException("Attribute '" + name |
314 |
+ "' not found.");
|
315 |
} |
316 |
|
317 |
this.defaultTimeAttributeIndex = attr.getIndex();
|
318 |
} |
319 |
} |