Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / org.gvsig.exportto / org.gvsig.exportto.swing / org.gvsig.exportto.swing.prov / org.gvsig.exportto.swing.prov.jdbc / src / main / java / org / gvsig / exportto / swing / prov / jdbc / ExporrtoJDBCService.java @ 38557

History | View | Annotate | Download (7.55 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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 2
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
 */
22
package org.gvsig.exportto.swing.prov.jdbc;
23

    
24
import org.gvsig.exportto.ExporttoService;
25
import org.gvsig.exportto.ExporttoServiceException;
26
import org.gvsig.exportto.ExporttoServiceFinishAction;
27
import org.gvsig.fmap.dal.DALLocator;
28
import org.gvsig.fmap.dal.DataManager;
29
import org.gvsig.fmap.dal.DataTypes;
30
import org.gvsig.fmap.dal.exception.DataException;
31
import org.gvsig.fmap.dal.exception.InitializeException;
32
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException;
33
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
34
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
35
import org.gvsig.fmap.dal.feature.EditableFeatureType;
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.fmap.dal.feature.NewFeatureStoreParameters;
41
import org.gvsig.fmap.dal.store.db.DBStoreParameters;
42
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorer;
43
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters;
44
import org.gvsig.tools.dispose.DisposableIterator;
45
import org.gvsig.tools.service.ServiceException;
46
import org.gvsig.tools.task.AbstractMonitorableTask;
47

    
48
/**
49
 * @author gvSIG Team
50
 * @version $Id$
51
 * 
52
 */
53
public class ExporrtoJDBCService extends AbstractMonitorableTask implements
54
    ExporttoService {
55

    
56
    private static final DataManager DATA_MANAGER = DALLocator.getDataManager();
57

    
58
    private String dbName = null;
59
    private String schema = null;
60
    private String tableName = null;
61
    private String pkField = null;
62
    private String userName = null;
63
    private String password = null;
64
    private String host = null;
65
    private int port = -1;
66
    private String explorerName = null;
67
    private String storeName = null;
68

    
69
    private NewFeatureStoreParameters newFeatureStoreParameters;
70
    private JDBCServerExplorer explorer;
71
    private FeatureStore featureStore;
72

    
73
    private ExporttoServiceFinishAction exporttoServiceFinishAction = null;
74

    
75
    /**
76
     * @param taskName
77
     * @throws ServiceException
78
     */
79
    protected ExporrtoJDBCService(FeatureStore featureStore, String dbName,
80
        String schema, String tableName, String pkField, String userName,
81
        String password, String host, int port, String explorerName,
82
        String storeName) {
83
        super("Export to JDBC");
84
        this.featureStore = featureStore;
85
        this.dbName = dbName;
86
        this.schema = schema;
87
        this.tableName = tableName;
88
        this.pkField = pkField;
89
        this.userName = userName;
90
        this.password = password;
91
        this.host = host;
92
        this.port = port;
93
        this.storeName = storeName;
94
        this.explorerName = explorerName;
95
    }
96

    
97
    public void export(FeatureSet featureSet) throws ExporttoServiceException {
98

    
99
        initializeParams(featureSet);
100

    
101
        DisposableIterator it1 = null;
102
        FeatureStore target = null;
103
        try {
104
            EditableFeatureType fType =
105
                featureStore.getDefaultFeatureType().getEditable();
106
            if (pkField != null) {
107
                EditableFeatureAttributeDescriptor pk =
108
                    fType.add(pkField, DataTypes.LONG);
109
                pk.setIsPrimaryKey(true);
110
                pk.setIsAutomatic(true);
111
            }
112

    
113
            newFeatureStoreParameters.setDefaultFeatureType(fType);
114
            explorer.add(storeName, newFeatureStoreParameters, true);
115

    
116
            target =
117
                (FeatureStore) DATA_MANAGER.openStore(storeName,
118
                    newFeatureStoreParameters);
119
            FeatureType targetType = target.getDefaultFeatureType();
120

    
121
            target.edit(FeatureStore.MODE_APPEND);
122

    
123
            taskStatus.setRangeOfValues(0, featureSet.getSize());
124
            int featureCount = 1;
125

    
126
            it1 = featureSet.fastIterator();
127
            while (it1.hasNext()) {
128
                Feature feature = (Feature) it1.next();
129
                target.insert(target.createNewFeature(targetType, feature));
130

    
131
                this.taskStatus.setCurValue(featureCount);
132

    
133
                if (this.taskStatus.isCancellationRequested()) {
134
                    return;
135
                }
136
                featureCount++;
137
            }
138
            target.finishEditing();
139

    
140
            if (exporttoServiceFinishAction != null) {
141
                exporttoServiceFinishAction.finished(
142
                    newFeatureStoreParameters.getDataStoreName(),
143
                    newFeatureStoreParameters);
144
            }
145

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

    
162
    private void initializeParams(FeatureSet featureSet)
163
        throws ExporttoServiceException {
164
        JDBCServerExplorerParameters explorerParam;
165
        try {
166
            explorerParam =
167
                (JDBCServerExplorerParameters) DATA_MANAGER
168
                    .createServerExplorerParameters(explorerName);
169

    
170
            explorerParam.setHost(host);
171
            if (port > -1) {
172
                explorerParam.setPort(port);
173
            }
174
            explorerParam.setDBName(dbName);
175
            explorerParam.setUser(userName);
176
            explorerParam.setPassword(password);
177
            if (schema != null && schema.length() > 0) {
178
                explorerParam.setSchema(schema);
179
            }
180

    
181
            explorer =
182
                (JDBCServerExplorer) DATA_MANAGER.openServerExplorer(
183
                    explorerName, explorerParam);
184

    
185
            newFeatureStoreParameters =
186
                (NewFeatureStoreParameters) explorer.getAddParameters();
187

    
188
            ((DBStoreParameters) newFeatureStoreParameters).setTable(tableName);
189
            ((DBStoreParameters) newFeatureStoreParameters)
190
                .setDefaultGeometryField(featureSet.getDefaultFeatureType()
191
                    .getDefaultGeometryAttributeName());
192

    
193
        } catch (InitializeException e) {
194
            throw new ExporttoServiceException(e);
195
        } catch (ProviderNotRegisteredException e) {
196
            throw new ExporttoServiceException(e);
197
        } catch (ValidateDataParametersException e) {
198
            throw new ExporttoServiceException(e);
199
        } catch (DataException e) {
200
            throw new ExporttoServiceException(e);
201
        }
202
    }
203

    
204
    public void setFinishAction(
205
        ExporttoServiceFinishAction exporttoServiceFinishAction) {
206
        this.exporttoServiceFinishAction = exporttoServiceFinishAction;
207
    }
208

    
209
}