Statistics
| Revision:

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

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 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
 *
11
 * 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
 *
16
 * 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
 *
20
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.exportto.swing.prov.jdbc;
24

    
25
import java.util.Map;
26
import org.apache.commons.lang3.StringUtils;
27
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
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
40
import org.gvsig.fmap.dal.feature.FeatureSet;
41
import org.gvsig.fmap.dal.feature.FeatureStore;
42
import org.gvsig.fmap.dal.feature.FeatureType;
43
import org.gvsig.fmap.dal.store.jdbc.JDBCNewStoreParameters;
44
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorer;
45
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters;
46
import org.gvsig.fmap.geom.Geometry;
47
import org.gvsig.tools.dispose.DisposableIterator;
48
import org.gvsig.tools.task.AbstractMonitorableTask;
49
import org.slf4j.Logger;
50
import org.slf4j.LoggerFactory;
51

    
52
/**
53
 * @author gvSIG Team
54
 * @version $Id$
55
 *
56
 */
57
public class ExporrtoJDBCService extends AbstractMonitorableTask implements
58
        ExporttoService {
59

    
60
    private static final Logger logger = LoggerFactory.getLogger(ExporrtoJDBCService.class);
61

    
62
    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

    
66
    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

    
70
    private ExporttoServiceFinishAction exporttoServiceFinishAction = null;
71
    private ExporttoJDBCOptions options;
72
//    private Map<String,String> translationIds;
73

    
74
    public ExporrtoJDBCService(ExporttoJDBCOptions options) {
75
        this.options = options;
76
    }
77

    
78
    protected String getTranslatedIdentifier(String identifier) {
79
        String s = identifier;
80
        if (this.options.getTranslateIdentifiersToLowerCase()) {
81
            s = s.toLowerCase();
82
        }
83
        if( this.options.getTranslateHyphens()) {
84
            s = s.replace("-", "_");
85
        }
86
        if (this.options.getRemoveSpacesInIdentifiers()) {
87
            s = StringUtils.normalizeSpace(s).replace(" ", "_");
88
        }
89
        return s;
90
    }
91

    
92
    protected void createTable(JDBCServerExplorer explorer) throws Exception {
93

    
94
        FeatureType targetFeatureType;
95
        EditableFeatureType targetEditableFeatureType;
96

    
97
        targetFeatureType = options.getSource().getDefaultFeatureType().getCopy();
98
        targetEditableFeatureType = targetFeatureType.getEditable();
99

    
100
        if (this.options.getTranslateIdentifiersToLowerCase()
101
                || this.options.getRemoveSpacesInIdentifiers() 
102
                || this.options.getTranslateHyphens()) {
103
            for (int i = 0; i < targetEditableFeatureType.size(); i++) {
104
                EditableFeatureAttributeDescriptor x = (EditableFeatureAttributeDescriptor) targetEditableFeatureType.get(i);
105
                x.setName(getTranslatedIdentifier(x.getName()));
106
            }
107
        }
108

    
109
        if (this.options.getPrimaryKey() != null) {
110
            EditableFeatureAttributeDescriptor pk = (EditableFeatureAttributeDescriptor) targetEditableFeatureType.get(getTranslatedIdentifier(options.getPrimaryKey()));
111
            if (pk == null) {
112
                pk = targetEditableFeatureType.add(
113
                        getTranslatedIdentifier(this.options.getPrimaryKey()),
114
                        DataTypes.LONG
115
                );
116
                pk.setIsPrimaryKey(true);
117
                pk.setIsAutomatic(true);
118
            } else {
119
                pk.setIsPrimaryKey(true);
120
            }
121
        }
122

    
123
        if (this.options.getCreateIndexInGeometryRow()) {
124
            EditableFeatureAttributeDescriptor x = (EditableFeatureAttributeDescriptor) targetEditableFeatureType.getDefaultGeometryAttribute();
125
            x.setIsIndexed(true);
126
        }
127

    
128
        // ======================================================
129
        // Reprojection: set SRS of geometry field to target SRS
130
        EditableFeatureAttributeDescriptor attrdescriptor
131
                = (EditableFeatureAttributeDescriptor) targetEditableFeatureType.getDefaultGeometryAttribute();
132
        if (attrdescriptor != null) {
133
            attrdescriptor.setSRS(this.options.getTargetProjection());
134
        }
135

    
136
        // ======================================
137
        JDBCNewStoreParameters createTableParams = (JDBCNewStoreParameters) explorer.getAddParameters();
138

    
139
        createTableParams.setSelectRole(this.options.getSelectRole());
140
        createTableParams.setInsertRole(this.options.getInsertRole());
141
        createTableParams.setUpdateRole(this.options.getUpdateRole());
142
        createTableParams.setDeleteRole(this.options.getDeleteRole());
143
        createTableParams.setTruncateRole(this.options.getTruncateRole());
144
        createTableParams.setReferenceRole(this.options.getReferenceRole());
145
        createTableParams.setTriggerRole(this.options.getTriggerRole());
146
        createTableParams.setAllRole(this.options.getAllRole());
147

    
148
        createTableParams.setSchema(this.options.getSchema());
149
        createTableParams.setPostCreatingStatement(this.options.getPostCreatingStatement());
150

    
151
        createTableParams.setDefaultFeatureType(targetEditableFeatureType);
152
        createTableParams.setTable(this.options.getTableName());
153
        explorer.add(explorer.getStoreName(), createTableParams, true);
154
    }
155

    
156
    private static class InvalidGeometryException extends ExporttoServiceException {
157

    
158
        public InvalidGeometryException(Feature feature, String checkMessage) {
159
            super(checkMessage, null, checkMessage, 0);
160
            this.feature = feature;
161
        }
162
    }
163

    
164
    public void export(FeatureSet featureSet) throws ExporttoServiceException {
165
        Geometry.ValidationStatus geometryCheck;
166

    
167
        DisposableIterator it = null;
168
        EditableFeature targetFeature = null;
169
        FeatureStore target = null;
170

    
171
        try {
172

    
173
            // ======================================
174
            // Reprojection
175
            FeatureAttributeDescriptor geo_att = this.options.getSource().getDefaultFeatureType().getDefaultGeometryAttribute();
176
            IProjection sourceProjection = null;
177
            ICoordTrans coord_trans = null;
178
            Geometry reproj_geom = null;
179
            IProjection targetProjection = this.options.getTargetProjection();
180
            if (geo_att != null) {
181
                sourceProjection = geo_att.getSRS();
182
                // this comparison is perhaps too preventive
183
                // we could  have two instances of same projection
184
                // so we would do more computations than needed
185
                if (sourceProjection != null && targetProjection != null && sourceProjection != targetProjection) {
186
                    coord_trans = sourceProjection.getCT(targetProjection);
187
                }
188
            }
189
            // ============================================
190

    
191
            DataManager dataManager = DALLocator.getDataManager();
192

    
193
            JDBCServerExplorer explorer = (JDBCServerExplorer) dataManager.openServerExplorer(
194
                    this.options.getExplorerParameters().getExplorerName(),
195
                    this.options.getExplorerParameters()
196
            );
197

    
198
            if (this.options.canCreatetable()) {
199
                logger.debug("Creating table");
200
                taskStatus.message("Creating table");
201
                this.createTable(explorer);
202
            }
203

    
204
            JDBCStoreParameters openParams = (JDBCStoreParameters) explorer.getOpenParameters();
205
            openParams.setSchema(this.options.getSchema());
206
            openParams.setTable(this.options.getTableName());
207
            openParams.setCRS(targetProjection);
208
            openParams.setDefaultGeometryField(
209
                    this.options.getSource().getDefaultFeatureType().getDefaultGeometryAttributeName()
210
            );
211
            target = (FeatureStore) explorer.open(openParams);
212

    
213
            FeatureType targetFeatureType = target.getDefaultFeatureType();
214
            FeatureType sourceFeatureType = featureSet.getDefaultFeatureType();
215

    
216
            target.edit(FeatureStore.MODE_APPEND);
217

    
218
            int featureCount = 1;
219
            taskStatus.setRangeOfValues(0, featureSet.getSize());
220

    
221
            int targetGeometryIndex = -1;
222
            int sourceGeometryIndex = -1;
223
            if( getGeometryColumnCount(sourceFeatureType)==1
224
                    && getGeometryColumnCount(targetFeatureType)==1 ) {
225
                // Si solo hay una columna de geometria asignaremos las geometrias
226
                // independientemente de como se llamen los campos.
227
                targetGeometryIndex = targetFeatureType.getDefaultGeometryAttributeIndex();
228
                sourceGeometryIndex = sourceFeatureType.getDefaultGeometryAttributeIndex();
229
            } else {
230
                FeatureAttributeDescriptor attr = sourceFeatureType.getDefaultGeometryAttribute();
231
                sourceGeometryIndex = attr.getIndex();
232
                targetGeometryIndex = targetFeatureType.getAttributeDescriptor(attr.getName()).getIndex();
233
            }
234

    
235
            logger.debug("Inserting rows");
236
            taskStatus.message("Inserting rows");
237
            it = featureSet.fastIterator();
238
            while (it.hasNext()) {
239
                Feature feature = (Feature) it.next();
240
                this.taskStatus.setCurValue(featureCount);
241

    
242
                targetFeature = target.createNewFeature(targetFeatureType, feature);
243
                for (int i = 0; i < sourceFeatureType.size(); i++) {
244
                    if (i == sourceGeometryIndex) {
245
                        // Es facil que los campos geometria no se llamen igual, asi que
246
                        // el campo geometria lo asignamos a capon.
247
                        // Esto puede ocasionar problemas cuando la tabla destino no tenga
248
                        // geometria o tenga mas de una.
249
                        targetFeature.set(targetGeometryIndex, feature.get(sourceGeometryIndex));
250
                    } else {
251
                        FeatureAttributeDescriptor x = sourceFeatureType.getAttributeDescriptor(i);
252
                        int targetAttributeIndex = targetFeatureType.getIndex(getTranslatedIdentifier(x.getName()));
253
                        if (targetAttributeIndex < 0) {
254
                            throw new RuntimeException("Can't locate column '" + x.getName() + "' in the target table.");
255
                        }
256
                        targetFeature.set(targetAttributeIndex, feature.get(x.getName()));
257
                    }
258
                }
259

    
260
                Geometry geometry = targetFeature.getGeometry(targetGeometryIndex);
261
                if (geometry != null) {
262
                    switch (this.options.getGeometryChecks()) {
263
                        case CHECK_IF_CORRUPT:
264
                            geometryCheck = geometry.getValidationStatus();
265
                            if (geometryCheck.getStatusCode() == Geometry.ValidationStatus.CURRUPTED) {
266
                                switch (this.options.getGeometryChecksAction()) {
267
                                    case ACTION_SET_GEOMETRY_TO_NULL:
268
                                        targetFeature.setDefaultGeometry(null);
269
                                        break;
270
                                    case ACTION_SKIP_FEATURE:
271
                                        continue;
272
                                    case ACTION_ABORT:
273
                                    default:
274
                                        throw new InvalidGeometryException(targetFeature, geometryCheck.getMessage());
275
                                }
276
                            }
277

    
278
                            break;
279

    
280
                        case CHECK_IF_VALID:
281
                            geometryCheck = geometry.getValidationStatus();
282
                            if (!geometryCheck.isValid()) {
283
                                Geometry g = null;
284
                                if (this.options.getTryToFixGeometry()) {
285
                                    g = geometry.makeValid();
286
                                    if (g != null) {
287
                                        targetFeature.setDefaultGeometry(g);
288
                                    }
289
                                }
290
                                if (g == null) {
291
                                    switch (this.options.getGeometryChecksAction()) {
292
                                        case ACTION_SET_GEOMETRY_TO_NULL:
293
                                            targetFeature.setDefaultGeometry(null);
294
                                            break;
295
                                        case ACTION_SKIP_FEATURE:
296
                                            continue;
297
                                        case ACTION_ABORT:
298
                                        default:
299
                                            throw new InvalidGeometryException(targetFeature, geometryCheck.getMessage());
300
                                    }
301
                                }
302
                            }
303

    
304
                            break;
305
                        case CHECK_NONE:
306
                        default:
307
                            break;
308
                    }
309
                    // ================================================
310
                    // Reprojection
311
                    if (geo_att != null && coord_trans != null) {
312
                        reproj_geom = targetFeature.getDefaultGeometry();
313
                        reproj_geom = reproj_geom.cloneGeometry();
314
                        reproj_geom.reProject(coord_trans);
315
                        targetFeature.setDefaultGeometry(reproj_geom);
316
                    }
317
                    // ================================================
318
                }
319

    
320
                target.insert(targetFeature);
321

    
322
                if (this.taskStatus.isCancellationRequested()) {
323
                    return;
324
                }
325
                featureCount++;
326
            }
327
            targetFeature = null;
328
            target.finishEditing();
329

    
330
            if (this.options.getUpdateTableStatistics()) {
331
                logger.debug("Updating statistics");
332
                taskStatus.message("Updating statistics");
333
                explorer.updateTableStatistics(
334
                        openParams.getDBName(),
335
                        openParams.getSchema(),
336
                        openParams.getTable()
337
                );
338
            }
339
            logger.debug("finish");
340

    
341
            if (exporttoServiceFinishAction != null) {
342
                exporttoServiceFinishAction.finished(
343
                        this.options.getTableName() + " (" + explorer.getStoreName() + ")",
344
                        openParams);
345
            }
346
            taskStatus.message("Exportation finished");
347

    
348
        } catch (Exception e) {
349
            logger.warn("Can't export data.", e);
350
            taskStatus.message("Problems exporting data");
351
            throw new ExporttoServiceException(e, targetFeature);
352

    
353
        } finally {
354
            if (it != null) {
355
                it.dispose();
356
            }
357
            featureSet.dispose();
358
            if (target != null) {
359
                if( target.isAppending() ) {
360
                    try {
361
                        target.cancelEditing();
362
                    } catch(Exception ex) {
363
                        // Do nothing
364
                    }
365
                }
366
                target.dispose();
367
            }
368
            this.taskStatus.terminate();
369
            this.taskStatus.remove();
370
        }
371
    }
372

    
373
    private int getGeometryColumnCount(FeatureType featureType) {
374
        int count = 0;
375
        for( int i=0; i<featureType.size(); i++ ) {
376
            if( featureType.getAttributeDescriptor(i).getType()==DataTypes.GEOMETRY ) {
377
                count++;
378
            }
379
        }
380
        return count;
381
    }
382

    
383
    public void setFinishAction(
384
            ExporttoServiceFinishAction exporttoServiceFinishAction) {
385
        this.exporttoServiceFinishAction = exporttoServiceFinishAction;
386
    }
387

    
388
}