svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.exportto / org.gvsig.exportto.swing / org.gvsig.exportto.swing.prov / org.gvsig.exportto.swing.prov.jdbc / src / main / java / org / gvsig / export / jdbc / service / ExportJDBCService.java @ 44469
History | View | Annotate | Download (16.9 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.export.jdbc.service; |
25 |
|
26 |
import java.text.MessageFormat; |
27 |
import org.cresques.cts.ICoordTrans; |
28 |
import org.gvsig.export.ExportAttributes; |
29 |
|
30 |
import org.gvsig.fmap.dal.exception.DataException; |
31 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
32 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
33 |
import org.gvsig.fmap.dal.feature.FeatureType; |
34 |
import org.gvsig.fmap.dal.DALLocator; |
35 |
import org.gvsig.fmap.dal.DataManager; |
36 |
import org.gvsig.fmap.dal.DataTypes; |
37 |
import org.gvsig.fmap.dal.NewDataStoreParameters; |
38 |
import org.gvsig.fmap.dal.OpenDataStoreParameters; |
39 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
40 |
import org.gvsig.fmap.dal.feature.Feature; |
41 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
42 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
43 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
44 |
import org.gvsig.fmap.dal.feature.FeatureStoreProviderFactory; |
45 |
import org.gvsig.fmap.dal.store.jdbc.JDBCNewStoreParameters; |
46 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorer; |
47 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
48 |
import org.gvsig.fmap.geom.Geometry; |
49 |
import org.gvsig.tools.dispose.DisposableIterator; |
50 |
import org.gvsig.export.ExportException; |
51 |
import org.gvsig.export.ExportLocator; |
52 |
import org.gvsig.export.spi.ExportService; |
53 |
import org.gvsig.export.spi.AbstractExportService; |
54 |
import org.gvsig.export.spi.ExportServiceFactory; |
55 |
import org.gvsig.export.spi.ExportServiceManager; |
56 |
import org.gvsig.export.spi.ExportServiceManager.FixGeometryStatus; |
57 |
|
58 |
/**
|
59 |
* @author gvSIG Team
|
60 |
* @version $Id$
|
61 |
*
|
62 |
*/
|
63 |
public class ExportJDBCService |
64 |
extends AbstractExportService
|
65 |
implements ExportService {
|
66 |
|
67 |
private FeatureType targetFeatureType;
|
68 |
|
69 |
protected ExportJDBCService(ExportServiceFactory factory, ExportJDBCParameters parameters) {
|
70 |
super(factory, parameters);
|
71 |
} |
72 |
|
73 |
@Override
|
74 |
public ExportJDBCParameters getParameters() {
|
75 |
return (ExportJDBCParameters) super.getParameters(); |
76 |
} |
77 |
|
78 |
@Override
|
79 |
protected JDBCServerExplorer createServerExplorer() throws ExportException { |
80 |
try {
|
81 |
DataManager dataManager = DALLocator.getDataManager(); |
82 |
|
83 |
JDBCServerExplorer explorer = (JDBCServerExplorer) dataManager.openServerExplorer( |
84 |
this.getParameters().getExplorerParameters().getExplorerName(),
|
85 |
this.getParameters().getExplorerParameters()
|
86 |
); |
87 |
return explorer;
|
88 |
} catch (Exception e) { |
89 |
throw new ExportException(e); |
90 |
} |
91 |
} |
92 |
|
93 |
@Override
|
94 |
protected NewDataStoreParameters createTargetNewStoreParameters() throws ExportException { |
95 |
try {
|
96 |
FeatureType targetTempFeatureType; |
97 |
EditableFeatureType targetEditableFeatureType; |
98 |
|
99 |
JDBCServerExplorer explorer = this.createServerExplorer();
|
100 |
|
101 |
DataManager manager = DALLocator.getDataManager(); |
102 |
FeatureStoreProviderFactory factory = (FeatureStoreProviderFactory) manager.getStoreProviderFactory(explorer.getStoreName()); |
103 |
|
104 |
boolean preferNotToUseNonNullRestrictions = factory.preferNotToUseNonNullRestrictions();
|
105 |
ExportAttributes exportAttributes = getParameters().getExportAttributes(); |
106 |
|
107 |
|
108 |
targetTempFeatureType = exportAttributes.getTargetFeatureType().getCopy(); |
109 |
targetEditableFeatureType = targetTempFeatureType.getEditable(); |
110 |
|
111 |
// Remove inherited primary keys from the data source and remove NotNull
|
112 |
// restrictions if proceed.
|
113 |
for (int i = 0; i < targetEditableFeatureType.size(); i++) { |
114 |
EditableFeatureAttributeDescriptor eAttr = (EditableFeatureAttributeDescriptor) targetEditableFeatureType.get(i); |
115 |
eAttr.setIsPrimaryKey(false);
|
116 |
if (preferNotToUseNonNullRestrictions) {
|
117 |
eAttr.setAllowNull(true);
|
118 |
} |
119 |
} |
120 |
|
121 |
|
122 |
String pkname = getParameters().getPrimaryKey();
|
123 |
if (pkname != null) { |
124 |
// pkname it's not part of exportattributes list.
|
125 |
pkname = getParameters().getExportAttributes().getNamesTranslator().getNameSuggestion(pkname); |
126 |
EditableFeatureAttributeDescriptor pk = (EditableFeatureAttributeDescriptor) targetEditableFeatureType.get(pkname); |
127 |
if (pk == null) { |
128 |
pk = targetEditableFeatureType.add(pkname, DataTypes.LONG); |
129 |
pk.setIsPrimaryKey(true);
|
130 |
pk.setIsAutomatic(true);
|
131 |
} else {
|
132 |
pk.setIsPrimaryKey(true);
|
133 |
} |
134 |
} |
135 |
|
136 |
if (this.getParameters().getCreateIndexInGeometryRow()) { |
137 |
EditableFeatureAttributeDescriptor x = (EditableFeatureAttributeDescriptor) targetEditableFeatureType.getDefaultGeometryAttribute(); |
138 |
x.setIsIndexed(true);
|
139 |
} |
140 |
|
141 |
// ======================================================
|
142 |
// Reprojection: set SRS of geometry field to target SRS
|
143 |
EditableFeatureAttributeDescriptor attrdescriptor |
144 |
= (EditableFeatureAttributeDescriptor) targetEditableFeatureType.getDefaultGeometryAttribute(); |
145 |
if (attrdescriptor != null) { |
146 |
attrdescriptor.setSRS(this.getParameters().getTargetProjection());
|
147 |
} |
148 |
|
149 |
// ======================================
|
150 |
// Remove index to improve performance
|
151 |
this.targetFeatureType = targetEditableFeatureType.getNotEditableCopy();
|
152 |
for (int i = 0; i < targetEditableFeatureType.size(); i++) { |
153 |
EditableFeatureAttributeDescriptor x = (EditableFeatureAttributeDescriptor) targetEditableFeatureType.get(i); |
154 |
//x.setIsPrimaryKey(false);
|
155 |
x.setIsIndexed(false);
|
156 |
} |
157 |
|
158 |
// ======================================
|
159 |
JDBCNewStoreParameters createTableParams = (JDBCNewStoreParameters) explorer.getAddParameters(); |
160 |
|
161 |
createTableParams.setSelectRole(this.getParameters().getSelectRole());
|
162 |
createTableParams.setInsertRole(this.getParameters().getInsertRole());
|
163 |
createTableParams.setUpdateRole(this.getParameters().getUpdateRole());
|
164 |
createTableParams.setDeleteRole(this.getParameters().getDeleteRole());
|
165 |
createTableParams.setTruncateRole(this.getParameters().getTruncateRole());
|
166 |
createTableParams.setReferenceRole(this.getParameters().getReferenceRole());
|
167 |
createTableParams.setTriggerRole(this.getParameters().getTriggerRole());
|
168 |
createTableParams.setAllRole(this.getParameters().getAllRole());
|
169 |
|
170 |
createTableParams.setSchema(this.getParameters().getSchema());
|
171 |
createTableParams.setPostCreatingStatement(this.getParameters().getPostCreatingStatement());
|
172 |
|
173 |
createTableParams.setDefaultFeatureType(targetEditableFeatureType); |
174 |
createTableParams.setTable(this.getParameters().getTableName());
|
175 |
|
176 |
return createTableParams;
|
177 |
|
178 |
} catch (Exception ex) { |
179 |
throw new ExportException(ex); |
180 |
} |
181 |
} |
182 |
|
183 |
@Override
|
184 |
public OpenDataStoreParameters createTargetOpenStoreParameters() throws ExportException { |
185 |
try {
|
186 |
DataManager dataManager = DALLocator.getDataManager(); |
187 |
JDBCServerExplorer explorer = this.createServerExplorer();
|
188 |
|
189 |
JDBCStoreParameters openStoreParameters = (JDBCStoreParameters) explorer.getOpenParameters(); |
190 |
openStoreParameters.setSchema(this.getParameters().getSchema());
|
191 |
openStoreParameters.setTable(this.getParameters().getTableName());
|
192 |
openStoreParameters.setCRS(this.getParameters().getTargetProjection());
|
193 |
openStoreParameters.setDefaultGeometryField( |
194 |
this.getParameters().getSourceFeatureType().getDefaultGeometryAttributeName()
|
195 |
); |
196 |
return openStoreParameters;
|
197 |
} catch (DataException ex) {
|
198 |
throw new ExportException(ex); |
199 |
} |
200 |
} |
201 |
|
202 |
|
203 |
|
204 |
private int getGeometryColumnCount(FeatureType featureType) { |
205 |
int count = 0; |
206 |
for (int i = 0; i < featureType.size(); i++) { |
207 |
if (featureType.getAttributeDescriptor(i).getType() == DataTypes.GEOMETRY) {
|
208 |
count++; |
209 |
} |
210 |
} |
211 |
return count;
|
212 |
} |
213 |
|
214 |
@Override
|
215 |
public void export(FeatureSet featureSet) throws ExportException { |
216 |
Geometry.ValidationStatus geometryCheck; |
217 |
|
218 |
DisposableIterator it = null;
|
219 |
EditableFeature targetFeature = null;
|
220 |
FeatureStore target = null;
|
221 |
|
222 |
try {
|
223 |
ExportServiceManager serviceManager = ExportLocator.getServiceManager(); |
224 |
|
225 |
// ======================================
|
226 |
// Reprojection
|
227 |
ICoordTrans coord_trans = this.getParameters().getTransformationToUse();
|
228 |
// ============================================
|
229 |
|
230 |
DataManager dataManager = DALLocator.getDataManager(); |
231 |
|
232 |
JDBCServerExplorer explorer = this.createServerExplorer();
|
233 |
|
234 |
if (this.getParameters().canCreatetable()) { |
235 |
LOG.debug("Creating table");
|
236 |
getTaskStatus().message("Creating table");
|
237 |
NewDataStoreParameters targetNewStoreParameters = this.createTargetNewStoreParameters();
|
238 |
explorer.add( |
239 |
targetNewStoreParameters.getDataStoreName(), |
240 |
targetNewStoreParameters, |
241 |
true
|
242 |
); |
243 |
} |
244 |
|
245 |
JDBCStoreParameters openParams = (JDBCStoreParameters) this.createTargetOpenStoreParameters();
|
246 |
target = (FeatureStore) explorer.open(openParams); |
247 |
|
248 |
FeatureType theTargetFeatureType = target.getDefaultFeatureType(); |
249 |
FeatureType theSourceFeatureType = featureSet.getDefaultFeatureType(); |
250 |
|
251 |
target.edit(FeatureStore.MODE_APPEND); |
252 |
|
253 |
long featureCount = 1; |
254 |
getTaskStatus().setRangeOfValues(0, featureSet.getSize());
|
255 |
|
256 |
int targetGeometryIndex;
|
257 |
int sourceGeometryIndex;
|
258 |
if (getGeometryColumnCount(theSourceFeatureType) == 1 |
259 |
&& getGeometryColumnCount(theTargetFeatureType) == 1) {
|
260 |
// Si solo hay una columna de geometria asignaremos las geometrias
|
261 |
// independientemente de como se llamen los campos.
|
262 |
targetGeometryIndex = theTargetFeatureType.getDefaultGeometryAttributeIndex(); |
263 |
sourceGeometryIndex = theSourceFeatureType.getDefaultGeometryAttributeIndex(); |
264 |
} else {
|
265 |
FeatureAttributeDescriptor attr = theSourceFeatureType.getDefaultGeometryAttribute(); |
266 |
sourceGeometryIndex = attr.getIndex(); |
267 |
targetGeometryIndex = theTargetFeatureType.getAttributeDescriptor(attr.getName()).getIndex(); |
268 |
} |
269 |
|
270 |
LOG.debug("Inserting rows");
|
271 |
getTaskStatus().message("Inserting rows");
|
272 |
it = featureSet.fastIterator(); |
273 |
while (it.hasNext()) {
|
274 |
Feature feature = (Feature) it.next(); |
275 |
this.getTaskStatus().setCurValue(featureCount);
|
276 |
|
277 |
targetFeature = target.createNewFeature(theTargetFeatureType, feature); |
278 |
for (int i = 0; i < theSourceFeatureType.size(); i++) { |
279 |
if (i == sourceGeometryIndex) {
|
280 |
// Es facil que los campos geometria no se llamen igual, asi que
|
281 |
// el campo geometria lo asignamos a capon.
|
282 |
// Esto puede ocasionar problemas cuando la tabla destino no tenga
|
283 |
// geometria o tenga mas de una.
|
284 |
targetFeature.set(targetGeometryIndex, feature.get(sourceGeometryIndex)); |
285 |
} else {
|
286 |
FeatureAttributeDescriptor sourceField = theSourceFeatureType.getAttributeDescriptor(i); |
287 |
// String name = getTranslatedIdentifier(x.getName());
|
288 |
String targetField = this.getParameters().getExportAttributes().getTargetName(sourceField.getName()); |
289 |
int targetAttributeIndex = theTargetFeatureType.getIndex(targetField);
|
290 |
if (targetAttributeIndex < 0) { |
291 |
continue;
|
292 |
//throw new RuntimeException("Can't locate column '" + x.getName() + "' in the target table.");
|
293 |
} |
294 |
targetFeature.set(targetAttributeIndex, feature.get(sourceField.getName())); |
295 |
} |
296 |
} |
297 |
|
298 |
Geometry geometry = targetFeature.getGeometry(targetGeometryIndex); |
299 |
FixGeometryStatus check = serviceManager.fixGeometry(getParameters(), coord_trans, geometry); |
300 |
switch (check.getState()) {
|
301 |
case FixGeometryStatus.STATE_OK:
|
302 |
targetFeature.setDefaultGeometry(check.getGeometry()); |
303 |
break;
|
304 |
case FixGeometryStatus.STATE_SKIP:
|
305 |
continue;
|
306 |
case FixGeometryStatus.STATE_ABORT:
|
307 |
throw new InvalidGeometryException(targetFeature, check.getMessage()); |
308 |
} |
309 |
|
310 |
target.insert(targetFeature); |
311 |
|
312 |
if (this.getTaskStatus().isCancellationRequested()) { |
313 |
return;
|
314 |
} |
315 |
featureCount++; |
316 |
} |
317 |
String msgRowCount = MessageFormat.format(" ({0,number, #,###} rows)", featureCount); |
318 |
targetFeature = null;
|
319 |
getTaskStatus().setIndeterminate(); |
320 |
LOG.debug("Finish editing" + msgRowCount);
|
321 |
if (featureCount > 50000) { |
322 |
getTaskStatus().message("Finishing insertion of records, may take a while" + msgRowCount);
|
323 |
} else {
|
324 |
getTaskStatus().message("Finishing insertion of records" + msgRowCount);
|
325 |
} |
326 |
target.finishEditing(); |
327 |
|
328 |
if (this.getParameters().canCreatetable()) { |
329 |
getTaskStatus().message("Preparing the update of the indices" + msgRowCount);
|
330 |
target.edit(); |
331 |
EditableFeatureType ft = target.getDefaultFeatureType().getEditable(); |
332 |
for (FeatureAttributeDescriptor attr : this.targetFeatureType) { |
333 |
EditableFeatureAttributeDescriptor attr2 = (EditableFeatureAttributeDescriptor) ft.getAttributeDescriptor(attr.getName()); |
334 |
if (attr2 != null) { |
335 |
//attr2.setIsPrimaryKey(attr.isPrimaryKey());
|
336 |
attr2.setIsIndexed(attr.isIndexed()); |
337 |
attr2.setIsAutomatic(attr.isAutomatic()); |
338 |
} |
339 |
} |
340 |
target.update(ft); |
341 |
getTaskStatus().message("Updating the indices" + msgRowCount);
|
342 |
target.finishEditing(); |
343 |
} |
344 |
if (this.getParameters().getUpdateTableStatistics()) { |
345 |
LOG.debug("Updating statistics");
|
346 |
getTaskStatus().message("Updating statistics" + msgRowCount);
|
347 |
explorer.updateTableStatistics( |
348 |
openParams.getDBName(), |
349 |
openParams.getSchema(), |
350 |
openParams.getTable() |
351 |
); |
352 |
} |
353 |
LOG.debug("finish");
|
354 |
getTaskStatus().message("Exportation finished");
|
355 |
getTaskStatus().terminate(); |
356 |
|
357 |
} catch (Exception e) { |
358 |
LOG.warn("Can't export data.", e);
|
359 |
getTaskStatus().message("Problems exporting data");
|
360 |
throw new ExportException(e, targetFeature); |
361 |
|
362 |
} finally {
|
363 |
if (it != null) { |
364 |
it.dispose(); |
365 |
} |
366 |
featureSet.dispose(); |
367 |
if (target != null) { |
368 |
if (target.isAppending()) {
|
369 |
try {
|
370 |
target.cancelEditing(); |
371 |
} catch (Exception ex) { |
372 |
LOG.warn("Can't cancel editing.", ex);
|
373 |
} |
374 |
} |
375 |
target.dispose(); |
376 |
} |
377 |
this.getTaskStatus().terminate();
|
378 |
this.getTaskStatus().remove();
|
379 |
} |
380 |
} |
381 |
|
382 |
} |