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

History | View | Annotate | Download (4.91 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.exportto.impl;
25

    
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.FeatureSet;
38
import org.gvsig.fmap.dal.feature.FeatureStore;
39
import org.gvsig.fmap.dal.feature.FeatureType;
40
import org.gvsig.tools.ToolsLocator;
41
import org.gvsig.tools.dispose.DisposableIterator;
42
import org.gvsig.tools.dispose.DisposeUtils;
43
import org.gvsig.tools.task.AbstractMonitorableTask;
44

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

    
54
    private DataServerExplorer dataServerExplorer;
55
    private NewDataStoreParameters newDataStoreParameters;
56

    
57
    private ExporttoServiceFinishAction exporttoServiceFinishAction;
58

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

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

    
73
    }
74

    
75
    public void export(FeatureSet featureSet) throws ExporttoServiceException {
76

    
77
        String providerName = newDataStoreParameters.getDataStoreName();
78
        String explorerName = dataServerExplorer.getProviderName();
79

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

    
86
            DataManager dataManager = DALLocator.getDataManager();
87

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

    
94
            FeatureType targetType = target.getDefaultFeatureType();
95

    
96
            target.edit(FeatureStore.MODE_APPEND);
97
            it = featureSet.fastIterator();
98

    
99
            long featureCount = 0;
100
            while (it.hasNext()) {
101
                Feature feature = (Feature) it.next();
102
                newfeature = target.createNewFeature(targetType, feature);
103
                target.insert(newfeature);
104

    
105
                featureCount++;
106
                this.taskStatus.setCurValue(featureCount);
107

    
108
                if (this.taskStatus.isCancellationRequested()) {
109
                    return;
110
                }
111
            }
112
            target.finishEditing();
113

    
114
            this.taskStatus.terminate();
115
            this.taskStatus.remove();
116
        } catch (Exception e) {
117
            throw new ExporttoServiceException(e, newfeature);
118
        } finally {
119
            if (target != null) {
120
                target.dispose();
121
            }
122
            if (it != null) {
123
                it.dispose();
124
            }
125
            if (featureSet != null) {
126
                featureSet.dispose();
127
            }
128
            DisposeUtils.dispose(it);
129
        }
130

    
131
        if (exporttoServiceFinishAction != null) {
132
            exporttoServiceFinishAction.finished(
133
                newDataStoreParameters.getDataStoreName(),
134
                newDataStoreParameters);
135
        }
136
    }
137

    
138
    public void setFinishAction(
139
        ExporttoServiceFinishAction exporttoServiceFinishAction) {
140
        this.exporttoServiceFinishAction = exporttoServiceFinishAction;
141
    }
142

    
143
}