Revision 40602

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.shape/src/main/java/org/gvsig/exportto/swing/prov/shape/ExporttoShapeService.java
25 25

  
26 26
import java.io.File;
27 27
import java.util.ArrayList;
28
import java.util.Collection;
29
import java.util.HashMap;
30
import java.util.Iterator;
28 31
import java.util.List;
32
import java.util.Map;
29 33

  
34
import javax.swing.JOptionPane;
35

  
30 36
import org.cresques.cts.ICoordTrans;
31 37
import org.cresques.cts.IProjection;
32 38
import org.slf4j.Logger;
......
35 41
import org.gvsig.exportto.ExporttoService;
36 42
import org.gvsig.exportto.ExporttoServiceException;
37 43
import org.gvsig.exportto.ExporttoServiceFinishAction;
44
import org.gvsig.exportto.swing.prov.file.panel.SelectFileOptionPanel;
38 45
import org.gvsig.fmap.dal.DALLocator;
39 46
import org.gvsig.fmap.dal.DataManager;
40 47
import org.gvsig.fmap.dal.exception.DataException;
......
64 71
import org.gvsig.fmap.geom.primitive.Point;
65 72
import org.gvsig.fmap.geom.primitive.Surface;
66 73
import org.gvsig.fmap.geom.type.GeometryType;
74
import org.gvsig.i18n.Messages;
67 75
import org.gvsig.tools.dispose.DisposableIterator;
68 76
import org.gvsig.tools.dispose.DisposeUtils;
69 77
import org.gvsig.tools.task.AbstractMonitorableTask;
......
78 86

  
79 87
    private static Logger logger = LoggerFactory.getLogger(ExporttoShapeService.class);
80 88
    
89
    public static final int MAX_FIELD_NAME_LENGTH = 11;
90
    
81 91
    private File theShapeFile;
82 92
    private IProjection projection;
83 93
    private FeatureStore featureStore;
94
    private Map<String, String> origNameToDbfName;
84 95

  
85 96
    private int geometryType = -1;
86 97
    private NewFeatureStoreParameters newFeatureStoreParameters;
87 98
    private FilesystemServerExplorer filesystemServerExplorer;
99
    private SelectFileOptionPanel filePanel = null;
88 100
    
89 101
    private static GeometryManager geoManager = GeometryLocator.getGeometryManager();
90 102

  
91 103
    private ExporttoServiceFinishAction exporttoServiceFinishAction;
