Search in sources :

Example 1 with EclipseShellCallback

use of org.mybatis.generator.eclipse.core.callback.EclipseShellCallback in project generator by mybatis.

the class GeneratorAntTask method execute.

/*
     * (non-Javadoc)
     * 
     * @see org.apache.tools.ant.Task#execute()
     */
@Override
public void execute() throws BuildException {
    if (!StringUtility.stringHasValue(configfile)) {
        throw new BuildException("configfile is a required parameter");
    }
    List<String> warnings = new ArrayList<String>();
    File configurationFile = new File(configfile);
    if (!configurationFile.exists()) {
        throw new BuildException("configfile " + configfile + " does not exist");
    }
    Set<String> fullyqualifiedTables = new HashSet<String>();
    if (StringUtility.stringHasValue(fullyQualifiedTableNames)) {
        //$NON-NLS-1$
        StringTokenizer st = new StringTokenizer(fullyQualifiedTableNames, ",");
        while (st.hasMoreTokens()) {
            String s = st.nextToken().trim();
            if (s.length() > 0) {
                fullyqualifiedTables.add(s);
            }
        }
    }
    Set<String> contexts = new HashSet<String>();
    if (StringUtility.stringHasValue(contextIds)) {
        //$NON-NLS-1$
        StringTokenizer st = new StringTokenizer(contextIds, ",");
        while (st.hasMoreTokens()) {
            String s = st.nextToken().trim();
            if (s.length() > 0) {
                contexts.add(s);
            }
        }
    }
    IProgressMonitor monitor = (IProgressMonitor) getProject().getReferences().get(AntCorePlugin.ECLIPSE_PROGRESS_MONITOR);
    if (monitor == null) {
        monitor = new NullProgressMonitor();
    }
    try {
        SubMonitor subMonitor = SubMonitor.convert(monitor, 1000);
        subMonitor.beginTask("Generating MyBatis/iBATIS Artifacts:", 1000);
        subMonitor.subTask("Parsing Configuration");
        Properties p = propertyset == null ? null : propertyset.getProperties();
        ConfigurationParser cp = new ConfigurationParser(p, warnings);
        Configuration config = cp.parseConfiguration(configurationFile);
        subMonitor.worked(50);
        monitor.subTask("Generating Files from Database Tables");
        MyBatisGenerator generator = new MyBatisGenerator(config, new EclipseShellCallback(), warnings);
        EclipseProgressCallback progressCallback = new EclipseProgressCallback(subMonitor.newChild(950));
        generator.generate(progressCallback, contexts, fullyqualifiedTables);
    } catch (XMLParserException e) {
        for (String error : e.getErrors()) {
            log(error, Project.MSG_ERR);
        }
        throw new BuildException(e.getMessage());
    } catch (SQLException e) {
        throw new BuildException(e.getMessage());
    } catch (IOException e) {
        throw new BuildException(e.getMessage());
    } catch (InvalidConfigurationException e) {
        throw new BuildException(e.getMessage());
    } catch (InterruptedException e) {
        throw new BuildException("Cancelled by user");
    } finally {
        monitor.done();
    }
    for (String warning : warnings) {
        log("WARNING: " + warning, Project.MSG_WARN);
    }
}
Also used : EclipseShellCallback(org.mybatis.generator.eclipse.core.callback.EclipseShellCallback) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) Configuration(org.mybatis.generator.config.Configuration) XMLParserException(org.mybatis.generator.exception.XMLParserException) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) SubMonitor(org.eclipse.core.runtime.SubMonitor) IOException(java.io.IOException) Properties(java.util.Properties) InvalidConfigurationException(org.mybatis.generator.exception.InvalidConfigurationException) StringTokenizer(java.util.StringTokenizer) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) EclipseProgressCallback(org.mybatis.generator.eclipse.core.callback.EclipseProgressCallback) ConfigurationParser(org.mybatis.generator.config.xml.ConfigurationParser) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) HashSet(java.util.HashSet) MyBatisGenerator(org.mybatis.generator.api.MyBatisGenerator)

Example 2 with EclipseShellCallback

use of org.mybatis.generator.eclipse.core.callback.EclipseShellCallback in project generator by mybatis.

the class RunGeneratorThread method run.

/*
     * (non-Javadoc)
     * 
     * @see org.eclipse.core.resources.IWorkspaceRunnable#run(org.eclipse.core.runtime.IProgressMonitor)
     */
