Revision 28248 trunk/applications/appgvSIG/src/com/iver/cit/gvsig/TableOperations.java

View differences:

TableOperations.java
54 54
import com.iver.andami.messages.NotificationManager;
55 55
import com.iver.andami.plugins.Extension;
56 56
import com.iver.andami.ui.mdiManager.IWindow;
57
import com.iver.cit.gvsig.exceptions.visitors.StartWriterVisitorException;
58
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
59 57
import com.iver.cit.gvsig.fmap.edition.EditableAdapter;
60
import com.iver.cit.gvsig.fmap.edition.EditionEvent;
61 58
import com.iver.cit.gvsig.fmap.layers.FBitSet;
62 59
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
63 60
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
......
406 403
		}
407 404
		String tableName1 = sds.getName();
408 405
		// get target table and target field
406
		SelectableDataSource tds=null;
409 407
		try {
410
			sds = targetProjectTable.getModelo().getRecordset();
408
			tds = targetProjectTable.getModelo().getRecordset();
411 409
		} catch (ReadDriverException e) {
412 410
			e.printStackTrace();
413 411
		}
414
		String tableName2 = sds.getName();
412
		String tableName2 = tds.getName();
415 413
		// compute the join
416 414
		String sql =
417 415
			"custom com_iver_cit_gvsig_arcjoin tables '" +
......
437 435
			joinedTables[joinedTables.length-1] = targetProjectTable.getName();
438 436
			sourceProjectTable.setJoinTable(joinedTables);
439 437
			sourceProjectTable.replaceDataSource(auxea);
440
			renameFields(auxea, sourceProjectTable, sanitizeFieldName(prefix1),
441
					targetProjectTable, sanitizeFieldName(prefix2));
438
			renameFields(sourceProjectTable, sds, sanitizeFieldName(prefix1),
439
					tds, sanitizeFieldName(prefix2));
442 440
		} catch (ParseException e) {
443 441
			throw new RuntimeException(e);
444 442
		} catch (DriverLoadException e) {
......
454 452
					e);
455 453
		}
456 454
	}
455
//	
456
//	/**
457
//	 * Renames fields in a JOIN, so that fields from TABLE1
458
//	 * become sourcePrefix.field1, sourcePrefix.field2, etc, and fields
459
//	 * from TABLE2 become targetPrefix.field1, targetPrefix.field2, etc.
460
//	 * @param eds
461
//	 * @param pt1
462
//	 * @param pt1
463
//	 */
457 464
	
458 465
	/**
459
	 * Renames fields in a JOIN, so that fields from TABLE1
460
	 * become sourcePrefix.field1, sourcePrefix.field2, etc, and fields
461
	 * from TABLE2 become targetPrefix.field1, targetPrefix.field2, etc.
462
	 * @param eds
463
	 * @param pt1
464
	 * @param pt1
465
	 */	
466
	private void renameFields(EditableAdapter eds,
467
			ProjectTable pt1, String sourcePrefix, ProjectTable pt2, String targetPrefix) {
466
	 * <p>Renames fields in a JOIN, so that fields from DataSource1
467
	 * become sourcePrefix_field1, sourcePrefix_field2, etc, and fields
468
	 * from TABLE2 become targetPrefix_field1, targetPrefix_field2, etc.
469
	 * ProjectTable aliases are used to rename fields.</p>
470
	 * 
471
	 * @param targetPt The ProjectTable whose fields are to be renamed
472
	 * @param ds1 The first datasource in the join
473
	 * @param prefix1 The prefix to apply to fields in ds1
474
	 * @param ds2 The second datasource in the join
475
	 * @param prefix2 The prefix to apply to fields in ds2
476
	 */
477
	private void renameFields(ProjectTable targetPt, DataSource ds1, String prefix1,
478
			DataSource ds2, String prefix2) {
468 479
		try {
469
			SelectableDataSource sourceDS1 = pt1.getModelo().getRecordset();
470
			SelectableDataSource sourceDS2 = pt2.getModelo().getRecordset();
471
			String[] aliases = new String[sourceDS1.getFieldCount()];
472
			
473
			int table1NumFields = pt1.getModelo().getRecordset().getFieldCount() - pt2.getModelo().getRecordset().getFieldCount();
474
			for (int i=0; i<aliases.length; i++) {
475
				if (i<table1NumFields) {
476
					aliases[i] = sourcePrefix+"_"+sourceDS1.getFieldName(i);
477
				}
478
				else {
479
					aliases[i] = targetPrefix+"_"+sourceDS2.getFieldName(i-table1NumFields);
480
				}
480
			SelectableDataSource sds = targetPt.getModelo().getRecordset();
481
			String[] aliases = new String[sds.getFieldCount()];			
482
			int i=0;
483
			while (i<aliases.length && i<ds1.getFieldCount()){
484
				aliases[i] = prefix1+"_"+sds.getFieldName(i);
485
				i++;
481 486
			}
482
			pt1.setAliases(aliases);
487
			while (i<aliases.length) {
488
				aliases[i] = prefix2 + "_" + sds.getFieldName(i).substring("link_".length());
489
				i++;
490
			}
491
			targetPt.setAliases(aliases);
483 492
		} catch (ReadDriverException e) {
484 493
			//just log the error, it's not bad if fields couldn't be renamed
485 494
			PluginServices.getLogger().error("Error renaming fields in a JOIN", e);

Also available in: Unified diff