summaryrefslogtreecommitdiffstats
path: root/das-java/trunk/rdb/src/main/java/org/apache/tuscany/das/rdb/config/wrapper/MappingWrapper.java
blob: 6c010b3ee8ba892429990e96bf5dc8b749526bb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.tuscany.das.rdb.config.wrapper;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

import org.apache.log4j.Logger;
import org.apache.tuscany.das.rdb.config.Column;
import org.apache.tuscany.das.rdb.config.Command;
import org.apache.tuscany.das.rdb.config.Config;
import org.apache.tuscany.das.rdb.config.ConfigFactory;
import org.apache.tuscany.das.rdb.config.ConnectionInfo;
import org.apache.tuscany.das.rdb.config.ConnectionProperties;
import org.apache.tuscany.das.rdb.config.Create;
import org.apache.tuscany.das.rdb.config.Delete;
import org.apache.tuscany.das.rdb.config.KeyPair;
import org.apache.tuscany.das.rdb.config.Parameters;
import org.apache.tuscany.das.rdb.config.Relationship;
import org.apache.tuscany.das.rdb.config.Table;
import org.apache.tuscany.das.rdb.config.Update;

import commonj.sdo.Property;

public class MappingWrapper {

    private static final ConfigFactory FACTORY = ConfigFactory.INSTANCE;

    private final Logger logger = Logger.getLogger(MappingWrapper.class);

    private Config config;
    
	// TUSCANY-2288
	private List insertOrder;
	private List deleteOrder;
	private Set rootTableNames;
	// --


    public MappingWrapper() {
        config = FACTORY.createConfig();
        //JIRA-952
        this.checkSchemaNameSupport();
    }

	//JIRA-952 - check if there is any entry with no schemaName when support is ON
    public void checkSchemaNameSupport(){
        if(config.isDatabaseSchemaNameSupported()){
        	List tableList = config.getTable();
        	for(int i=0; i<tableList.size(); i++){
        		Table t = (Table)tableList.get(i);
        		if(t.getSchemaName() == null || t.getSchemaName().equals("")){
        			throw new RuntimeException("No schemaName provided for tableName "+t.getTableName()+" when schemaNameSupport is ON");
        		}
        		
        		if(t.getTypeName()==null){
        			t.setTypeName(t.getSchemaName()+"."+t.getTableName());
        		}
        	}
        }
    }
    public MappingWrapper(Config mapping) {
        if (mapping == null) {
            this.config = FACTORY.createConfig();
        } else {
            this.config = mapping;
        }
        //JIRA-952
        checkSchemaNameSupport();
    }

    public Config getConfig() {
        return this.config;
    }

    public Table getTable(String tableName) {

        if (this.logger.isDebugEnabled()) {
            this.logger.debug("Looking for table " + tableName);
        }

        Iterator i = config.getTable().iterator();
        while (i.hasNext()) {
            Table t = (Table) i.next();
            //JIRA-952
            if(this.config.isDatabaseSchemaNameSupported()){
            	if (tableName.equalsIgnoreCase(t.getSchemaName()+"."+t.getTableName())) {
                    return t;
                }
            }else{
            if (tableName.equalsIgnoreCase(t.getTableName())) {
                return t;
                }            	
            }
        }

        return null;
    }

    public Table getTableByTypeName(String typeName) {

        if (this.logger.isDebugEnabled()) {
            this.logger.debug("Looking for table by property: " + typeName);
        }

        Iterator i = config.getTable().iterator();
        while (i.hasNext()) {
            Table t = (Table) i.next();
            TableWrapper wrapper = new TableWrapper(t);
            if (typeName.equals(wrapper.getTypeName())) {
                return t;
            }
        }
        return null;

    }

