1 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 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 datasourceName; 52 53 private TableClosure tableClosure; 54 private SelectedTables selectedTables; 55 56 private SourceGroup location; 57 private String 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 125 public void setTableSource(SchemaElement schemaElement, DatabaseConnection dbconn, String datasourceName) { 126 this.schemaElement = schemaElement; 127 this.dbconn = dbconn; 128 this.dbschemaFile = null; 129 this.datasourceName = datasourceName; 130 131 updateTableSource(); 132 } 133 134 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 182 public String getPackageName() { 183 return packageName; 184 } 185 186 189 public void setPackageName(String 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 220 public void buildBeans() { 221 TableSource.put(project, tableSource); 222 223 GenerateTablesImpl genTables = new GenerateTablesImpl(); 224 FileObject rootFolder = getLocation().getRootFolder(); 225 String packageName = getPackageName(); 226 227 for (Table table : selectedTables.getTables()) { 228 genTables.addTable(table.getName(), rootFolder, packageName, selectedTables.getClassName(table)); 229 } 230 231 for (Table table : tableClosure.getAvailableTables()) { 234 if (table.getDisabledReason() instanceof Table.ExistingDisabledReason) { 235 Table.ExistingDisabledReason exDisReason = (Table.ExistingDisabledReason)table.getDisabledReason(); 236 String fqClassName = exDisReason.getFQClassName(); 237 SourceGroup sourceGroup = Util.getClassSourceGroup(getProject(), fqClassName); 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 > tableNames = new HashSet<String >(); 259 private final Map<String , FileObject> rootFolders = new HashMap<String , FileObject>(); 260 private final Map<String , String > packageNames = new HashMap<String , String >(); 261 private final Map<String , String > classNames = new HashMap<String , String >(); 262 263 public Set<String > getTableNames() { 264 return Collections.unmodifiableSet(tableNames); 265 } 266 267 private void addTable(String tableName, FileObject rootFolder, String packageName, String 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 tableName) { 275 return rootFolders.get(tableName); 276 } 277 278 public String getPackageName(String tableName) { 279 return packageNames.get(tableName); 280 } 281 282 public String getClassName(String tableName) { 283 return classNames.get(tableName); 284 } 285 } 286 } 287 | Popular Tags |