001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.mashup.db.ui.wizard;
042:
043: import java.awt.Dimension;
044: import java.awt.event.ActionEvent;
045: import java.awt.event.ActionListener;
046: import java.sql.Connection;
047: import java.sql.Statement;
048: import java.util.Iterator;
049: import java.util.List;
050:
051: import org.netbeans.modules.mashup.db.common.FlatfileDBConnectionFactory;
052: import org.netbeans.modules.mashup.db.model.FlatfileDBTable;
053: import org.netbeans.modules.mashup.db.model.FlatfileDatabaseModel;
054: import org.netbeans.modules.mashup.tables.wizard.MashupTableWizardIterator;
055: import org.openide.DialogDisplayer;
056: import org.openide.NotifyDescriptor;
057: import org.openide.WizardDescriptor;
058:
059: import com.sun.sql.framework.exception.BaseException;
060: import net.java.hulp.i18n.Logger;
061: import org.netbeans.modules.etl.logger.Localizer;
062: import org.netbeans.modules.etl.logger.LogUtil;
063: import org.netbeans.modules.sql.framework.model.DBConnectionDefinition;
064:
065: /**
066: * Wizard panel to select tables and columns to be included in an Flatfile DB instance.
067: *
068: * @author Jonathan Giron
069: * @author Ahimanikya Satapathy
070: * @version $Revision$
071: */
072: public class PreviewDatabasePanel extends AbstractWizardPanel implements
073: ActionListener, WizardDescriptor.FinishablePanel {
074:
075: private PreviewDatabaseVisualPanel component;
076: private static transient final Logger mLogger = LogUtil
077: .getLogger(PreviewDatabasePanel.class.getName());
078: private static transient final Localizer mLoc = Localizer.get();
079:
080: /** Creates a new instance of PreviewDatabasePanel */
081: public PreviewDatabasePanel() {
082: component = new PreviewDatabaseVisualPanel(this );
083: component.setPreferredSize(new Dimension(400, 350));
084: }
085:
086: /**
087: * Invoked when an action occurs.
088: *
089: * @param e ActionEvent to handle
090: */
091: public void actionPerformed(ActionEvent e) {
092: }
093:
094: /**
095: * @see org.openide.WizardDescriptor.Panel#getComponent
096: */
097: public java.awt.Component getComponent() {
098: return component;
099: }
100:
101: /**
102: * @see org.openide.WizardDescriptor.Panel#getHelp
103: */
104: public org.openide.util.HelpCtx getHelp() {
105: return null;
106: }
107:
108: public String getStepLabel() {
109: String nbBundle1 = mLoc
110: .t("PRSR001: Preview Flat File Database Definition");
111: return Localizer.parse(nbBundle1);
112: }
113:
114: public String getTitle() {
115: return (component != null) ? component.getName()
116: : "*** Preview Flat File Database ***";
117: }
118:
119: public boolean isValid() {
120: return component.hasValidData();
121: }
122:
123: /**
124: * @see org.openide.WizardDescriptor.Panel#readSettings
125: */
126: public void readSettings(Object settings) {
127: if (settings instanceof WizardDescriptor) {
128: WizardDescriptor wd = (WizardDescriptor) settings;
129:
130: FlatfileDatabaseModel folder = (FlatfileDatabaseModel) wd
131: .getProperty(MashupTableWizardIterator.PROP_FLATFILEDBMODEL);
132: if (folder == null || folder.getTables().size() == 0) {
133: throw new IllegalStateException(
134: "Context must contain a populated FlatfileDatabaseModel.");
135: }
136:
137: component.setModel(folder);
138: super .fireChangeEvent();
139: }
140: }
141:
142: /**
143: * @see org.openide.WizardDescriptor.Panel#storeSettings
144: */
145: public void storeSettings(Object settings) {
146: if (settings instanceof WizardDescriptor) {
147: WizardDescriptor wd = (WizardDescriptor) settings;
148:
149: // Don't commit if user didn't click next or finish.
150: if (wd.getValue() == WizardDescriptor.NEXT_OPTION
151: || wd.getValue() == WizardDescriptor.FINISH_OPTION) {
152: Connection conn = null;
153: Statement stmt = null;
154: try {
155: FlatfileDatabaseModel model = component.getModel();
156: DBConnectionDefinition def = model
157: .getFlatfileDBConnectionDefinition(true);
158: conn = FlatfileDBConnectionFactory.getInstance()
159: .getConnection(def.getConnectionURL());
160: List tables = model.getTables();
161: Iterator it = tables.iterator();
162: while (it.hasNext()) {
163: FlatfileDBTable table = (FlatfileDBTable) it
164: .next();
165: String sql = table.getCreateStatementSQL(table
166: .getLocalFilePath(), table
167: .getTableName(), null, false, true);
168: try {
169: stmt = conn.createStatement();
170: stmt.execute(sql);
171: } catch (Exception ex) {
172: //ignore
173: }
174: }
175: } catch (BaseException ex) {
176: //ignore
177: }
178: if (conn != null) {
179: try {
180: conn.commit();
181: } catch (Exception ex) {
182: //ignore
183: } finally {
184: try {
185: stmt.execute("shutdown");
186: stmt.close();
187: conn.close();
188: } catch (Exception ex) {
189: //ignore
190: }
191: }
192: } else {
193: NotifyDescriptor d = new NotifyDescriptor.Message(
194: "Table(s) creation failed.",
195: NotifyDescriptor.INFORMATION_MESSAGE);
196: DialogDisplayer.getDefault().notify(d);
197: }
198: }
199: }
200: }
201:
202: public boolean isFinishPanel() {
203: return true;
204: }
205: }
|