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 / DefaultFeatureStoreTransforms.java @ 45426
History | View | Annotate | Download (14.3 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.util.ArrayList; |
27 |
import java.util.Collections; |
28 |
import java.util.Iterator; |
29 |
import java.util.List; |
30 |
import java.util.ListIterator; |
31 |
|
32 |
import org.gvsig.fmap.dal.exception.DataException; |
33 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
34 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
35 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
36 |
import org.gvsig.fmap.dal.feature.Feature; |
37 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
38 |
import org.gvsig.fmap.dal.feature.FeatureStoreNotification; |
39 |
import org.gvsig.fmap.dal.feature.FeatureStoreTransform; |
40 |
import org.gvsig.fmap.dal.feature.FeatureStoreTransforms; |
41 |
import org.gvsig.fmap.dal.feature.FeatureType; |
42 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
43 |
import org.gvsig.tools.ToolsLocator; |
44 |
import org.gvsig.tools.dynobject.DynStruct; |
45 |
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException; |
46 |
import org.gvsig.tools.persistence.Persistent; |
47 |
import org.gvsig.tools.persistence.PersistentState; |
48 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
49 |
import org.slf4j.Logger; |
50 |
import org.slf4j.LoggerFactory; |
51 |
import org.gvsig.fmap.dal.feature.FeatureExtraColumns; |
52 |
|
53 |
public class DefaultFeatureStoreTransforms |
54 |
implements FeatureStoreTransforms,
|
55 |
Persistent { |
56 |
|
57 |
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultFeatureStoreTransforms.class); |
58 |
|
59 |
private DefaultFeatureStore store;
|
60 |
private List<FeatureStoreTransform> transforms; |
61 |
private Boolean isTramsformValues; |
62 |
private TransformTemporalList lastTransformStack;
|
63 |
|
64 |
public DefaultFeatureStoreTransforms() {
|
65 |
this.store = null; |
66 |
this.transforms = new ArrayList(); |
67 |
this.isTramsformValues = false; |
68 |
} |
69 |
|
70 |
public DefaultFeatureStoreTransforms(DefaultFeatureStore store) {
|
71 |
this.store = store;
|
72 |
this.transforms = new ArrayList(); |
73 |
} |
74 |
|
75 |
protected void checkEditingMode() { |
76 |
if( store == null || store.getMode() != FeatureStore.MODE_QUERY ) { |
77 |
throw new IllegalStateException(); |
78 |
} |
79 |
} |
80 |
|
81 |
protected void notifyChangeToStore() { |
82 |
this.store.notifyChange(FeatureStoreNotification.TRANSFORM_CHANGE);
|
83 |
} |
84 |
|
85 |
@Override
|
86 |
public FeatureStoreTransform add(FeatureStoreTransform transform)
|
87 |
throws DataException {
|
88 |
checkEditingMode(); |
89 |
try {
|
90 |
transform.setUp(); |
91 |
} catch (Exception ex) { |
92 |
throw new RuntimeException("Can't set up transformation.", ex); |
93 |
} |
94 |
if( transform.getDefaultFeatureType() != null ) { |
95 |
if( !transform.getFeatureTypes().contains(transform.getDefaultFeatureType()) ) {
|
96 |
throw new IllegalArgumentException(); // FIXME: Add specific type |
97 |
} |
98 |
this.transforms.add(transform);
|
99 |
transform.setFeatureStore(store); |
100 |
transform.setSourceMetadata(this.store.metadata);
|
101 |
this.notifyChangeToStore();
|
102 |
if( this.isTramsformValues == null || (!this.isTramsformValues) ) { |
103 |
if( transform.isTransformsOriginalValues() ) {
|
104 |
this.isTramsformValues = true; |
105 |
} |
106 |
|
107 |
} |
108 |
} |
109 |
|
110 |
return transform;
|
111 |
} |
112 |
|
113 |
@Override
|
114 |
public void clear() { |
115 |
checkEditingMode(); |
116 |
this.transforms.clear();
|
117 |
this.notifyChangeToStore();
|
118 |
this.isTramsformValues = Boolean.FALSE; |
119 |
} |
120 |
|
121 |
@Override
|
122 |
public FeatureStoreTransform getTransform(int index) { |
123 |
return (FeatureStoreTransform) this.transforms.get(index); |
124 |
} |
125 |
|
126 |
@Override
|
127 |
public Iterator<FeatureStoreTransform> iterator() { |
128 |
return Collections.unmodifiableList(transforms).iterator(); |
129 |
} |
130 |
|
131 |
@Override
|
132 |
public Object remove(int index) { |
133 |
checkEditingMode(); |
134 |
Object trans = this.transforms.remove(index); |
135 |
this.notifyChangeToStore();
|
136 |
this.isTramsformValues = null; |
137 |
return trans;
|
138 |
} |
139 |
|
140 |
@Override
|
141 |
public boolean remove(FeatureStoreTransform transform) { |
142 |
checkEditingMode(); |
143 |
boolean removed = this.transforms.remove(transform); |
144 |
if( removed ) {
|
145 |
this.notifyChangeToStore();
|
146 |
|
147 |
} |
148 |
this.isTramsformValues = null; |
149 |
return removed;
|
150 |
|
151 |
} |
152 |
|
153 |
@Override
|
154 |
public int size() { |
155 |
return this.transforms.size(); |
156 |
} |
157 |
|
158 |
@Override
|
159 |
public boolean isEmpty() { |
160 |
return this.transforms.isEmpty(); |
161 |
} |
162 |
|
163 |
private class TransformTemporalListElement { |
164 |
|
165 |
public FeatureStoreTransform transform = null; |
166 |
public FeatureType targetFeatureType = null; |
167 |
} |
168 |
|
169 |
private class TransformTemporalList |
170 |
extends ArrayList { |
171 |
|
172 |
/**
|
173 |
*
|
174 |
*/
|
175 |
private static final long serialVersionUID = 1677014259279944000L; |
176 |
private FeatureType targetFType;
|
177 |
|
178 |
@Override
|
179 |
public boolean add(Object arg0) { |
180 |
if( this.isEmpty() ) { |
181 |
targetFType = ((TransformTemporalListElement) arg0).targetFeatureType; |
182 |
} |
183 |
return super.add(arg0); |
184 |
} |
185 |
|
186 |
} |
187 |
|
188 |
private TransformTemporalList getTransformTemporalList(FeatureType targetFeatureType) {
|
189 |
if( this.lastTransformStack == null |
190 |
|| this.lastTransformStack.size() != this.transforms.size() |
191 |
|| !(this.lastTransformStack.targetFType.equals(targetFeatureType)) ) {
|
192 |
TransformTemporalList result = new TransformTemporalList();
|
193 |
TransformTemporalListElement item; |
194 |
FeatureType nextFType = targetFeatureType; |
195 |
|
196 |
for( int i = transforms.size() - 1; i > -1; i-- ) { |
197 |
item = new TransformTemporalListElement();
|
198 |
item.transform = (FeatureStoreTransform) transforms.get(i); |
199 |
item.targetFeatureType = nextFType; |
200 |
nextFType = item.transform.getSourceFeatureTypeFrom(item.targetFeatureType); |
201 |
result.add(item); |
202 |
} |
203 |
this.lastTransformStack = result;
|
204 |
} |
205 |
return this.lastTransformStack; |
206 |
} |
207 |
|
208 |
public Feature applyTransform(DefaultFeature source,
|
209 |
FeatureType targetFeatureType) |
210 |
throws DataException {
|
211 |
if( this.transforms.isEmpty() ) { |
212 |
return source;
|
213 |
} |
214 |
|
215 |
TransformTemporalList stack = this
|
216 |
.getTransformTemporalList(targetFeatureType); |
217 |
TransformTemporalListElement item; |
218 |
FeatureProvider targetData; |
219 |
EditableFeature target; |
220 |
ListIterator iterator = stack.listIterator(stack.size());
|
221 |
FeatureType tft = null;
|
222 |
|
223 |
while( iterator.hasPrevious() ) {
|
224 |
item = (TransformTemporalListElement) iterator.previous(); |
225 |
|
226 |
tft = item.targetFeatureType; |
227 |
if( tft instanceof EditableFeatureType ) { |
228 |
/*
|
229 |
* Must be non-editable to create
|
230 |
* DefaultFeatureProvider
|
231 |
*/
|
232 |
tft = ((EditableFeatureType) tft).getNotEditableCopy(); |
233 |
} |
234 |
|
235 |
targetData = this.store.createDefaultFeatureProvider(tft);
|
236 |
targetData.setOID(source.getData().getOID()); |
237 |
targetData.setNew(false);
|
238 |
target = new DefaultEditableFeature(this.store, targetData); |
239 |
item.transform.applyTransform(source, target); |
240 |
source = (DefaultFeature) target.getNotEditableCopy(); |
241 |
} |
242 |
|
243 |
return source;
|
244 |
|
245 |
} |
246 |
|
247 |
public FeatureType getSourceFeatureTypeFrom(FeatureType targetFeatureType) {
|
248 |
FeatureType tmpFType = targetFeatureType; |
249 |
|
250 |
for( int i = transforms.size() - 1; i > -1; i-- ) { |
251 |
FeatureStoreTransform transform = transforms.get(i); |
252 |
tmpFType = transform.getSourceFeatureTypeFrom(tmpFType); |
253 |
} |
254 |
// se mantienen
|
255 |
List<EditableFeatureAttributeDescriptor> extraCols = targetFeatureType.getExtraColumns().getColumns();
|
256 |
if (extraCols!=null & !extraCols.isEmpty()) { |
257 |
FeatureExtraColumns tmpExtraCols = tmpFType.getExtraColumns(); |
258 |
tmpExtraCols.merge(targetFeatureType.getExtraColumns()); |
259 |
((DefaultFeatureType) tmpFType).setExtraColumn(tmpExtraCols); |
260 |
} |
261 |
return tmpFType;
|
262 |
} |
263 |
|
264 |
public FeatureType getDefaultFeatureType() throws DataException { |
265 |
if( this.transforms.isEmpty() ) { |
266 |
return null; |
267 |
} |
268 |
FeatureStoreTransform transform = this.transforms.get(this.transforms.size() - 1); |
269 |
return transform.getDefaultFeatureType();
|
270 |
} |
271 |
|
272 |
public List<FeatureType> getFeatureTypes() throws DataException { |
273 |
if( this.transforms.isEmpty() ) { |
274 |
return null; |
275 |
} |
276 |
FeatureStoreTransform transform = this.transforms.get(this.transforms.size() - 1); |
277 |
return transform.getFeatureTypes();
|
278 |
} |
279 |
|
280 |
public FeatureStore getFeatureStore() {
|
281 |
return this.store; |
282 |
} |
283 |
|
284 |
public void setFeatureStore(FeatureStore featureStore) { |
285 |
if( this.store != null ) { |
286 |
throw new IllegalStateException(); |
287 |
} |
288 |
this.store = (DefaultFeatureStore) featureStore;
|
289 |
for( FeatureStoreTransform transform : this ) { |
290 |
transform.setFeatureStore(featureStore); |
291 |
} |
292 |
} |
293 |
|
294 |
public void setStoreForClone(FeatureStore featureStore) { |
295 |
this.store = (DefaultFeatureStore) featureStore;
|
296 |
} |
297 |
|
298 |
@Override
|
299 |
public boolean isTransformsOriginalValues() { |
300 |
if( this.isTramsformValues == null ) { |
301 |
this.isTramsformValues = false; |
302 |
for( FeatureStoreTransform transform : this ) { |
303 |
if( transform.isTransformsOriginalValues() ) {
|
304 |
this.isTramsformValues = true; |
305 |
break;
|
306 |
} |
307 |
} |
308 |
} |
309 |
return this.isTramsformValues; |
310 |
} |
311 |
|
312 |
public FeatureType getFeatureType(String featureTypeId) |
313 |
throws DataException {
|
314 |
if( this.transforms.isEmpty() ) { |
315 |
return null; |
316 |
} |
317 |
if( featureTypeId == null ) { |
318 |
return this.getDefaultFeatureType(); |
319 |
} |
320 |
for( FeatureType fType : (List<FeatureType>) this.getFeatureTypes() ) { |
321 |
|
322 |
} |
323 |
Iterator iter = this.getFeatureTypes().iterator(); |
324 |
FeatureType fType; |
325 |
while( iter.hasNext() ) {
|
326 |
fType = (FeatureType) iter.next(); |
327 |
if( fType.getId().equals(featureTypeId) ) {
|
328 |
return fType;
|
329 |
} |
330 |
} |
331 |
return null; |
332 |
} |
333 |
|
334 |
@Override
|
335 |
public void saveToState(PersistentState state) throws PersistenceException { |
336 |
// state.set("store", store);
|
337 |
state.set("isTramsformValues", this.isTramsformValues); |
338 |
state.set("transforms", this.transforms); |
339 |
} |
340 |
|
341 |
@Override
|
342 |
public void loadFromState(PersistentState state) |
343 |
throws PersistenceException {
|
344 |
this.isTramsformValues = (Boolean) state.get("isTramsformValues"); |
345 |
this.transforms = new ArrayList(); |
346 |
List<FeatureStoreTransform> x = (List<FeatureStoreTransform>) state.get("transforms"); |
347 |
this.transforms.addAll(x);
|
348 |
// this.store = (DefaultFeatureStore) state.get("store");
|
349 |
} |
350 |
|
351 |
public static void registerPersistent() { |
352 |
DynStruct definition = ToolsLocator.getPersistenceManager().addDefinition( |
353 |
DefaultFeatureStoreTransforms.class, |
354 |
"DefaultFeatureStoreTransforms",
|
355 |
"DefaultFeatureStoreTransforms Persistent definition",
|
356 |
null,
|
357 |
null
|
358 |
); |
359 |
|
360 |
definition.addDynFieldBoolean("isTramsformValues").setMandatory(false); |
361 |
definition.addDynFieldList("transforms").setClassOfItems(FeatureStoreTransform.class).setMandatory(true); |
362 |
definition.addDynFieldObject("store").setClassOfValue(FeatureStore.class).setMandatory(false); |
363 |
|
364 |
} |
365 |
|
366 |
public Object getDynValue(String name) throws DynFieldNotFoundException { |
367 |
if( !this.transforms.isEmpty() ) { |
368 |
for (int i = this.transforms.size() - 1; i >= 0; i--) { |
369 |
FeatureStoreTransform transform = transforms.get(i); |
370 |
if( transform!=null && transform.hasDynValue(name) ) { |
371 |
Object value;
|
372 |
try {
|
373 |
value = transform.getDynValue(name); |
374 |
if( value!=null ) { |
375 |
return value;
|
376 |
} |
377 |
} catch(Exception ex) { |
378 |
LOGGER.warn("Cant retrieve value of '"+name+ "' in transform '"+transform.getName()+"'.", ex); |
379 |
} |
380 |
} |
381 |
} |
382 |
} |
383 |
return null; |
384 |
} |
385 |
|
386 |
public void setDynValue(String name, Object value) throws DynFieldNotFoundException { |
387 |
if( !this.transforms.isEmpty() ) { |
388 |
FeatureStoreTransform transform = (FeatureStoreTransform) transforms.get(this.transforms.size() - 1); |
389 |
transform.setDynValue(name, value); |
390 |
} |
391 |
} |
392 |
|
393 |
public boolean hasDynValue(String name) { |
394 |
if( !this.transforms.isEmpty() ) { |
395 |
for (int i = this.transforms.size() - 1; i >= 0; i--) { |
396 |
FeatureStoreTransform transform = transforms.get(i); |
397 |
if( transform.hasDynValue(name) ) {
|
398 |
return true; |
399 |
} |
400 |
} |
401 |
} |
402 |
return false; |
403 |
} |
404 |
|
405 |
@Override
|
406 |
public Object clone() throws CloneNotSupportedException { |
407 |
DefaultFeatureStoreTransforms cloned = (DefaultFeatureStoreTransforms) super.clone();
|
408 |
cloned.transforms = new ArrayList(); |
409 |
for( FeatureStoreTransform transform : transforms ) {
|
410 |
cloned.transforms.add((FeatureStoreTransform) transform.clone()); |
411 |
} |
412 |
cloned.lastTransformStack = null;
|
413 |
return cloned;
|
414 |
} |
415 |
|
416 |
} |