KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
23 import org.netbeans.api.db.explorer.DatabaseConnection;
24 import org.netbeans.api.project.Project;
25 import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.PersistenceUnit;
26 import org.netbeans.modules.j2ee.persistence.entitygenerator.DbSchemaEjbGenerator;
27 import org.netbeans.modules.j2ee.persistence.entitygenerator.EntityClass;
28 import org.netbeans.modules.j2ee.persistence.entitygenerator.EntityRelation;
29 import org.netbeans.modules.j2ee.persistence.entitygenerator.GeneratedTables;
30 import org.netbeans.modules.j2ee.persistence.wizard.Util;
31 import org.openide.filesystems.*;
32 import org.netbeans.api.project.SourceGroup;
33 import org.netbeans.modules.dbschema.SchemaElement;
34
35 /**
36  * This class provides a simple collector for information necessary to support
37  * the CMP set wizard.
38  *
39  * @author Chris Webster, Andrei Badea
40  */

41 public class RelatedCMPHelper {
42
43     private final Project project;
44     private final FileObject configFilesFolder;
45     private final PersistenceGenerator persistenceGen;
46     private final DBSchemaFileList dbschemaFileList;
47     
48     private SchemaElement schemaElement;
49     private DatabaseConnection dbconn;
50     private FileObject dbschemaFile;
51     private String JavaDoc datasourceName;
52
53     private TableClosure tableClosure;
54     private SelectedTables selectedTables;
55     
56     private SourceGroup location;
57     private String JavaDoc packageName;
58     
59     private boolean cmpFieldsInInterface;
60     private boolean generateFinderMethods;
61     
62     private DbSchemaEjbGenerator generator;
63     
64     private TableSource tableSource;
65     
66     private PersistenceUnit persistenceUnit;
67     
68     public RelatedCMPHelper(Project project, FileObject configFilesFolder, PersistenceGenerator persistenceGen) {
69         this.project = project;
70         this.configFilesFolder = configFilesFolder;
71         this.persistenceGen = persistenceGen;
72         
73         tableSource = TableSource.get(project);
74         dbschemaFileList = new DBSchemaFileList(project, configFilesFolder);
75     }
76     
77     public Project getProject() {
78         return project;
79     }
80     
81     FileObject getConfigFilesFolder() {
82         return configFilesFolder;
83     }
84     
85     PersistenceGenerator getPersistenceGenerator() {
86         return persistenceGen;
87     }
88     
89     public DBSchemaFileList getDBSchemaFileList() {
90         return dbschemaFileList;
91     }
92     
93     public void setTableClosure(TableClosure tableClosure) {
94         assert tableClosure != null;
95         this.tableClosure = tableClosure;
96     }
97     
98     public TableClosure getTableClosure() {
99         return tableClosure;
100     }
101     
102     public void setSelectedTables(SelectedTables selectedTables) {
103         assert selectedTables != null;
104         this.selectedTables = selectedTables;
105     }
106
107     public PersistenceUnit getPersistenceUnit() {
108         return persistenceUnit;
109     }
110
111     public void setPersistenceUnit(PersistenceUnit persistenceUnit) {
112         this.persistenceUnit = persistenceUnit;
113     }
114     
115     /**
116      * Sets the source of the tables when the source is a database connection
117      * (possibly retrieved from a data source).
118      *
119      * @param schemaElement the SchemaElement instance containing the database tables.
120      * @param dbconn the database connection which was used to retrieve <code>schemaElement</code>.
121      * @param dataSourceName the JNDI name of the {@link org.netbeans.modules.j2ee.deployment.common.api.Datasource data source}
122      * which was used to retrieve <code>dbconn</code> or null if the connection
123      * was not retrieved from a data source.
124      */

125     public void setTableSource(SchemaElement schemaElement, DatabaseConnection dbconn, String JavaDoc datasourceName) {
126         this.schemaElement = schemaElement;
127         this.dbconn = dbconn;
128         this.dbschemaFile = null;
129         this.datasourceName = datasourceName;
130         
131         updateTableSource();
132     }
133     
134     /**
135      * Sets the source of the tables when the source is a dbschema file.
136      *
137      * @param schemaElement the SchemaElement instance containing the database tables.
138      * @param dbschemaFile the dbschema file which was used to retrieve <code>schemaElement</code>.
139      */

140     public void setTableSource(SchemaElement schemaElement, FileObject dbschemaFile) {
141         this.schemaElement = schemaElement;
142         this.dbconn = null;
143         this.dbschemaFile = dbschemaFile;
144         this.datasourceName = null;
145         
146         updateTableSource();
147     }
148     
149     public TableSource getTableSource() {
150         return tableSource;
151     }
152     
153     private void updateTableSource() {
154         if (dbconn != null) {
155             if (datasourceName != null) {
156                 tableSource = new TableSource(datasourceName, TableSource.Type.DATA_SOURCE);
157             } else {
158                 tableSource = new TableSource(dbconn.getName(), TableSource.Type.CONNECTION);
159             }
160         } else if (dbschemaFile != null) {
161             tableSource = new TableSource(FileUtil.toFile(dbschemaFile).getAbsolutePath(), TableSource.Type.SCHEMA_FILE);
162         } else {
163             tableSource = null;
164         }
165     }
166     
167     public SchemaElement getSchemaElement() {
168         return schemaElement;
169     }
170     
171     public DatabaseConnection getDatabaseConnection(){
172         return dbconn;
173     }
174     
175     public FileObject getDBSchemaFile() {
176         return dbschemaFile;
177     }
178     
179     /**
180      * Returns the package for bean and module generation.
181      */

182     public String JavaDoc getPackageName() {
183         return packageName;
184     }
185     
186     /**
187      * Sets the package for bean and module generation.
188      */

189     public void setPackageName(String JavaDoc packageName) {
190         this.packageName = packageName;
191     }
192     
193     public SourceGroup getLocation() {
194         return location;
195     }
196     
197     public void setLocation(SourceGroup location) {
198         this.location = location;
199     }
200     
201     public boolean isCmpFieldsInInterface() {
202         return cmpFieldsInInterface;
203     }
204     
205     public void setCmpFieldsInInterface(boolean cmpFieldsInInterface) {
206         this.cmpFieldsInInterface = cmpFieldsInInterface;
207     }
208     
209     public boolean isGenerateFinderMethods() {
210         return this.generateFinderMethods;
211     }
212     
213     public void setGenerateFinderMethods(boolean generateFinderMethods) {
214         this.generateFinderMethods = generateFinderMethods;
215     }
216     
217     /**
218      * Public because used in J2EE functional tests.
219      */

220     public void buildBeans() {
221         TableSource.put(project, tableSource);
222         
223         GenerateTablesImpl genTables = new GenerateTablesImpl();
224         FileObject rootFolder = getLocation().getRootFolder();
225         String JavaDoc packageName = getPackageName();
226
227         for (Table table : selectedTables.getTables()) {
228             genTables.addTable(table.getName(), rootFolder, packageName, selectedTables.getClassName(table));
229         }
230
231         // add the (possibly related) disabled tables, so that the relationships are created correctly
232
// XXX what if this adds related tables that the user didn't want, such as join tables?
233
for (Table table : tableClosure.getAvailableTables()) {
234             if (table.getDisabledReason() instanceof Table.ExistingDisabledReason) {
235                 Table.ExistingDisabledReason exDisReason = (Table.ExistingDisabledReason)table.getDisabledReason();
236                 String JavaDoc fqClassName = exDisReason.getFQClassName();
237                 SourceGroup sourceGroup = Util.getClassSourceGroup(getProject(), fqClassName); // NOI18N
238
if (sourceGroup != null) {
239                     genTables.addTable(table.getName(), sourceGroup.getRootFolder(),
240                             Util.getPackageName(fqClassName), Util.getClassName(fqClassName));
241                 }
242             }
243         }
244
245         generator = new DbSchemaEjbGenerator(genTables, schemaElement);
246     }
247     
248     public EntityClass[] getBeans() {
249         return generator.getBeans();
250     }
251     
252     public EntityRelation[] getRelations() {
253         return generator.getRelations();
254     }
255     
256     private static final class GenerateTablesImpl implements GeneratedTables {
257
258         private final Set<String JavaDoc> tableNames = new HashSet<String JavaDoc>();
259         private final Map<String JavaDoc, FileObject> rootFolders = new HashMap<String JavaDoc, FileObject>();
260         private final Map<String JavaDoc, String JavaDoc> packageNames = new HashMap<String JavaDoc, String JavaDoc>();
261         private final Map<String JavaDoc, String JavaDoc> classNames = new HashMap<String JavaDoc, String JavaDoc>();
262         
263         public Set<String JavaDoc> getTableNames() {
264             return Collections.unmodifiableSet(tableNames);
265         }
266         
267         private void addTable(String JavaDoc tableName, FileObject rootFolder, String JavaDoc packageName, String JavaDoc className) {
268             tableNames.add(tableName);
269             rootFolders.put(tableName, rootFolder);
270             packageNames.put(tableName, packageName);
271             classNames.put(tableName, className);
272         }
273
274         public FileObject getRootFolder(String JavaDoc tableName) {
275             return rootFolders.get(tableName);
276         }
277
278         public String JavaDoc getPackageName(String JavaDoc tableName) {
279             return packageNames.get(tableName);
280         }
281         
282         public String JavaDoc getClassName(String JavaDoc tableName) {
283             return classNames.get(tableName);
284         }
285     }
286 }
287
Popular Tags