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.util.ArrayList;
044: import java.util.Collections;
045: import java.util.Iterator;
046: import java.util.List;
047: import java.util.MissingResourceException;
048:
049: import net.java.hulp.i18n.Logger;
050: import org.openide.WizardDescriptor;
051: import org.netbeans.api.db.explorer.ConnectionManager;
052: import org.netbeans.api.db.explorer.DatabaseConnection;
053: import org.netbeans.modules.etl.logger.Localizer;
054: import org.netbeans.modules.etl.logger.LogUtil;
055: import org.netbeans.modules.etl.model.impl.ETLDefinitionImpl;
056: import org.netbeans.modules.sql.framework.common.utils.DBExplorerUtil;
057: import org.netbeans.modules.sql.framework.model.SQLDBModel;
058: import org.netbeans.modules.sql.framework.model.SQLDBTable;
059:
060: /**
061: * Presents choice source and target tables for inclusion in an ETL Definition.
062: *
063: * @author Ahimanikya Satapathy
064: * @version $Revision$
065: */
066: public class ETLTableSelectionWizard extends ETLWizard {
067:
068: private static transient final Logger mLogger = LogUtil
069: .getLogger(ETLTableSelectionWizard.class.getName());
070: private static transient final Localizer mLoc = Localizer.get();
071:
072: class Descriptor extends ETLWizardDescriptor {
073:
074: public Descriptor(WizardDescriptor.Iterator iter) {
075: super (iter, context);
076: }
077: }
078:
079: class WizardIterator extends ETLWizardIterator {
080:
081: private List panels;
082: public WizardDescriptor wiz;
083:
084: public WizardIterator(ETLDefinitionImpl def) {
085: panels = new ArrayList(2);
086: List srcModel = new ArrayList();
087: List destModel = new ArrayList();
088: List dbModels = getModelConnections();
089:
090: String nbBundle1 = mLoc.t("PRSR001: Select Source Tables.");
091: String selectSrcTitle = Localizer.parse(nbBundle1);
092: sourceTransferPanel = new ETLCollaborationWizardTransferFinishPanel(
093: selectSrcTitle, dbModels, srcModel, true);
094: panels.add(sourceTransferPanel);
095: String nbBundle2 = mLoc.t("PRSR001: Select Target Tables.");
096: String selectTrgtTitle = Localizer.parse(nbBundle2);
097: targetTransferPanel = new ETLCollaborationWizardTransferFinishPanel(
098: selectTrgtTitle, dbModels, destModel, false);
099: panels.add(targetTransferPanel);
100: }
101:
102: private List getModelConnections() {
103: List model = new ArrayList();
104: DBExplorerUtil
105: .recreateMissingFlatfileConnectionInDBExplorer();
106: model
107: .addAll(DBExplorerUtil
108: .getDatabasesForCurrentProject());
109:
110: DatabaseConnection[] conns = ConnectionManager.getDefault()
111: .getConnections();
112: if (conns.length > 0) {
113: for (int i = 0; i < conns.length; i++) {
114: if (conns[i] == null) {
115: model.add("<NULL>");
116: } else {
117: model.add(conns[i]);
118: }
119: }
120: } else if (model.size() == 0) {
121: model.add("<None>");
122: }
123: return model;
124: }
125:
126: public String name() {
127: String nbBundle4 = mLoc
128: .t("PRSR001: Select Source/Target Tables");
129: return Localizer.parse(nbBundle4);
130: }
131:
132: protected List createPanels() {
133: return Collections.unmodifiableList(panels);
134: }
135:
136: protected String[] createSteps() {
137: try {
138: String nbBundle5 = mLoc
139: .t("PRSR001: Select Source Tables");
140: String nbBundle6 = mLoc
141: .t("PRSR001: Select Target Tables");
142: return new String[] { Localizer.parse(nbBundle5),
143: Localizer.parse(nbBundle6) };
144: } catch (MissingResourceException e) {
145: return new String[] {};
146: }
147: }
148:
149: protected List createPanels(WizardDescriptor wiz) {
150: List dbModels = getModelConnections();
151: storeSettings(wiz, dbModels);
152: return createPanels();
153: }
154:
155: public void storeSettings(Object settings, List models) {
156: WizardDescriptor wd = null;
157: if (settings instanceof ETLWizardContext) {
158: ETLWizardContext wizardContext = (ETLWizardContext) settings;
159: wd = (WizardDescriptor) wizardContext
160: .getProperty(ETLWizardContext.WIZARD_DESCRIPTOR);
161:
162: } else if (settings instanceof WizardDescriptor) {
163: wd = (WizardDescriptor) settings;
164: }
165: if (wd != null) {
166: // Don't commit if user didn't click next.
167: // if (wd.getValue() != WizardDescriptor.NEXT_OPTION) {
168: // return;
169: // }
170: wd.putProperty(ETLCollaborationWizard.DATABASE_SOURCES,
171: models.toArray());
172: wd.putProperty(ETLCollaborationWizard.DATABASE_TARGETS,
173: models.toArray());
174: }
175: }
176: }
177:
178: /** Constant string key for storing/retrieving destination Databases from wizard context. */
179: public static final String DESTINATION_MODELS = ETLCollaborationWizard.TARGET_DB;
180: /** Constant string key for storing/retrieving destination tables from wizard context. */
181: public static final String DESTINATION_TABLES = "destination_tables"; // NOI18N
182: /** Constant string key for storing/retrieving source Databases from wizard context. */
183: public static final String SOURCE_MODELS = ETLCollaborationWizard.SOURCE_DB;
184: /** Constant string key for storing/retrieving source tables from wizard context. */
185: public static final String SOURCE_TABLES = "source_tables"; // NOI18N
186:
187: /**
188: * Iterates through the given List, removing db model that have no DBTable instances where
189: * isSelected() returns true.
190: *
191: * @param List of DatabaseModel instances to be filtered
192: * @return List (possibly empty) of DatabaseModel instances containing only selected
193: * DBTable instances.
194: */
195: public static final List removeModelsWithNoSelectedTables(List list) {
196: List filteredList = Collections.EMPTY_LIST;
197:
198: if (list != null && !list.isEmpty()) {
199: filteredList = new ArrayList(list.size());
200: Iterator iter = list.iterator();
201: while (iter.hasNext()) {
202: SQLDBModel dbModel = (SQLDBModel) iter.next();
203:
204: // Don't filter out unselected tables from Databases just yet...user might
205: // go back and change their selections, but the underlying DBModel
206: // will be out-of-sync with the GUI.
207: if (hasSelectedTables(dbModel)) {
208: filteredList.add(dbModel);
209: }
210: }
211: }
212:
213: return filteredList;
214: }
215:
216: static final void removeUnselectedTables(List db) {
217: Iterator it = db.iterator();
218: while (it.hasNext()) {
219: removeUnselectedTables((SQLDBModel) it.next());
220: }
221: }
222:
223: static final void removeUnselectedTables(SQLDBModel dbModel) {
224: List tables = dbModel.getTables();
225: Iterator it = tables.iterator();
226:
227: while (it.hasNext()) {
228: SQLDBTable table = (SQLDBTable) it.next();
229: if (!table.isSelected() || !table.isEditable()) {
230: dbModel.deleteTable(dbModel
231: .getFullyQualifiedTableName(table));
232: }
233: }
234: }
235:
236: /*
237: * Indicates whether the given SQLDBModel instance has at least one table
238: * selected by the user. Does not remove unselected tables from <code> dbModel </code>
239: * as is the case with <code> removeUnselectedTables </code> . @param dbModel
240: * SQLDBModel instance whose tables are to be inspected @return true if
241: * <code> dbModel </code> has at least one selected table.
242: */
243: private static final boolean hasSelectedTables(SQLDBModel dbModel) {
244: boolean hasSelected = false;
245:
246: List tables = dbModel.getTables();
247: Iterator it = tables.iterator();
248:
249: while (it.hasNext()) {
250: SQLDBTable table = (SQLDBTable) it.next();
251: if (table.isSelected() && table.isEditable()) {
252: hasSelected = true;
253: break;
254: }
255: }
256:
257: return hasSelected;
258: }
259:
260: /* Defines panels to be displayed */
261: private WizardDescriptor descriptor;
262: private ETLDefinitionImpl etlDef;
263: /* Wizard iterator; handles display and movement among wizard panels */
264: private ETLWizardIterator iterator;
265: private ETLCollaborationWizardTransferFinishPanel sourceTransferPanel;
266: private ETLCollaborationWizardTransferFinishPanel targetTransferPanel;
267:
268: /**
269: * Creates a new instance of DatabaseWizard
270: *
271: * @param def ETLDefinitionProjectElement containing table data
272: */
273: public ETLTableSelectionWizard(ETLDefinitionImpl def) {
274: etlDef = def;
275: }
276:
277: /**
278: * @see ETLWizard#getDescriptor
279: */
280: public WizardDescriptor getDescriptor() {
281: return descriptor;
282: }
283:
284: /**
285: * @see ETLWizard#getIterator
286: */
287: public WizardDescriptor.Iterator getIterator() {
288: return iterator;
289: }
290:
291: /**
292: * Gets List of destination Databases as selected by user.
293: *
294: * @return List (possibly empty) of selected destination Databases
295: */
296: public List getSelectedDestinationModels() {
297: return getSelectedModelsOfType(ETLTableSelectionWizard.DESTINATION_MODELS);
298: }
299:
300: /**
301: * Gets List of source Models as selected by user.
302: *
303: * @return List (possibly empty) of selected source Databases
304: */
305: public List getSelectedSourceModels() {
306: return getSelectedModelsOfType(ETLTableSelectionWizard.SOURCE_MODELS);
307: }
308:
309: /**
310: * Initializes iterator and descriptor for this wizard.
311: */
312: public void initialize() {
313: iterator = new WizardIterator(etlDef);
314: descriptor = new Descriptor(iterator);
315: if (sourceTransferPanel != null) {
316: sourceTransferPanel.updatePanelState();
317: }
318:
319: if (targetTransferPanel != null) {
320: targetTransferPanel.updatePanelState();
321: }
322: }
323:
324: /**
325: * Performs processing to handle cancellation of this wizard.
326: */
327: protected void cancel() {
328: }
329:
330: /**
331: * Performs processing to cleanup any resources used by this wizard.
332: */
333: protected void cleanup() {
334: }
335:
336: /**
337: * Performs processing to handle committal of data gathered by this wizard.
338: */
339: protected void commit() {
340: // Create union of source and target model lists, then remove unselected tables
341: // from them.
342: List models = new ArrayList(getSelectedSourceModels());
343: models.addAll(getSelectedDestinationModels());
344:
345: ETLTableSelectionWizard.removeUnselectedTables(models);
346: }
347:
348: /**
349: * @see org.netbeans.modules.etl.ui.view.wizards.ETLWizard#getDialogTitle()
350: */
351: @Override
352: protected String getDialogTitle() {
353: String nbBundle3 = mLoc
354: .t("PRSR001: Select Source and Target Tables");
355: return Localizer.parse(nbBundle3);
356: }
357:
358: private List getSelectedModelsOfType(String typeKey) {
359: List selections = Collections.EMPTY_LIST;
360:
361: if (descriptor != null && typeKey != null) {
362: selections = ETLTableSelectionWizard
363: .removeModelsWithNoSelectedTables((List) descriptor
364: .getProperty(typeKey));
365: }
366:
367: return selections;
368: }
369: }
|