001: package org.netbeans.modules.mashup.tables.wizard;
002:
003: import java.io.File;
004: import java.sql.Connection;
005: import java.sql.SQLException;
006: import java.sql.Statement;
007: import java.util.HashMap;
008: import java.util.Iterator;
009: import java.util.Map;
010: import javax.swing.JPanel;
011:
012: import org.netbeans.modules.mashup.db.bootstrap.FlatfileBootstrapParserFactory;
013: import org.netbeans.modules.mashup.db.common.FlatfileDBConnectionFactory;
014: import org.netbeans.modules.mashup.db.common.PropertyKeys;
015: import org.netbeans.modules.mashup.db.model.FlatfileDBTable;
016: import org.netbeans.modules.mashup.db.model.FlatfileDatabaseModel;
017:
018: import com.sun.sql.framework.utils.StringUtil;
019: import java.awt.Dimension;
020: import java.awt.event.KeyAdapter;
021: import java.awt.event.KeyEvent;
022: import net.java.hulp.i18n.Logger;
023: import org.netbeans.modules.etl.logger.Localizer;
024: import org.netbeans.modules.etl.logger.LogUtil;
025:
026: public final class TableDetailsVisualPanel extends JPanel {
027:
028: private static transient final Logger mLogger = LogUtil
029: .getLogger(TableDetailsVisualPanel.class.getName());
030: private static transient final Localizer mLoc = Localizer.get();
031: private TableDetailsPanel owner;
032: private FlatfileDatabaseModel currentModel;
033: private FlatfileDBTable currentTable;
034: private String url;
035: private static Map<String, String> encodingMap = new HashMap<String, String>();
036: private static Map<String, String> typeMap = new HashMap<String, String>();
037:
038: static {
039: encodingMap.put("ASCII (ISO646-US)", "US-ASCII");
040: }
041:
042: static {
043: typeMap.put("Spreadsheet (MS Excel)", PropertyKeys.SPREADSHEET);
044: typeMap.put("Web Row Set", PropertyKeys.WEBROWSET);
045: typeMap.put("Web (HTML)", PropertyKeys.WEB);
046: typeMap.put("XML", PropertyKeys.XML);
047: typeMap.put("Delimited Flatfile", PropertyKeys.DELIMITED);
048: typeMap.put("Fixed Width Flatfile", PropertyKeys.FIXEDWIDTH);
049: typeMap.put("RSS", PropertyKeys.RSS);
050: }
051:
052: /**
053: * Creates new form ChooseTableVisualPanel
054: */
055: public TableDetailsVisualPanel(TableDetailsPanel panel) {
056: owner = panel;
057: initComponents();
058: tableName.addKeyListener(new KeyAdapter() {
059:
060: @Override
061: public void keyReleased(KeyEvent e) {
062: checkTableName(tableName.getText().trim());
063: owner.fireChangeEvent();
064: }
065: });
066: setMinimumSize(new Dimension(100, 100));
067: setMaximumSize(new Dimension(100, 100));
068: setPreferredSize(new Dimension(100, 100));
069: }
070:
071: @Override
072: public String getName() {
073: String nbBundle1 = mLoc.t("PRSR001: Enter Table Details");
074: return Localizer.parse(nbBundle1);
075: }
076:
077: /*
078: * Checks whether the contents of the table name text field are valid. @return
079: * INVALID_NAME if name is invalid, DUPLICATE_NAME if name is already in use in the
080: * current MashupDatabaseModel, or OK if name is valid.
081: */
082: private boolean checkTableName(String tableName) {
083: String newName = tableName.trim();
084:
085: if (!StringUtil.isValid(newName, "[A-Za-z][A-Za-z0-9_]*")) {
086: setError("Invalid Table Name.");
087: return false;
088: } else if (!isUniqueTableName(newName)) {
089: setError("Duplicate Table Name");
090: return false;
091: }
092:
093: if (isAxionReservedName(newName)) {
094: setError("Reserved Table Name used.");
095: return false;
096: }
097: setError("");
098: return true;
099: }
100:
101: public void setDBModel(FlatfileDatabaseModel model) {
102: currentModel = model;
103: }
104:
105: public void setCurrentTable(FlatfileDBTable table) {
106: currentTable = table;
107: }
108:
109: public void setJDBCUrl(String jdbcURL) {
110: url = jdbcURL;
111: }
112:
113: public String getTableName() {
114: return tableName.getText().trim();
115: }
116:
117: public String getTableType() {
118: String type = (String) typeCombo.getSelectedItem();
119: return typeMap.get(type);
120: }
121:
122: public String getEncoding() {
123: String key = (String) encodingCombo.getSelectedItem();
124: return encodingMap.get(key);
125: }
126:
127: private void setError(String errorText) {
128: error.setText(errorText);
129: }
130:
131: private boolean isUniqueTableName(String tblName) {
132: if (currentModel == null) {
133: return false;
134: }
135: int ind = 0;
136: ind = url.indexOf(":", ind + 1);
137: ind = url.indexOf(":", ind + 1);
138: String path = url.substring(url.indexOf(":", ind + 1) + 1)
139: + "\\";
140: File f = new File(path + tblName);
141: if (f.exists()) {
142: return false;
143: }
144: FlatfileDBTable match = currentModel
145: .getFileMatchingTableName(tblName);
146: return (match == null) || (match == currentTable);
147: }
148:
149: private boolean isAxionReservedName(String newName) {
150: boolean isReservedName = false;
151: Connection conn = null;
152: Statement stmt = null;
153:
154: try {
155: conn = FlatfileDBConnectionFactory.getInstance()
156: .getConnection(url);
157: stmt = conn.createStatement();
158: stmt.execute("create table " + newName + " (id int)");
159: } catch (Exception se) {
160: isReservedName = true;
161: } finally {
162: if (stmt != null) {
163: try {
164: stmt.execute("drop table " + newName);
165: stmt.execute("shutdown");
166: } catch (SQLException ignore) {
167: // Ignore.
168: }
169: }
170:
171: if (conn != null) {
172: try {
173: conn.close();
174: } catch (SQLException ignore) {
175: // Ignore.
176: }
177: }
178: }
179:
180: return isReservedName;
181: }
182:
183: /** This method is called from within the constructor to
184: * initialize the form.
185: * WARNING: Do NOT modify this code. The content of this method is
186: * always regenerated by the Form Editor.
187: */
188: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
189: private void initComponents() {
190: jPanel1 = new javax.swing.JPanel();
191: jLabel1 = new javax.swing.JLabel();
192: tableName = new javax.swing.JTextField();
193: jLabel2 = new javax.swing.JLabel();
194: encodingCombo = new javax.swing.JComboBox();
195: jLabel3 = new javax.swing.JLabel();
196: typeCombo = new javax.swing.JComboBox();
197: error = new javax.swing.JLabel();
198: jLabel4 = new javax.swing.JLabel();
199: resourceUrl = new javax.swing.JLabel();
200:
201: setMaximumSize(new java.awt.Dimension(450, 350));
202: setMinimumSize(new java.awt.Dimension(100, 100));
203: setPreferredSize(new java.awt.Dimension(400, 200));
204: jPanel1.setBorder(javax.swing.BorderFactory
205: .createTitledBorder("Enter Table Details"));
206: org.openide.awt.Mnemonics.setLocalizedText(jLabel1,
207: "Table Name");
208:
209: org.openide.awt.Mnemonics.setLocalizedText(jLabel2, "Encoding");
210:
211: encodingCombo.setModel(new javax.swing.DefaultComboBoxModel(
212: new String[] { "ASCII (ISO646-US)" }));
213:
214: org.openide.awt.Mnemonics.setLocalizedText(jLabel3,
215: "Table Type");
216:
217: typeCombo.setModel(new javax.swing.DefaultComboBoxModel(
218: new String[] { "Delimited Flatfile",
219: "Fixed Width Flatfile", "RSS",
220: "Spreadsheet (MS Excel)", "XML", "Web (HTML)",
221: "Web Row Set" }));
222:
223: error.setForeground(new java.awt.Color(255, 102, 102));
224:
225: org.openide.awt.Mnemonics.setLocalizedText(jLabel4,
226: "Resource URL");
227:
228: resourceUrl.setForeground(new java.awt.Color(0, 0, 255));
229:
230: org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(
231: jPanel1);
232: jPanel1.setLayout(jPanel1Layout);
233: jPanel1Layout
234: .setHorizontalGroup(jPanel1Layout
235: .createParallelGroup(
236: org.jdesktop.layout.GroupLayout.LEADING)
237: .add(
238: org.jdesktop.layout.GroupLayout.TRAILING,
239: jPanel1Layout
240: .createSequentialGroup()
241: .add(
242: jPanel1Layout
243: .createParallelGroup(
244: org.jdesktop.layout.GroupLayout.LEADING)
245: .add(
246: jPanel1Layout
247: .createParallelGroup(
248: org.jdesktop.layout.GroupLayout.LEADING)
249: .add(
250: jPanel1Layout
251: .createSequentialGroup()
252: .add(
253: jPanel1Layout
254: .createParallelGroup(
255: org.jdesktop.layout.GroupLayout.LEADING)
256: .add(
257: jLabel1,
258: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
259: 70,
260: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
261: .add(
262: org.jdesktop.layout.GroupLayout.TRAILING,
263: jLabel2,
264: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
265: 107,
266: Short.MAX_VALUE))
267: .addPreferredGap(
268: org.jdesktop.layout.LayoutStyle.RELATED))
269: .add(
270: jPanel1Layout
271: .createSequentialGroup()
272: .add(
273: jLabel3,
274: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
275: 74,
276: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
277: .add(
278: 20,
279: 20,
280: 20)))
281: .add(
282: jPanel1Layout
283: .createSequentialGroup()
284: .add(
285: jLabel4)
286: .add(
287: 34,
288: 34,
289: 34)))
290: .add(
291: jPanel1Layout
292: .createParallelGroup(
293: org.jdesktop.layout.GroupLayout.LEADING)
294: .add(
295: org.jdesktop.layout.GroupLayout.TRAILING,
296: error,
297: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
298: 261,
299: Short.MAX_VALUE)
300: .add(
301: tableName,
302: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
303: 261,
304: Short.MAX_VALUE)
305: .add(
306: jPanel1Layout
307: .createParallelGroup(
308: org.jdesktop.layout.GroupLayout.TRAILING,
309: false)
310: .add(
311: org.jdesktop.layout.GroupLayout.LEADING,
312: encodingCombo,
313: 0,
314: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
315: Short.MAX_VALUE)
316: .add(
317: org.jdesktop.layout.GroupLayout.LEADING,
318: typeCombo,
319: 0,
320: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
321: Short.MAX_VALUE))
322: .add(
323: resourceUrl,
324: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
325: 249,
326: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
327: .addContainerGap()));
328: jPanel1Layout
329: .setVerticalGroup(jPanel1Layout
330: .createParallelGroup(
331: org.jdesktop.layout.GroupLayout.LEADING)
332: .add(
333: jPanel1Layout
334: .createSequentialGroup()
335: .add(
336: jPanel1Layout
337: .createParallelGroup(
338: org.jdesktop.layout.GroupLayout.BASELINE)
339: .add(
340: tableName,
341: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
342: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
343: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
344: .add(jLabel1))
345: .addPreferredGap(
346: org.jdesktop.layout.LayoutStyle.RELATED)
347: .add(
348: error,
349: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
350: 13,
351: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
352: .addPreferredGap(
353: org.jdesktop.layout.LayoutStyle.RELATED)
354: .add(
355: jPanel1Layout
356: .createParallelGroup(
357: org.jdesktop.layout.GroupLayout.BASELINE)
358: .add(jLabel2)
359: .add(
360: encodingCombo,
361: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
362: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
363: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
364: .add(25, 25, 25)
365: .add(
366: jPanel1Layout
367: .createParallelGroup(
368: org.jdesktop.layout.GroupLayout.BASELINE)
369: .add(jLabel3)
370: .add(
371: typeCombo,
372: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
373: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
374: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
375: .add(30, 30, 30)
376: .add(
377: jPanel1Layout
378: .createParallelGroup(
379: org.jdesktop.layout.GroupLayout.BASELINE)
380: .add(jLabel4)
381: .add(
382: resourceUrl,
383: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
384: 14,
385: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
386: .addContainerGap(11,
387: Short.MAX_VALUE)));
388:
389: org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
390: this );
391: this .setLayout(layout);
392: layout.setHorizontalGroup(layout.createParallelGroup(
393: org.jdesktop.layout.GroupLayout.LEADING).add(
394: org.jdesktop.layout.GroupLayout.TRAILING, jPanel1,
395: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
396: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
397: Short.MAX_VALUE));
398: layout
399: .setVerticalGroup(layout
400: .createParallelGroup(
401: org.jdesktop.layout.GroupLayout.LEADING)
402: .add(
403: org.jdesktop.layout.GroupLayout.TRAILING,
404: layout
405: .createSequentialGroup()
406: .add(
407: jPanel1,
408: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
409: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
410: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
411: .addContainerGap(
412: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
413: Short.MAX_VALUE)));
414: }// </editor-fold>//GEN-END:initComponents
415:
416: public boolean canAdvance() {
417: return (typeCombo.getSelectedIndex() != -1 && checkTableName(tableName
418: .getText().trim()));
419: }
420:
421: public void guessParserType(FlatfileDBTable table) {
422: String type = FlatfileBootstrapParserFactory.getInstance()
423: .getParserType(table);
424: if (typeMap.containsValue(type)) {
425: typeCombo.setSelectedIndex(0);
426: Iterator it = typeMap.keySet().iterator();
427: while (it.hasNext()) {
428: String key = (String) it.next();
429: if (typeMap.get(key).equals(type)) {
430: typeCombo.setSelectedItem(key);
431: break;
432: }
433: }
434: }
435: }
436:
437: private String getTableName(String fileName) {
438: // Use only fileName
439: if (fileName.lastIndexOf("//") != -1) {
440: fileName = fileName
441: .substring(fileName.lastIndexOf("/") + 1);
442: } else {
443: if (fileName.lastIndexOf("/") != -1) {
444: fileName = fileName
445: .substring(fileName.lastIndexOf("/") + 1);
446: }
447: if (fileName.lastIndexOf("\\") != -1) {
448: fileName = fileName.substring(fileName
449: .lastIndexOf("\\") + 1);
450: }
451:
452: if (fileName.lastIndexOf(".") != -1) {
453: fileName = fileName.substring(0, fileName
454: .lastIndexOf("."));
455: }
456: }
457: return StringUtil.isNullString(fileName) ? "<table name>"
458: : StringUtil.createTableNameFromFileName(fileName);
459: }
460:
461: public void setFileName(String fileName) {
462: tableName.setText(getTableName(fileName));
463: }
464:
465: public void setResourceUrl(String text) {
466: resourceUrl.setText(text.trim());
467: }
468:
469: public String getResourceUrl() {
470: return resourceUrl.getText().trim();
471: }
472:
473: // Variables declaration - do not modify//GEN-BEGIN:variables
474: private javax.swing.JComboBox encodingCombo;
475: private javax.swing.JLabel error;
476: private javax.swing.JLabel jLabel1;
477: private javax.swing.JLabel jLabel2;
478: private javax.swing.JLabel jLabel3;
479: private javax.swing.JLabel jLabel4;
480: private javax.swing.JPanel jPanel1;
481: private javax.swing.JLabel resourceUrl;
482: private javax.swing.JTextField tableName;
483: private javax.swing.JComboBox typeCombo;
484: // End of variables declaration//GEN-END:variables
485: }
|