KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > eclipse > console > wizards > ArtifactGeneratorWizard


1 package org.hibernate.eclipse.console.wizards;
2
3 import java.io.File JavaDoc;
4 import java.lang.reflect.InvocationTargetException JavaDoc;
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 /**
39  * This is a sample new wizard. Its role is to create a new file
40  * resource in the provided container. If the container resource
41  * (a folder or a project) is selected in the workspace
42  * when the wizard is opened, it will accept it as the target
43  * container. The wizard creates one file with the extension
44  * "mpe". If a sample multi-page editor (also available
45  * as a template) is registered for the same extension, it will
46  * be able to open it.
47  */

48
49 public class ArtifactGeneratorWizard extends Wizard implements INewWizard {
50     private BasicGeneratorSettingsPage page;
51     private ISelection selection;
52
53     /**
54      * Constructor for ArtifactGeneratorWizard.
55      */

56     public ArtifactGeneratorWizard() {
57         super();
58         setDefaultPageImageDescriptor(EclipseImages.getImageDescriptor(ImageConstants.NEW_WIZARD));
59         setNeedsProgressMonitor(true);
60     }
61     
62     /**
63      * Adding the page to the wizard.
64      */

65
66     public void addPages() {
67         page = new BasicGeneratorSettingsPage(selection);
68         addPage(page);
69     }
70
71     /**
72      * This method is called when 'Finish' button is pressed in
73      * the wizard. We will create an operation and run it
74      * using wizard as execution context.
75      */

76     public boolean performFinish() {
77         final String JavaDoc 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 JavaDoc 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 JavaDoc {
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 JavaDoc(e);
101                 } finally {
102                     monitor.done();
103                 }
104             }
105         };
106         try {
107             getContainer().run(true, false, op);
108         } catch (InterruptedException JavaDoc e) {
109             return false;
110         } catch (InvocationTargetException JavaDoc e) {
111             Throwable JavaDoc realException = e.getTargetException();
112             HibernateConsolePlugin.getDefault().showError(getShell(), "Error under artifact generation", realException);
113             return false;
114         }
115         return true;
116     }
117     
118     /**
119      * The worker method. It will find the container, create the
120      * file if missing or just replace its contents, and open
121      * the editor on the newly created file.
122      * @param outputPackage
123      * @param revengsettings
124      * @param gencfg
125      * @param genhbm
126      * @param genjava
127      * @param reveng
128      * @param preferRawCompositeids
129      * @param gendoc
130      */

131
132     private void doFinish(
133         String JavaDoc configName, IPath output,
134         String JavaDoc 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         // create a sample file
137
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         /*if (!resource.exists() || !(resource instanceof IContainer)) {
144             throwCoreException("Output directory \"" + configName + "\" does not exist.");
145         }*/

146         /*IContainer container = (IContainer) resource;*/
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 JavaDoc 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 JavaDoc execute() {
169                 File JavaDoc outputdir = resource.getRawLocation().toFile();
170                 
171                 String JavaDoc[] templatePaths = new String JavaDoc[0];
172         
173                 if(templateres!=null) {
174                     templatePaths = new String JavaDoc[] { 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); // TODO: expose generics as an option
180
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     /**
221      * @param reveng
222      * @param cc
223      * @param rawCompositeids
224      * @param configurableReverseNamingStrategy TODO
225      * @return
226      */

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() { // need to execute in the consoleconfiguration to let it handle classpath stuff!
235

236                 public Object JavaDoc 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 JavaDoc execute() {
250                     
251                     configuration.buildMappings();
252                     return configuration;
253                 }
254             });
255             return configuration;
256         }
257     }
258
259     /**
260      * We will accept the selection in the workbench to see if
261      * we can initialize from it.
262      * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection)
263      */

264     public void init(IWorkbench workbench, IStructuredSelection selection) {
265         this.selection = selection;
266     }
267 }
Popular Tags