Statistics
| Revision:

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 @ 44207

History | View | Annotate | Download (6.02 KB)

1 40559 jjdelcerro
/**
2
 * gvSIG. Desktop Geographic Information System.
3 40435 jjdelcerro
 *
4 40559 jjdelcerro
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6 40435 jjdelcerro
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8 40559 jjdelcerro
 * as published by the Free Software Foundation; either version 3
9 40435 jjdelcerro
 * of the License, or (at your option) any later version.
10 40559 jjdelcerro
 *
11 40435 jjdelcerro
 * 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 40559 jjdelcerro
 *
16 40435 jjdelcerro
 * 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 40559 jjdelcerro
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 40435 jjdelcerro
 * MA  02110-1301, USA.
20 40559 jjdelcerro
 *
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 40435 jjdelcerro
 */
24
package org.gvsig.fmap.dal.feature.impl.featureset;
25
26
import org.gvsig.fmap.dal.exception.DataException;
27 41183 jldominguez
import org.gvsig.fmap.dal.feature.EditableFeature;
28
import org.gvsig.fmap.dal.feature.Feature;
29
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
30
import org.gvsig.fmap.dal.feature.FeatureAttributeGetter;
31 40435 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureReference;
32 41183 jldominguez
import org.gvsig.fmap.dal.feature.FeatureType;
33
import org.gvsig.fmap.dal.feature.impl.DefaultEditableFeature;
34 40435 jjdelcerro
import org.gvsig.fmap.dal.feature.impl.DefaultFeature;
35
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureReference;
36
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
37
import org.gvsig.tools.exception.BaseException;
38
39
/**
40
 * Iterator implementation which shares the returned Feature object instance.
41
 *
42
 * @author gvSIG Team
43
 * @version $Id$
44
 */
45
public class FastEditedIterator extends EditedIterator {
46
47
    DefaultFeature myFeature;
48
49
    public FastEditedIterator(DefaultFeatureSet featureSet, long index)
50
        throws DataException {
51
        super(featureSet);
52
        this.initializeFeature();
53
        if (index > 0) {
54 40629 jldominguez
55
            this.iterator = featureSet.provider.fastIterator();
56
            setNewsFeatures(null);
57
            skypto(index);
58
59
            /*
60
            * This did not work because the provider
61
            * skips to the index without considering the
62
            * changes in editing mode. We need the
63
            * "skypto" method which knows about those
64
            * changes.
65
                *
66 40435 jjdelcerro
            if (featureSet.provider.canIterateFromIndex()) {
67
                try {
68
                    this.iterator = featureSet.provider.fastIterator(index);
69
                } catch (IllegalArgumentException e) {
70
                    this.iterator = featureSet.provider.fastIterator();
71
                    setNewsFeatures(null);
72
                    skypto(index);
73
                } catch (UnsupportedOperationException e) {
74
                    this.iterator = featureSet.provider.fastIterator();
75
                    setNewsFeatures(null);
76
                    skypto(index);
77
                }
78
            } else {
79
                this.iterator = featureSet.provider.fastIterator();
80
                setNewsFeatures(null);
81
                skypto(index);
82
            }
83 40629 jldominguez
            */
84
85 40435 jjdelcerro
        } else {
86
            this.iterator = featureSet.provider.fastIterator();
87
            setNewsFeatures(featureManager.getInserted());
88
        }
89
90
    }
91
92
    protected void initializeFeature() {
93
        myFeature = new DefaultFeature(fset.store);
94
    }
95
96
    protected DefaultFeature createFeature(FeatureProvider data) throws DataException {
97
98
        DefaultFeature f = null;
99
        try {
100
            data.setNew(isFeatureIsNew());
101 43727 jjdelcerro
            FeatureReference ref = new DefaultFeatureReference(fset.store, data);
102
            f = (DefaultFeature) featureManager.get(ref, fset.store);
103 40435 jjdelcerro
        } catch (DataException e) {
104
            RuntimeException ex = new RuntimeException();
105
            e.initCause(e);
106
            throw ex;
107
        }
108
        if (f == null) {
109
            this.myFeature.setData(data);
110
        } else {
111
            // TODO Sacamos una copia del data o no???
112
            this.myFeature.setData(f.getData().getCopy());
113
        }
114
        if (this.fset.transform.isEmpty()) {
115
            return myFeature;
116
        } else {
117 41183 jldominguez
118
            if (f == null) {
119
                myFeature = (DefaultFeature)
120
                    this.fset.transform.applyTransform(myFeature,
121
                        fset.getDefaultFeatureType());
122
            } else {
123
                /*
124
                 * In this case "f != null" the data comes from the featureManager,
125
                 * so it is data inserted/updated by the user, so we must
126
                 * overwrite the data after applying the transformations
127
                 * because the transformations can be "Adding field"
128
                 * and are necessary in some cases:
129
                 */
130
                DefaultFeature saved_feat = (DefaultFeature) myFeature.getCopy();
131
                myFeature = (DefaultFeature)
132
                    this.fset.transform.applyTransform(myFeature,
133
                        fset.getDefaultFeatureType());
134
                myFeature = overwrite(myFeature, saved_feat);
135
            }
136
            return myFeature;
137 40435 jjdelcerro
        }
138
139
    }
140
141 41183 jldominguez
    private DefaultFeature overwrite(
142
        DefaultFeature old_feat,
143
        DefaultFeature new_feat) {
144
145
        DefaultEditableFeature resp = (DefaultEditableFeature) old_feat.getEditable();
146
147
        FeatureType fty = new_feat.getData().getType();
148
        FeatureAttributeDescriptor[] atts = fty.getAttributeDescriptors();
149
        for (int i=0; i<atts.length; i++) {
150
            try {
151
                resp.set(atts[i].getName(), new_feat.get(atts[i].getName()));
152
            } catch (Exception exc) {
153
                // Field not found
154
            }
155
        }
156
        return (DefaultFeature) resp.getNotEditableCopy();
157
    }
158
159 40435 jjdelcerro
    public void remove() {
160
        super.remove();
161
        this.initializeFeature();
162
    }
163
164
    protected void doDispose() throws BaseException {
165
        super.doDispose();
166
        myFeature = null;
167
    }
168
169
}