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

History | View | Annotate | Download (4.49 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
    private DefaultFeature myFeature;
43

    
44
    public FastEditedIterator(DefaultFeatureSet featureSet, long index)
45
        throws DataException {
46
        super(featureSet, index);
47
    }
48

    
49
    @Override
50
    protected DefaultFeature createFeature(FeatureProvider data) throws DataException {
51
        if(myFeature == null){
52
            myFeature = new DefaultFeature(fset.store);
53
        }
54

    
55
        DefaultFeature f = this.featureManager.get(data);
56
//        DefaultFeature f = null;
57
//        try {
58
//            FeatureReference ref = new DefaultFeatureReference(fset.store, data);
59
//            f = (DefaultFeature) featureManager.get(ref, fset.store); 
60
//        } catch (DataException e) {
61
//            RuntimeException ex = new RuntimeException();
62
//            e.initCause(e);
63
//            throw ex;
64
//        }
65
        if (f == null) {
66
            this.myFeature.setData(data);
67
        } else {
68
            // TODO Sacamos una copia del data o no???
69
            this.myFeature.setData(f.getData().getCopy());
70
        }
71
        if (this.fset.transform.isEmpty()) {
72
            return myFeature;
73
        } else {
74
            
75
            if (f == null) {
76
                myFeature = (DefaultFeature)
77
                    this.fset.transform.applyTransform(myFeature,
78
                        fset.getDefaultFeatureType());
79
            } else {
80
                /*
81
                 * In this case "f != null" the data comes from the featureManager,
82
                 * so it is data inserted/updated by the user, so we must
83
                 * overwrite the data after applying the transformations
84
                 * because the transformations can be "Adding field"
85
                 * and are necessary in some cases:
86
                 */
87
                DefaultFeature saved_feat = (DefaultFeature) myFeature.getCopy();
88
                myFeature = (DefaultFeature)
89
                    this.fset.transform.applyTransform(myFeature,
90
                        fset.getDefaultFeatureType());
91
                myFeature = overwrite(myFeature, saved_feat);
92
            }
93
            return myFeature;
94
        }       
95

    
96
    }
97

    
98
    private DefaultFeature overwrite(
99
        DefaultFeature old_feat,
100
        DefaultFeature new_feat) {
101
        
102
        DefaultEditableFeature resp = (DefaultEditableFeature) old_feat.getEditable();
103
        
104
        FeatureType fty = new_feat.getData().getType();
105
        FeatureAttributeDescriptor[] atts = fty.getAttributeDescriptors();
106
        for (int i=0; i<atts.length; i++) {
107
            try {
108
                resp.set(atts[i].getName(), new_feat.get(atts[i].getName()));
109
            } catch (Exception exc) {
110
                // Field not found
111
            }
112
        }
113
        return (DefaultFeature) resp.getNotEditableCopy();
114
    }
115

    
116
    @Override
117
    public void remove() {
118
        super.remove();
119
        myFeature = new DefaultFeature(fset.store);
120
    }
121

    
122
    @Override
123
    protected void doDispose() throws BaseException {
124
        super.doDispose();
125
        myFeature = null;
126
    }
127

    
128
}