1 package org.hibernate.eclipse.console.wizards; 2 3 import java.io.File ; 4 import java.lang.reflect.InvocationTargetException ; 5 6 import org.eclipse.core.resources.IResource; 7 import org.eclipse.core.resources.IWorkspaceRoot; 8 import org.eclipse.core.resources.ResourcesPlugin; 9 import org.eclipse.core.runtime.CoreException; 10 import org.eclipse.core.runtime.IPath; 11 import org.eclipse.core.runtime.IProgressMonitor; 12 import org.eclipse.jface.dialogs.MessageDialog; 13 import org.eclipse.jface.operation.IRunnableWithProgress; 14 import org.eclipse.jface.viewers.ISelection; 15 import org.eclipse.jface.viewers.IStructuredSelection; 16 import org.eclipse.jface.wizard.Wizard; 17 import org.eclipse.ui.INewWizard; 18 import org.eclipse.ui.IWorkbench; 19 import org.eclipse.ui.IWorkbenchWizard; 20 import org.hibernate.cfg.Configuration; 21 import org.hibernate.cfg.JDBCMetaDataConfiguration; 22 import org.hibernate.cfg.reveng.DefaultReverseEngineeringStrategy; 23 import org.hibernate.cfg.reveng.OverrideRepository; 24 import org.hibernate.cfg.reveng.ReverseEngineeringStrategy; 25 import org.hibernate.console.ConsoleConfiguration; 26 import org.hibernate.console.ImageConstants; 27 import org.hibernate.console.KnownConfigurations; 28 import org.hibernate.console.ConsoleConfiguration.Command; 29 import org.hibernate.eclipse.console.HibernateConsolePlugin; 30 import org.hibernate.eclipse.console.utils.EclipseImages; 31 import org.hibernate.tool.hbm2x.ConfigurationNavigator; 32 import org.hibernate.tool.hbm2x.DocExporter; 33 import org.hibernate.tool.hbm2x.Exporter; 34 import org.hibernate.tool.hbm2x.HibernateConfigurationExporter; 35 import org.hibernate.tool.hbm2x.HibernateMappingExporter; 36 import org.hibernate.tool.hbm2x.POJOExporter; 37 38 48 49 public class ArtifactGeneratorWizard extends Wizard implements INewWizard { 50 private BasicGeneratorSettingsPage page; 51 private ISelection selection; 52 53 56 public ArtifactGeneratorWizard() { 57 super(); 58 setDefaultPageImageDescriptor(EclipseImages.getImageDescriptor(ImageConstants.NEW_WIZARD)); 59 setNeedsProgressMonitor(true); 60 } 61 62 65 66 public void addPages() { 67 page = new BasicGeneratorSettingsPage(selection); 68 addPage(page); 69 } 70 71 76 public boolean performFinish() { 77 final String outputPackage = page.getOutputPackage(); 78 final IPath output = page.getOutputDirectory(); 79 80 if(!MessageDialog.openQuestion(getShell(), "Start artifact generation", "Do you want to start generating artifcats into " + output.toPortableString() + ",\npossibly overwriting existing files in this directory ?")) { 81 return false; 82 } 83 84 final IPath revengsettings = page.getReverseEngineeringSettingsFile(); 85 final String configurationName = page.getConfigurationName(); 86 final boolean reveng = page.isReverseEngineerEnabled(); 87 final boolean genjava = page.isGenerateJava(); 88 final boolean genhbm = page.isGenerateMappings(); 89 final boolean gencfg = page.isGenerateCfg(); 90 final boolean preferRaw = page.isPreferRawCompositeIds(); 91 final boolean ejb3 = page.isEJB3Enabled(); 92 final boolean gendoc = page.isGenerateDoc(); 93 94 final IPath templatedir = page.getTemplateDirectory(); 95 IRunnableWithProgress op = new IRunnableWithProgress() { 96 public void run(IProgressMonitor monitor) throws InvocationTargetException { 97 try { 98 doFinish(configurationName, output, outputPackage, revengsettings, reveng, genjava, genhbm, gencfg, monitor, preferRaw, templatedir, ejb3, gendoc); 99 } catch (CoreException e) { 100 throw new InvocationTargetException (e); 101 } finally { 102 monitor.done(); 103 } 104 } 105 }; 106 try { 107 getContainer().run(true, false, op); 108 } catch (InterruptedException e) { 109 return false; 110 } catch (InvocationTargetException e) { 111 Throwable realException = e.getTargetException(); 112 HibernateConsolePlugin.getDefault().showError(getShell(), "Error under artifact generation", realException); 113 return false; 114 } 115 return true; 116 } 117 118 131 132 private void doFinish( 133 String configName, IPath output, 134 String outputPackage, IPath revengsettings, boolean reveng, final boolean genjava, final boolean genhbm, final boolean gencfg, final IProgressMonitor monitor, boolean preferRawCompositeids, IPath templateDir, final boolean ejb3, final boolean gendoc) 135 throws CoreException { 136 monitor.beginTask("Generating artifacts for " + configName, 10); 138 139 IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); 140 final IResource resource = root.findMember(output); 141 final IResource templateres = root.findMember(templateDir); 142 final IResource revengres = root.findMember(revengsettings); 143 146 147 148 ConsoleConfiguration cc = KnownConfigurations.getInstance().find(configName); 149 ReverseEngineeringStrategy res = null; 150 if (reveng) { 151 monitor.subTask("reading jdbc metadata"); 152 153 DefaultReverseEngineeringStrategy configurableNamingStrategy = new DefaultReverseEngineeringStrategy(); 154 configurableNamingStrategy.setPackageName(outputPackage); 155 156 res = configurableNamingStrategy; 157 if(revengsettings!=null) { 158 File file = revengres.getRawLocation().toFile(); 159 OverrideRepository repository = new OverrideRepository(); 160 repository.addFile(file); 161 res = repository.getReverseEngineeringStrategy(res); 162 } 163 } 164 final Configuration cfg = buildConfiguration(reveng, cc, res, preferRawCompositeids); 165 monitor.worked(3); 166 167 cc.execute(new Command() { 168 public Object execute() { 169 File outputdir = resource.getRawLocation().toFile(); 170 171 String [] templatePaths = new String [0]; 172 173 if(templateres!=null) { 174 templatePaths = new String [] { templateres.getRawLocation().toOSString() }; 175 } 176 177 final HibernateMappingExporter hbmExporter = new HibernateMappingExporter(cfg, outputdir); 178 hbmExporter.setTemplatePaths(templatePaths); 179 final POJOExporter javaExporter = new POJOExporter(cfg, outputdir); javaExporter.setEjb3(ejb3); 181 javaExporter.setGenerics(ejb3); 182 javaExporter.setTemplatePaths(templatePaths); 183 final Exporter cfgExporter = new HibernateConfigurationExporter(cfg, outputdir); 184 185 if(genhbm) { 186 monitor.subTask("mapping files"); 187 hbmExporter.start(); 188 monitor.worked(5); 189 } 190 191 if(genjava) { 192 monitor.subTask("domain code"); 193 javaExporter.start(); 194 monitor.worked(6); 195 } 196 197 if(gencfg) { 198 monitor.subTask("hibernate configuration"); 199 cfgExporter.start(); 200 monitor.worked(7); 201 } 202 203 if(gendoc) { 204 monitor.subTask("hibernate doc"); 205 new DocExporter(cfg, outputdir).start(); 206 monitor.worked(8); 207 } 208 try { 209 resource.refreshLocal(IResource.DEPTH_INFINITE, monitor); 210 } catch (CoreException e) { 211 HibernateConsolePlugin.getDefault().logErrorMessage("Problem refreshing", e); 212 } 213 214 monitor.worked(10); 215 return null; 216 } 217 }); 218 } 219 220 227 private Configuration buildConfiguration(boolean reveng, ConsoleConfiguration cc, ReverseEngineeringStrategy revEngStrategy, boolean rawCompositeids) { 228 if(reveng) { 229 final JDBCMetaDataConfiguration cfg = new JDBCMetaDataConfiguration(); 230 cc.buildWith(cfg,false); 231 cfg.setReverseEngineeringStrategy(revEngStrategy); 232 cfg.setPreferRawCompositeIds(rawCompositeids); 233 234 cc.execute(new Command() { 236 public Object execute() { 237 cfg.readFromJDBC(); 238 cfg.buildMappings(); 239 return null; 240 } 241 }); 242 243 return cfg; 244 } else { 245 final Configuration configuration = new Configuration(); 246 cc.buildWith(configuration, true); 247 248 cc.execute(new Command() { 249 public Object execute() { 250 251 configuration.buildMappings(); 252 return configuration; 253 } 254 }); 255 return configuration; 256 } 257 } 258 259 264 public void init(IWorkbench workbench, IStructuredSelection selection) { 265 this.selection = selection; 266 } 267 } | Popular Tags |