    public void addImpliedRelationship(String parentTableName, String childTableName, String fkColumnName) {

        // Don't create a relationship for something like Book.Book_ID
        if (parentTableName.equalsIgnoreCase(childTableName)) {
            return;
        }

        // Don't create a relationship if one already exists in the config
        Iterator i = config.getRelationship().iterator();
        while (i.hasNext()) {
            Relationship r = (Relationship) i.next();
            if (r.getPrimaryKeyTable().equalsIgnoreCase(parentTableName) && r.getForeignKeyTable().equalsIgnoreCase(childTableName)) {
                return;
            }
        }

        Relationship r = FACTORY.createRelationship();
        r.setName(childTableName);
        r.setPrimaryKeyTable(parentTableName);
        r.setForeignKeyTable(childTableName);

        KeyPair pair = FACTORY.createKeyPair();
        pair.setPrimaryKeyColumn("ID");
        pair.setForeignKeyColumn(fkColumnName);

        r.getKeyPair().add(pair);
        r.setMany(true);

        config.getRelationship().add(r);
    }

    public Relationship addRelationship(String parentName, String childName) {
    	return addRelationship(parentName, childName, null); 
    }
    
    public Relationship addRelationship(String parentName, String childName, String relationshipName) {

    	//JIRA-952
        QualifiedColumn parent = null;
        QualifiedColumn child = null;

        if(this.config.isDatabaseSchemaNameSupported()){
        	parent = new QualifiedColumn(parentName, true);
        	child = new QualifiedColumn(childName, true);
        }
        else{
        	parent = new QualifiedColumn(parentName);
        	child = new QualifiedColumn(childName);        	
        }
        
        Relationship r = FACTORY.createRelationship();
        //JIRA-952
        if(this.config.isDatabaseSchemaNameSupported()){        	
        	if(relationshipName == null)
        		r.setName(child.getSchemaName()+"."+child.getTableName());
        	else
        		r.setName(relationshipName);
        	
            r.setPrimaryKeyTable(parent.getSchemaName()+"."+parent.getTableName());
            r.setForeignKeyTable(child.getSchemaName()+"."+child.getTableName());        	
        }
        else{
        	if(relationshipName == null)
        		r.setName(child.getTableName());
        	else
        		r.setName(relationshipName);
        	
	        r.setPrimaryKeyTable(parent.getTableName());
	        r.setForeignKeyTable(child.getTableName());
        }

        if (this.logger.isDebugEnabled()) {
            this.logger.debug("Created relationship from " + r.getPrimaryKeyTable() 
                    + " to " + r.getForeignKeyTable() + " named " + r.getName());
        }

        KeyPair pair = FACTORY.createKeyPair();
        pair.setPrimaryKeyColumn(parent.getColumnName());
        pair.setForeignKeyColumn(child.getColumnName());

        r.getKeyPair().add(pair);
        r.setMany(true);

        config.getRelationship().add(r);

        return r;

    }
    
    public Relationship addRelationship(Vector parentNames, Vector childNames) {
    	return addRelationship(parentNames, childNames, null);
    }
    
    public Relationship addRelationship(Vector parentNames, Vector childNames, String relationshipName) {
        //create vector for each
        if(parentNames.size() != childNames.size()){
            throw new RuntimeException("Can not for relationship for multiple keys, different sizes");
        }
        
        Vector parentColumns = new Vector();
        Vector childColumns = new Vector();
        
        for(int i=0; i<parentNames.size(); i++){
            QualifiedColumn parent = null;
            QualifiedColumn child = null;
            
            if(this.config.isDatabaseSchemaNameSupported()){
            	parent = new QualifiedColumn((String)parentNames.get(i), true);
            	child = new QualifiedColumn((String)childNames.get(i), true);
            }
            else{
            	parent = new QualifiedColumn((String)parentNames.get(i));
            	child = new QualifiedColumn((String)childNames.get(i));
            }
            
            parentColumns.add(parent);
            childColumns.add(child);
        }

        //all parent columns should be from same table and schema
        //all child columns should be from same table and schema
        checkTableNames(parentColumns);
    	checkSchemaNames(parentColumns);
        
        checkTableNames(childColumns);
    	checkSchemaNames(childColumns);
        
        Relationship r = FACTORY.createRelationship();
        //JIRA-952
        if(this.config.isDatabaseSchemaNameSupported()){
        	if(relationshipName == null)
        		r.setName(((QualifiedColumn)childColumns.get(0)).getSchemaName()+"."
        				+((QualifiedColumn)childColumns.get(0)).getTableName());
        	else
        		r.setName(relationshipName);
        	
            r.setPrimaryKeyTable(((QualifiedColumn)parentColumns.get(0)).getSchemaName()+"."
            		+((QualifiedColumn)parentColumns.get(0)).getTableName());
            r.setForeignKeyTable(((QualifiedColumn)childColumns.get(0)).getSchemaName()+"."
            		+((QualifiedColumn)childColumns.get(0)).getTableName());        	
        }
        else{
        	if(relationshipName == null)
        		r.setName(((QualifiedColumn)childColumns.get(0)).getTableName());
        	else
        		r.setName(relationshipName);
        	
	        r.setPrimaryKeyTable(((QualifiedColumn)parentColumns.get(0)).getTableName());
	        r.setForeignKeyTable(((QualifiedColumn)childColumns.get(0)).getTableName());
        }

        if (this.logger.isDebugEnabled()) {
            this.logger.debug("Created relationship from " + r.getPrimaryKeyTable() 
                    + " to " + r.getForeignKeyTable() + " named " + r.getName());
        }
        
        KeyPair pair = null;
        
        for(int i=0; i<parentColumns.size(); i++){
            pair = FACTORY.createKeyPair();
            pair.setPrimaryKeyColumn(((QualifiedColumn)parentColumns.get(i)).getColumnName());
            pair.setForeignKeyColumn(((QualifiedColumn)childColumns.get(i)).getColumnName());
            r.getKeyPair().add(pair);
        }
        
        r.setMany(true);
        config.getRelationship().add(r);

        return r;
    }    
    
