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

History | View | Annotate | Download (16.4 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
                // Solo si 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
            }
230

    
231
            logger.debug("Inserting rows");
232
            taskStatus.message("Inserting rows");
233
            it = featureSet.fastIterator();
234
            while (it.hasNext()) {
235
                Feature feature = (Feature) it.next();
236
                this.taskStatus.setCurValue(featureCount);
237

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

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

    
274
                            break;
275

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

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

    
316
                target.insert(targetFeature);
317

    
318
                if (this.taskStatus.isCancellationRequested()) {
319
                    return;
320
                }
321
                featureCount++;
322
            }
323
            targetFeature = null;
324
            target.finishEditing();
325

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

    
337
            if (exporttoServiceFinishAction != null) {
338
                exporttoServiceFinishAction.finished(
339
                        this.options.getTableName() + " (" + explorer.getStoreName() + ")",
340
                        openParams);
341
            }
342
            taskStatus.message("Exportation finished");
343

    
344
        } catch (Exception e) {
345
            logger.warn("Can't export data.", e);
346
            taskStatus.message("Problems exporting data");
347
            throw new ExporttoServiceException(e, targetFeature);
348

    
349
        } finally {
350
            if (it != null) {
351
                it.dispose();
352
            }
353
            featureSet.dispose();
354
            if (target != null) {
355
                target.dispose();
356
            }
357
            this.taskStatus.terminate();
358
            this.taskStatus.remove();
359
        }
360
    }
361

    
362
    private int getGeometryColumnCount(FeatureType featureType) {
363
        int count = 0;
364
        for( int i=0; i<featureType.size(); i++ ) {
365
            if( featureType.getAttributeDescriptor(i).getType()==DataTypes.GEOMETRY ) {
366
                count++;
367
            }
368
        }
369
        return count;
370
    }
371

    
372
    public void setFinishAction(
373
            ExporttoServiceFinishAction exporttoServiceFinishAction) {
374
        this.exporttoServiceFinishAction = exporttoServiceFinishAction;
375
    }
376

    
377
}