001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.j2ee.persistence.wizard.fromdb;
043:
044: import java.util.Collections;
045: import java.util.HashMap;
046: import java.util.HashSet;
047: import java.util.Map;
048: import java.util.Set;
049: import org.netbeans.modules.dbschema.ForeignKeyElement;
050: import org.netbeans.modules.dbschema.TableElement;
051: import org.netbeans.modules.j2ee.persistence.wizard.fromdb.Table.DisabledReason;
052:
053: /**
054: *
055: * @author Andrei Badea
056: */
057: public class TableProviderImpl implements TableProvider {
058:
059: private final Set<Table> tables;
060: private final Map<String, Table> name2Table = new HashMap<String, Table>();
061:
062: public TableProviderImpl(Map<String, Set<String>> tablesAndRefs) {
063: this (tablesAndRefs, emptyDisabledReasonMap());
064: }
065:
066: public TableProviderImpl(Map<String, Set<String>> tablesAndRefs,
067: Map<String, DisabledReason> disabledReasons) {
068: Map<String, TableImpl> name2Table = new HashMap<String, TableImpl>();
069: Map<String, Set<Table>> name2Referenced = new HashMap<String, Set<Table>>();
070: Map<String, Set<Table>> name2ReferencedBy = new HashMap<String, Set<Table>>();
071: Map<String, Set<Table>> name2Join = new HashMap<String, Set<Table>>();
072:
073: // need to create all the tables first
074: for (String tableName : tablesAndRefs.keySet()) {
075: DisabledReason disabledReason = disabledReasons
076: .get(tableName);
077: boolean join = tableName.contains("_");
078: TableImpl table = new TableImpl(tableName, join,
079: disabledReason);
080:
081: name2Table.put(tableName, table);
082: name2Referenced.put(tableName, new HashSet<Table>());
083: name2ReferencedBy.put(tableName, new HashSet<Table>());
084: name2Join.put(tableName, new HashSet<Table>());
085: }
086:
087: // referenced, referenced by and join tables
088: for (String tableName : tablesAndRefs.keySet()) {
089: Table table = name2Table.get(tableName);
090:
091: for (String referencedTableName : tablesAndRefs
092: .get(tableName)) {
093: Table referencedTable = name2Table
094: .get(referencedTableName);
095:
096: name2Referenced.get(tableName).add(referencedTable);
097: name2ReferencedBy.get(referencedTableName).add(table);
098:
099: if (table.isJoin()) {
100: name2Join.get(referencedTableName).add(table);
101: }
102: }
103: }
104:
105: Set<Table> tmpTables = new HashSet<Table>();
106: for (TableImpl table : name2Table.values()) {
107: String tableName = table.getName();
108:
109: table.setReferencedTables(Collections
110: .unmodifiableSet(name2Referenced.get(tableName)));
111: table.setReferencedByTables(Collections
112: .unmodifiableSet(name2ReferencedBy.get(tableName)));
113: table.setJoinTables(Collections.unmodifiableSet(name2Join
114: .get(tableName)));
115:
116: tmpTables.add(table);
117: this .name2Table.put(table.getName(), table);
118: }
119: tables = Collections.unmodifiableSet(tmpTables);
120: }
121:
122: public Set<Table> getTables() {
123: return tables;
124: }
125:
126: Table getTableByName(String name) {
127: return name2Table.get(name);
128: }
129:
130: private static Map<String, DisabledReason> emptyDisabledReasonMap() {
131: return Collections.emptyMap();
132: }
133:
134: private static final class TableImpl extends Table {
135:
136: private Set<Table> referencedTables;
137: private Set<Table> referencedByTables;
138: private Set<Table> joinTables;
139:
140: public TableImpl(String name, boolean join,
141: DisabledReason disabledReason) {
142: super (name, join, disabledReason);
143: }
144:
145: private void setReferencedTables(Set<Table> referencedTables) {
146: this .referencedTables = referencedTables;
147: }
148:
149: public Set<Table> getReferencedTables() {
150: return referencedTables;
151: }
152:
153: private void setReferencedByTables(Set<Table> referencedByTables) {
154: this .referencedByTables = referencedByTables;
155: }
156:
157: public Set<Table> getReferencedByTables() {
158: return referencedByTables;
159: }
160:
161: private void setJoinTables(Set<Table> joinTables) {
162: this .joinTables = joinTables;
163: }
164:
165: public Set<Table> getJoinTables() {
166: return joinTables;
167: }
168: }
169: }
|