    public void checkTableNames(Vector columns){
        String expectedTableName = ((QualifiedColumn)columns.get(0)).getTableName();        

        for(int i=0; i<columns.size(); i++){
            QualifiedColumn currColumn = (QualifiedColumn)columns.get(i);
            String currTableName = currColumn.getTableName();
            
            if(!currTableName.equals(expectedTableName)){
                throw new RuntimeException("Columns in one side of relationship can not be from different tables");
            }           
        }       
    }
    
    public void checkSchemaNames(Vector columns){
    	if(!this.config.isDatabaseSchemaNameSupported()){
    		return;
    	}
    	
    	 String	expectedSchemaName = ((QualifiedColumn)columns.get(0)).getSchemaName();

    	for(int i=0; i<columns.size(); i++){
    		QualifiedColumn currColumns = (QualifiedColumn)columns.get(i);
    		String	currSchemaName = currColumns.getSchemaName();
    		    		
    		if(!currSchemaName.equals(expectedSchemaName)){
    			throw new RuntimeException("Columns in one side of relationship can not be from different schema");
    		}
    	}
    }
    public void addPrimaryKey(String columnName) {
        addPrimaryKey(Collections.singletonList(columnName));
    }

    public void addPrimaryKey(String columnName, String propertyName) {
        addPrimaryKey(Collections.singletonList(columnName), propertyName);
    }
    
    public void addPrimaryKey(List columnNames) {

        Iterator i = columnNames.iterator();
        while (i.hasNext()) {
            String columnName = (String) i.next();

            QualifiedColumn pkColumn = null;
           	pkColumn = new QualifiedColumn(columnName, this.config.isDatabaseSchemaNameSupported());
            //Table t = findOrCreateTable(pkColumn.getTableName());
            Table t = findOrCreateTable(pkColumn);
            Column c = findOrCreateColumn(t, pkColumn.getColumnName());
            c.setPrimaryKey(true);
        }
    }

    public void addPrimaryKey(List columnNames, String propertyName) {

        Iterator i = columnNames.iterator();
        while (i.hasNext()) {
            String columnName = (String) i.next();

            QualifiedColumn pkColumn = null;
           	pkColumn = new QualifiedColumn(columnName, this.config.isDatabaseSchemaNameSupported());
            //Table t = findOrCreateTable(pkColumn.getTableName());
            Table t = findOrCreateTable(pkColumn);
            Column c = findOrCreateColumn(t, pkColumn.getColumnName());
            c.setPrimaryKey(true);
            c.setPropertyName(propertyName);
        }
    }
    
