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 / exportto / swing / prov / jdbc / ExporrtoJDBCService.java @ 42090
History | View | Annotate | Download (16.1 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40559 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | 40435 | jjdelcerro | *
|
6 | 41486 | jjdelcerro | * 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 | 40435 | jjdelcerro | *
|
11 | 41486 | jjdelcerro | * 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 | 40435 | jjdelcerro | *
|
16 | 41486 | jjdelcerro | * 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 | 40435 | jjdelcerro | *
|
20 | 41486 | jjdelcerro | * For any additional information, do not hesitate to contact us at info AT
|
21 | * gvsig.com, or visit our website www.gvsig.com.
|
||
22 | 40435 | jjdelcerro | */
|
23 | package org.gvsig.exportto.swing.prov.jdbc; |
||
24 | |||
25 | 41598 | jjdelcerro | import java.util.Map; |
26 | import org.apache.commons.lang3.StringUtils; |
||
27 | 40435 | jjdelcerro | import org.cresques.cts.ICoordTrans; |
28 | import org.cresques.cts.IProjection; |
||
29 | import org.gvsig.exportto.ExporttoService; |
||
30 | import org.gvsig.exportto.ExporttoServiceException; |
||
31 | import org.gvsig.exportto.ExporttoServiceFinishAction; |
||
32 | import org.gvsig.fmap.dal.DALLocator; |
||
33 | import org.gvsig.fmap.dal.DataManager; |
||
34 | import org.gvsig.fmap.dal.DataTypes; |
||
35 | import org.gvsig.fmap.dal.feature.EditableFeature; |
||
36 | import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
||
37 | import org.gvsig.fmap.dal.feature.EditableFeatureType; |
||
38 | import org.gvsig.fmap.dal.feature.Feature; |
||
39 | 41127 | jldominguez | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
40 | 40435 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureSet; |
41 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
42 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
43 | 41492 | jjdelcerro | import org.gvsig.fmap.dal.store.jdbc.JDBCNewStoreParameters; |
44 | 40435 | jjdelcerro | import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorer; |
45 | 40606 | jldominguez | import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
46 | 40435 | jjdelcerro | import org.gvsig.fmap.geom.Geometry; |
47 | import org.gvsig.tools.dispose.DisposableIterator; |
||
48 | import org.gvsig.tools.task.AbstractMonitorableTask; |
||
49 | 41488 | jjdelcerro | import org.slf4j.Logger; |
50 | import org.slf4j.LoggerFactory; |
||
51 | 40435 | jjdelcerro | |
52 | /**
|
||
53 | * @author gvSIG Team
|
||
54 | * @version $Id$
|
||
55 | 41486 | jjdelcerro | *
|
56 | 40435 | jjdelcerro | */
|
57 | public class ExporrtoJDBCService extends AbstractMonitorableTask implements |
||
58 | 41486 | jjdelcerro | ExporttoService { |
59 | 40435 | jjdelcerro | |
60 | 41488 | jjdelcerro | private static final Logger logger = LoggerFactory.getLogger(ExporrtoJDBCService.class); |
61 | |||
62 | 41486 | jjdelcerro | public static final int CHECK_NONE = 0; |
63 | public static final int CHECK_IF_CORRUPT = 1; |
||
64 | public static final int CHECK_IF_VALID = 2; |
||
65 | 40435 | jjdelcerro | |
66 | 41486 | jjdelcerro | public static final int ACTION_SET_GEOMETRY_TO_NULL = 0; |
67 | public static final int ACTION_SKIP_FEATURE = 1; |
||
68 | public static final int ACTION_ABORT = 2; |
||
69 | 40435 | jjdelcerro | |
70 | private ExporttoServiceFinishAction exporttoServiceFinishAction = null; |
||
71 | 41598 | jjdelcerro | private ExporttoJDBCOptions options;
|
72 | // private Map<String,String> translationIds;
|
||
73 | 40435 | jjdelcerro | |
74 | 41598 | jjdelcerro | public ExporrtoJDBCService(ExporttoJDBCOptions options) {
|
75 | this.options = options;
|
||
76 | 40435 | jjdelcerro | } |
77 | |||
78 | 41598 | jjdelcerro | protected String getTranslatedIdentifier(String identifier) { |
79 | String s = identifier;
|
||
80 | 41681 | jjdelcerro | if (this.options.getTranslateIdentifiersToLowerCase()) { |
81 | 41598 | jjdelcerro | s = s.toLowerCase(); |
82 | } |
||
83 | 41681 | jjdelcerro | if (this.options.getRemoveSpacesInIdentifiers()) { |
84 | 41598 | jjdelcerro | s = StringUtils.normalizeSpace(s).replace(" ", "_"); |
85 | } |
||
86 | return s;
|
||
87 | } |
||
88 | 41681 | jjdelcerro | |
89 | 41486 | jjdelcerro | protected void createTable(JDBCServerExplorer explorer) throws Exception { |
90 | 40435 | jjdelcerro | |
91 | 41488 | jjdelcerro | FeatureType targetFeatureType; |
92 | EditableFeatureType targetEditableFeatureType; |
||
93 | 40435 | jjdelcerro | |
94 | 41598 | jjdelcerro | targetFeatureType = options.getSource().getDefaultFeatureType().getCopy(); |
95 | 41486 | jjdelcerro | targetEditableFeatureType = targetFeatureType.getEditable(); |
96 | 41681 | jjdelcerro | |
97 | if (this.options.getTranslateIdentifiersToLowerCase() |
||
98 | || this.options.getRemoveSpacesInIdentifiers()) {
|
||
99 | for (int i = 0; i < targetEditableFeatureType.size(); i++) { |
||
100 | EditableFeatureAttributeDescriptor x = (EditableFeatureAttributeDescriptor) targetEditableFeatureType.get(i); |
||
101 | 41598 | jjdelcerro | x.setName(getTranslatedIdentifier(x.getName())); |
102 | } |
||
103 | } |
||
104 | 41681 | jjdelcerro | |
105 | 41598 | jjdelcerro | if (this.options.getPrimaryKey() != null) { |
106 | EditableFeatureAttributeDescriptor pk = (EditableFeatureAttributeDescriptor) targetEditableFeatureType.get(getTranslatedIdentifier(options.getPrimaryKey())); |
||
107 | 41488 | jjdelcerro | if (pk == null) { |
108 | 41598 | jjdelcerro | pk = targetEditableFeatureType.add( |
109 | 41681 | jjdelcerro | getTranslatedIdentifier(this.options.getPrimaryKey()),
|
110 | 41598 | jjdelcerro | DataTypes.LONG |
111 | ); |
||
112 | 41488 | jjdelcerro | pk.setIsPrimaryKey(true);
|
113 | pk.setIsAutomatic(true);
|
||
114 | } else {
|
||
115 | pk.setIsPrimaryKey(true);
|
||
116 | } |
||
117 | 41486 | jjdelcerro | } |
118 | |||
119 | 41681 | jjdelcerro | if (this.options.getCreateIndexInGeometryRow()) { |
120 | EditableFeatureAttributeDescriptor x = (EditableFeatureAttributeDescriptor) targetEditableFeatureType.getDefaultGeometryAttribute(); |
||
121 | 41638 | jjdelcerro | x.setIsIndexed(true);
|
122 | } |
||
123 | 41681 | jjdelcerro | |
124 | 41486 | jjdelcerro | // ======================================================
|
125 | // Reprojection: set SRS of geometry field to target SRS
|
||
126 | EditableFeatureAttributeDescriptor attrdescriptor |
||
127 | = (EditableFeatureAttributeDescriptor) targetEditableFeatureType.getDefaultGeometryAttribute(); |
||
128 | if (attrdescriptor != null) { |
||
129 | 41598 | jjdelcerro | attrdescriptor.setSRS(this.options.getTargetProjection());
|
130 | 41486 | jjdelcerro | } |
131 | |||
132 | // ======================================
|
||
133 | 41492 | jjdelcerro | JDBCNewStoreParameters createTableParams = (JDBCNewStoreParameters) explorer.getAddParameters(); |
134 | 41486 | jjdelcerro | |
135 | 41655 | jjdelcerro | createTableParams.setSelectRole(this.options.getSelectRole());
|
136 | createTableParams.setInsertRole(this.options.getInsertRole());
|
||
137 | createTableParams.setUpdateRole(this.options.getUpdateRole());
|
||
138 | createTableParams.setDeleteRole(this.options.getDeleteRole());
|
||
139 | createTableParams.setTruncateRole(this.options.getTruncateRole());
|
||
140 | createTableParams.setReferenceRole(this.options.getReferenceRole());
|
||
141 | createTableParams.setTriggerRole(this.options.getTriggerRole());
|
||
142 | createTableParams.setAllRole(this.options.getAllRole());
|
||
143 | 41681 | jjdelcerro | |
144 | 41655 | jjdelcerro | createTableParams.setSchema(this.options.getSchema());
|
145 | createTableParams.setPostCreatingStatement(this.options.getPostCreatingStatement());
|
||
146 | 41681 | jjdelcerro | |
147 | 41486 | jjdelcerro | createTableParams.setDefaultFeatureType(targetEditableFeatureType); |
148 | 41681 | jjdelcerro | createTableParams.setTable(this.options.getTableName());
|
149 | 41486 | jjdelcerro | explorer.add(explorer.getStoreName(), createTableParams, true);
|
150 | } |
||
151 | |||
152 | private static class InvalidGeometryException extends ExporttoServiceException { |
||
153 | |||
154 | public InvalidGeometryException(Feature feature, String checkMessage) { |
||
155 | super(checkMessage, null, checkMessage, 0); |
||
156 | this.feature = feature;
|
||
157 | } |
||
158 | } |
||
159 | |||
160 | public void export(FeatureSet featureSet) throws ExporttoServiceException { |
||
161 | Geometry.ValidationStatus geometryCheck; |
||
162 | |||
163 | DisposableIterator it = null;
|
||
164 | EditableFeature targetFeature = null;
|
||
165 | 40435 | jjdelcerro | FeatureStore target = null;
|
166 | 41488 | jjdelcerro | |
167 | 40435 | jjdelcerro | try {
|
168 | 41488 | jjdelcerro | |
169 | 40435 | jjdelcerro | // ======================================
|
170 | // Reprojection
|
||
171 | 41598 | jjdelcerro | FeatureAttributeDescriptor geo_att = this.options.getSource().getDefaultFeatureType().getDefaultGeometryAttribute();
|
172 | 41127 | jldominguez | IProjection sourceProjection = null;
|
173 | 40435 | jjdelcerro | ICoordTrans coord_trans = null;
|
174 | 41486 | jjdelcerro | Geometry reproj_geom = null;
|
175 | 41127 | jldominguez | if (geo_att != null) { |
176 | sourceProjection = geo_att.getSRS(); |
||
177 | // this comparison is perhaps too preventive
|
||
178 | // we could have two instances of same projection
|
||
179 | // so we would do more computations than needed
|
||
180 | 42090 | jbadia | if (sourceProjection != null && sourceProjection != this.options.getTargetProjection()) { |
181 | 41598 | jjdelcerro | coord_trans = sourceProjection.getCT(this.options.getTargetProjection());
|
182 | 41127 | jldominguez | } |
183 | 40435 | jjdelcerro | } |
184 | // ============================================
|
||
185 | |||
186 | 41486 | jjdelcerro | DataManager dataManager = DALLocator.getDataManager(); |
187 | 40435 | jjdelcerro | |
188 | 41486 | jjdelcerro | JDBCServerExplorer explorer = (JDBCServerExplorer) dataManager.openServerExplorer( |
189 | 41598 | jjdelcerro | this.options.getExplorerParameters().getExplorerName(),
|
190 | this.options.getExplorerParameters()
|
||
191 | 41486 | jjdelcerro | ); |
192 | |||
193 | 41681 | jjdelcerro | if (this.options.canCreatetable()) { |
194 | 41880 | jjdelcerro | logger.debug("Creating table");
|
195 | 41654 | jjdelcerro | taskStatus.message("Creating table");
|
196 | 41486 | jjdelcerro | this.createTable(explorer);
|
197 | } |
||
198 | |||
199 | JDBCStoreParameters openParams = (JDBCStoreParameters) explorer.getOpenParameters(); |
||
200 | 41664 | jjdelcerro | openParams.setSchema(this.options.getSchema());
|
201 | 41598 | jjdelcerro | openParams.setTable(this.options.getTableName());
|
202 | openParams.setCRS(this.options.getTargetProjection());
|
||
203 | 41486 | jjdelcerro | openParams.setDefaultGeometryField( |
204 | 41598 | jjdelcerro | this.options.getSource().getDefaultFeatureType().getDefaultGeometryAttributeName()
|
205 | 41486 | jjdelcerro | ); |
206 | target = (FeatureStore) explorer.open(openParams); |
||
207 | |||
208 | FeatureType targetFeatureType = target.getDefaultFeatureType(); |
||
209 | 41598 | jjdelcerro | FeatureType sourceFeatureType = featureSet.getDefaultFeatureType(); |
210 | 41681 | jjdelcerro | |
211 | 40435 | jjdelcerro | target.edit(FeatureStore.MODE_APPEND); |
212 | |||
213 | int featureCount = 1; |
||
214 | 41486 | jjdelcerro | taskStatus.setRangeOfValues(0, featureSet.getSize());
|
215 | |||
216 | 41681 | jjdelcerro | int targetGeometryIndex = -1; |
217 | int sourceGeometryIndex = -1; |
||
218 | if( getGeometryColumnCount(sourceFeatureType)==1 |
||
219 | && getGeometryColumnCount(targetFeatureType)==1 ) {
|
||
220 | // Solo si hay una columna de geometria asignaremos las geometrias
|
||
221 | // independientemente de como se llamen los campos.
|
||
222 | targetGeometryIndex = targetFeatureType.getDefaultGeometryAttributeIndex(); |
||
223 | sourceGeometryIndex = sourceFeatureType.getDefaultGeometryAttributeIndex(); |
||
224 | } |
||
225 | |||
226 | 41880 | jjdelcerro | logger.debug("Inserting rows");
|
227 | 41654 | jjdelcerro | taskStatus.message("Inserting rows");
|
228 | 41486 | jjdelcerro | it = featureSet.fastIterator(); |
229 | while (it.hasNext()) {
|
||
230 | Feature feature = (Feature) it.next(); |
||
231 | 41681 | jjdelcerro | this.taskStatus.setCurValue(featureCount);
|
232 | |||
233 | 41486 | jjdelcerro | targetFeature = target.createNewFeature(targetFeatureType, feature); |
234 | 41681 | jjdelcerro | for (int i = 0; i < sourceFeatureType.size(); i++) { |
235 | if (i == sourceGeometryIndex) {
|
||
236 | // Es facil que los campos geometria no se llamen igual, asi que
|
||
237 | // el campo geometria lo asignamos a capon.
|
||
238 | // Esto puede ocasionar problemas cuando la tabla destino no tenga
|
||
239 | // geometria o tenga mas de una.
|
||
240 | targetFeature.set(targetGeometryIndex, feature.get(sourceGeometryIndex)); |
||
241 | } else {
|
||
242 | FeatureAttributeDescriptor x = sourceFeatureType.getAttributeDescriptor(i); |
||
243 | int targetAttributeIndex = targetFeatureType.getIndex(getTranslatedIdentifier(x.getName()));
|
||
244 | if (targetAttributeIndex < 0) { |
||
245 | throw new RuntimeException("Can't locate column '" + x.getName() + "' in the target table."); |
||
246 | } |
||
247 | targetFeature.set(targetAttributeIndex, feature.get(x.getName())); |
||
248 | } |
||
249 | 41598 | jjdelcerro | } |
250 | 41486 | jjdelcerro | |
251 | Geometry geometry = targetFeature.getDefaultGeometry(); |
||
252 | 41488 | jjdelcerro | if (geometry != null) { |
253 | 41598 | jjdelcerro | switch (this.options.getGeometryChecks()) { |
254 | 41488 | jjdelcerro | case CHECK_IF_CORRUPT:
|
255 | geometryCheck = geometry.getValidationStatus(); |
||
256 | if (geometryCheck.getStatusCode() == Geometry.ValidationStatus.CURRUPTED) {
|
||
257 | 41598 | jjdelcerro | switch (this.options.getGeometryChecksAction()) { |
258 | 41488 | jjdelcerro | case ACTION_SET_GEOMETRY_TO_NULL:
|
259 | targetFeature.setDefaultGeometry(null);
|
||
260 | break;
|
||
261 | case ACTION_SKIP_FEATURE:
|
||
262 | continue;
|
||
263 | case ACTION_ABORT:
|
||
264 | default:
|
||
265 | throw new InvalidGeometryException(targetFeature, geometryCheck.getMessage()); |
||
266 | } |
||
267 | 41486 | jjdelcerro | } |
268 | |||
269 | 41488 | jjdelcerro | break;
|
270 | 41486 | jjdelcerro | |
271 | 41488 | jjdelcerro | case CHECK_IF_VALID:
|
272 | geometryCheck = geometry.getValidationStatus(); |
||
273 | if (!geometryCheck.isValid()) {
|
||
274 | Geometry g = null;
|
||
275 | 41681 | jjdelcerro | if (this.options.getTryToFixGeometry()) { |
276 | 41488 | jjdelcerro | g = geometry.makeValid(); |
277 | if (g != null) { |
||
278 | targetFeature.setDefaultGeometry(g); |
||
279 | } |
||
280 | } |
||
281 | if (g == null) { |
||
282 | 41598 | jjdelcerro | switch (this.options.getGeometryChecksAction()) { |
283 | 41488 | jjdelcerro | case ACTION_SET_GEOMETRY_TO_NULL:
|
284 | targetFeature.setDefaultGeometry(null);
|
||
285 | break;
|
||
286 | case ACTION_SKIP_FEATURE:
|
||
287 | continue;
|
||
288 | case ACTION_ABORT:
|
||
289 | default:
|
||
290 | throw new InvalidGeometryException(targetFeature, geometryCheck.getMessage()); |
||
291 | } |
||
292 | } |
||
293 | 41486 | jjdelcerro | } |
294 | |||
295 | 41488 | jjdelcerro | break;
|
296 | case CHECK_NONE:
|
||
297 | default:
|
||
298 | break;
|
||
299 | } |
||
300 | // ================================================
|
||
301 | // Reprojection
|
||
302 | if (geo_att != null && coord_trans != null) { |
||
303 | reproj_geom = targetFeature.getDefaultGeometry(); |
||
304 | reproj_geom = reproj_geom.cloneGeometry(); |
||
305 | reproj_geom.reProject(coord_trans); |
||
306 | targetFeature.setDefaultGeometry(reproj_geom); |
||
307 | } |
||
308 | // ================================================
|
||
309 | 41486 | jjdelcerro | } |
310 | |||
311 | target.insert(targetFeature); |
||
312 | 40435 | jjdelcerro | |
313 | if (this.taskStatus.isCancellationRequested()) { |
||
314 | return;
|
||
315 | } |
||
316 | featureCount++; |
||
317 | } |
||
318 | 41486 | jjdelcerro | targetFeature = null;
|
319 | 40435 | jjdelcerro | target.finishEditing(); |
320 | 41681 | jjdelcerro | |
321 | if (this.options.getUpdateTableStatistics()) { |
||
322 | 41880 | jjdelcerro | logger.debug("Updating statistics");
|
323 | 41654 | jjdelcerro | taskStatus.message("Updating statistics");
|
324 | 41638 | jjdelcerro | explorer.updateTableStatistics(openParams.tableID()); |
325 | } |
||
326 | 41880 | jjdelcerro | logger.debug("finish");
|
327 | 40435 | jjdelcerro | |
328 | if (exporttoServiceFinishAction != null) { |
||
329 | 41127 | jldominguez | exporttoServiceFinishAction.finished( |
330 | 41598 | jjdelcerro | this.options.getTableName() + " (" + explorer.getStoreName() + ")", |
331 | 41486 | jjdelcerro | openParams); |
332 | 40435 | jjdelcerro | } |
333 | 41654 | jjdelcerro | taskStatus.message("Exportation finished");
|
334 | 41681 | jjdelcerro | |
335 | 40435 | jjdelcerro | } catch (Exception e) { |
336 | 41488 | jjdelcerro | logger.warn("Can't export data.", e);
|
337 | 41654 | jjdelcerro | taskStatus.message("Problems exporting data");
|
338 | 41486 | jjdelcerro | throw new ExporttoServiceException(e, targetFeature); |
339 | |||
340 | 40435 | jjdelcerro | } finally {
|
341 | 41486 | jjdelcerro | if (it != null) { |
342 | it.dispose(); |
||
343 | 40435 | jjdelcerro | } |
344 | featureSet.dispose(); |
||
345 | if (target != null) { |
||
346 | target.dispose(); |
||
347 | } |
||
348 | this.taskStatus.terminate();
|
||
349 | this.taskStatus.remove();
|
||
350 | } |
||
351 | } |
||
352 | 41681 | jjdelcerro | |
353 | private int getGeometryColumnCount(FeatureType featureType) { |
||
354 | int count = 0; |
||
355 | for( int i=0; i<featureType.size(); i++ ) { |
||
356 | if( featureType.getAttributeDescriptor(i).getType()==DataTypes.GEOMETRY ) {
|
||
357 | count++; |
||
358 | } |
||
359 | } |
||
360 | return count;
|
||
361 | } |
||
362 | 40435 | jjdelcerro | |
363 | public void setFinishAction( |
||
364 | 41486 | jjdelcerro | ExporttoServiceFinishAction exporttoServiceFinishAction) { |
365 | 40435 | jjdelcerro | this.exporttoServiceFinishAction = exporttoServiceFinishAction;
|
366 | } |
||
367 | |||
368 | } |