1 19 20 package org.netbeans.modules.j2ee.persistence.wizard.fromdb; 21 22 import java.util.Collections ; 23 import java.util.HashMap ; 24 import java.util.HashSet ; 25 import java.util.Map ; 26 import java.util.Set ; 27 import org.netbeans.modules.dbschema.ForeignKeyElement; 28 import org.netbeans.modules.dbschema.TableElement; 29 import org.netbeans.modules.j2ee.persistence.wizard.fromdb.Table.DisabledReason; 30 31 35 public class TableProviderImpl implements TableProvider { 36 37 private final Set <Table> tables; 38 private final Map <String , Table> name2Table = new HashMap <String , Table>(); 39 40 public TableProviderImpl(Map <String , Set <String >> tablesAndRefs) { 41 this(tablesAndRefs, emptyDisabledReasonMap()); 42 } 43 44 public TableProviderImpl(Map <String , Set <String >> tablesAndRefs, Map <String , DisabledReason> disabledReasons) { 45 Map <String , TableImpl> name2Table = new HashMap <String , TableImpl>(); 46 Map <String , Set <Table>> name2Referenced = new HashMap <String , Set <Table>>(); 47 Map <String , Set <Table>> name2ReferencedBy = new HashMap <String , Set <Table>>(); 48 Map <String , Set <Table>> name2Join = new HashMap <String , Set <Table>>(); 49 50 for (String tableName : tablesAndRefs.keySet()) { 52 DisabledReason disabledReason = disabledReasons.get(tableName); 53 boolean join = tableName.contains("_"); 54 TableImpl table = new TableImpl(tableName, join, disabledReason); 55 56 name2Table.put(tableName, table); 57 name2Referenced.put(tableName, new HashSet <Table>()); 58 name2ReferencedBy.put(tableName, new HashSet <Table>()); 59 name2Join.put(tableName, new HashSet <Table>()); 60 } 61 62 for (String tableName : tablesAndRefs.keySet()) { 64 Table table = name2Table.get(tableName); 65 66 for (String referencedTableName : tablesAndRefs.get(tableName)) { 67 Table referencedTable = name2Table.get(referencedTableName); 68 69 name2Referenced.get(tableName).add(referencedTable); 70 name2ReferencedBy.get(referencedTableName).add(table); 71 72 if (table.isJoin()) { 73 name2Join.get(referencedTableName).add(table); 74 } 75 } 76 } 77 78 Set <Table> tmpTables = new HashSet <Table>(); 79 for (TableImpl table : name2Table.values()) { 80 String tableName = table.getName(); 81 82 table.setReferencedTables(Collections.unmodifiableSet(name2Referenced.get(tableName))); 83 table.setReferencedByTables(Collections.unmodifiableSet(name2ReferencedBy.get(tableName))); 84 table.setJoinTables(Collections.unmodifiableSet(name2Join.get(tableName))); 85 86 tmpTables.add(table); 87 this.name2Table.put(table.getName(), table); 88 } 89 tables = Collections.unmodifiableSet(tmpTables); 90 } 91 92 public Set <Table> getTables() { 93 return tables; 94 } 95 96 Table getTableByName(String name) { 97 return name2Table.get(name); 98 } 99 100 private static Map <String , DisabledReason> emptyDisabledReasonMap() { 101 return Collections.emptyMap(); 102 } 103 104 private static final class TableImpl extends Table { 105 106 private Set <Table> referencedTables; 107 private Set <Table> referencedByTables; 108 private Set <Table> joinTables; 109 110 public TableImpl(String name, boolean join, DisabledReason disabledReason) { 111 super(name, join, disabledReason); 112 } 113 114 private void setReferencedTables(Set <Table> referencedTables) { 115 this.referencedTables = referencedTables; 116 } 117 118 public Set <Table> getReferencedTables() { 119 return referencedTables; 120 } 121 122 private void setReferencedByTables(Set <Table> referencedByTables) { 123 this.referencedByTables = referencedByTables; 124 } 125 126 public Set <Table> getReferencedByTables() { 127 return referencedByTables; 128 } 129 130 private void setJoinTables(Set <Table> joinTables) { 131 this.joinTables = joinTables; 132 } 133 134 public Set <Table> getJoinTables() { 135 return joinTables; 136 } 137 } 138 } 139 | Popular Tags |