    public String getTableTypeName(String tableName) {
        Table t = getTable(tableName);
        if (t == null) {
            return tableName;
        }
        String propertyName = t.getTypeName();

        if (propertyName == null) {
            return tableName;
        }

        return propertyName;
    }

    public Column getColumn(Table t, String columnName) {
        if (t == null) {
            return null;
        }
        Iterator i = t.getColumn().iterator();
        while (i.hasNext()) {
            Column c = (Column) i.next();
            if (c.getColumnName().equals(columnName)) {
                return c;
            }
        }

        if (this.logger.isDebugEnabled()) {
            this.logger.debug("WARNING: Could not find column " + columnName + " in table " + t.getTableName());
        }

        return null;
    }

    public Column getColumnByPropertyName(Table t, String propertyName) {
        if (t == null) {
            return null;
        }
        
        Iterator i = t.getColumn().iterator();
        while (i.hasNext()) {
            Column c = (Column) i.next();
            
            if (c.getColumnName().equals(propertyName)) {
                return c;
            }
            
            if (c.getPropertyName() != null && c.getPropertyName().equals(propertyName)) {
                return c;
            }
        }

        if (this.logger.isDebugEnabled()) {
            this.logger.debug("WARNING: Could not find column " + propertyName + " in table " + t.getTableName());
        }

        return null;
    }

    public String getColumnPropertyName(String tableName, String columnName) {
        Table t = getTable(tableName);
        Column c = getColumn(t, columnName);
        if (c == null) {
            return columnName;
        }

        String propertyName = c.getPropertyName();
        if (propertyName == null) {
            return c.getColumnName();
        }

        return propertyName;
    }

    public Table addTable(String tableName, String typeName) {
        Table table = getTable(tableName);
        if (table != null) {
            throw new RuntimeException("Table " + tableName + "already exists");
        }

        table = ConfigFactory.INSTANCE.createTable();
        table.setTableName(tableName);
        table.setTypeName(typeName);
        config.getTable().add(table);

        return table;
    }

    //JIRA-952
    public Table addTable(String tableName, String schemaName, String typeName) {    	
        Table table = null;
        
        if(this.config.isDatabaseSchemaNameSupported()){
        	table = getTable(schemaName+"."+tableName);	
        }
        else{
        	table = getTable(tableName);
        }
        
        if (table != null) {
        	if(this.config.isDatabaseSchemaNameSupported()){
        		throw new RuntimeException("Table " + schemaName+"."+tableName + "already exists");	
        	}
        	else{
        		throw new RuntimeException("Table " + tableName + "already exists");
        	}            
        }

        table = ConfigFactory.INSTANCE.createTable();
        table.setTableName(tableName);
        
        if(this.config.isDatabaseSchemaNameSupported()){
        	table.setSchemaName(schemaName);
        }
        else{
        	table.setSchemaName("");
        }
        
        table.setTypeName(typeName);
        config.getTable().add(table);

        return table;
    }
    public Column addColumn(Table table, String name, String propertyName) {
        Column column = ConfigFactory.INSTANCE.createColumn();
        column.setColumnName(name);
        column.setPropertyName(propertyName);
        
        table.getColumn().add(column);
        return column;
    }
    
    //JIRA-952
    private Table findOrCreateTable(String schemaName, String tableName) {
        Table table = null;
        
        if(this.config.isDatabaseSchemaNameSupported()){
        	table = getTable(schemaName+"."+tableName);	
        }
        else{
        	table = getTable(tableName);	
        }        
        if (table == null) {
            table = ConfigFactory.INSTANCE.createTable();
            table.setTableName(tableName);
            table.setSchemaName(schemaName);
            
            if(this.config.isDatabaseSchemaNameSupported()){
            	table.setTypeName(schemaName+"."+tableName);
            }
            else{
            	table.setTypeName(tableName);
            }
            config.getTable().add(table);
        }
        return table;

    }

