001: package org.netbeans.modules.mashup.db.wizard;
002:
003: import java.awt.Dialog;
004: import java.sql.Connection;
005: import java.sql.SQLException;
006: import java.sql.Statement;
007: import java.text.MessageFormat;
008: import java.util.Iterator;
009: import java.util.List;
010:
011: import net.java.hulp.i18n.Logger;
012: import org.netbeans.modules.etl.logger.Localizer;
013: import org.netbeans.modules.etl.logger.LogUtil;
014: import org.netbeans.modules.mashup.db.common.FlatfileDBConnectionFactory;
015: import org.netbeans.modules.mashup.db.common.PropertyKeys;
016: import org.netbeans.modules.mashup.db.model.FlatfileDBTable;
017: import org.netbeans.modules.mashup.db.model.FlatfileDatabaseModel;
018: import org.netbeans.modules.mashup.db.model.impl.FlatfileDBTableImpl;
019: import org.netbeans.modules.mashup.tables.wizard.MashupTableWizardIterator;
020: import org.openide.DialogDisplayer;
021: import org.openide.ErrorManager;
022: import org.openide.NotifyDescriptor;
023: import org.openide.WizardDescriptor;
024: import org.openide.util.HelpCtx;
025: import org.openide.util.actions.CallableSystemAction;
026:
027: public final class NewFlatfileTableAction extends CallableSystemAction {
028: private static transient final Logger mLogger = LogUtil
029: .getLogger(NewFlatfileTableAction.class.getName());
030: private static transient final Localizer mLoc = Localizer.get();
031: public String nbBundle1 = mLoc.t("PRSR001: Add External Table(s)");
032:
033: public void performAction() {
034: WizardDescriptor.Iterator iterator = new MashupTableWizardIterator();
035: WizardDescriptor wizardDescriptor = new WizardDescriptor(
036: iterator);
037: wizardDescriptor.setTitleFormat(new MessageFormat("{0} ({1})"));
038: wizardDescriptor.setTitle(Localizer.parse(nbBundle1));
039: ((MashupTableWizardIterator) iterator)
040: .setDescriptor(wizardDescriptor);
041: Dialog dialog = DialogDisplayer.getDefault().createDialog(
042: wizardDescriptor);
043: dialog
044: .getAccessibleContext()
045: .setAccessibleDescription(
046: "This dialog helps to create a flatfile table from xls,csv and txt sources");
047: dialog.setVisible(true);
048: dialog.toFront();
049: boolean cancelled = wizardDescriptor.getValue() != WizardDescriptor.FINISH_OPTION;
050: if (!cancelled) {
051: String error = null;
052: boolean status = false;
053: String jdbcUrl = (String) wizardDescriptor
054: .getProperty("url");
055: FlatfileDatabaseModel model = (FlatfileDatabaseModel) wizardDescriptor
056: .getProperty(MashupTableWizardIterator.PROP_FLATFILEDBMODEL);
057: List<String> urls = (List<String>) wizardDescriptor
058: .getProperty(MashupTableWizardIterator.URL_LIST);
059: List<String> tables = (List<String>) wizardDescriptor
060: .getProperty(MashupTableWizardIterator.TABLE_LIST);
061: Connection conn = null;
062: Statement stmt = null;
063: try {
064: conn = FlatfileDBConnectionFactory.getInstance()
065: .getConnection(jdbcUrl);
066: if (conn != null) {
067: conn.setAutoCommit(true);
068: stmt = conn.createStatement();
069: }
070:
071: if (model != null) {
072: Iterator tablesIt = model.getTables().iterator();
073: while (tablesIt.hasNext()) {
074: FlatfileDBTable table = (FlatfileDBTable) tablesIt
075: .next();
076: int i = tables.indexOf(table.getName());
077: ((FlatfileDBTableImpl) table).setOrPutProperty(
078: PropertyKeys.FILENAME, urls.get(i));
079: String sql = table.getCreateStatementSQL();
080: stmt.execute(sql);
081: }
082: }
083: status = true;
084: } catch (Exception ex) {
085: ErrorManager.getDefault().log(ex.getMessage());
086: status = false;
087: } finally {
088: if (conn != null) {
089: try {
090: if (stmt != null) {
091: stmt.execute("shutdown");
092: }
093: conn.close();
094: } catch (SQLException ex) {
095: conn = null;
096: }
097: }
098: }
099: if (status) {
100: String nbBundle2 = mLoc
101: .t("PRSR001: Table successfully created.");
102: NotifyDescriptor d = new NotifyDescriptor.Message(
103: Localizer.parse(nbBundle2),
104: NotifyDescriptor.INFORMATION_MESSAGE);
105: DialogDisplayer.getDefault().notify(d);
106: } else {
107: String nbBundle3 = mLoc
108: .t("PRSR001: Table creation failed.");
109: String msg = Localizer.parse(nbBundle3);
110: if (error != null) {
111: msg = msg + "CAUSE:" + error;
112: }
113: NotifyDescriptor d = new NotifyDescriptor.Message(msg,
114: NotifyDescriptor.WARNING_MESSAGE);
115: DialogDisplayer.getDefault().notify(d);
116: }
117: }
118: }
119:
120: public String getName() {
121: return Localizer.parse(nbBundle1);
122: }
123:
124: @Override
125: protected void initialize() {
126: super .initialize();
127: putValue("noIconInMenu", Boolean.TRUE);
128: }
129:
130: public HelpCtx getHelpCtx() {
131: return HelpCtx.DEFAULT_HELP;
132: }
133:
134: @Override
135: protected boolean asynchronous() {
136: return false;
137: }
138: }
|