001: package org.netbeans.modules.mashup.db.wizard;
002:
003: import java.awt.Component;
004: import java.awt.Dialog;
005: import java.sql.Connection;
006: import java.sql.SQLException;
007: import java.sql.Statement;
008: import java.text.MessageFormat;
009: import java.util.Iterator;
010: import java.util.List;
011: import javax.swing.JComponent;
012: import net.java.hulp.i18n.Logger;
013: import org.axiondb.ExternalConnectionProvider;
014: import org.netbeans.modules.etl.logger.Localizer;
015: import org.netbeans.modules.etl.logger.LogUtil;
016: import org.netbeans.modules.mashup.db.common.FlatfileDBConnectionFactory;
017: import org.netbeans.modules.mashup.db.ui.wizard.SelectDatabasePanel;
018: import org.netbeans.modules.mashup.tables.wizard.JDBCTablePanel;
019: import org.netbeans.modules.sql.framework.ui.utils.AxionExternalConnectionProvider;
020: import org.openide.DialogDisplayer;
021: import org.openide.NotifyDescriptor;
022: import org.openide.WizardDescriptor;
023: import org.openide.util.HelpCtx;
024: import org.openide.util.actions.CallableSystemAction;
025:
026: public final class NewJDBCTableAction extends CallableSystemAction {
027: private static transient final Logger mLogger = LogUtil
028: .getLogger(NewJDBCTableAction.class.getName());
029: private static transient final Localizer mLoc = Localizer.get();
030: public String nbBundle1 = mLoc.t("PRSR001: Add JDBC Table(s)");
031: private WizardDescriptor.Panel[] panels;
032: public static final String DEFAULT_FLATFILE_JDBC_URL_PREFIX = "jdbc:axiondb:";
033:
034: public void performAction() {
035: WizardDescriptor wizardDescriptor = new WizardDescriptor(
036: getPanels());
037: // {0} will be replaced by WizardDesriptor.Panel.getComponent().getName()
038: wizardDescriptor.setTitleFormat(new MessageFormat("{0}"));
039: wizardDescriptor.setTitle(Localizer.parse(nbBundle1));
040: Dialog dialog = DialogDisplayer.getDefault().createDialog(
041: wizardDescriptor);
042: dialog
043: .getAccessibleContext()
044: .setAccessibleDescription(
045: "This dialog lets user to create flatfile tables from jdbc sources");
046: dialog.setVisible(true);
047: dialog.toFront();
048: boolean cancelled = wizardDescriptor.getValue() != WizardDescriptor.FINISH_OPTION;
049: if (!cancelled) {
050: boolean status = false;
051: try {
052: Thread.currentThread().getContextClassLoader()
053: .loadClass(
054: AxionExternalConnectionProvider.class
055: .getName());
056: System
057: .setProperty(
058: ExternalConnectionProvider.EXTERNAL_CONNECTION_PROVIDER_PROPERTY_NAME,
059: AxionExternalConnectionProvider.class
060: .getName());
061: } catch (ClassNotFoundException ex) {
062: //ignore
063: }
064: String jdbcUrl = (String) wizardDescriptor
065: .getProperty("url");
066: List<String> dblinks = (List<String>) wizardDescriptor
067: .getProperty("dblinks");
068: List<String> statements = (List<String>) wizardDescriptor
069: .getProperty("statements");
070: Connection conn = null;
071: Statement stmt = null;
072: try {
073: conn = FlatfileDBConnectionFactory.getInstance()
074: .getConnection(jdbcUrl);
075: if (conn != null) {
076: conn.setAutoCommit(true);
077: stmt = conn.createStatement();
078: }
079:
080: Iterator it = dblinks.iterator();
081: while (it.hasNext()) {
082: String sql = (String) it.next();
083: stmt.execute(sql);
084: }
085:
086: it = statements.iterator();
087: while (it.hasNext()) {
088: String sql = (String) it.next();
089: stmt.execute(sql);
090: }
091: status = true;
092: } catch (Exception ex) {
093:
094: } finally {
095: if (conn != null) {
096: try {
097: if (stmt != null) {
098: stmt.execute("shutdown");
099: }
100: conn.close();
101: } catch (SQLException ex) {
102: conn = null;
103: }
104: }
105: }
106: if (status) {
107: String nbBundle2 = mLoc
108: .t("PRSR001: Tables successfully created.");
109: NotifyDescriptor d = new NotifyDescriptor.Message(
110: Localizer.parse(nbBundle2),
111: NotifyDescriptor.INFORMATION_MESSAGE);
112: DialogDisplayer.getDefault().notify(d);
113: } else {
114: String nbBundle3 = mLoc
115: .t("PRSR001: Tables creation failed.");
116: NotifyDescriptor d = new NotifyDescriptor.Message(
117: Localizer.parse(nbBundle3),
118: NotifyDescriptor.WARNING_MESSAGE);
119: DialogDisplayer.getDefault().notify(d);
120: }
121: }
122: }
123:
124: /**
125: * Initialize panels representing individual wizard's steps and sets
126: * various properties for them influencing wizard appearance.
127: */
128: private WizardDescriptor.Panel[] getPanels() {
129: if (panels == null) {
130: panels = new WizardDescriptor.Panel[] {
131: new SelectDatabasePanel(), new JDBCTablePanel() };
132: String[] steps = new String[panels.length];
133: for (int i = 0; i < panels.length; i++) {
134: Component c = panels[i].getComponent();
135: // Default step name to component name of panel. Mainly useful
136: // for getting the name of the target chooser to appear in the
137: // list of steps.
138: steps[i] = c.getName();
139: if (c instanceof JComponent) { // assume Swing components
140: JComponent jc = (JComponent) c;
141: // Sets step number of a component
142: jc.putClientProperty(
143: "WizardPanel_contentSelectedIndex",
144: new Integer(i));
145: // Sets steps names for a panel
146: jc.putClientProperty("WizardPanel_contentData",
147: steps);
148: // Turn on subtitle creation on each step
149: jc.putClientProperty("WizardPanel_autoWizardStyle",
150: Boolean.TRUE);
151: // Show steps on the left side with the image on the background
152: jc.putClientProperty(
153: "WizardPanel_contentDisplayed",
154: Boolean.TRUE);
155: // Turn on numbering of all steps
156: jc.putClientProperty("WizardPanel_contentNumbered",
157: Boolean.TRUE);
158: }
159: }
160: }
161: return panels;
162: }
163:
164: public String getName() {
165: return Localizer.parse(nbBundle1);
166: }
167:
168: @Override
169: public String iconResource() {
170: return null;
171: }
172:
173: public HelpCtx getHelpCtx() {
174: return HelpCtx.DEFAULT_HELP;
175: }
176:
177: @Override
178: protected boolean asynchronous() {
179: return false;
180: }
181: }
|