    //JIRA-952
    private Table findOrCreateTable(QualifiedColumn column) {   	
    	Table table = null;
    	if(this.config.isDatabaseSchemaNameSupported()){
    		table = getTable(column.getSchemaName()+"."+column.getTableName());
    	}
    	else{
    		table = getTable(column.getTableName());
    	}

        if (table == null) {
            table = ConfigFactory.INSTANCE.createTable();
            table.setTableName(column.getTableName());
            table.setSchemaName(column.getSchemaName());
            config.getTable().add(table);
        }
        return table;

    }
    private Column findOrCreateColumn(Table t, String name) {
        Iterator i = t.getColumn().iterator();
        while (i.hasNext()) {
            Column c = (Column) i.next();
            if (name.equals(c.getColumnName())) {
                return c;
            }
        }

        Column c = ConfigFactory.INSTANCE.createColumn();
        c.setColumnName(name);
        t.getColumn().add(c);
        return c;
    }

    public boolean hasRecursiveRelationships() {
        if (config != null) {
            Iterator i = getConfig().getRelationship().iterator();
            while (i.hasNext()) {
                Relationship r = (Relationship) i.next();
                if (r.getPrimaryKeyTable().equals(r.getForeignKeyTable())) {
                    return true;
                }
            }
        }
        return false;
    }
    
    public Set getRecursiveTypeNames() {
    	Set typeNames = new HashSet();
        if (config != null) {
            Iterator i = getConfig().getRelationship().iterator();
            while (i.hasNext()) {
                Relationship r = (Relationship) i.next();
                if (r.getPrimaryKeyTable().equals(r.getForeignKeyTable())) {
                	typeNames.add(getTableTypeName(r.getPrimaryKeyTable()));
                }
            }
        }
        return typeNames;
    	
    }

    /*Parameter name is SDO Type name and so requires mapping in table<->type, if one is available in Config*/
    public Collection getRelationshipsByChildTable(String name) {
    	Table table = getTableByTypeName(name);
    	
    	if(table != null) {
	    	if(config.isDatabaseSchemaNameSupported()) {
	    		name = table.getSchemaName()+"."+table.getTableName();	
	    	} else {
	    		name = table.getTableName();
	    	}
    	}
    	//table can be null, when no <Table> in Config and Query based Dynamic Types are used during query
    	
        List results = new ArrayList();
        if (config != null) {
            Iterator i = getConfig().getRelationship().iterator();
            while (i.hasNext()) {
                Relationship r = (Relationship) i.next();
                if (name.equals(r.getForeignKeyTable())) {
                    results.add(r);
                }
            }
        }
        return results;
    }

	public Set getRootTableNames() {
		if (this.logger.isDebugEnabled()) {
			this.logger.debug("Getting root tables");
		}
		if (rootTableNames == null) {
			rootTableNames = new HashSet();
			if (config == null) return rootTableNames;
			// parse all relationships
			Set allParents = new HashSet();
			Set allChildren = new HashSet();
			Iterator i = getConfig().getRelationship().iterator();
			while (i.hasNext()) {
				Relationship r = (Relationship) i.next();
				String parent = r.getPrimaryKeyTable();
				String child = r.getForeignKeyTable();
				if (parent.equals(child)) {
					// self-relationship
					// do not add to all children list to allow root detection
					allParents.add(parent);
				} else {
					allParents.add(parent);
					allChildren.add(child);
				}
			}
			// find roots (depth 0)
			// roots are tables that are present in the parents set, but not in the children set
			for (Iterator itParents = allParents.iterator(); itParents.hasNext(); ) {
				String parent = (String) itParents.next();
				if (!allChildren.contains(parent)) {
					rootTableNames.add(parent);
				}
			}
		}
		if (this.logger.isDebugEnabled()) {
			this.logger.debug(rootTableNames);
		}
		return rootTableNames;
	}
    
