KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > eclipse > console > views > SchemaExportAction


1 /*
2  * Created on 08-Dec-2004
3  *
4  */

5 package org.hibernate.eclipse.console.views;
6
7 import java.util.Iterator JavaDoc;
8
9 import org.eclipse.jface.dialogs.MessageDialog;
10 import org.eclipse.jface.viewers.StructuredViewer;
11 import org.hibernate.HibernateException;
12 import org.hibernate.console.ConsoleConfiguration;
13 import org.hibernate.console.node.BaseNode;
14 import org.hibernate.eclipse.console.HibernateConsolePlugin;
15 import org.hibernate.eclipse.console.actions.ConsoleConfigurationBasedAction;
16 import org.hibernate.tool.hbm2ddl.SchemaExport;
17
18 /**
19  * @author max
20  *
21  */

22 public class SchemaExportAction extends ConsoleConfigurationBasedAction {
23
24     private StructuredViewer viewer;
25
26     /**
27      * @param text
28      */

29     protected SchemaExportAction(String JavaDoc text) {
30         super(text);
31     }
32
33     /**
34      * @param selectionProvider
35      */

36     public SchemaExportAction(StructuredViewer selectionProvider) {
37         super("Run SchemaExport");
38         this.viewer = selectionProvider;
39     }
40
41     public void doRun() {
42         for (Iterator JavaDoc i = getSelectedNonResources().iterator(); i.hasNext();) {
43             try {
44                 BaseNode node = ((BaseNode) i.next());
45                 final ConsoleConfiguration config = node
46                         .getConsoleConfiguration();
47                 config
48                         .execute(new org.hibernate.console.ConsoleConfiguration.Command() {
49                             public Object JavaDoc execute() {
50                                 if (config.getConfiguration() != null
51                                         && MessageDialog.openConfirm(viewer
52                                                 .getControl().getShell(),
53                                                 "Run SchemaExport",
54                                                 "Are you sure you want to run SchemaExport on '"
55                                                         + config.getName()
56                                                         + "'?")) {
57                                     SchemaExport export = new SchemaExport(
58                                             config.getConfiguration());
59                                     export.create(false, true);
60                                     if (!export.getExceptions().isEmpty()) {
61                                         Iterator JavaDoc iterator = export
62                                                 .getExceptions().iterator();
63                                         int cnt = 1;
64                                         while (iterator.hasNext()) {
65                                             Throwable JavaDoc element = (Throwable JavaDoc) iterator
66                                                     .next();
67                                             HibernateConsolePlugin
68                                                     .getDefault().logErrorMessage(
69                                                             "Error "
70                                                                     + cnt++
71                                                                     + " while performing SchemaExport",
72                                                             element);
73                                         }
74                                         HibernateConsolePlugin
75                                                 .getDefault().showError(
76                                                         viewer.getControl()
77                                                                 .getShell(),
78                                                         cnt
79                                                                 - 1
80                                                                 + " error(s) while performing SchemaExport, see Error Log for details",
81                                                         null);
82                                     }
83                                 }
84                                 return null;
85                             }
86                         });
87                 viewer.refresh(node); // todo: should we do it here or should
88
// the view just react to config being
89
// build ?
90
} catch (HibernateException he) {
91                 HibernateConsolePlugin.getDefault().showError(
92                         viewer.getControl().getShell(),
93                         "Exception while running SchemaExport", he);
94             }
95         }
96     }
97
98     /*
99      * (non-Javadoc)
100      *
101      * @see org.hibernate.eclipse.console.actions.SessionFactoryBasedAction#updateState(org.hibernate.console.ConsoleConfiguration)
102      */

103     protected boolean updateState(ConsoleConfiguration consoleConfiguration) {
104         return consoleConfiguration.hasConfiguration();
105     }
106 }
107
Popular Tags