public void run(IProgressMonitor monitor) throws CoreException {
    SubMonitor subMonitor = SubMonitor.convert(monitor, 1000);
    subMonitor.beginTask("Generating MyBatis/iBATIS Artifacts:", 1000);
    setClassLoader();
    try {
        subMonitor.subTask("Parsing Configuration");
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(inputFile.getLocation().toFile());
        subMonitor.worked(50);
        MyBatisGenerator mybatisGenerator = new MyBatisGenerator(config, new EclipseShellCallback(), warnings);
        monitor.subTask("Generating Files from Database Tables");
        SubMonitor spm = subMonitor.newChild(950);
        mybatisGenerator.generate(new EclipseProgressCallback(spm));
    } catch (InterruptedException e) {
        throw new OperationCanceledException();
    } catch (SQLException e) {
        Status status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, e.getMessage(), e);
        Activator.getDefault().getLog().log(status);
        throw new CoreException(status);
    } catch (IOException e) {
        Status status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, e.getMessage(), e);
        Activator.getDefault().getLog().log(status);
        throw new CoreException(status);
    } catch (XMLParserException e) {
        List<String> errors = e.getErrors();
        MultiStatus multiStatus = new MultiStatus(Activator.PLUGIN_ID, IStatus.ERROR, "XML Parser Errors\n  See Details for more Information", null);
        Iterator<String> iter = errors.iterator();
        while (iter.hasNext()) {
            Status message = new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, iter.next(), null);
            multiStatus.add(message);
        }
        throw new CoreException(multiStatus);
    } catch (InvalidConfigurationException e) {
        List<String> errors = e.getErrors();
        MultiStatus multiStatus = new MultiStatus(Activator.PLUGIN_ID, IStatus.ERROR, "Invalid Configuration\n  See Details for more Information", null);
        Iterator<String> iter = errors.iterator();
        while (iter.hasNext()) {
            Status message = new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, iter.next(), null);
            multiStatus.add(message);
        }
        throw new CoreException(multiStatus);
    } finally {
        monitor.done();
        restoreClassLoader();
    }
}
Also used : EclipseShellCallback(org.mybatis.generator.eclipse.core.callback.EclipseShellCallback) MultiStatus(org.eclipse.core.runtime.MultiStatus) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) Configuration(org.mybatis.generator.config.Configuration) SQLException(java.sql.SQLException) XMLParserException(org.mybatis.generator.exception.XMLParserException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) SubMonitor(org.eclipse.core.runtime.SubMonitor) MultiStatus(org.eclipse.core.runtime.MultiStatus) IOException(java.io.IOException) InvalidConfigurationException(org.mybatis.generator.exception.InvalidConfigurationException) EclipseProgressCallback(org.mybatis.generator.eclipse.core.callback.EclipseProgressCallback) CoreException(org.eclipse.core.runtime.CoreException) ConfigurationParser(org.mybatis.generator.config.xml.ConfigurationParser) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) MyBatisGenerator(org.mybatis.generator.api.MyBatisGenerator)

Example 3 with EclipseShellCallback

use of org.mybatis.generator.eclipse.core.callback.EclipseShellCallback in project generator by mybatis.

the class EclipseShellCallbackTest method testCalculatingDirectoryOnDefaultSourceFolder.

@Test
public void testCalculatingDirectoryOnDefaultSourceFolder() throws ShellException {
    EclipseShellCallback callback = new EclipseShellCallback();
    File directory = callback.getDirectory(javaProject.getElementName(), "org.mybatis.test");
    assertThat(directory, is(notNullValue()));
    assertThat(directory.exists(), is(true));
    IPath actualPath = new Path(directory.getAbsolutePath());
    IPath workspacePath = getWorkspace().getRoot().getLocation();
    IPath expectedPath = workspacePath.append(javaProject.getElementName()).append("src").append("main").append("java").append("org").append("mybatis").append("test");
    assertThat(expectedPath, is(equalTo(actualPath)));
}
Also used : EclipseShellCallback(org.mybatis.generator.eclipse.core.callback.EclipseShellCallback) IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IPath(org.eclipse.core.runtime.IPath) File(java.io.File) Test(org.junit.Test)

Example 4 with EclipseShellCallback

use of org.mybatis.generator.eclipse.core.callback.EclipseShellCallback in project generator by mybatis.

the class EclipseShellCallbackTest method testCalculatingDirectoryOnNonExistingSourceFolder.

@Test(expected = ShellException.class)
public void testCalculatingDirectoryOnNonExistingSourceFolder() throws ShellException {
    EclipseShellCallback callback = new EclipseShellCallback();
    callback.getDirectory(javaProject.getElementName() + "/othersrc", "org.mybatis.test");
}
Also used : EclipseShellCallback(org.mybatis.generator.eclipse.core.callback.EclipseShellCallback) Test(org.junit.Test)

Example 5 with EclipseShellCallback

use of org.mybatis.generator.eclipse.core.callback.EclipseShellCallback in project generator by mybatis.

the class EclipseShellCallbackTest method testCalculatingDirectoryOnSpecificSourceFolder.

@Test
public void testCalculatingDirectoryOnSpecificSourceFolder() throws ShellException {
    EclipseShellCallback callback = new EclipseShellCallback();
    File directory = callback.getDirectory(javaProject.getElementName() + "/generatedsrc", "org.mybatis.test");
    assertThat(directory, is(notNullValue()));
    assertThat(directory.exists(), is(true));
    IPath actualPath = new Path(directory.getAbsolutePath());
    IPath workspacePath = getWorkspace().getRoot().getLocation();
    IPath expectedPath = workspacePath.append(javaProject.getElementName()).append("generatedsrc").append("org").append("mybatis").append("test");
    assertThat(expectedPath, is(equalTo(actualPath)));
}
Also used : EclipseShellCallback(org.mybatis.generator.eclipse.core.callback.EclipseShellCallback) IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IPath(org.eclipse.core.runtime.IPath) File(java.io.File) Test(org.junit.Test)

Aggregations

EclipseShellCallback (org.mybatis.generator.eclipse.core.callback.EclipseShellCallback)5 File (java.io.File)3 Test (org.junit.Test)3 IOException (java.io.IOException)2 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 IPath (org.eclipse.core.runtime.IPath)2 Path (org.eclipse.core.runtime.Path)2 SubMonitor (org.eclipse.core.runtime.SubMonitor)2 MyBatisGenerator (org.mybatis.generator.api.MyBatisGenerator)2 Configuration (org.mybatis.generator.config.Configuration)2 ConfigurationParser (org.mybatis.generator.config.xml.ConfigurationParser)2 EclipseProgressCallback (org.mybatis.generator.eclipse.core.callback.EclipseProgressCallback)2 InvalidConfigurationException (org.mybatis.generator.exception.InvalidConfigurationException)2 XMLParserException (org.mybatis.generator.exception.XMLParserException)2 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Properties (java.util.Properties)1 StringTokenizer (java.util.StringTokenizer)1