Search in sources :

Example 1 with SunCmpMapping

use of com.sun.jdo.api.persistence.mapping.ejb.beans.SunCmpMapping 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());
            }
        }
    }
}
Also used : SunCmpMapping(com.sun.jdo.api.persistence.mapping.ejb.beans.SunCmpMapping) Set(java.util.Set) HashSet(java.util.HashSet) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) Connection(java.sql.Connection) IOException(java.io.IOException) DatabaseMetaData(java.sql.DatabaseMetaData) LinkedList(java.util.LinkedList) ConnectionProvider(org.netbeans.modules.dbschema.jdbcimpl.ConnectionProvider) SchemaElementImpl(org.netbeans.modules.dbschema.jdbcimpl.SchemaElementImpl) FileOutputStream(java.io.FileOutputStream) SchemaElement(org.netbeans.modules.dbschema.SchemaElement) GeneratorException(com.sun.jdo.spi.persistence.support.ejb.codegen.GeneratorException) MappingFile(com.sun.jdo.api.persistence.mapping.ejb.MappingFile) File(java.io.File) HashSet(java.util.HashSet)

Aggregations

MappingFile (com.sun.jdo.api.persistence.mapping.ejb.MappingFile)1 SunCmpMapping (com.sun.jdo.api.persistence.mapping.ejb.beans.SunCmpMapping)1 GeneratorException (com.sun.jdo.spi.persistence.support.ejb.codegen.GeneratorException)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 Connection (java.sql.Connection)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 Set (java.util.Set)1 SchemaElement (org.netbeans.modules.dbschema.SchemaElement)1 ConnectionProvider (org.netbeans.modules.dbschema.jdbcimpl.ConnectionProvider)1 SchemaElementImpl (org.netbeans.modules.dbschema.jdbcimpl.SchemaElementImpl)1