92 104

  
93
    public ExporttoShapeService(File shapeFile, FeatureStore featureStore,
94
        IProjection projection) {
105
    public ExporttoShapeService(
106
    		SelectFileOptionPanel fPanel,
107
    		FeatureStore featureStore,
108
    		IProjection projection) {
109
    	
95 110
        super("Export to shape");
96 111
        this.featureStore = featureStore;
97
        this.theShapeFile = shapeFile;
112
        this.filePanel = fPanel;
113
        this.theShapeFile = fPanel.getSelectedFile();
98 114
        this.projection = projection;
115
        
116
        try {
117
			origNameToDbfName = getNames(featureStore.getDefaultFeatureType());
118
		} catch (DataException e) {
119
			logger.error("While getting def feat type.", e);
120
		}
99 121
    }
100 122

  
101
    public void export(FeatureSet featureSet) throws ExporttoServiceException {
123
    private Map<String, String> getNames(FeatureType ft) {
124
    	
125
    	Map<String, String> resp = new HashMap<String, String>();
126
    	FeatureAttributeDescriptor[] atts = ft.getAttributeDescriptors();
127
    	for (int i=0; i<atts.length; i++) {
128
    		if (atts[i].getName().length() > MAX_FIELD_NAME_LENGTH) {
129
    			String new_name = getNewName(atts[i].getName(), resp.values());
130
    			resp.put(atts[i].getName(), new_name);
131
    		}
132
    	}
133
		return resp;
134
	}
102 135

  
136
	private String getNewName(String name, Collection<String> values) {
137
		
138
		int len = name.length(); 
139
		if (len <= MAX_FIELD_NAME_LENGTH) {
140
			/*
141
			 * Should not happen
142
			 */
143
			return name;
144
		}
145
		/*
146
		 * Composes a new name with start and end of old name and an index
147
		 * THISISAVERYLONGNAME => THISI_AME_1, THISI_AME_2 ... THISI_AME_9
148
		 */
149
		String pref = name.substring(0, 6) + "_" + name.substring(len - 2) + "_";
150
		String resp = null;
151
		for (int i=0; i<10; i++) {
152
			resp = pref + i;
153
			if (!values.contains(resp)) {
154
				return resp;
155
			}
156
		}
157
		/*
158
		 * Very strange to get here...
159
		 * THISISAVERYLONGNAME => THISISA_1, THISISA_2 ... THISISA_999
160
		 */
161
		pref = name.substring(0, 7) + "_";
162
		for (int i=0; i<1000; i++) {
163
			resp = pref + i;
164
			if (!values.contains(resp)) {
165
				return resp;
166
			}
167
		}
168
		/*
169
		 * Should not get here
170
		 */
171
		return name.substring(0, 4) + "_" + (System.currentTimeMillis() % 1000000);
172
	}
173

  
174
	public void export(FeatureSet featureSet) throws ExporttoServiceException {
175

  
103 176
        ExporttoServiceException throw_exp = null;
104 177
        File item_shp = null;
105 178
        String pathFile = theShapeFile.getAbsolutePath();
......
181 254
        
182 255
        if (throw_exp != null) {
183 256
            throw throw_exp;
257
        } else {
258
        	if (origNameToDbfName.size() > 0) {
259
        		showNewNamesDialog(origNameToDbfName);
260
        	}
184 261
        }
185 262
    }
186 263

  
187
    private void initializeParams(
264
    private void showNewNamesDialog(Map<String, String> map) {
265
    	
266
    	Iterator<String> iter = map.keySet().iterator();
267
    	
268
    	String msg = Messages.getText("_Some_field_names_too_long_automatically_changed");
269
    	msg = msg + ":\n";
270
    	String k, v;
271
    	while (iter.hasNext()) {
272
    		k = iter.next();
273
    		v = map.get(k);
274
    		msg = msg + "\n" + k + " > " + v;
275
    	}
276
    	
277
    	JOptionPane.showMessageDialog(
278
    			this.filePanel,
279
    			msg,
280
    			Messages.getText("export_progress"),
281
    			JOptionPane.WARNING_MESSAGE);
282
	}
283

  
284
	private void initializeParams(
188 285
        FeatureSet featureSet, File out_shp_file)
189 286
        throws ExporttoServiceException {
190 287

  
......
233 330
    private void export(FilesystemServerExplorer explorer,
234 331
        NewFeatureStoreParameters params, FeatureSet featureSet,
235 332
        int geometryType, boolean checkType) throws ExporttoServiceException {
236

  
333
    	
237 334
        String providerName = params.getDataStoreName();
238 335
        String explorerName = explorer.getProviderName();
239 336
        boolean there_was_error = false;
......
241 338
        DisposableIterator it = null;
242 339
        try {
243 340
            EditableFeatureType type =
244
                featureStore.getDefaultFeatureType().getEditable();
341
                featureStore.getDefaultFeatureType().getCopy().getEditable();
245 342
            FeatureAttributeDescriptor fad =
246 343
                (FeatureAttributeDescriptor) type.get(type
247 344
                    .getDefaultGeometryAttributeName());
......
262 359
            
263 360
            efad.setPrecision(fad.getPrecision());
264 361
            type.setDefaultGeometryAttributeName(fad.getName());
362
            
363
            // ==========================
364
            fixNames(type);
365
            // ==========================
366
            
265 367
            params.setDefaultFeatureType(type);
266 368
            
267 369
            params.setDynValue("geometryType", null);
......
336 438
                    gitem = force2D(gitem, geometryType);
337 439
                    edit_feat = target.createNewFeature(true);
338 440
                    there_was_error = there_was_error |
339
                        // accumulate error boolean
340
                        setNonNulls(targetType, feature, edit_feat);
441
                    		/*
442
                    		 * Accumulate error in boolean.
443
                    		 * This also fixes field names (using origNameToDbfName)
444
                    		 */
445
                    		setNonNulls(featureStore.getDefaultFeatureType(),
446
                    				targetType, feature, edit_feat);
341 447
                    edit_feat.setDefaultGeometry(gitem);
342 448
                    // ================================================
343 449
                    // Reprojection stuff
......
374 480
    }
375 481

  
376 482
    
377
    /**
483
    private void fixNames(EditableFeatureType eft) {
484
    	
485
    	FeatureAttributeDescriptor[] atts = eft.getAttributeDescriptors();
486
    	EditableFeatureAttributeDescriptor efad = null;
487
    	for (int i=0; i<atts.length; i++) {
488
    		String new_name = origNameToDbfName.get(atts[i].getName()); 
489
    		if (new_name != null) {
490
    			eft.remove(atts[i].getName());
491
    			efad = eft.add(new_name, atts[i].getType(), atts[i].getSize());
492
    			efad.setPrecision(atts[i].getPrecision());
493
    		}
494
    	}
495
	}
496

  
497
	/**
378 498
     * @param gitem
379 499
     * @param geometryType2
380 500
     * @return
......
405 525
     * @param edit_feat
406 526
     */
407 527
    private boolean setNonNulls(
408
        FeatureType ft,
528
        FeatureType src_ft, FeatureType target_ft,
409 529
        Feature feat, EditableFeature edit_f) {
410 530
        
411 531
        boolean error = false;
412
        FeatureAttributeDescriptor[] atts = ft.getAttributeDescriptors();
532
        FeatureAttributeDescriptor[] atts = src_ft.getAttributeDescriptors();
533
        String orig_name = null;
534
        String dbf_name = null;
413 535
        for (int i=0; i<atts.length; i++) {
414 536
            if (atts[i].getType() != org.gvsig.fmap.geom.DataTypes.GEOMETRY) {
415 537
                
416 538
                Object val = null;
417 539
                
418 540
                try {
419
                    val = feat.get(atts[i].getName());
541
                	orig_name = atts[i].getName();
542
                	dbf_name = origNameToDbfName.get(orig_name);
543
                	if (dbf_name == null) {
544
                		dbf_name = orig_name; 
545
                	}
546
                    val = feat.get(orig_name);
420 547
                    if (val != null) {
421
                        edit_f.set(
422
                            atts[i].getName(),
423
                            feat.get(atts[i].getName()));
548
                        edit_f.set(dbf_name, val);
424 549
                    }
425 550
                } catch (Exception ex) {
426 551
                    logger.info("Error while getting/setting value", ex);
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.shape/src/main/java/org/gvsig/exportto/swing/prov/shape/ExporttoShapeProvider.java
55 55

  
56 56
    public ExporttoService createExporttoService() {
57 57
        return new ExporttoShapeService(
58
            selectFileOptionPanel.getSelectedFile(), featureStore, projection);
58
            selectFileOptionPanel, featureStore, projection);
59 59
    }
60 60
}
trunk/org.gvsig.desktop/org.gvsig.desktop.plugin/org.gvsig.exportto.app/org.gvsig.exportto.app.mainplugin/src/main/resources-plugin/i18n/text.properties
1 1
add_table_to_project=Desea a?adir la tabla al proyecto?
2 2
add_table=A?adir tabla
3 3
exportto_preferences=Exportar a
4
exportto_preferences_panel_description=Marque los formatos de exportaci?n a que quiera activar o desactivar
4
exportto_preferences_panel_description=Marque los formatos de exportaci?n a que quiera activar o desactivar
5
_Some_field_names_too_long_automatically_changed=Los nombre de algunos campos eran demasiado largos y se han cambiado
trunk/org.gvsig.desktop/org.gvsig.desktop.plugin/org.gvsig.exportto.app/org.gvsig.exportto.app.mainplugin/src/main/resources-plugin/i18n/text_en.properties
1 1
add_table_to_project=Add the table to the project?
2 2
add_table=Add table
3 3
exportto_preferences=Export to
4
exportto_preferences_panel_description=Enable or disable "export to" formats
4
exportto_preferences_panel_description=Enable or disable "export to" formats
5
_Some_field_names_too_long_automatically_changed=Some field names were too long and were automatically changed

Also available in: Unified diff