KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Set JavaDoc;
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 /**
40  * An implementation of a table provider backed by a dbschema.
41  *
42  * @author Andrei Badea
43  */

44 public class DBSchemaTableProvider implements TableProvider {
45
46     private final SchemaElement schemaElement;
47     private final PersistenceGenerator persistenceGen;
48     private final Set JavaDoc<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 JavaDoc<Table> getTables() {
58         return tables;
59     }
60
61     private Set JavaDoc<Table> buildTables() {
62         Map JavaDoc<String JavaDoc, DBSchemaTable> name2Table = new HashMap JavaDoc<String JavaDoc, DBSchemaTable>();
63         Map JavaDoc<String JavaDoc, Set JavaDoc<Table>> name2Referenced = new HashMap JavaDoc<String JavaDoc, Set JavaDoc<Table>>();
64         Map JavaDoc<String JavaDoc, Set JavaDoc<Table>> name2ReferencedBy = new HashMap JavaDoc<String JavaDoc, Set JavaDoc<Table>>();
65         Map JavaDoc<String JavaDoc, Set JavaDoc<Table>> name2Join = new HashMap JavaDoc<String JavaDoc, Set JavaDoc<Table>>();
66
67         // need to create all the tables first
68
TableElement[] tableElements = schemaElement.getTables();
69         for (TableElement tableElement : tableElements) {
70             if (tableElement.isTable()) {
71                 boolean join = DbSchemaEjbGenerator.isJoinTable(tableElement);
72
73                 List JavaDoc<DisabledReason> disabledReasons = getDisabledReasons(tableElement, persistenceGen);
74                 DisabledReason disabledReason = null;
75                 // only take the first disabled reason
76
for (DisabledReason reason : disabledReasons) {
77                     // issue 76202: join tables are not required to have a primary key
78
// so try the other disabled reasons, if any
79
if (!(join && reason instanceof NoPrimaryKeyDisabledReason)) {
80                         disabledReason = reason;
81                         break;
82                     }
83                 }
84
85                 String JavaDoc 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 JavaDoc<Table>());
90                 name2ReferencedBy.put(tableName, new HashSet JavaDoc<Table>());
91                 name2Join.put(tableName, new HashSet JavaDoc<Table>());
92             }
93         }
94
95         // referenced, referenced by and join tables
96
for (TableElement tableElement : tableElements) {
97             if (!tableElement.isTable()) {
98                 continue;
99             }
100
101             String JavaDoc 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 JavaDoc 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 JavaDoc<Table> result = new HashSet JavaDoc<Table>();
122         for (DBSchemaTable table : name2Table.values()) {
123             String JavaDoc 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 JavaDoc<DisabledReason> getDisabledReasons(TableElement tableElement, PersistenceGenerator persistenceGen) {
136         List JavaDoc<DisabledReason> result = new ArrayList JavaDoc<DisabledReason>();
137
138         if (hasNoPrimaryKey(tableElement)) {
139             result.add(new NoPrimaryKeyDisabledReason());
140         }
141
142         String JavaDoc 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 JavaDoc<Table> referencedTables;
159         private Set JavaDoc<Table> referencedByTables;
160         private Set JavaDoc<Table> joinTables;
161
162         public DBSchemaTable(String JavaDoc name, boolean join, DisabledReason disabledReason, PersistenceGenerator persistenceGen) {
163             super(name, join, disabledReason);
164             this.persistenceGen = persistenceGen;
165         }
166
167         public Set JavaDoc<Table> getReferencedTables() {
168             return referencedTables;
169         }
170
171         private void setReferencedTables(Set JavaDoc<Table> referencedTables) {
172             this.referencedTables = referencedTables;
173         }
174
175         public Set JavaDoc<Table> getReferencedByTables() {
176             return referencedByTables;
177         }
178
179         private void setReferencedByTables(Set JavaDoc<Table> referencedByTables) {
180             this.referencedByTables = referencedByTables;
181         }
182
183         public Set JavaDoc<Table> getJoinTables() {
184             return joinTables;
185         }
186
187         private void setJoinTables(Set JavaDoc<Table> joinTables) {
188             this.joinTables = joinTables;
189         }
190     }
191 }
192
Popular Tags