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