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

History | View | Annotate | Download (17.1 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.text.MessageFormat;
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.exportto.swing.spi.ExportGeometryUtils;
33
import org.gvsig.exportto.swing.spi.ExportGeometryUtils.FixGeometryStatus;
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.feature.EditableFeature;
38
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
39
import org.gvsig.fmap.dal.feature.EditableFeatureType;
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.feature.FeatureType;
46
import org.gvsig.fmap.dal.store.jdbc.JDBCNewStoreParameters;
47
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorer;
48
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters;
49
import org.gvsig.fmap.geom.Geometry;
50
import org.gvsig.tools.dispose.DisposableIterator;
51
import org.gvsig.tools.task.AbstractMonitorableTask;
52
import org.slf4j.Logger;
53
import org.slf4j.LoggerFactory;
54

    
55
/**
56
 * @author gvSIG Team
57
 * @version $Id$
58
 *
59
 */
60
public class ExporrtoJDBCService extends AbstractMonitorableTask implements
61
        ExporttoService {
62

    
63
    private static final Logger logger = LoggerFactory.getLogger(ExporrtoJDBCService.class);
64

    
65
    private ExporttoServiceFinishAction exporttoServiceFinishAction = null;
66
    private final ExporttoJDBCOptions options;
67
    private FeatureType targetFeatureType = null;
68
//    private Map<String,String> translationIds;
69

    
70
    public ExporrtoJDBCService(ExporttoJDBCOptions options) {
71
        this.options = options;
72
    }
73

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

    
88
    protected void createTable(JDBCServerExplorer explorer) throws Exception {
89

    
90
        FeatureType targetTempFeatureType;
91
        EditableFeatureType targetEditableFeatureType;
92

    
93
        DataManager manager = DALLocator.getDataManager();
94
        FeatureStoreProviderFactory factory = (FeatureStoreProviderFactory) manager.getStoreProviderFactory(explorer.getStoreName());
95
        
96
        boolean preferNotToUseNonNullRestrictions = factory.preferNotToUseNonNullRestrictions();
97
        
98
        targetTempFeatureType = options.getSourceFeatureType().getCopy();
99
        targetEditableFeatureType = targetTempFeatureType.getEditable();
100

    
101
        // Remove inherited primary keys from the data source and remove NotNull
102
        // restrictions if proceed.
103
        for (int i = 0; i < targetEditableFeatureType.size(); i++) {
104
            EditableFeatureAttributeDescriptor x = (EditableFeatureAttributeDescriptor) targetEditableFeatureType.get(i);
105
            x.setIsPrimaryKey(false);
106
            if( preferNotToUseNonNullRestrictions ) {
107
                x.setAllowNull(true);
108
            }
109
        }
110
                
111
        if (this.options.getTranslateIdentifiersToLowerCase()
112
                || this.options.getRemoveSpacesInIdentifiers() 
113
                || this.options.getTranslateHyphens()) {
114
            for (int i = 0; i < targetEditableFeatureType.size(); i++) {
115
                EditableFeatureAttributeDescriptor x = (EditableFeatureAttributeDescriptor) targetEditableFeatureType.get(i);
116
                x.setName(getTranslatedIdentifier(x.getName()));
117
            }
118
        }
119

    
120
        if (this.options.getPrimaryKey() != null) {
121
            EditableFeatureAttributeDescriptor pk = (EditableFeatureAttributeDescriptor) targetEditableFeatureType.get(getTranslatedIdentifier(options.getPrimaryKey()));
122
            if (pk == null) {
123
                pk = targetEditableFeatureType.add(
124
                        getTranslatedIdentifier(this.options.getPrimaryKey()),
125
                        DataTypes.LONG
126
                );
127
                pk.setIsPrimaryKey(true);
128
                pk.setIsAutomatic(true);
129
            } else {
130
                pk.setIsPrimaryKey(true);
131
            }
132
        }
133

    
134
        if (this.options.getCreateIndexInGeometryRow()) {
135
            EditableFeatureAttributeDescriptor x = (EditableFeatureAttributeDescriptor) targetEditableFeatureType.getDefaultGeometryAttribute();
136
            x.setIsIndexed(true);
137
        }
138

    
139
        // ======================================================
140
        // Reprojection: set SRS of geometry field to target SRS
141
        EditableFeatureAttributeDescriptor attrdescriptor
142
                = (EditableFeatureAttributeDescriptor) targetEditableFeatureType.getDefaultGeometryAttribute();
143
        if (attrdescriptor != null) {
144
            attrdescriptor.setSRS(this.options.getTargetProjection());
145
        }
146

    
147
        // ======================================
148
        // Remove index to improve performance
149
        this.targetFeatureType = targetEditableFeatureType.getNotEditableCopy();
150
        for (int i = 0; i < targetEditableFeatureType.size(); i++) {
151
            EditableFeatureAttributeDescriptor x = (EditableFeatureAttributeDescriptor) targetEditableFeatureType.get(i);
152
            //x.setIsPrimaryKey(false);
153
            x.setIsIndexed(false);
154
        }
155
        
156
        // ======================================
157
        
158

    
159
        JDBCNewStoreParameters createTableParams = (JDBCNewStoreParameters) explorer.getAddParameters();
160

    
161
        createTableParams.setSelectRole(this.options.getSelectRole());
162
        createTableParams.setInsertRole(this.options.getInsertRole());
163
        createTableParams.setUpdateRole(this.options.getUpdateRole());
164
        createTableParams.setDeleteRole(this.options.getDeleteRole());
165
        createTableParams.setTruncateRole(this.options.getTruncateRole());
166
        createTableParams.setReferenceRole(this.options.getReferenceRole());
167
        createTableParams.setTriggerRole(this.options.getTriggerRole());
168
        createTableParams.setAllRole(this.options.getAllRole());
169

    
170
        createTableParams.setSchema(this.options.getSchema());
171
        createTableParams.setPostCreatingStatement(this.options.getPostCreatingStatement());
172

    
173
        createTableParams.setDefaultFeatureType(targetEditableFeatureType);
174
        createTableParams.setTable(this.options.getTableName());
175
        explorer.add(explorer.getStoreName(), createTableParams, true);
176
    }
177

    
178
    private static class InvalidGeometryException extends ExporttoServiceException {
179

    
180
        private static final long serialVersionUID = 3082742759529054747L;
181

    
182
        public InvalidGeometryException(Feature feature, String checkMessage) {
183
            super(checkMessage, null, checkMessage, 0);
184
            this.feature = feature;
185
        }
186
    }
187

    
188
    @Override
189
    public void export(FeatureSet featureSet) throws ExporttoServiceException {
190
        Geometry.ValidationStatus geometryCheck;
191

    
192
        DisposableIterator it = null;
193
        EditableFeature targetFeature = null;
194
        FeatureStore target = null;
195

    
196
        try {
197

    
198
            // ======================================
199
            // Reprojection
200
            FeatureAttributeDescriptor geo_att = this.options.getSourceFeatureType().getDefaultGeometryAttribute();
201
            IProjection sourceProjection;
202
            ICoordTrans coord_trans = null;
203
            Geometry reproj_geom;
204
            IProjection targetProjection = this.options.getTargetProjection();
205
            if (geo_att != null) {
206
                sourceProjection = geo_att.getSRS();
207
                // this comparison is perhaps too preventive
208
                // we could  have two instances of same projection
209
                // so we would do more computations than needed
210
                if (sourceProjection != null && targetProjection != null && sourceProjection != targetProjection) {
211
                    coord_trans = sourceProjection.getCT(targetProjection);
212
                }
213
            }
214
            // ============================================
215

    
216
            DataManager dataManager = DALLocator.getDataManager();
217

    
218
            JDBCServerExplorer explorer = (JDBCServerExplorer) dataManager.openServerExplorer(
219
                    this.options.getExplorerParameters().getExplorerName(),
220
                    this.options.getExplorerParameters()
221
            );
222

    
223
            if (this.options.canCreatetable()) {
224
                logger.debug("Creating table");
225
                taskStatus.message("Creating table");
226
                this.createTable(explorer);
227
            }
228

    
229
            JDBCStoreParameters openParams = (JDBCStoreParameters) explorer.getOpenParameters();
230
            openParams.setSchema(this.options.getSchema());
231
            openParams.setTable(this.options.getTableName());
232
            openParams.setCRS(targetProjection);
233
            openParams.setDefaultGeometryField(
234
                    this.options.getSourceFeatureType().getDefaultGeometryAttributeName()
235
            );
236
            target = (FeatureStore) explorer.open(openParams);
237

    
238
            FeatureType targetFeatureType = target.getDefaultFeatureType();
239
            FeatureType sourceFeatureType = featureSet.getDefaultFeatureType();
240

    
241
            target.edit(FeatureStore.MODE_APPEND);
242

    
243
            long featureCount = 1;
244
            taskStatus.setRangeOfValues(0, featureSet.getSize());
245

    
246
            int targetGeometryIndex;
247
            int sourceGeometryIndex;
248
            if( getGeometryColumnCount(sourceFeatureType)==1
249
                    && getGeometryColumnCount(targetFeatureType)==1 ) {
250
                // Si solo hay una columna de geometria asignaremos las geometrias
251
                // independientemente de como se llamen los campos.
252
                targetGeometryIndex = targetFeatureType.getDefaultGeometryAttributeIndex();
253
                sourceGeometryIndex = sourceFeatureType.getDefaultGeometryAttributeIndex();
254
            } else {
255
                FeatureAttributeDescriptor attr = sourceFeatureType.getDefaultGeometryAttribute();
256
                sourceGeometryIndex = attr.getIndex();
257
                targetGeometryIndex = targetFeatureType.getAttributeDescriptor(attr.getName()).getIndex();
258
            }
259

    
260
            logger.debug("Inserting rows");
261
            taskStatus.message("Inserting rows");
262
            it = featureSet.fastIterator();
263
            while (it.hasNext()) {
264
                Feature feature = (Feature) it.next();
265
                this.taskStatus.setCurValue(featureCount);
266

    
267
                targetFeature = target.createNewFeature(targetFeatureType, feature);
268
                for (int i = 0; i < sourceFeatureType.size(); i++) {
269
                    if (i == sourceGeometryIndex) {
270
                        // Es facil que los campos geometria no se llamen igual, asi que
271
                        // el campo geometria lo asignamos a capon.
272
                        // Esto puede ocasionar problemas cuando la tabla destino no tenga
273
                        // geometria o tenga mas de una.
274
                        targetFeature.set(targetGeometryIndex, feature.get(sourceGeometryIndex));
275
                    } else {
276
                        FeatureAttributeDescriptor x = sourceFeatureType.getAttributeDescriptor(i);
277
                        int targetAttributeIndex = targetFeatureType.getIndex(getTranslatedIdentifier(x.getName()));
278
                        if (targetAttributeIndex < 0) {
279
                            throw new RuntimeException("Can't locate column '" + x.getName() + "' in the target table.");
280
                        }
281
                        targetFeature.set(targetAttributeIndex, feature.get(x.getName()));
282
                    }
283
                }
284

    
285
                Geometry geometry = targetFeature.getGeometry(targetGeometryIndex);
286
                FixGeometryStatus check = ExportGeometryUtils.fixGeometry(options, coord_trans, geometry);
287
                switch(check.getState()) {
288
                    case FixGeometryStatus.STATE_OK:
289
                        targetFeature.setDefaultGeometry(check.getGeometry());
290
                        break;
291
                    case FixGeometryStatus.STATE_SKIP:
292
                        continue;
293
                    case FixGeometryStatus.STATE_ABORT:
294
                        throw new InvalidGeometryException(targetFeature, check.getMessage());
295
                }
296

    
297
                target.insert(targetFeature);
298

    
299
                if (this.taskStatus.isCancellationRequested()) {
300
                    return;
301
                }
302
                featureCount++;
303
            }
304
            String msgRowCount = MessageFormat.format(" ({0,number, #,###} rows)",featureCount);
305
            targetFeature = null;
306
            taskStatus.setIndeterminate();
307
            logger.debug("Finish editing"+msgRowCount);
308
            if( featureCount > 50000 ) {
309
                taskStatus.message("Finishing insertion of records, may take a while" + msgRowCount);
310
            } else {
311
                taskStatus.message("Finishing insertion of records"+msgRowCount);
312
            }
313
            target.finishEditing();
314
            
315
             if (this.options.canCreatetable()) {
316
                taskStatus.message("Preparing the update of the indices"+msgRowCount);
317
                target.edit();
318
                EditableFeatureType ft = target.getDefaultFeatureType().getEditable();
319
                for( FeatureAttributeDescriptor attr : this.targetFeatureType) {
320
                    EditableFeatureAttributeDescriptor attr2 = (EditableFeatureAttributeDescriptor) ft.getAttributeDescriptor(attr.getName());
321
                    if( attr2!=null ) {
322
                        //attr2.setIsPrimaryKey(attr.isPrimaryKey());
323
                        attr2.setIsIndexed(attr.isIndexed());
324
                        attr2.setIsAutomatic(attr.isAutomatic());
325
                    }
326
                }
327
                target.update(ft);
328
                taskStatus.message("Updating the indices"+msgRowCount);
329
                target.finishEditing();
330
            }
331
            if (this.options.getUpdateTableStatistics()) {
332
                logger.debug("Updating statistics");
333
                taskStatus.message("Updating statistics"+msgRowCount);
334
                explorer.updateTableStatistics(
335
                        openParams.getDBName(),
336
                        openParams.getSchema(),
337
                        openParams.getTable()
338
                );
339
            }
340
            logger.debug("finish");
341
            taskStatus.message("Exportation finished");
342
            this.taskStatus.terminate();
343

    
344
            if (exporttoServiceFinishAction != null) {
345
                exporttoServiceFinishAction.finished(
346
                        this.options.getTableName() + " (" + explorer.getStoreName() + ")",
347
                        openParams);
348
            }
349

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

    
355
        } finally {
356
            if (it != null) {
357
                it.dispose();
358
            }
359
            featureSet.dispose();
360
            if (target != null) {
361
                if( target.isAppending() ) {
362
                    try {
363
                        target.cancelEditing();
364
                    } catch(Exception ex) {
365
                        logger.warn("Can't cancel editing.",ex);
366
                    }
367
                }
368
                target.dispose();
369
            }
370
            this.taskStatus.terminate();
371
            this.taskStatus.remove();
372
        }
373
    }
374

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

    
385
    @Override
386
    public void setFinishAction(
387
            ExporttoServiceFinishAction exporttoServiceFinishAction) {
388
        this.exporttoServiceFinishAction = exporttoServiceFinishAction;
389
    }
390

    
391
}