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.etl.ui.view.wizards;
042:
043: import java.io.IOException;
044: import java.util.ArrayList;
045: import java.util.Collections;
046: import java.util.HashSet;
047: import java.util.List;
048: import java.util.MissingResourceException;
049: import java.util.Set;
050: import javax.swing.SwingUtilities;
051: import net.java.hulp.i18n.Logger;
052: import org.netbeans.api.db.explorer.ConnectionManager;
053: import org.netbeans.api.db.explorer.DatabaseConnection;
054: import org.netbeans.api.project.Project;
055: import org.netbeans.api.project.ProjectUtils;
056: import org.netbeans.api.project.SourceGroup;
057: import org.netbeans.api.project.Sources;
058: import org.netbeans.modules.etl.logger.Localizer;
059: import org.netbeans.modules.etl.logger.LogUtil;
060: import org.netbeans.modules.etl.ui.ETLDataObject;
061: import org.netbeans.modules.sql.framework.common.utils.DBExplorerUtil;
062: import org.netbeans.modules.sql.framework.model.SQLJoinView;
063: import org.netbeans.spi.project.ui.templates.support.Templates;
064: import org.openide.WizardDescriptor;
065: import org.openide.cookies.OpenCookie;
066: import org.openide.filesystems.FileObject;
067: import org.openide.loaders.DataFolder;
068: import org.openide.loaders.DataObject;
069:
070: /**
071: * Wizard to collect name and participating tables information to be used in creating a
072: * new ETL collaboration.
073: */
074: public class ETLCollaborationWizard extends ETLWizard {
075:
076: private static transient final Logger mLogger = LogUtil
077: .getLogger(ETLCollaborationWizard.class.getName());
078: private static transient final Localizer mLoc = Localizer.get();
079:
080: public ETLCollaborationWizard() {
081: initialize();
082: }
083:
084: class Descriptor extends ETLWizardDescriptor {
085:
086: public Descriptor(WizardDescriptor.Iterator iter) {
087: super (iter, context);
088: }
089: }
090:
091: class WizardIterator extends ETLWizardIterator {
092:
093: private WizardDescriptor.Panel collaborationNamePanel;
094: private ETLCollaborationWizardJoinFinishPanel joinSelectionPanel;
095: private List panels;
096: private ETLCollaborationWizardTransferFinishPanel sourceTableSelectionPanel;
097: private ETLCollaborationWizardTransferFinishPanel targetTableSelectionPanel;
098: private ETLCollaborationWizard mWizard;
099:
100: public WizardIterator(ETLCollaborationWizard wizard) {
101: this .mWizard = wizard;
102: //
103: // NOTE: If the order of source and target panels are changed, please update
104: // the values of SOURCE_PANEL_INDEX and TARGET_PANEL_INDEX appropriately.
105: //
106: // These variables are used to determine how to skip past the join panel
107: // between source and target panels if fewer than two source tables are
108: // selected.
109: //
110: String nbBundle1 = mLoc
111: .t("PRSR001: Enter a Unique Name for This Collaboration.");
112: collaborationNamePanel = new ETLCollaborationWizardNameFinishPanel(
113: ETLCollaborationWizard.this , Localizer
114: .parse(nbBundle1));
115: }
116:
117: public String name() {
118: return "";
119: }
120:
121: @Override
122: public void initialize(WizardDescriptor wiz) {
123: this .mWizard.setDescriptor(wiz);
124: super .initialize(wiz);
125: }
126:
127: /**
128: * Overrides parent implementation to test for duplicate collab name before
129: * advancing to next panel, and skip join panel if fewer than two source tables
130: * are selected.
131: *
132: * @see org.openide.WizardDescriptor.Iterator#nextPanel
133: */
134: @Override
135: public void nextPanel() {
136: if (current().equals(sourceTableSelectionPanel)) { // Currently in source Database panel.
137: ETLCollaborationWizardTransferPanel xferPanel = (ETLCollaborationWizardTransferPanel) current();
138:
139: // Skip join panel if we don't have two or more tables selected,
140: // and no joins have been created.
141:
142: if (!xferPanel.hasEnoughTablesForJoin()
143: && descriptor
144: .getProperty(ETLCollaborationWizard.JOIN_VIEW) == null) {
145: super .nextPanel();
146: super .nextPanel();
147: return;
148: }
149: }
150:
151: super .nextPanel(); // Otherwise allow advance.
152: }
153:
154: /**
155: * Overrides parent implementation to skip join panel if fewer than two source
156: * tables are selected.
157: *
158: */
159: @Override
160: public void previousPanel() {
161: if (current().equals(targetTableSelectionPanel)) {
162:
163: // Skip join panel if we don't have two or more tables selected in the
164: // source panel, and no joins have been created.
165: if (!sourceTableSelectionPanel.hasEnoughTablesForJoin()
166: && descriptor
167: .getProperty(ETLCollaborationWizard.JOIN_VIEW) == null) {
168: super .previousPanel();
169: super .previousPanel();
170: return;
171: }
172: }
173: super .previousPanel(); // Otherwise use parent implementation.
174: }
175:
176: private List getModelConnections() {
177: List model = new ArrayList();
178: DBExplorerUtil
179: .recreateMissingFlatfileConnectionInDBExplorer();
180: model
181: .addAll(DBExplorerUtil
182: .getDatabasesForCurrentProject());
183:
184: DatabaseConnection[] conns = ConnectionManager.getDefault()
185: .getConnections();
186: if (conns.length > 0) {
187: for (int i = 0; i < conns.length; i++) {
188: if (conns[i] == null) {
189: model.add("<NULL>");
190: } else {
191: model.add(conns[i]);
192: }
193: }
194: } else if (model.size() == 0) {
195: model.add("<None>");
196: }
197: return model;
198: }
199:
200: protected List createPanels(WizardDescriptor wiz) {
201: List srcModel = new ArrayList();
202: List destModel = new ArrayList();
203:
204: List dbModels = getModelConnections();
205: storeSettings(wiz, dbModels);
206:
207: Project project = Templates.getProject(wiz);
208: if (project != null) {
209: Sources sources = ProjectUtils.getSources(project);
210: SourceGroup[] groups = sources
211: .getSourceGroups(Sources.TYPE_GENERIC);
212:
213: if ((groups == null) || (groups.length < 1)) {
214: groups = sources
215: .getSourceGroups(Sources.TYPE_GENERIC);
216: }
217:
218: collaborationNamePanel = new SimpleTargetChooserPanel(
219: project, groups, null, false);
220: }
221: String nbBundle2 = mLoc.t("PRSR001: Select Source Tables.");
222: sourceTableSelectionPanel = new ETLCollaborationWizardTransferFinishPanel(
223: Localizer.parse(nbBundle2), dbModels, srcModel,
224: true);
225: String nbBundle3 = mLoc
226: .t("PRSR001: Select Source Tables to Create Join.");
227: joinSelectionPanel = new ETLCollaborationWizardJoinFinishPanel(
228: ETLCollaborationWizard.this , Localizer
229: .parse(nbBundle3), null);
230: String nbBundle4 = mLoc.t("PRSR001: Select Target Tables.");
231: targetTableSelectionPanel = new ETLCollaborationWizardTransferFinishPanel(
232: Localizer.parse(nbBundle4), dbModels, destModel,
233: false);
234:
235: panels = new ArrayList(4);
236: if (collaborationNamePanel != null) {
237: panels.add(collaborationNamePanel);
238: }
239:
240: panels.add(sourceTableSelectionPanel);
241: panels.add(joinSelectionPanel);
242: panels.add(targetTableSelectionPanel);
243: return Collections.unmodifiableList(panels);
244: }
245:
246: public void storeSettings(Object settings, List models) {
247: WizardDescriptor wd = null;
248: if (settings instanceof ETLWizardContext) {
249: ETLWizardContext wizardContext = (ETLWizardContext) settings;
250: wd = (WizardDescriptor) wizardContext
251: .getProperty(ETLWizardContext.WIZARD_DESCRIPTOR);
252:
253: } else if (settings instanceof WizardDescriptor) {
254: wd = (WizardDescriptor) settings;
255: }
256:
257: if (wd != null) {
258: // Don't commit if user didn't click next.
259: // if (wd.getValue() != WizardDescriptor.NEXT_OPTION) {
260: // return;
261: // }
262: wd.putProperty(ETLCollaborationWizard.DATABASE_SOURCES,
263: models.toArray());
264: wd.putProperty(ETLCollaborationWizard.DATABASE_TARGETS,
265: models.toArray());
266: }
267: }
268:
269: protected String[] createSteps() {
270: try {
271: String nbBundle5 = mLoc
272: .t("PRSR001: Enter Collaboration Name");
273: String nbBundle6 = mLoc
274: .t("PRSR001: Select Source Tables");
275: String nbBundle7 = mLoc
276: .t("PRSR001: Select Source Tables for Join");
277: String nbBundle8 = mLoc
278: .t("PRSR001: Select Target Tables");
279: String nbBundle10 = mLoc.t("PRSR001: Choose File Type");
280: return new String[] {
281: //TODO - need make wizard steps text match actual panel being viewed
282: Localizer.parse(nbBundle10), //TODO - use bundle property
283: Localizer.parse(nbBundle5),
284: Localizer.parse(nbBundle6),
285: Localizer.parse(nbBundle7),
286: Localizer.parse(nbBundle8), };
287: } catch (MissingResourceException e) {
288: mLogger.errorNoloc(mLoc.t(
289: "PRSR029: Could not locate steps strings.{0}",
290: LOG_CATEGORY), e);
291: return new String[] {};
292: }
293: }
294:
295: @Override
296: public Set instantiate() throws IOException {
297: commit();
298:
299: FileObject dir = Templates.getTargetFolder(descriptor);
300: if (dir != null) {
301: DataFolder df = DataFolder.findFolder(dir);
302: FileObject template = Templates.getTemplate(descriptor);
303:
304: DataObject dTemplate = DataObject.find(template);
305: DataObject dobj = dTemplate.createFromTemplate(df,
306: Templates.getTargetName(descriptor));
307: if (dobj instanceof ETLDataObject) {
308: final ETLDataObject etlDataObj = (ETLDataObject) dobj;
309: Runnable run = new Runnable() {
310:
311: public void run() {
312: etlDataObj.initialize(descriptor);
313: if (etlDataObj.getNodeDelegate() != null) {
314: OpenCookie openCookie = etlDataObj
315: .getNodeDelegate().getCookie(
316: OpenCookie.class);
317: openCookie.open();
318: }
319: }
320: };
321:
322: SwingUtilities.invokeLater(run);
323: }
324:
325: return Collections.singleton(dobj.getPrimaryFile());
326: }
327: return new HashSet();
328: }
329: }
330:
331: /** Key name used to reference database sources in wizard context. */
332: public static final String DATABASE_SOURCES = "database_sources";
333: /** Key name used to reference database sources in wizard context. */
334: public static final String DATABASE_TARGETS = "database_targets";
335: /** Key name used to reference collaboration name in wizard context. */
336: public static final String COLLABORATION_NAME = "collaboration_name";
337: /** Key name used to reference List of destination Database in wizard context. */
338: public static final String TARGET_DB = "destination_dbs";
339: /** Key name used to reference List of destination tables in wizard context. */
340: public static final String DESTINATION_TABLES = "destination_tables";
341: /** Key name used to reference SQLJoinView if any created by user */
342: public static final String JOIN_VIEW = "join_view";
343: /** Key name used to reference List of visible columns selected by user */
344: public static final String JOIN_VIEW_VISIBLE_COLUMNS = "join_view_visible_columns";
345: /** Key name used to reference Project in wizard context. */
346: public static final String PROJECT = "project";
347: /** Key name used to reference Collection of runtime input args in wizard context. */
348: public static final String RUNTIME_INPUTS = "runtime_inputs";
349: /** Key name used to reference List of source Database in wizard context. */
350: public static final String SOURCE_DB = "source_dbs";
351: public static final int SOURCE_PANEL_INDEX = 2;
352: /** Key name used to reference List of source tables in wizard context. */
353: public static final String SOURCE_TABLES = "source_tables";
354: public static final int TARGET_PANEL_INDEX = 4;
355: /* Log4J category string */
356: private static final String LOG_CATEGORY = ETLCollaborationWizard.class
357: .getName();
358: /* Defines panels to be displayed */
359: private WizardDescriptor descriptor;
360: /* Wizard iterator; handles display and movement among wizard panels */
361: private ETLWizardIterator iterator;
362:
363: public static WizardDescriptor.Iterator newTemplateIterator() {
364: ETLCollaborationWizard wizard = new ETLCollaborationWizard();
365: return wizard.getIterator();
366: }
367:
368: /**
369: * @see ETLWizard#getDescriptor
370: */
371: public WizardDescriptor getDescriptor() {
372: if (descriptor == null) {
373: descriptor = new Descriptor(iterator);
374: }
375: return descriptor;
376: }
377:
378: public void setDescriptor(WizardDescriptor wd) {
379: this .descriptor = wd;
380: }
381:
382: /**
383: * @see ETLWizard#getIterator
384: */
385: public WizardDescriptor.Iterator getIterator() {
386: return iterator;
387: }
388:
389: /**
390: * Gets List of destination Databases as selected by user.
391: *
392: * @return List (possibly empty) of selected destination Databases
393: */
394: public List getSelectedDestinationDb() {
395: return getSelectedDbOfType(ETLCollaborationWizard.TARGET_DB);
396: }
397:
398: /**
399: * Gets List of source Databases as selected by user.
400: *
401: * @return List (possibly empty) of selected source Databases
402: */
403: public List getSelectedSourceDb() {
404: return getSelectedDbOfType(ETLCollaborationWizard.SOURCE_DB);
405: }
406:
407: public SQLJoinView getSQLJoinView() {
408: return (SQLJoinView) descriptor.getProperty(JOIN_VIEW);
409: }
410:
411: public List getTableColumnNodes() {
412: return (List) descriptor.getProperty(JOIN_VIEW_VISIBLE_COLUMNS);
413: }
414:
415: /**
416: * Initializes iterator and descriptor for this wizard.
417: */
418: public void initialize() {
419: iterator = new WizardIterator(this );
420: }
421:
422: /**
423: * Performs processing to handle cancellation of this wizard.
424: */
425: protected void cancel() {
426: }
427:
428: /**
429: * Performs processing to cleanup any resources used by this wizard.
430: */
431: protected void cleanup() {
432: }
433:
434: /**
435: * Performs processing to handle committal of data gathered by this wizard.
436: */
437: protected void commit() {
438: }
439:
440: /**
441: * @see org.netbeans.modules.etl.ui.view.wizards.ETLWizard#getDialogTitle()
442: */
443: @Override
444: protected String getDialogTitle() {
445: String nbBundle9 = mLoc
446: .t("PRSR001: New Collaboration Definition Wizard (ETL)");
447: return Localizer.parse(nbBundle9);
448: }
449:
450: private List getSelectedDbOfType(String typeKey) {
451: return (List) descriptor.getProperty(typeKey);
452: }
453: }
|