Search in sources :

Example 1 with NullProgressCallback

use of org.mybatis.generator.internal.NullProgressCallback in project generator by mybatis.

the class MyBatisGenerator method generate.

/**
     * This is the main method for generating code. This method is long running, but progress can be provided and the
     * method can be cancelled through the ProgressCallback interface.
     *
     * @param callback
     *            an instance of the ProgressCallback interface, or <code>null</code> if you do not require progress
     *            information
     * @param contextIds
     *            a set of Strings containing context ids to run. Only the contexts with an id specified in this list
     *            will be run. If the list is null or empty, than all contexts are run.
     * @param fullyQualifiedTableNames
     *            a set of table names to generate. The elements of the set must be Strings that exactly match what's
     *            specified in the configuration. For example, if table name = "foo" and schema = "bar", then the fully
     *            qualified table name is "foo.bar". If the Set is null or empty, then all tables in the configuration
     *            will be used for code generation.
     * @param writeFiles
     *            if true, then the generated files will be written to disk.  If false,
     *            then the generator runs but nothing is written
     * @throws SQLException
     *             the SQL exception
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     * @throws InterruptedException
     *             if the method is canceled through the ProgressCallback
     */
public void generate(ProgressCallback callback, Set<String> contextIds, Set<String> fullyQualifiedTableNames, boolean writeFiles) throws SQLException, IOException, InterruptedException {
    if (callback == null) {
        callback = new NullProgressCallback();
    }
    generatedJavaFiles.clear();
    generatedXmlFiles.clear();
    ObjectFactory.reset();
    RootClassInfo.reset();
    // calculate the contexts to run
    List<Context> contextsToRun;
    if (contextIds == null || contextIds.size() == 0) {
        contextsToRun = configuration.getContexts();
    } else {
        contextsToRun = new ArrayList<Context>();
        for (Context context : configuration.getContexts()) {
            if (contextIds.contains(context.getId())) {
                contextsToRun.add(context);
            }
        }
    }
    // setup custom classloader if required
    if (configuration.getClassPathEntries().size() > 0) {
        ClassLoader classLoader = getCustomClassloader(configuration.getClassPathEntries());
        ObjectFactory.addExternalClassLoader(classLoader);
    }
    // now run the introspections...
    int totalSteps = 0;
    for (Context context : contextsToRun) {
        totalSteps += context.getIntrospectionSteps();
    }
    callback.introspectionStarted(totalSteps);
    for (Context context : contextsToRun) {
        context.introspectTables(callback, warnings, fullyQualifiedTableNames);
    }
    // now run the generates
    totalSteps = 0;
    for (Context context : contextsToRun) {
        totalSteps += context.getGenerationSteps();
    }
    callback.generationStarted(totalSteps);
    for (Context context : contextsToRun) {
        context.generateFiles(callback, generatedJavaFiles, generatedXmlFiles, warnings);
    }
    // now save the files
    if (writeFiles) {
        callback.saveStarted(generatedXmlFiles.size() + generatedJavaFiles.size());
        for (GeneratedXmlFile gxf : generatedXmlFiles) {
            projects.add(gxf.getTargetProject());
            writeGeneratedXmlFile(gxf, callback);
        }
        for (GeneratedJavaFile gjf : generatedJavaFiles) {
            projects.add(gjf.getTargetProject());
            writeGeneratedJavaFile(gjf, callback);
        }
        for (String project : projects) {
            shellCallback.refreshProject(project);
        }
    }
    callback.done();
}
Also used : Context(org.mybatis.generator.config.Context) NullProgressCallback(org.mybatis.generator.internal.NullProgressCallback) Messages.getString(org.mybatis.generator.internal.util.messages.Messages.getString)

Aggregations

Context (org.mybatis.generator.config.Context)1 NullProgressCallback (org.mybatis.generator.internal.NullProgressCallback)1 Messages.getString (org.mybatis.generator.internal.util.messages.Messages.getString)1