Revision 41681

View differences:

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
77 77

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

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

  
91 91
        FeatureType targetFeatureType;
......
93 93

  
94 94
        targetFeatureType = options.getSource().getDefaultFeatureType().getCopy();
95 95
        targetEditableFeatureType = targetFeatureType.getEditable();
96
        
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);
96

  
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 101
                x.setName(getTranslatedIdentifier(x.getName()));
102 102
            }
103 103
        }
104
        
104

  
105 105
        if (this.options.getPrimaryKey() != null) {
106 106
            EditableFeatureAttributeDescriptor pk = (EditableFeatureAttributeDescriptor) targetEditableFeatureType.get(getTranslatedIdentifier(options.getPrimaryKey()));
107 107
            if (pk == null) {
108 108
                pk = targetEditableFeatureType.add(
109
                        getTranslatedIdentifier(this.options.getPrimaryKey()), 
109
                        getTranslatedIdentifier(this.options.getPrimaryKey()),
110 110
                        DataTypes.LONG
111 111
                );
112 112
                pk.setIsPrimaryKey(true);
......
116 116
            }
117 117
        }
118 118

  
119
        if( this.options.getCreateIndexInGeometryRow() ) {
120
            EditableFeatureAttributeDescriptor x = (EditableFeatureAttributeDescriptor)targetEditableFeatureType.getDefaultGeometryAttribute();
119
        if (this.options.getCreateIndexInGeometryRow()) {
120
            EditableFeatureAttributeDescriptor x = (EditableFeatureAttributeDescriptor) targetEditableFeatureType.getDefaultGeometryAttribute();
121 121
            x.setIsIndexed(true);
122 122
        }
123
        
123

  
124 124
        // ======================================================
125 125
        // Reprojection: set SRS of geometry field to target SRS
126 126
        EditableFeatureAttributeDescriptor attrdescriptor
......
140 140
        createTableParams.setReferenceRole(this.options.getReferenceRole());
141 141
        createTableParams.setTriggerRole(this.options.getTriggerRole());
142 142
        createTableParams.setAllRole(this.options.getAllRole());
143
        
143

  
144 144
        createTableParams.setSchema(this.options.getSchema());
145 145
        createTableParams.setPostCreatingStatement(this.options.getPostCreatingStatement());
146
        
146

  
147 147
        createTableParams.setDefaultFeatureType(targetEditableFeatureType);
148
        createTableParams.setTable(this.options.getTableName()); 
148
        createTableParams.setTable(this.options.getTableName());
149 149
        explorer.add(explorer.getStoreName(), createTableParams, true);
150 150
    }
151 151

  
......
190 190
                    this.options.getExplorerParameters()
191 191
            );
192 192

  
193
            if (this.options.canCreatetable() ) {
193
            if (this.options.canCreatetable()) {
194 194
                taskStatus.message("Creating table");
195 195
                this.createTable(explorer);
196 196
            }
......
206 206

  
207 207
            FeatureType targetFeatureType = target.getDefaultFeatureType();
208 208
            FeatureType sourceFeatureType = featureSet.getDefaultFeatureType();
209
            
209

  
210 210
            target.edit(FeatureStore.MODE_APPEND);
211 211

  
212 212
            int featureCount = 1;
213 213
            taskStatus.setRangeOfValues(0, featureSet.getSize());
214 214

  
215
            int targetGeometryIndex = -1;
216
            int sourceGeometryIndex = -1;
217
            if( getGeometryColumnCount(sourceFeatureType)==1 
218
                    && getGeometryColumnCount(targetFeatureType)==1 ) {
219
                // Solo si hay una columna de geometria asignaremos las geometrias 
220
                // independientemente de como se llamen los campos.
221
                targetGeometryIndex = targetFeatureType.getDefaultGeometryAttributeIndex();
222
                sourceGeometryIndex = sourceFeatureType.getDefaultGeometryAttributeIndex();
223
            }
224
            
215 225
            taskStatus.message("Inserting rows");
216 226
            it = featureSet.fastIterator();
217 227
            while (it.hasNext()) {
218 228
                Feature feature = (Feature) it.next();
229
                this.taskStatus.setCurValue(featureCount);
230

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

  
225 249
                Geometry geometry = targetFeature.getDefaultGeometry();
......
246 270
                            geometryCheck = geometry.getValidationStatus();
247 271
                            if (!geometryCheck.isValid()) {
248 272
                                Geometry g = null;
249
                                if (this.options.getTryToFixGeometry() ) {
273
                                if (this.options.getTryToFixGeometry()) {
250 274
                                    g = geometry.makeValid();
251 275
                                    if (g != null) {
252 276
                                        targetFeature.setDefaultGeometry(g);
......
283 307
                }
284 308

  
285 309
                target.insert(targetFeature);
286
                this.taskStatus.setCurValue(featureCount);
287 310

  
288 311
                if (this.taskStatus.isCancellationRequested()) {
289 312
                    return;
......
292 315
            }
293 316
            targetFeature = null;
294 317
            target.finishEditing();
295
            
296
            if( this.options.getUpdateTableStatistics() ) {
318

  
319
            if (this.options.getUpdateTableStatistics()) {
297 320
                taskStatus.message("Updating statistics");
298 321
                explorer.updateTableStatistics(openParams.tableID());
299 322
            }
......
304 327
                        openParams);
305 328
            }
306 329
            taskStatus.message("Exportation finished");
307
            
330

  
308 331
        } catch (Exception e) {
309 332
            logger.warn("Can't export data.", e);
310 333
            taskStatus.message("Problems exporting data");
......
322 345
            this.taskStatus.remove();
323 346
        }
324 347
    }
348
    
349
    private int getGeometryColumnCount(FeatureType featureType) {
350
        int count = 0;
351
        for( int i=0; i<featureType.size(); i++ ) {
352
            if( featureType.getAttributeDescriptor(i).getType()==DataTypes.GEOMETRY ) {
353
                count++;
354
            }
355
        }
356
        return count;
357
    }
325 358

  
326 359
    public void setFinishAction(
327 360
            ExporttoServiceFinishAction exporttoServiceFinishAction) {

Also available in: Unified diff