	// TUSCANY-2288
	public List getInsertOrder() {
		if (this.logger.isDebugEnabled()) {
			this.logger.debug("Getting insert order");
		}
		if (insertOrder == null) {
			insertOrder = new ArrayList();
			if (config == null) return insertOrder;
			// correct insert order: tables sorted by ascending depth
			// parse all relationships
			Set allParents = new HashSet();
			Set allChildren = new HashSet();
			Map parentToChild = new HashMap();
			Iterator i = getConfig().getRelationship().iterator();
			while (i.hasNext()) {
				Relationship r = (Relationship) i.next();
				String parent = r.getPrimaryKeyTable();
				String child = r.getForeignKeyTable();
				if (parent.equals(child)) {
					// self-relationship
					// do not add to parent to child map to avoid loops
					// do not add to all children list to allow root detection
					allParents.add(parent);
				} else {
					allParents.add(parent);
					allChildren.add(child);
					Set children = (Set) parentToChild.get(parent);
					if (children == null) {
						children = new HashSet();
						parentToChild.put(parent, children);
					}
					children.add(child);
				}
			}
			// build list of tables ordered by depth
			List depthList = new ArrayList();
			// find roots (depth 0)
			// roots are tables that are present in the parents set, but not in the children set
			Set roots = new HashSet();
			for (Iterator itParents = allParents.iterator(); itParents.hasNext(); ) {
				String parent = (String) itParents.next();
				if (!allChildren.contains(parent)) {
					roots.add(parent);
				}
			}
			// traverse table graph to populate depth list
			traverseTableGraph(roots, 0, parentToChild, depthList, new ArrayList());
			// build insert order from depth list
			for (Iterator itDepths = depthList.iterator(); itDepths.hasNext(); ) {
				Set tables = (Set) itDepths.next();
				insertOrder.addAll(tables);
			}
		}
		if (this.logger.isDebugEnabled()) {
			this.logger.debug(insertOrder);
		}
		return insertOrder;
	}

	private void traverseTableGraph(Set parents, int parentDepth, Map parentToChild, List depthList, List branch) {
		int childDepth = parentDepth + 1;
		// expand depth list if necessary
		for (int i = depthList.size() - 1; i < parentDepth; i++) {
			Set tables = new HashSet();
			depthList.add(tables);
		}
		// loop thru parents
		for (Iterator itParents = parents.iterator(); itParents.hasNext(); ) {
			String parent = (String) itParents.next();
			if (branch.contains(parent)) {
				// we found a cycle
				// we don't handle cycles
				// stop traversing branch to avoid infinite loop
				break;
			}
			// add parent to depth list
			addTableToDepthList(parent, parentDepth, depthList);
			// make recursive call on children
			Set children = (Set) parentToChild.get(parent);
			if (children != null && children.size() > 0) {
				List parentBranch = new ArrayList();
				parentBranch.addAll(branch);
				parentBranch.add(parent);
				traverseTableGraph(children, childDepth, parentToChild, depthList, parentBranch);
			}
		}
	}

	private void addTableToDepthList(String table, int depth, List depthList) {
		// in this method, keep in mind that the same table can appear multiple times at multiple depths inside the table graph
		// check if table is already somewhere in depth list
		int currentDepth = getTableDepth(table, depthList);
		if (currentDepth == -1) {
			// table not in depth list
			// add it
			Set tables = (Set) depthList.get(depth);
			tables.add(table);
		} else if (currentDepth < depth) {
			// table already in depth list, at a lower depth
			// the table should only be present once, at the greatest possible depth
			// replace table at the new depth
			Set tables = (Set) depthList.get(currentDepth);
			tables.remove(table);
			tables = (Set) depthList.get(depth);
			tables.add(table);
		} else {
			// table already in depth list, at a greater or same depth
			// nothing to do, since the table should only be present once, at the greatest possible depth
		}
	}

	private int getTableDepth(String table, List depthList) {
		int depth = -1;
		// loop thru depth list
		int count = depthList.size();
		for (int i = 0; i < count; i++) {
			Set tables = (Set) depthList.get(i);
			if (tables != null && tables.contains(table)) {
				depth = i;
				break;
			}
		}
		return depth;
	}


	public List getDeleteOrder() {
		if (deleteOrder == null) {
			deleteOrder = new ArrayList();
			deleteOrder.addAll(getInsertOrder());
			Collections.reverse(deleteOrder);
		}
		return deleteOrder;
	}

	// --


    //JIRA-952
    public void addConverter(String name, String converter) {
        QualifiedColumn column = new QualifiedColumn(name, this.config.isDatabaseSchemaNameSupported());
        Table t = null;
       	t = findOrCreateTable(column);
        Column c = findOrCreateColumn(t, column.getColumnName());
        c.setConverterClassName(converter);
    }

