use of org.netbeans.modules.dbschema.jdbcimpl.ConnectionProvider in project Payara by payara.
the class MappingGenerator method ensureDBSchemaExistence.
/**
* Check that there is a dbschema for each element of the SunCmpMappings.
* For those which are missing, create a corresponding .dbschema file.
* @param cmpResource Provides JNDI name for getting database connection
* @param sunCmpMappings SunCmpMappings which is checked for having schema
* @param inputFilesPath the directory where this bundle's files are located
* @param classout the directory where the classes are located
* @exception DBException Thrown if database model throws it
* @exception IOException Thrown if .dbschema file cannot be created.
* @exception SQLException Thrown if we cannot get get required info from
* the database.
*/
private void ensureDBSchemaExistence(ResourceReferenceDescriptor cmpResource, SunCmpMappings sunCmpMappings, String inputFilesPath, File classout) throws DBException, SQLException, GeneratorException {
String generatedSchemaName = getInfoHelper().getSchemaNameToGenerate();
Set tables = new HashSet();
int size = sunCmpMappings.sizeSunCmpMapping();
// and save it.
for (int i = 0; i < size; i++) {
SunCmpMapping sunCmpMapping = sunCmpMappings.getSunCmpMapping(i);
String schemaName = sunCmpMapping.getSchema();
if (StringHelper.isEmpty(schemaName)) {
if (!isVerifyFlag) {
// The tables in this section need to be captured.
addAllTables(sunCmpMapping, tables);
sunCmpMapping.setSchema(generatedSchemaName);
} else {
// If it is from verifier, capture schema internally
// to perform sun-cmp-mappings.xml and EJB validation
getConversionHelper().setEnsureValidation(false);
}
} else {
File dbschemaFile = new File(new StringBuffer(inputFilesPath).append(File.separator).append(schemaName).append(DBSCHEMA_EXTENSION).toString());
if (!(dbschemaFile.exists() && dbschemaFile.isFile() && dbschemaFile.canRead())) {
throw new GeneratorException(I18NHelper.getMessage(// NOI18N
messages, // NOI18N
"CMG.MissingDBSchema", bundle.getApplication().getRegistrationName(), JDOCodeGeneratorHelper.getModuleName(bundle), schemaName));
}
}
}
// Now we need to go and capture those tables.
if (tables.size() > 0) {
String userSchema = null;
Connection con = DeploymentHelper.getConnection(cmpResource.getJndiName());
DatabaseMetaData dmd = con.getMetaData();
if (DBVendorTypeHelper.requireUpperCaseSchema(dmd)) {
userSchema = dmd.getUserName().trim().toUpperCase();
}
ConnectionProvider cp = new ConnectionProvider(con, dmd.getDriverName().trim());
if (userSchema != null) {
cp.setSchema(userSchema);
}
OutputStream outstream = null;
try {
SchemaElementImpl outSchemaImpl = new SchemaElementImpl(cp);
SchemaElement schemaElement = new SchemaElement(outSchemaImpl);
schemaElement.setName(DBIdentifier.create(generatedSchemaName));
if (dmd.getDatabaseProductName().compareToIgnoreCase("MYSQL") == 0)
outSchemaImpl.initTables(cp, new LinkedList(tables), new LinkedList(), true);
else
outSchemaImpl.initTables(cp, new LinkedList(tables), new LinkedList(), false);
outstream = new FileOutputStream(new File(classout, new StringBuffer(generatedSchemaName).append(DBSCHEMA_EXTENSION).toString()));
// XXX Unfortunately, if SchemaElement.save gets an
// IOException, it prints the stack trace but does not
// let us handle it :-(
schemaElement.save(outstream);
} catch (IOException ex) {
// Catch FileNotFound, etc.
throw JDOCodeGeneratorHelper.createGeneratorException("CMG.CannotSaveDBSchema", bundle, // NOI18N
ex);
} finally {
cp.closeConnection();
try {
if (outstream != null) {
outstream.close();
}
} catch (IOException ex) {
if (logger.isLoggable(Logger.FINE))
logger.fine(ex.toString());
}
}
}
}
Aggregations