svn-gvsig-desktop / 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 @ 34981
History | View | Annotate | Download (7.38 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 |
/**
|
50 |
* @author gvSIG Team
|
51 |
* @version $Id$
|
52 |
*
|
53 |
*/
|
54 |
public class ExporrtoJDBCService extends AbstractMonitorableTask implements ExporttoService { |
55 |
private static final DataManager DATA_MANAGER = DALLocator.getDataManager(); |
56 |
|
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, String schema, String tableName, String pkField, String userName, String password, |
80 |
String host, int port, String explorerName, String storeName) { |
81 |
super("Export to JDBC"); |
82 |
this.featureStore = featureStore;
|
83 |
this.dbName = dbName;
|
84 |
this.schema = schema;
|
85 |
this.tableName = tableName;
|
86 |
this.pkField = pkField;
|
87 |
this.userName = userName;
|
88 |
this.password = password;
|
89 |
this.host = host;
|
90 |
this.port = port;
|
91 |
this.storeName = storeName;
|
92 |
this.explorerName = explorerName;
|
93 |
} |
94 |
|
95 |
public void export(FeatureSet featureSet) throws ExporttoServiceException { |
96 |
|
97 |
initializeParams(featureSet); |
98 |
|
99 |
DisposableIterator it1 = null;
|
100 |
FeatureStore target = null;
|
101 |
try {
|
102 |
EditableFeatureType fType = featureStore.getDefaultFeatureType().getEditable(); |
103 |
if (pkField != null) { |
104 |
EditableFeatureAttributeDescriptor pk = fType.add(pkField, |
105 |
DataTypes.LONG); |
106 |
pk.setIsPrimaryKey(true);
|
107 |
pk.setIsAutomatic(true);
|
108 |
} |
109 |
|
110 |
newFeatureStoreParameters.setDefaultFeatureType(fType); |
111 |
explorer.add(storeName, newFeatureStoreParameters, true);
|
112 |
|
113 |
target = (FeatureStore) DATA_MANAGER.openStore(storeName, newFeatureStoreParameters); |
114 |
FeatureType targetType = target.getDefaultFeatureType(); |
115 |
|
116 |
target.edit(FeatureStore.MODE_APPEND); |
117 |
|
118 |
taskStatus.setRangeOfValues(0, featureSet.getSize());
|
119 |
int featureCount = 1; |
120 |
|
121 |
it1 = featureSet.fastIterator(); |
122 |
while (it1.hasNext()) {
|
123 |
Feature feature = (Feature) it1.next(); |
124 |
target.insert(target.createNewFeature(targetType, feature)); |
125 |
|
126 |
this.taskStatus.setCurValue(featureCount);
|
127 |
|
128 |
if (this.taskStatus.isCancellationRequested()) { |
129 |
return;
|
130 |
} |
131 |
featureCount++; |
132 |
} |
133 |
target.finishEditing(); |
134 |
|
135 |
if (exporttoServiceFinishAction != null){ |
136 |
exporttoServiceFinishAction.finished(newFeatureStoreParameters.getDataStoreName(), newFeatureStoreParameters); |
137 |
} |
138 |
|
139 |
} catch (Exception e) { |
140 |
taskStatus.message(e.getMessage()); |
141 |
throw new ExporttoServiceException(e); |
142 |
} finally {
|
143 |
if (it1 != null) { |
144 |
it1.dispose(); |
145 |
} |
146 |
featureSet.dispose(); |
147 |
if (target != null) { |
148 |
target.dispose(); |
149 |
} |
150 |
this.taskStatus.terminate();
|
151 |
this.taskStatus.remove();
|
152 |
} |
153 |
} |
154 |
|
155 |
|
156 |
private void initializeParams(FeatureSet featureSet) throws ExporttoServiceException { |
157 |
JDBCServerExplorerParameters explorerParam; |
158 |
try {
|
159 |
explorerParam = (JDBCServerExplorerParameters) DATA_MANAGER |
160 |
.createServerExplorerParameters(explorerName); |
161 |
|
162 |
explorerParam.setHost(host); |
163 |
if (port > -1){ |
164 |
explorerParam.setPort(port); |
165 |
} |
166 |
explorerParam.setDBName(dbName); |
167 |
explorerParam.setUser(userName); |
168 |
explorerParam.setPassword(password); |
169 |
if (schema != null && schema.length() > 0) { |
170 |
explorerParam.setSchema(schema); |
171 |
} |
172 |
|
173 |
explorer = (JDBCServerExplorer) DATA_MANAGER.openServerExplorer(explorerName, explorerParam); |
174 |
|
175 |
newFeatureStoreParameters = (NewFeatureStoreParameters) explorer.getAddParameters(); |
176 |
|
177 |
((DBStoreParameters) newFeatureStoreParameters).setTable(tableName); |
178 |
((DBStoreParameters) newFeatureStoreParameters).setDefaultGeometryField( |
179 |
featureSet.getDefaultFeatureType().getDefaultGeometryAttributeName()); |
180 |
|
181 |
} catch (InitializeException e) {
|
182 |
throw new ExporttoServiceException(e); |
183 |
} catch (ProviderNotRegisteredException e) {
|
184 |
throw new ExporttoServiceException(e); |
185 |
} catch (ValidateDataParametersException e) {
|
186 |
throw new ExporttoServiceException(e); |
187 |
} catch (DataException e) {
|
188 |
throw new ExporttoServiceException(e); |
189 |
} |
190 |
} |
191 |
|
192 |
public void setFinishAction( |
193 |
ExporttoServiceFinishAction exporttoServiceFinishAction) { |
194 |
this.exporttoServiceFinishAction = exporttoServiceFinishAction;
|
195 |
} |
196 |
|
197 |
} |