001: /*
002: * DataObjectHelper.java
003: *
004: * Created on June 25, 2006, 5:05 PM
005: *
006: * To change this template, choose Tools | Template Manager
007: * and open the template in the editor.
008: */
009:
010: package org.netbeans.modules.etl.ui;
011:
012: import java.awt.Cursor;
013: import java.awt.Frame;
014: import java.awt.event.MouseAdapter;
015: import java.awt.event.MouseMotionAdapter;
016: import java.io.InputStream;
017: import java.sql.Types;
018: import java.util.Iterator;
019: import java.util.List;
020: import javax.swing.JFrame;
021: import javax.swing.JPanel;
022: import javax.swing.SwingUtilities;
023:
024: import org.netbeans.modules.etl.ui.model.impl.ETLCollaborationModel;
025: import org.netbeans.modules.etl.ui.view.ETLCollaborationTopPanel;
026: import org.netbeans.modules.etl.ui.view.property.ETLResourceManager;
027: import org.netbeans.modules.etl.ui.view.wizards.ETLWizardHelper;
028: import org.netbeans.modules.sql.framework.model.RuntimeDatabaseModel;
029: import org.netbeans.modules.sql.framework.model.RuntimeInput;
030: import org.netbeans.modules.sql.framework.model.RuntimeOutput;
031: import org.netbeans.modules.sql.framework.model.SQLDBColumn;
032: import org.netbeans.modules.sql.framework.model.SQLDBModel;
033: import org.netbeans.modules.sql.framework.model.SQLDBTable;
034: import org.netbeans.modules.sql.framework.model.SQLDefinition;
035: import org.netbeans.modules.sql.framework.model.SQLJoinView;
036: import org.netbeans.modules.sql.framework.model.SQLModelObjectFactory;
037: import org.netbeans.modules.sql.framework.model.SourceColumn;
038: import org.netbeans.modules.sql.framework.model.TargetTable;
039: import org.netbeans.modules.sql.framework.model.impl.RuntimeDatabaseModelImpl;
040: import org.netbeans.modules.sql.framework.model.impl.RuntimeOutputImpl;
041: import org.netbeans.modules.sql.framework.model.utils.SQLObjectUtil;
042: import org.netbeans.modules.sql.framework.ui.editor.property.impl.PropertyViewManager;
043: import org.netbeans.modules.sql.framework.ui.view.join.JoinUtility;
044: import org.openide.ErrorManager;
045: import org.openide.WizardDescriptor;
046: import org.openide.windows.WindowManager;
047:
048: import com.sun.sql.framework.exception.BaseException;
049: import java.io.IOException;
050: import org.netbeans.modules.sql.framework.model.impl.RuntimeInputImpl;
051:
052: /**
053: *
054: * @author radval
055: */
056: public class DataObjectHelper {
057:
058: private static ETLDataObject mDataObject;
059:
060: private static PropertyViewManager pvMgr;
061:
062: /** Creates a new instance of DataObjectHelper */
063: public DataObjectHelper(ETLDataObject dataObject) {
064: mDataObject = dataObject;
065: }
066:
067: public DataObjectHelper() {
068: }
069:
070: public void initializeETLDataObject(WizardDescriptor descriptor,
071: ETLDataObject mObj, ETLEditorSupport editorSupport)
072: throws Exception {
073: mDataObject = mObj;
074: ETLWizardHelper wHelper = new ETLWizardHelper(descriptor);
075:
076: List sourceDbList = wHelper.getSelectedSourceDb();
077: List destinationDbList = wHelper.getSelectedDestinationDb();
078: SQLJoinView joinView = wHelper.getSQLJoinView();
079: List jVisibleColumns = wHelper.getTableColumnNodes();
080:
081: // for now we need to have an editor top component so that table can be added to
082: // it
083: final ETLCollaborationTopPanel etlEditor = mDataObject
084: .getETLEditorTopPanel();
085: ETLCollaborationModel collabModel = mObj.getModel();
086:
087: // first add join view
088: if (joinView != null) {
089: JoinUtility.handleNewJoinCreation(joinView,
090: jVisibleColumns, etlEditor.getGraphView());
091:
092: // WT #67643: Ensure that flatfile tables in join views have filename runtime
093: // inputs.
094: establishRuntimeInputs(collabModel, joinView
095: .getSourceTables());
096: }
097:
098: if (sourceDbList != null) {
099: addDestinationDb(sourceDbList, collabModel);
100: }
101:
102: if (destinationDbList != null) {
103: addDestinationDb(destinationDbList, collabModel);
104: }
105: establishRuntimeOutputs(mObj);
106: //make sure editor document has etl content when first time created
107: try {
108: String content = collabModel.getETLDefinition()
109: .toXMLString("");
110: editorSupport.openDocument();
111: editorSupport.getDocument().remove(0,
112: editorSupport.getDocument().getLength());
113: editorSupport.getDocument().insertString(0, content, null);
114: } catch (Exception ex) {
115: ErrorManager.getDefault().notify(ex);
116: }
117: }
118:
119: private static void addDestinationDb(List db,
120: ETLCollaborationModel collabModel) throws BaseException {
121: for (int i = 0; i < db.size(); i++) {
122: SQLDBModel dbModel = (SQLDBModel) db.get(i);
123:
124: // Add database model only if at least one table in it was selected by user
125: if (dbModel.getTables().size() == 0) {
126: continue;
127: }
128:
129: addTables(dbModel, collabModel);
130: }
131: }
132:
133: private static void addTables(SQLDBModel dbModel,
134: ETLCollaborationModel collabModel) throws BaseException {
135: List tables = dbModel.getTables();
136: Iterator it = tables.iterator();
137: while (it.hasNext()) {
138: SQLDBTable table = (SQLDBTable) it.next();
139: collabModel.addObject(table);
140: }
141:
142: establishRuntimeInputs(collabModel, tables);
143: }
144:
145: private static void establishRuntimeInputs(
146: ETLCollaborationModel collabModel, List tables)
147: throws BaseException {
148: Iterator it = tables.iterator();
149: while (it.hasNext()) {
150: // if table is source table and is a flat file we auto create a run
151: SQLDBTable sTable = (SQLDBTable) it.next();
152: SourceColumn runtimeArg = SQLObjectUtil.createRuntimeInput(
153: sTable, collabModel.getSQLDefinition());
154: if (runtimeArg != null) {
155: RuntimeInput runtimeInput = (RuntimeInput) runtimeArg
156: .getParent();
157:
158: if (runtimeInput != null) {
159: // if runtime input is not in SQL definition then add it
160: if ((collabModel.getSQLDefinition()
161: .isTableExists(runtimeInput)) == null) {
162: collabModel.addObject(runtimeInput);
163: }
164: }
165: }
166: SQLObjectUtil.setOrgProperties(sTable);
167: }
168: }
169:
170: /**
171: * Updates selected source and target tables for the given eTL Collaboration
172: * ProjectElement.
173: *
174: * @param dObj etldataobject(representing an eTL Collaboration) whose table
175: * selections are to be updated
176: * @param sourcedb List of source table Databases to be added to <code>element</code>
177: * @param targetdb List of target table Databases to be added to <code>element</code>
178: * @throws Exception if error occurs during update
179: */
180: public void updateTableSelections(ETLDataObject dObj,
181: List sourceDb, List targetDb) throws Exception {
182: ETLCollaborationModel collabModel = null;
183: collabModel = dObj.getModel();
184: if (collabModel == null) {
185: return;
186: }
187:
188: if (sourceDb != null) {
189: addDestinationDb(sourceDb, collabModel);
190: }
191:
192: if (targetDb != null) {
193: addDestinationDb(targetDb, collabModel);
194: }
195: }
196:
197: /**
198: * Gets instance of PropertyViewManager.
199: *
200: * @return instance of PropertyViewManager
201: */
202: public static PropertyViewManager getPropertyViewManager() {
203: InputStream stream = null;
204: if (pvMgr == null) {
205: try {
206: stream = DataObjectHelper.class
207: .getClassLoader()
208: .getResourceAsStream(
209: "org/netbeans/modules/etl/ui/resources/etl_properties.xml");
210: pvMgr = new PropertyViewManager(stream,
211: new ETLResourceManager());
212: } finally {
213: if (stream != null) {
214: try {
215: stream.close();
216: } catch (IOException ignore) {
217: // ignore
218: }
219: }
220: }
221: }
222: return pvMgr;
223: }
224:
225: /**
226: * Displays default cursor in Editor.
227: */
228: public static void setDefaultCursor() {
229: Runnable r = new Runnable() {
230: public synchronized void run() {
231: Frame mainWindow = WindowManager.getDefault()
232: .getMainWindow();
233: if (mainWindow instanceof JFrame) {
234: JFrame frame = (JFrame) mainWindow;
235: JPanel glass = (JPanel) frame.getGlassPane();
236: glass.setCursor(Cursor.getDefaultCursor());
237: glass.setVisible(false);
238: }
239: }
240: };
241:
242: SwingUtilities.invokeLater(r);
243: }
244:
245: /**
246: * Displays wait cursor in Editor.
247: */
248: public static void setWaitCursor() {
249: Runnable r = new Runnable() {
250: public synchronized void run() {
251: Frame mainWindow = WindowManager.getDefault()
252: .getMainWindow();
253:
254: if (mainWindow instanceof JFrame) {
255: JFrame frame = (JFrame) mainWindow;
256: JPanel glass = (JPanel) frame.getGlassPane();
257: glass.addMouseListener(new MouseAdapter() {
258: });
259: glass
260: .addMouseMotionListener(new MouseMotionAdapter() {
261: });
262: glass.setCursor(Cursor
263: .getPredefinedCursor(Cursor.WAIT_CURSOR));
264: glass.setVisible(true);
265: }
266: }
267: };
268:
269: SwingUtilities.invokeLater(r);
270: }
271:
272: private void establishRuntimeOutputs(ETLDataObject mObj) {
273: try {
274: SQLDefinition sqlDefn = mObj.getETLDefinition()
275: .getSQLDefinition();
276: RuntimeDatabaseModel rtModel = sqlDefn.getRuntimeDbModel();
277: if (rtModel == null) {
278: rtModel = new RuntimeDatabaseModelImpl();
279: }
280: RuntimeOutput rtOutTable = new RuntimeOutputImpl();
281: // add STATUS
282: SQLDBColumn column = SQLModelObjectFactory.getInstance()
283: .createTargetColumn("STATUS", Types.VARCHAR, 0, 0,
284: true);
285: column.setEditable(false);
286: rtOutTable.addColumn(column);
287:
288: // add STARTTIME
289: column = SQLModelObjectFactory.getInstance()
290: .createTargetColumn("STARTTIME", Types.TIMESTAMP,
291: 0, 0, true);
292: column.setEditable(false);
293: rtOutTable.addColumn(column);
294:
295: // add ENDTIME
296: column = SQLModelObjectFactory.getInstance()
297: .createTargetColumn("ENDTIME", Types.TIMESTAMP, 0,
298: 0, true);
299: column.setEditable(false);
300: rtOutTable.addColumn(column);
301:
302: Iterator it = sqlDefn.getTargetTables().iterator();
303: while (it.hasNext()) {
304: TargetTable targetTable = (TargetTable) it.next();
305: String argName = SQLObjectUtil
306: .getTargetTableCountRuntimeOutput(targetTable);
307: column = SQLModelObjectFactory.getInstance()
308: .createTargetColumn(argName, Types.INTEGER, 0,
309: 0, true);
310: column.setEditable(false);
311: rtOutTable.addColumn(column);
312: }
313: rtModel.addTable(rtOutTable);
314: sqlDefn.addObject(rtModel);
315: } catch (Exception ex) {
316: //ignore
317: }
318: }
319:
320: private void establishRuntimeOutputs(SQLDefinition sqlDefn) {
321: try {
322: RuntimeDatabaseModel rtModel = sqlDefn.getRuntimeDbModel();
323: if (rtModel == null) {
324: rtModel = new RuntimeDatabaseModelImpl();
325: }
326: RuntimeOutput rtOutTable = new RuntimeOutputImpl();
327: // add STATUS
328: SQLDBColumn column = SQLModelObjectFactory.getInstance()
329: .createTargetColumn("STATUS", Types.VARCHAR, 0, 0,
330: true);
331: column.setEditable(false);
332: rtOutTable.addColumn(column);
333:
334: // add STARTTIME
335: column = SQLModelObjectFactory.getInstance()
336: .createTargetColumn("STARTTIME", Types.TIMESTAMP,
337: 0, 0, true);
338: column.setEditable(false);
339: rtOutTable.addColumn(column);
340:
341: // add ENDTIME
342: column = SQLModelObjectFactory.getInstance()
343: .createTargetColumn("ENDTIME", Types.TIMESTAMP, 0,
344: 0, true);
345: column.setEditable(false);
346: rtOutTable.addColumn(column);
347:
348: Iterator it = sqlDefn.getTargetTables().iterator();
349: while (it.hasNext()) {
350: TargetTable targetTable = (TargetTable) it.next();
351: String argName = SQLObjectUtil
352: .getTargetTableCountRuntimeOutput(targetTable);
353: column = SQLModelObjectFactory.getInstance()
354: .createTargetColumn(argName, Types.INTEGER, 0,
355: 0, true);
356: column.setEditable(false);
357: rtOutTable.addColumn(column);
358: }
359: rtModel.addTable(rtOutTable);
360: sqlDefn.addObject(rtModel);
361: } catch (Exception ex) {
362: //ignore
363: }
364: }
365: }
|