001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: /*
021: * Sun Public License Notice
022: *
023: * The contents of this file are subject to the Sun Public License
024: * Version 1.0 (the "License"). You may not use this file except in
025: * compliance with the License. A copy of the License is available at
026: * http://www.sun.com/
027: *
028: * The Original Code is NetBeans. The Initial Developer of the Original
029: * Code is Sun Microsystems, Inc. Portions Copyright 1997-2005 Sun
030: * Microsystems, Inc. All Rights Reserved.
031: */
032:
033: package org.netbeans.modules.sql.project.wsdl;
034:
035: import java.awt.event.ActionEvent;
036: import java.beans.PropertyChangeListener;
037: import java.io.File;
038: import java.io.FileFilter;
039: import java.io.IOException;
040: import java.sql.Connection;
041: import java.util.ArrayList;
042: import java.util.HashMap;
043: import java.util.List;
044: import java.util.Map;
045: import javax.swing.JFrame;
046: import javax.swing.JOptionPane;
047: import javax.wsdl.Definition;
048: import javax.xml.parsers.DocumentBuilderFactory;
049: import javax.xml.parsers.ParserConfigurationException;
050:
051: import org.netbeans.api.db.explorer.ConnectionManager;
052: import org.netbeans.api.db.explorer.DatabaseConnection;
053: import org.netbeans.api.project.Project;
054: import org.openide.util.HelpCtx;
055: import org.openide.util.actions.CallableSystemAction;
056: import org.openide.filesystems.FileObject;
057: import org.w3c.dom.Document;
058: import org.w3c.dom.Node;
059: import org.w3c.dom.NodeList;
060: import org.xml.sax.SAXException;
061: import org.openide.util.NbBundle;
062:
063: public class ActionImpl extends CallableSystemAction {
064:
065: /**
066: *
067: */
068: private static final long serialVersionUID = 1L;
069: public static final String NAME = "Name";
070: private String mSrcDirectoryLocation;
071: private String mBuildDirectoryLocation;
072: private String mBaseDirectoryLocation;
073: private String dbURL = "";
074: private String jndi_name = "";
075: private String transactionRequired = "";
076: private static final String CONNECTION_FILE = "connectivityInfo.xml";
077: private String engineFileName = "sqlse_engine.xml";
078: private JFrame frame;
079: DatabaseConnection dbConn = null;
080: Connection conn = null;
081: Project project = null;
082:
083: public void setProject(Project project) {
084: this .project = project;
085: }
086:
087: /* public void addPropertyChangeListener(PropertyChangeListener listener) {
088: this.listener = listener;
089: }
090:
091: public Object getValue(String key) {
092: return null;
093: }
094:
095: public boolean isEnabled() {
096: return false;
097: }
098:
099: public void putValue(String key, Object value) {
100:
101: }*/
102:
103: public String getName() {
104: //return NbBundle.getMessage(ActionImpl.class, "CTL_Connect");
105: //return "Generate Wsdl ...";
106: return NbBundle.getMessage(ActionImpl.class,
107: "LBL_Generate_WSDL");
108: }
109:
110: /*public void removePropertyChangeListener(PropertyChangeListener listener) {
111: if(this.listener.equals(listener)){
112: this.listener = null;
113:
114:
115: }*/
116: /**
117: * Default implementation of getHelpCtx().
118: *
119: * @return The HelpCtx value
120: */
121: public HelpCtx getHelpCtx() {
122: return HelpCtx.DEFAULT_HELP;
123: }
124:
125: /**
126: * Action to perform
127: */
128: public void performAction() {
129:
130: }
131:
132: /**
133: * Override to perform action synchronous
134: */
135: public boolean asynchronous() {
136: return false;
137: }
138:
139: public void setEnabled(boolean b) {
140:
141: }
142:
143: public void actionPerformed(ActionEvent e) {
144: try {
145: execute();
146: } catch (Exception e1) {
147: //e1.printStackTrace();
148: }
149: }
150:
151: /**
152: * @return Returns the srcDirectoryLocation.
153: */
154: public String getSrcDirectoryLocation() {
155: return mSrcDirectoryLocation;
156: }
157:
158: /**
159: * @param srcDirectoryLocation The srcDirectoryLocation to set.
160: */
161: public void setSrcDirectoryLocation(String srcDirectoryLocation) {
162: mSrcDirectoryLocation = srcDirectoryLocation;
163: }
164:
165: /**
166: * @param srcDirectoryLocation The srcDirectoryLocation to set.
167: */
168: public void setBaseDirectoryLocation(String baseDirectoryLocation) {
169: mBaseDirectoryLocation = baseDirectoryLocation;
170: }
171:
172: public String getBuildDirectoryLocation() {
173: return mBuildDirectoryLocation;
174: }
175:
176: /**
177: * @param buildDirectoryLocation The buildDirectoryLocation to set.
178: */
179: public void setBuildDirectoryLocation(String buildDirectoryLocation) {
180: mBuildDirectoryLocation = buildDirectoryLocation;
181: engineFileName = "sqlse_engine.xml";
182: }
183:
184: public static List getFilesRecursively(File dir, FileFilter filter) {
185: List ret = new ArrayList();
186: if (!dir.isDirectory()) {
187: return ret;
188: }
189: File[] fileNdirs = dir.listFiles(filter);
190: for (int i = 0, I = fileNdirs.length; i < I; i++) {
191: if (fileNdirs[i].isDirectory()) {
192: ret.addAll(getFilesRecursively(fileNdirs[i], filter));
193: } else {
194: ret.add(fileNdirs[i]);
195: }
196: }
197: return ret;
198: }
199:
200: public static List getFilesRecursively(File dir, String[] extensions) {
201: FileFilter filter = null;
202: if (extensions[0].equals(".sql")) {
203: filter = new ExtensionFilter(extensions);
204: }
205: return getFilesRecursively(dir, filter);
206: }
207:
208: public void execute() throws Exception {
209: Map wsdlMap = new HashMap();
210:
211: try {
212: String baseDir = this .project.getProjectDirectory()
213: .getPath();
214: mBuildDirectoryLocation = baseDir + "/build";
215: mSrcDirectoryLocation = baseDir + "/src";
216: File srcDir = new File(mSrcDirectoryLocation);
217: if (!srcDir.exists()) {
218: throw new Exception("Directory "
219: + mSrcDirectoryLocation + " does not exit.");
220: }
221: //String baseDir = mBaseDirectoryLocation;
222: int length = baseDir.length();
223: String projectName = baseDir.substring(baseDir
224: .lastIndexOf(File.separator) + 1, baseDir.length());
225: if (projectName.length() == length) {
226: projectName = baseDir.substring(baseDir
227: .lastIndexOf("/") + 1, baseDir.length());
228: }
229: boolean b = (new File(mBuildDirectoryLocation + "/META-INF"))
230: .mkdirs();
231: SQLEngineFileGenerator engineFileGen = new SQLEngineFileGenerator(
232: mBuildDirectoryLocation + "/" + engineFileName,
233: projectName);
234: String srcDirPath = srcDir.getAbsolutePath();
235: String[] ext = new String[] { ".sql" };
236: List sqlFiles = getFilesRecursively(srcDir, ext);
237: readConnectionInfo(CONNECTION_FILE);
238: getDatabaseConnection();
239: if (conn == null) {
240: throw new Exception(
241: "Unable to retrieve any database connections with the url "
242: + dbURL
243: + "\n Please associate a Connection for the sql file in netbeans runtime tab or if it exists, connect to the external");
244: }
245:
246: for (int i = 0, I = sqlFiles.size(); i < I; i++) {
247: File f = (File) sqlFiles.get(i);
248: if (f != null) {
249: engineFileGen.addSQLDefinition(f.getName(), dbConn);
250: }
251: }
252: engineFileGen.persistEngineFile(jndi_name,
253: transactionRequired);
254:
255: //call generate wsdl in model.
256: WSDLGenerator wsdlgen = new WSDLGenerator(conn, sqlFiles,
257: projectName, srcDirPath, engineFileName);
258:
259: wsdlgen.setDBConnection(dbConn);
260:
261: Definition def = wsdlgen.generateWSDL();
262:
263: SQLMapWriter sqlw = new SQLMapWriter(sqlFiles, def,
264: new File(mBuildDirectoryLocation)
265: .getCanonicalPath());
266: sqlw.writeMap();
267: JBIFileWriter fw = new JBIFileWriter(
268: mBuildDirectoryLocation + "/META-INF/jbi.xml",
269: mBuildDirectoryLocation + "/sqlmap.xml",
270: mBuildDirectoryLocation);
271: fw.writeJBI();
272: project.getProjectDirectory().refresh();
273: JOptionPane.showMessageDialog(frame, NbBundle.getMessage(
274: ActionImpl.class, "LBL_WSDL_Generated"),
275: "Information", JOptionPane.INFORMATION_MESSAGE);
276:
277: } catch (Exception e) {
278: throw new Exception(e.getMessage());
279: }
280: }
281:
282: private void readConnectionInfo(String fileName) throws Exception {
283: if (fileName.endsWith(".xml")) {
284: DocumentBuilderFactory factory = DocumentBuilderFactory
285: .newInstance();
286: try {
287: Document doc = factory.newDocumentBuilder()
288: .parse(
289: new File(mSrcDirectoryLocation,
290: CONNECTION_FILE));
291: NodeList nl = doc.getDocumentElement()
292: .getElementsByTagName("database-url");
293: if (nl != null) {
294: Node n = nl.item(0);
295: if (n != null) {
296: Node n2 = n.getAttributes().getNamedItem(
297: "value");
298: if (n2 != null) {
299: dbURL = n2.getNodeValue();
300: //log("Using Database URL value: " + dbURL);
301: }
302: }
303: }
304: NodeList n3 = doc.getDocumentElement()
305: .getElementsByTagName("jndi-name");
306: if (n3 != null) {
307: Node n = n3.item(0);
308: if (n != null) {
309: Node n4 = n.getAttributes().getNamedItem(
310: "value");
311: if (n4 != null) {
312: jndi_name = n4.getNodeValue();
313: //log("jndi value: " + jndi_name);
314: }
315: }
316: }
317: NodeList n5 = doc.getDocumentElement()
318: .getElementsByTagName("transaction-required");
319: if (n5 != null) {
320: Node n = n5.item(0);
321: if (n != null) {
322: Node n6 = n.getAttributes().getNamedItem(
323: "value");
324: if (n6 != null) {
325: transactionRequired = n6.getNodeValue();
326: //log("TransactionRequired value: " + transactionRequired);
327: }
328: }
329: }
330:
331: } catch (SAXException e) {
332: //log(e.getLocalizedMessage());
333: } catch (IOException e) {
334: //log(e.getLocalizedMessage());
335: } catch (ParserConfigurationException e) {
336: //log(e.getLocalizedMessage());
337: }
338: } else {
339: throw new Exception(
340: "No File with name connectivityInfo.xml was found in the src directory");
341: }
342: }
343:
344: private void getDatabaseConnection() {
345: DatabaseConnection[] dbConnections = ConnectionManager
346: .getDefault().getConnections();
347: if (dbConn == null) {
348: for (int j = 0; j < dbConnections.length; j++) {
349: if (dbConnections[j].getDatabaseURL().equalsIgnoreCase(
350: dbURL)) {
351: dbConn = dbConnections[j];
352: conn = dbConn.getJDBCConnection();
353: break;
354: }
355: }
356: }
357: if ((dbConn != null) && (conn == null)) {
358: ConnectionManager.getDefault().showConnectionDialog(dbConn);
359: conn = dbConn.getJDBCConnection();
360: }
361: }
362:
363: }
|