KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > persistence > wizard > fromdb > TableProviderImpl


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.persistence.wizard.fromdb;
21
22 import java.util.Collections JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Set JavaDoc;
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 /**
32  *
33  * @author Andrei Badea
34  */

35 public class TableProviderImpl implements TableProvider {
36
37     private final Set JavaDoc<Table> tables;
38     private final Map JavaDoc<String JavaDoc, Table> name2Table = new HashMap JavaDoc<String JavaDoc, Table>();
39
40     public TableProviderImpl(Map JavaDoc<String JavaDoc, Set JavaDoc<String JavaDoc>> tablesAndRefs) {
41         this(tablesAndRefs, emptyDisabledReasonMap());
42     }
43
44     public TableProviderImpl(Map JavaDoc<String JavaDoc, Set JavaDoc<String JavaDoc>> tablesAndRefs, Map JavaDoc<String JavaDoc, DisabledReason> disabledReasons) {
45         Map JavaDoc<String JavaDoc, TableImpl> name2Table = new HashMap JavaDoc<String JavaDoc, TableImpl>();
46         Map JavaDoc<String JavaDoc, Set JavaDoc<Table>> name2Referenced = new HashMap JavaDoc<String JavaDoc, Set JavaDoc<Table>>();
47         Map JavaDoc<String JavaDoc, Set JavaDoc<Table>> name2ReferencedBy = new HashMap JavaDoc<String JavaDoc, Set JavaDoc<Table>>();
48         Map JavaDoc<String JavaDoc, Set JavaDoc<Table>> name2Join = new HashMap JavaDoc<String JavaDoc, Set JavaDoc<Table>>();
49
50         // need to create all the tables first
51
for (String JavaDoc 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 JavaDoc<Table>());
58             name2ReferencedBy.put(tableName, new HashSet JavaDoc<Table>());
59             name2Join.put(tableName, new HashSet JavaDoc<Table>());
60         }
61
62         // referenced, referenced by and join tables
63
for (String JavaDoc tableName : tablesAndRefs.keySet()) {
64             Table table = name2Table.get(tableName);
65
66             for (String JavaDoc 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 JavaDoc<Table> tmpTables = new HashSet JavaDoc<Table>();
79         for (TableImpl table : name2Table.values()) {
80             String JavaDoc 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 JavaDoc<Table> getTables() {
93         return tables;
94     }
95
96     Table getTableByName(String JavaDoc name) {
97         return name2Table.get(name);
98     }
99
100     private static Map JavaDoc<String JavaDoc, DisabledReason> emptyDisabledReasonMap() {
101         return Collections.emptyMap();
102     }
103
104     private static final class TableImpl extends Table {
105
106         private Set JavaDoc<Table> referencedTables;
107         private Set JavaDoc<Table> referencedByTables;
108         private Set JavaDoc<Table> joinTables;
109
110         public TableImpl(String JavaDoc name, boolean join, DisabledReason disabledReason) {
111             super(name, join, disabledReason);
112         }
113
114         private void setReferencedTables(Set JavaDoc<Table> referencedTables) {
115             this.referencedTables = referencedTables;
116         }
117
118         public Set JavaDoc<Table> getReferencedTables() {
119             return referencedTables;
120         }
121
122         private void setReferencedByTables(Set JavaDoc<Table> referencedByTables) {
123             this.referencedByTables = referencedByTables;
124         }
125
126         public Set JavaDoc<Table> getReferencedByTables() {
127             return referencedByTables;
128         }
129
130         private void setJoinTables(Set JavaDoc<Table> joinTables) {
131             this.joinTables = joinTables;
132         }
133
134         public Set JavaDoc<Table> getJoinTables() {
135             return joinTables;
136         }
137     }
138 }
139
Popular Tags