Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.exportto / org.gvsig.exportto.lib / org.gvsig.exportto.lib.impl / src / main / java / org / gvsig / exportto / impl / DefaultExporttoService.java @ 42984

History | View | Annotate | Download (6.42 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 modify it under
7
 * the terms of the GNU General Public License as published by the Free Software
8
 * Foundation; either version 3 of the License, or (at your option) any later
9
 * version.
10
 *
11
 * This program is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
 * details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with
17
 * this program; if not, write to the Free Software Foundation, Inc., 51
18
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.exportto.impl;
24

    
25
import org.cresques.cts.ICoordTrans;
26
import org.cresques.cts.IProjection;
27

    
28
import org.gvsig.exportto.ExporttoService;
29
import org.gvsig.exportto.ExporttoServiceException;
30
import org.gvsig.exportto.ExporttoServiceFinishAction;
31
import org.gvsig.fmap.dal.DALLocator;
32
import org.gvsig.fmap.dal.DataManager;
33
import org.gvsig.fmap.dal.DataServerExplorer;
34
import org.gvsig.fmap.dal.NewDataStoreParameters;
35
import org.gvsig.fmap.dal.feature.EditableFeature;
36
import org.gvsig.fmap.dal.feature.Feature;
37
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
38
import org.gvsig.fmap.dal.feature.FeatureSet;
39
import org.gvsig.fmap.dal.feature.FeatureStore;
40
import org.gvsig.fmap.dal.feature.FeatureType;
41
import org.gvsig.fmap.geom.Geometry;
42
import org.gvsig.tools.ToolsLocator;
43
import org.gvsig.tools.dispose.DisposableIterator;
44
import org.gvsig.tools.dispose.DisposeUtils;
45
import org.gvsig.tools.task.AbstractMonitorableTask;
46

    
47
/**
48
 * Default {@link ExporttoService} implementation.
49
 *
50
 * @author gvSIG Team
51
 * @version $Id$
52
 */
53
public class DefaultExporttoService extends AbstractMonitorableTask implements
54
        ExporttoService {
55

    
56
    private DataServerExplorer dataServerExplorer;
57
    private NewDataStoreParameters newDataStoreParameters;
58

    
59
    private ExporttoServiceFinishAction exporttoServiceFinishAction;
60

    
61
    /**
62
     * {@link DefaultExporttoService} constructor
63
     */
64
    public DefaultExporttoService(DataServerExplorer dataServerExplorer,
65
            NewDataStoreParameters newDataStoreParameters) {
66
        super(ToolsLocator.getI18nManager().getTranslation("Exportto"));
67
        this.dataServerExplorer = dataServerExplorer;
68
        this.newDataStoreParameters = newDataStoreParameters;
69
    }
70

    
71
    public void export(FeatureStore featureStore, IProjection projection,
72
            FeatureSet featureSet) throws ExporttoServiceException {
73
        // TODO Auto-generated method stub
74

    
75
    }
76

    
77
    public void export(FeatureSet featureSet) throws ExporttoServiceException {
78

    
79
        String providerName = newDataStoreParameters.getDataStoreName();
80
        String explorerName = dataServerExplorer.getProviderName();
81

    
82
        DisposableIterator it = null;
83
        FeatureStore target = null;
84
        EditableFeature newfeature = null;
85
        try {
86
            taskStatus.setRangeOfValues(0, featureSet.getSize());
87

    
88
            DataManager dataManager = DALLocator.getDataManager();
89

    
90
            dataManager.newStore(explorerName, providerName,
91
                    newDataStoreParameters, true);
92
            target
93
                    = (FeatureStore) dataManager.openStore(providerName,
94
                            newDataStoreParameters);
95

    
96
            FeatureType targetType = target.getDefaultFeatureType();
97

    
98
            target.edit(FeatureStore.MODE_APPEND);
99
            it = featureSet.fastIterator();
100

    
101
            IProjection targetProjection;
102
            FeatureAttributeDescriptor defaultGeom = target.getDefaultFeatureType().getDefaultGeometryAttribute();
103
            if (defaultGeom!=null) {
104
                    targetProjection = defaultGeom.getSRS();
105
            }
106
            else {
107
                    targetProjection = null;
108
            }
109

    
110
            long featureCount = 0;
111
            while (it.hasNext()) {
112
                Feature feature = (Feature) it.next();
113
                newfeature = target.createNewFeature(targetType, feature);
114
                // ================================================
115
                // Reprojection stuff
116
                Geometry reproj_geom = null;
117
                IProjection sourceProjection = feature.getDefaultSRS();
118

    
119
                ICoordTrans coord_trans = null;
120
                // this comparison is perhaps too preventive
121
                // we could  have two instances of same projection
122
                // so we would do more computations than needed
123
                if (sourceProjection != null
124
                        && targetProjection != null
125
                        && sourceProjection != targetProjection) {
126

    
127
                    coord_trans = sourceProjection.getCT(targetProjection);
128
                    reproj_geom = feature.getDefaultGeometry();
129
                    reproj_geom = reproj_geom.cloneGeometry();
130
                    reproj_geom.reProject(coord_trans);
131
                    newfeature.setDefaultGeometry(reproj_geom);
132
                }
133
                // ================================================
134

    
135
                target.insert(newfeature);
136

    
137
                featureCount++;
138
                this.taskStatus.setCurValue(featureCount);
139

    
140
                if (this.taskStatus.isCancellationRequested()) {
141
                    return;
142
                }
143
            }
144
            target.finishEditing();
145

    
146
            this.taskStatus.terminate();
147
            this.taskStatus.remove();
148
        } catch (Exception e) {
149
            throw new ExporttoServiceException(e, newfeature);
150
        } finally {
151
            if (target != null) {
152
                target.dispose();
153
            }
154
            if (it != null) {
155
                it.dispose();
156
            }
157
            if (featureSet != null) {
158
                featureSet.dispose();
159
            }
160
            DisposeUtils.dispose(it);
161
        }
162

    
163
        if (exporttoServiceFinishAction != null) {
164
            exporttoServiceFinishAction.finished(
165
                    newDataStoreParameters.getDataStoreName(),
166
                    newDataStoreParameters);
167
        }
168
    }
169

    
170
    public void setFinishAction(
171
            ExporttoServiceFinishAction exporttoServiceFinishAction) {
172
        this.exporttoServiceFinishAction = exporttoServiceFinishAction;
173
    }
174

    
175
}