    public String getConverter(String tableName, String columnName) {
        Table t = getTable(tableName);
        Column c = getColumn(t, columnName);
        if (c != null) {
            return c.getConverterClassName();
        }
        return null;
    }

    public Map getConverters(Table table) {
        Map converters = new HashMap();

        Iterator columns = table.getColumn().iterator();
        while (columns.hasNext()) {
            Column c = (Column) columns.next();
            if (c.getConverterClassName() != null) {
                String property = c.getPropertyName();
                if (property == null) {
                    property = c.getColumnName();
                }
                converters.put(property, c.getConverterClassName());
            }
        }
        return converters;
    }

    public Relationship getRelationshipByReference(Property ref) {
        Iterator i = config.getRelationship().iterator();
        while (i.hasNext()) {
            Relationship r = (Relationship) i.next();
            if (ref.getName().equals(r.getName())) {
            	if (getTableTypeName(r.getPrimaryKeyTable()).equals(ref.getContainingType().getName())) return r;
            } else if(ref.getOpposite().getName().equals(r.getName())) {
            	if (getTableTypeName(r.getForeignKeyTable()).equals(ref.getContainingType().getName())) return r;
            }
        }
        throw new RuntimeException("Could not find relationship " + ref.getName() + " in the configuration");
    }

    public Relationship getRelationshipByName(String name) {
        Iterator i = config.getRelationship().iterator();
        while (i.hasNext()) {
            Relationship r = (Relationship) i.next();
            if (name.equals(r.getName())) {
                return r;
            }
        }
        throw new RuntimeException("Could not find relationship " + name + " in the configuration");
    }

    public void addUpdateStatement(Table table, String statement, Parameters parameters) {

        Update update = ConfigFactory.INSTANCE.createUpdate();
        update.setSql(statement);
        update.setParameters(parameters);
        table.setUpdate(update);

    }

    public void addDeleteStatement(Table table, String statement, Parameters parameters) {

        Delete delete = ConfigFactory.INSTANCE.createDelete();
        delete.setSql(statement);
        delete.setParameters(parameters);
        table.setDelete(delete);

    }

    public void addCreateStatement(Table table, String statement, Parameters parameters) {

        Create create = ConfigFactory.INSTANCE.createCreate();
        create.setSql(statement);
        create.setParameters(parameters);
        table.setCreate(create);

    }

    //JIRA-948 support for driver manager connection
    public void addConnectionInfo(String dataSourceName, boolean managedtx){
        ConnectionInfo info = ConfigFactory.INSTANCE.createConnectionInfo();
        info.setDataSource(dataSourceName);
        info.setManagedtx(managedtx);

        config.setConnectionInfo(info);
    }
    
    public void addConnectionInfo(String driverClass, String connectionURL, String user, String password, int loginTimeout) {
        ConnectionInfo info = ConfigFactory.INSTANCE.createConnectionInfo();
        
        ConnectionProperties connectionProperties = ConfigFactory.INSTANCE.createConnectionProperties(); 
        connectionProperties.setDriverClass(driverClass);
        connectionProperties.setDatabaseURL(connectionURL);
        connectionProperties.setUserName(user);
        connectionProperties.setPassword(password);
        connectionProperties.setLoginTimeout(loginTimeout);            

        info.setConnectionProperties(connectionProperties);
        config.setConnectionInfo(info);
    }
    //JIRA-948 end

    public Command addCommand(String name, String sql, String kind) {
        Command cmd = ConfigFactory.INSTANCE.createCommand();
        cmd.setName(name);
        cmd.setKind(kind);
        cmd.setSQL(sql);

        config.getCommand().add(cmd);

        return cmd;
    }

    //JIRA-952
    public void addImpliedPrimaryKey(String schemaName, String tableName, String columnName) {
        Table t = findOrCreateTable(schemaName, tableName);//JIRA-952

        Iterator i = t.getColumn().iterator();
        boolean hasPK = false;
        while (i.hasNext()) {
            Column c = (Column) i.next();
            if (c.isPrimaryKey()) {
                hasPK = true;
            }
        }

        if (!hasPK) {
            Column c = findOrCreateColumn(t, columnName);
            c.setPrimaryKey(true);
        }

    }

}