001: /*
002: * Sun Public License Notice
003: *
004: * The contents of this file are subject to the Sun Public License
005: * Version 1.0 (the "License"). You may not use this file except in
006: * compliance with the License. A copy of the License is available at
007: * http://www.sun.com/
008: *
009: * The Original Code is NetBeans. The Initial Developer of the Original
010: * Code is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
011: * Microsystems, Inc. All Rights Reserved.
012: If you wish your version of this file to be governed by only the CDDL
013: or only the GPL Version 2, indicate your decision by adding
014: "[Contributor] elects to include this software in this distribution
015: under the [CDDL or GPL Version 2] license." If you do not indicate a
016: single choice of license, a recipient has the option to distribute
017: your version of this file under either the CDDL, the GPL Version 2 or
018: to extend the choice of license to its licensees as provided above.
019: However, if you add GPL Version 2 code and therefore, elected the GPL
020: Version 2 license, then the option applies only if the new code is
021: made subject to such option by the copyright holder.
022: If you wish your version of this file to be governed by only the CDDL
023: or only the GPL Version 2, indicate your decision by adding
024: "[Contributor] elects to include this software in this distribution
025: under the [CDDL or GPL Version 2] license." If you do not indicate a
026: single choice of license, a recipient has the option to distribute
027: your version of this file under either the CDDL, the GPL Version 2 or
028: to extend the choice of license to its licensees as provided above.
029: However, if you add GPL Version 2 code and therefore, elected the GPL
030: Version 2 license, then the option applies only if the new code is
031: made subject to such option by the copyright holder.
032: */
033: package org.netbeans.modules.etl.ui.view.graph.actions;
034:
035: import java.awt.BorderLayout;
036: import java.awt.Dialog;
037: import java.awt.Dimension;
038: import java.awt.Font;
039: import java.io.BufferedReader;
040: import java.io.File;
041: import java.io.FileNotFoundException;
042: import java.io.FileReader;
043: import java.io.FileWriter;
044: import java.io.IOException;
045: import java.util.Iterator;
046: import javax.swing.BorderFactory;
047: import javax.swing.JLabel;
048: import javax.swing.JPanel;
049: import javax.swing.SwingConstants;
050: import org.w3c.dom.Element;
051: import org.w3c.dom.NodeList;
052: import org.netbeans.modules.etl.ui.ETLDataObject;
053: import org.netbeans.modules.etl.ui.view.ConfigParamsTreeView;
054: import org.netbeans.modules.etl.ui.view.ConfigureParametersPanel;
055: import org.netbeans.modules.sql.framework.model.SQLDBModel;
056: import org.netbeans.modules.sql.framework.model.SQLDBTable;
057: import org.netbeans.modules.sql.framework.model.SQLDefinition;
058: import org.netbeans.modules.sql.framework.model.impl.SQLDefinitionImpl;
059: import org.netbeans.modules.sql.framework.ui.editor.property.IPropertySheet;
060: import org.openide.nodes.Node;
061: import org.openide.util.HelpCtx;
062: import org.openide.DialogDescriptor;
063: import org.openide.util.actions.CookieAction;
064: import org.openide.DialogDisplayer;
065: import org.openide.NotifyDescriptor;
066: import org.openide.filesystems.FileUtil;
067: import com.sun.sql.framework.exception.BaseException;
068: import net.java.hulp.i18n.Logger;
069: import com.sun.sql.framework.utils.XmlUtil;
070: import org.netbeans.modules.etl.logger.Localizer;
071: import org.netbeans.modules.etl.logger.LogUtil;
072: import org.openide.awt.StatusDisplayer;
073:
074: /**
075: * Action class used for configuring ETL Collaborations.
076: *
077: * @author karthik
078: */
079: public final class ConfigureParametersAction extends CookieAction {
080:
081: private final String PATH_SUFFIX = "\\..\\..\\nbproject\\config\\";
082: private final String CONF_FILE = ".conf";
083: private static transient final Logger mLogger = LogUtil
084: .getLogger(ConfigureParametersAction.class.getName());
085: private static transient final Localizer mLoc = Localizer.get();
086: private File configFile;
087: ETLDataObject mObj;
088:
089: protected void performAction(Node[] activatedNodes) {
090: ETLDataObject dObj = activatedNodes[0]
091: .getCookie(ETLDataObject.class);
092: String msg = null;
093: boolean syncReqd = true;
094: if (dObj != null) {
095: this .mObj = dObj;
096: dObj.getPrimaryFile().refresh();
097: String path = dObj.getPrimaryFile().getPath() + PATH_SUFFIX;
098: configFile = new File(path);
099: if (!configFile.exists()) {
100: configFile.mkdir();
101: }
102: path = path + dObj.getName() + CONF_FILE;
103: configFile = new File(path);
104: FileWriter writer = null;
105: if (!configFile.exists()) {
106: try {
107: // This one doesn't work if ETL Collaboration is not yet opened. Only after opening
108: // the ETL Collab in the editor, the db models are updated into the SQL definition.
109: //writer.write(dObj.getETLDefinition().getSQLDefinition().toXMLString(""));
110: writeToConfigFile(configFile,
111: createSQLDefinition(dObj));
112:
113: // SQL Definition is not necessary. Probably should be using dbModels directly.
114: // for simplicity sake, using sql defn as of now.
115: syncReqd = false;
116: } catch (Exception ex) {
117: // ignore
118: }
119: }
120: if (configFile.exists()) {
121: if (syncReqd) {
122: syncConfigWithCollab(configFile, dObj);
123: }
124: showConfigPanel();
125: } else {
126: StatusDisplayer.getDefault().setStatusText(
127: "\nFailed to create the config file.");
128: }
129: } else {
130: StatusDisplayer.getDefault().setStatusText(
131: "\nFailed to initialize.");
132: }
133: }
134:
135: protected int mode() {
136: return CookieAction.MODE_EXACTLY_ONE;
137: }
138:
139: public String getName() {
140: String nbBundle1 = mLoc.t("PRSR001: Configure Parameters");
141: return Localizer.parse(nbBundle1);
142: }
143:
144: protected Class[] cookieClasses() {
145: return new Class[] { ETLDataObject.class };
146: }
147:
148: @Override
149: protected void initialize() {
150: super .initialize();
151: // see org.openide.util.actions.SystemAction.iconResource() javadoc for more details
152: putValue("noIconInMenu", Boolean.TRUE);
153: }
154:
155: public HelpCtx getHelpCtx() {
156: return HelpCtx.DEFAULT_HELP;
157: }
158:
159: @Override
160: protected boolean asynchronous() {
161: return false;
162: }
163:
164: /**
165: *
166: *
167: */
168: public void showConfigPanel() {
169: String nbBundle2 = mLoc
170: .t("PRSR001: Configure Deployment Parameters");
171: JLabel panelTitle = new JLabel(Localizer.parse(nbBundle2));
172: panelTitle.getAccessibleContext().setAccessibleName(
173: Localizer.parse(nbBundle2));
174: panelTitle.setDisplayedMnemonic(Localizer.parse(nbBundle2)
175: .charAt(0));
176: panelTitle.setFont(panelTitle.getFont().deriveFont(Font.BOLD));
177: panelTitle.setFocusable(false);
178: panelTitle.setHorizontalAlignment(SwingConstants.LEADING);
179:
180: ConfigureParametersPanel editPanel = new ConfigureParametersPanel(
181: mObj);
182:
183: JPanel contentPane = new JPanel();
184: contentPane.setBorder(BorderFactory.createEmptyBorder(2, 2, 2,
185: 2));
186: contentPane.setLayout(new BorderLayout());
187: contentPane.add(panelTitle, BorderLayout.NORTH);
188: contentPane.add(editPanel, BorderLayout.CENTER);
189:
190: DialogDescriptor dd = new DialogDescriptor(contentPane,
191: "Configure Deployment Parameters");
192: Dialog dlg = DialogDisplayer.getDefault().createDialog(dd);
193: dlg.getAccessibleContext().setAccessibleDescription(
194: "This is a dialog to configure Deployment Parameters");
195: dlg.setSize(new Dimension(600, 450));
196: dlg.setVisible(true);
197: if (NotifyDescriptor.OK_OPTION.equals(dd.getValue())) {
198: ConfigParamsTreeView configModelTreeView = editPanel
199: .getConfigModelTreeView();
200: if (configModelTreeView != null) {
201: IPropertySheet propSheet = configModelTreeView
202: .getPropSheet();
203: if (propSheet != null) {
204: propSheet.commitChanges();
205: }
206: SQLDefinition defn = configModelTreeView.getData();
207: if (defn != null) {
208: this .configFile.delete();
209: try {
210: writeToConfigFile(this .configFile, defn);
211: StatusDisplayer
212: .getDefault()
213: .setStatusText(
214: "\nDeployment parameters successfully updated.");
215: } catch (IOException ex) {
216: StatusDisplayer.getDefault().setStatusText(
217: "\nFailed to update changes.");
218: } catch (BaseException baseEx) {
219: StatusDisplayer.getDefault().setStatusText(
220: "\nFailed to read SQL Definition.");
221: }
222: }
223: }
224: } else {
225: StatusDisplayer.getDefault().setStatusText(
226: "\nAll the edits are discarded.");
227: }
228: }
229:
230: private void syncConfigWithCollab(File configFile,
231: ETLDataObject dObj) {
232: SQLDefinition srcDefn = createSQLDefinition(dObj);
233: SQLDefinition confDefn = getConfigData(configFile);
234:
235: // sync Source db Models.
236: confDefn = compareAndSync(srcDefn, confDefn, true);
237: // sync Target db Models.
238: confDefn = compareAndSync(srcDefn, confDefn, false);
239: try {
240:
241: // write new config data into config file.
242: writeToConfigFile(configFile, confDefn);
243: } catch (Exception ex) {
244: mLogger
245: .infoNoloc(mLoc
246: .t(
247: "PRSR022: ConfigureParametersAction.class.getName(){0}",
248: ex.getMessage()));
249: }
250: }
251:
252: private SQLDefinition compareAndSync(SQLDefinition srcDefn,
253: SQLDefinition tgtDefn, boolean isSource) {
254: // sync the Models.
255: Iterator it = null;
256: if (isSource) {
257: it = srcDefn.getSourceDatabaseModels().iterator();
258: } else {
259: it = srcDefn.getTargetDatabaseModels().iterator();
260: }
261: while (it.hasNext()) {
262: boolean exists = false;
263: SQLDBModel match = null;
264: SQLDBModel srcModel = (SQLDBModel) it.next();
265: Iterator confIterator = null;
266: if (isSource) {
267: confIterator = tgtDefn.getSourceDatabaseModels()
268: .iterator();
269: } else {
270: confIterator = tgtDefn.getTargetDatabaseModels()
271: .iterator();
272: }
273: while (confIterator.hasNext()) {
274: SQLDBModel tgtModel = (SQLDBModel) confIterator.next();
275: if (tgtModel.getModelName().equals(
276: srcModel.getModelName())) {
277: exists = true;
278: match = tgtModel;
279: break;
280: }
281: }
282: if (exists) {
283: try {
284: tgtDefn.removeObject(match);
285: match = syncTables(srcModel, match);
286: tgtDefn.addObject(match);
287: } catch (BaseException ex) {
288: // ignore
289: }
290: } else {
291: try {
292: tgtDefn.addObject(srcModel);
293: } catch (BaseException ex) {
294: // ignore
295: }
296: }
297: }
298: return tgtDefn;
299: }
300:
301: private SQLDBModel syncTables(SQLDBModel srcModel,
302: SQLDBModel tgtModel) {
303: Iterator it = srcModel.getTables().iterator();
304: while (it.hasNext()) {
305: boolean exists = false;
306: SQLDBTable srcTbl = (SQLDBTable) it.next();
307: Iterator confIterator = tgtModel.getTables().iterator();
308: while (confIterator.hasNext()) {
309: SQLDBTable tgtTbl = (SQLDBTable) confIterator.next();
310: if (tgtTbl.getName().equals(srcTbl.getName())) {
311: exists = true;
312: break;
313: }
314: }
315: if (!exists) {
316: tgtModel.addTable(srcTbl);
317: }
318: }
319: return tgtModel;
320: }
321:
322: private SQLDefinition getConfigData(File configFile) {
323: org.w3c.dom.Node rootNode = null;
324: SQLDefinition sqlDefn = null;
325: try {
326: Element element = XmlUtil.loadXMLFile(new BufferedReader(
327: new FileReader(configFile)));
328: rootNode = (org.w3c.dom.Node) element;
329: } catch (Exception ex) {
330: mLogger
331: .infoNoloc(mLoc
332: .t(
333: "PRSR023: ConfigureParametersAction.class.getName(){0}",
334: ex.getMessage()));
335: }
336: if (rootNode != null) {
337: org.w3c.dom.Node sqlNode = rootNode.getFirstChild();
338: try {
339: sqlDefn = new SQLDefinitionImpl((Element) sqlNode);
340: } catch (Exception ex) {
341: mLogger
342: .infoNoloc(mLoc
343: .t(
344: "PRSR024: ConfigureParametersAction.class.getName(){0}",
345: ex.getMessage()));
346: }
347: }
348: return sqlDefn;
349: }
350:
351: private SQLDefinition createSQLDefinition(ETLDataObject dObj) {
352: Element element = null;
353: try {
354: element = XmlUtil.loadXMLFile(new BufferedReader(
355: new FileReader(FileUtil.toFile(dObj
356: .getPrimaryFile()))));
357: } catch (FileNotFoundException ex) {
358: // ignore
359: }
360: SQLDefinition sqlDefn = null;
361: if (element != null) {
362: NodeList list = element
363: .getElementsByTagName("sqlDefinition");
364: Element sqlElement = (Element) list.item(0);
365: try {
366: sqlDefn = new SQLDefinitionImpl(sqlElement);
367: } catch (BaseException ex) {
368: // ignore
369: }
370: }
371: return sqlDefn;
372: }
373:
374: private void writeToConfigFile(File configFile,
375: SQLDefinition confDefn) throws IOException, BaseException {
376: if (configFile.exists()) {
377: configFile.delete();
378: }
379: configFile.createNewFile();
380: FileWriter writer = new FileWriter(configFile);
381: writer.write("<ETLConfig>");
382: writer.write(confDefn.toXMLString(""));
383: writer.write("</ETLConfig>");
384: writer.close();
385: }
386: }
|