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 / featureset / FastEditedIterator.java @ 45647
History | View | Annotate | Download (4.43 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.featureset; |
25 |
|
26 |
import org.gvsig.fmap.dal.exception.DataException; |
27 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
28 |
import org.gvsig.fmap.dal.feature.FeatureType; |
29 |
import org.gvsig.fmap.dal.feature.impl.DefaultEditableFeature; |
30 |
import org.gvsig.fmap.dal.feature.impl.DefaultFeature; |
31 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
32 |
import org.gvsig.tools.exception.BaseException; |
33 |
|
34 |
/**
|
35 |
* Iterator implementation which shares the returned Feature object instance.
|
36 |
*
|
37 |
* @author gvSIG Team
|
38 |
* @version $Id$
|
39 |
*/
|
40 |
public class FastEditedIterator extends EditedIterator { |
41 |
|
42 |
DefaultFeature myFeature; |
43 |
|
44 |
public FastEditedIterator(DefaultFeatureSet featureSet, long index) |
45 |
throws DataException {
|
46 |
super(featureSet, index);
|
47 |
myFeature = new DefaultFeature(fset.store);
|
48 |
} |
49 |
|
50 |
@Override
|
51 |
protected DefaultFeature createFeature(FeatureProvider data) throws DataException { |
52 |
|
53 |
DefaultFeature f = this.featureManager.get(data);
|
54 |
// DefaultFeature f = null;
|
55 |
// try {
|
56 |
// FeatureReference ref = new DefaultFeatureReference(fset.store, data);
|
57 |
// f = (DefaultFeature) featureManager.get(ref, fset.store);
|
58 |
// } catch (DataException e) {
|
59 |
// RuntimeException ex = new RuntimeException();
|
60 |
// e.initCause(e);
|
61 |
// throw ex;
|
62 |
// }
|
63 |
if (f == null) { |
64 |
this.myFeature.setData(data);
|
65 |
} else {
|
66 |
// TODO Sacamos una copia del data o no???
|
67 |
this.myFeature.setData(f.getData().getCopy());
|
68 |
} |
69 |
if (this.fset.transform.isEmpty()) { |
70 |
return myFeature;
|
71 |
} else {
|
72 |
|
73 |
if (f == null) { |
74 |
myFeature = (DefaultFeature) |
75 |
this.fset.transform.applyTransform(myFeature,
|
76 |
fset.getDefaultFeatureType()); |
77 |
} else {
|
78 |
/*
|
79 |
* In this case "f != null" the data comes from the featureManager,
|
80 |
* so it is data inserted/updated by the user, so we must
|
81 |
* overwrite the data after applying the transformations
|
82 |
* because the transformations can be "Adding field"
|
83 |
* and are necessary in some cases:
|
84 |
*/
|
85 |
DefaultFeature saved_feat = (DefaultFeature) myFeature.getCopy(); |
86 |
myFeature = (DefaultFeature) |
87 |
this.fset.transform.applyTransform(myFeature,
|
88 |
fset.getDefaultFeatureType()); |
89 |
myFeature = overwrite(myFeature, saved_feat); |
90 |
} |
91 |
return myFeature;
|
92 |
} |
93 |
|
94 |
} |
95 |
|
96 |
private DefaultFeature overwrite(
|
97 |
DefaultFeature old_feat, |
98 |
DefaultFeature new_feat) { |
99 |
|
100 |
DefaultEditableFeature resp = (DefaultEditableFeature) old_feat.getEditable(); |
101 |
|
102 |
FeatureType fty = new_feat.getData().getType(); |
103 |
FeatureAttributeDescriptor[] atts = fty.getAttributeDescriptors();
|
104 |
for (int i=0; i<atts.length; i++) { |
105 |
try {
|
106 |
resp.set(atts[i].getName(), new_feat.get(atts[i].getName())); |
107 |
} catch (Exception exc) { |
108 |
// Field not found
|
109 |
} |
110 |
} |
111 |
return (DefaultFeature) resp.getNotEditableCopy();
|
112 |
} |
113 |
|
114 |
@Override
|
115 |
public void remove() { |
116 |
super.remove();
|
117 |
myFeature = new DefaultFeature(fset.store);
|
118 |
} |
119 |
|
120 |
@Override
|
121 |
protected void doDispose() throws BaseException { |
122 |
super.doDispose();
|
123 |
myFeature = null;
|
124 |
} |
125 |
|
126 |
} |