1 19 20 package org.netbeans.modules.j2ee.persistence.wizard.fromdb; 21 22 import java.util.ArrayList ; 23 import java.util.Collections ; 24 import java.util.HashMap ; 25 import java.util.HashSet ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.Map ; 29 import java.util.Set ; 30 import org.netbeans.modules.dbschema.ForeignKeyElement; 31 import org.netbeans.modules.dbschema.SchemaElement; 32 import org.netbeans.modules.dbschema.TableElement; 33 import org.netbeans.modules.j2ee.persistence.entitygenerator.DbSchemaEjbGenerator; 34 import org.netbeans.modules.j2ee.persistence.entitygenerator.EntityMember; 35 import org.netbeans.modules.j2ee.persistence.wizard.fromdb.Table.DisabledReason; 36 import org.netbeans.modules.j2ee.persistence.wizard.fromdb.Table.ExistingDisabledReason; 37 import org.netbeans.modules.j2ee.persistence.wizard.fromdb.Table.NoPrimaryKeyDisabledReason; 38 39 44 public class DBSchemaTableProvider implements TableProvider { 45 46 private final SchemaElement schemaElement; 47 private final PersistenceGenerator persistenceGen; 48 private final Set <Table> tables; 49 50 public DBSchemaTableProvider(SchemaElement schemaElement, PersistenceGenerator persistenceGen) { 51 this.schemaElement = schemaElement; 52 this.persistenceGen = persistenceGen; 53 54 tables = buildTables(); 55 } 56 57 public Set <Table> getTables() { 58 return tables; 59 } 60 61 private Set <Table> buildTables() { 62 Map <String , DBSchemaTable> name2Table = new HashMap <String , DBSchemaTable>(); 63 Map <String , Set <Table>> name2Referenced = new HashMap <String , Set <Table>>(); 64 Map <String , Set <Table>> name2ReferencedBy = new HashMap <String , Set <Table>>(); 65 Map <String , Set <Table>> name2Join = new HashMap <String , Set <Table>>(); 66 67 TableElement[] tableElements = schemaElement.getTables(); 69 for (TableElement tableElement : tableElements) { 70 if (tableElement.isTable()) { 71 boolean join = DbSchemaEjbGenerator.isJoinTable(tableElement); 72 73 List <DisabledReason> disabledReasons = getDisabledReasons(tableElement, persistenceGen); 74 DisabledReason disabledReason = null; 75 for (DisabledReason reason : disabledReasons) { 77 if (!(join && reason instanceof NoPrimaryKeyDisabledReason)) { 80 disabledReason = reason; 81 break; 82 } 83 } 84 85 String tableName = tableElement.getName().getName(); 86 DBSchemaTable table = new DBSchemaTable(tableName, join, disabledReason, persistenceGen); 87 88 name2Table.put(tableName, table); 89 name2Referenced.put(tableName, new HashSet <Table>()); 90 name2ReferencedBy.put(tableName, new HashSet <Table>()); 91 name2Join.put(tableName, new HashSet <Table>()); 92 } 93 } 94 95 for (TableElement tableElement : tableElements) { 97 if (!tableElement.isTable()) { 98 continue; 99 } 100 101 String tableName = tableElement.getName().getName(); 102 Table table = name2Table.get(tableName); 103 104 ForeignKeyElement[] foreignKeyElements = tableElement.getForeignKeys(); 105 if (foreignKeyElements != null) { 106 for (ForeignKeyElement foreignKeyElement : foreignKeyElements) { 107 TableElement referencedTableElement = foreignKeyElement.getReferencedTable(); 108 String referencedTableName = referencedTableElement.getName().getName(); 109 Table referencedTable = name2Table.get(referencedTableName); 110 111 name2Referenced.get(tableName).add(referencedTable); 112 name2ReferencedBy.get(referencedTableName).add(table); 113 114 if (table.isJoin()) { 115 name2Join.get(referencedTableName).add(table); 116 } 117 } 118 } 119 } 120 121 Set <Table> result = new HashSet <Table>(); 122 for (DBSchemaTable table : name2Table.values()) { 123 String tableName = table.getName(); 124 125 table.setReferencedTables(Collections.unmodifiableSet(name2Referenced.get(tableName))); 126 table.setReferencedByTables(Collections.unmodifiableSet(name2ReferencedBy.get(tableName))); 127 table.setJoinTables(Collections.unmodifiableSet(name2Join.get(tableName))); 128 129 result.add(table); 130 } 131 132 return Collections.unmodifiableSet(result); 133 } 134 135 private static List <DisabledReason> getDisabledReasons(TableElement tableElement, PersistenceGenerator persistenceGen) { 136 List <DisabledReason> result = new ArrayList <DisabledReason>(); 137 138 if (hasNoPrimaryKey(tableElement)) { 139 result.add(new NoPrimaryKeyDisabledReason()); 140 } 141 142 String fqClassName = persistenceGen.getFQClassName(tableElement.getName().getName()); 143 if (fqClassName != null) { 144 result.add(new ExistingDisabledReason(fqClassName)); 145 } 146 147 return result; 148 } 149 150 private static boolean hasNoPrimaryKey(TableElement tableElement) { 151 return tableElement.getPrimaryKey() == null; 152 } 153 154 private static final class DBSchemaTable extends Table { 155 156 private final PersistenceGenerator persistenceGen; 157 158 private Set <Table> referencedTables; 159 private Set <Table> referencedByTables; 160 private Set <Table> joinTables; 161 162 public DBSchemaTable(String name, boolean join, DisabledReason disabledReason, PersistenceGenerator persistenceGen) { 163 super(name, join, disabledReason); 164 this.persistenceGen = persistenceGen; 165 } 166 167 public Set <Table> getReferencedTables() { 168 return referencedTables; 169 } 170 171 private void setReferencedTables(Set <Table> referencedTables) { 172 this.referencedTables = referencedTables; 173 } 174 175 public Set <Table> getReferencedByTables() { 176 return referencedByTables; 177 } 178 179 private void setReferencedByTables(Set <Table> referencedByTables) { 180 this.referencedByTables = referencedByTables; 181 } 182 183 public Set <Table> getJoinTables() { 184 return joinTables; 185 } 186 187 private void setJoinTables(Set <Table> joinTables) { 188 this.joinTables = joinTables; 189 } 190 } 191 } 192 | Popular Tags |