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: package org.netbeans.modules.jdbcwizard.builder.wsdl;
020:
021: import java.util.logging.Logger;
022: import java.util.logging.Level;
023: import java.util.List;
024: import java.util.Iterator;
025: import java.util.HashMap;
026:
027: import java.net.URL;
028: import java.io.File;
029: import java.io.Writer;
030: import java.io.FileWriter;
031: import java.io.IOException;
032:
033: import javax.wsdl.factory.WSDLFactory;
034: import javax.wsdl.xml.WSDLWriter;
035: import javax.wsdl.xml.WSDLReader;
036: import javax.wsdl.Definition;
037:
038: import javax.wsdl.WSDLException;
039: import javax.wsdl.Types;
040:
041: import javax.wsdl.extensions.schema.Schema;
042: import javax.wsdl.extensions.UnknownExtensibilityElement;
043: import javax.xml.namespace.QName;
044: import javax.xml.parsers.DocumentBuilderFactory;
045: import javax.xml.parsers.ParserConfigurationException;
046:
047: import org.w3c.dom.Element;
048: import org.w3c.dom.NodeList;
049: import org.w3c.dom.Node;
050: import org.w3c.dom.Attr;
051: import org.w3c.dom.Document;
052: import org.xml.sax.SAXException;
053:
054: import org.netbeans.modules.jdbcwizard.builder.dbmodel.DBConnectionDefinition;
055: import org.netbeans.modules.jdbcwizard.builder.dbmodel.DBTable;
056: import org.netbeans.modules.jdbcwizard.builder.model.DBQueryModel;
057: import org.netbeans.modules.jdbcwizard.builder.model.DerbyQueryGenerator;
058: import org.netbeans.modules.jdbcwizard.builder.model.OracleQueryGenerator;
059: import org.netbeans.modules.jdbcwizard.builder.model.DB2QueryGenerator;
060: import org.netbeans.modules.jdbcwizard.builder.model.SQLServerQueryGenerator;
061: import org.netbeans.modules.jdbcwizard.builder.model.JdbcQueryGenerator;
062: import org.netbeans.modules.jdbcwizard.builder.model.MySQLQueryGenerator;
063: import org.netbeans.modules.jdbcwizard.builder.util.XMLCharUtil;
064:
065: public class WSDLGenerator {
066:
067: public static HashMap builtInTypes = new HashMap();
068: private static final Logger logger = Logger
069: .getLogger(WSDLGenerator.class.getName());
070: private static WSDLFactory factory;
071: private static DocumentBuilderFactory docBuilderfactory;
072:
073: public WSDLReader reader = null;
074:
075: private Definition def;
076: private String mWSDLFileName;
077: private String wsdlFileLocation;
078: private Document doc;
079: private static final String IMPORT_ELEMENT = "xsd:import";
080: private static final String NAMESPACE_ATTR = "namespace";
081: private static final String SCHEMALOCATION_ATTR = "schemaLocation";
082: private static final String TARGET_NS = "targetNamespace";
083: private static final String TARGET_NS_PREFIX_STRING = "http://j2ee.netbeans.org/wsdl/";
084: private static final String TNS_STRING = "xmlns:tns";
085: private static final String NAME = "name";
086:
087: private static final String PART_ELEMENT = "part";
088: private static final String NAME_ATTR = "name";
089: private static final String ELEMENT_ATTR = "element";
090: private static final String TARGETNAMESPACE = "http://j2ee.netbeans.org/xsd/tableSchema";
091: private static final String INPUTMESSAGE_NAME = "inputMsg";
092: private static final String OUTPUTMESSAGE_NAME = "outputMsg";
093: private static final String XMLSCHEMA_NAMESPACE = "http://www.w3.org/2001/XMLSchema";
094:
095: private static final String INSERT_QUERY = "insertQuery";
096: private static final String UPDATE_QUERY = "updateQuery";
097: private static final String DELETE_QUERY = "deleteQuery";
098: private static final String FIND_QUERY = "findQuery";
099: private static final String POLL_QUERY = "pollQuery";
100:
101: // private static final String JDBC_SQL_ELEMENT = "jdbc:sql";
102: private static final String JDBC_SQL_ELEMENT = "jdbc:input";
103: private static final String SQL_ELEMENT = "sql";
104: private static final String PARAM_ORDER = "paramOrder";
105: private static final String JDBC_NAMESPACE = "http://schemas.sun.com/jbi/wsdl-extensions/jdbc/";
106:
107: private static final String SCHEMA_ELE = "xsd:schema";
108: private static final String PRIMARYKEY_ATTR = "PKName";
109: private static final String TRANSACTION = "Transaction";
110: private static final String MARK_COLUMN_VALUE = "MarkColumnValue";
111: private static final String MARK_COLUMN_NAME = "MarkColumnName";
112: private static final String POLL_POST_PROCESS = "PollingPostProcessing";
113: private static final String TABLE_NAME = "TableName";
114:
115: private static final String NUMNBER_OF_RECORDS = "numberOfRecords";
116: private static final String MOVEROW_TABLE_NAME = "MoveRowToTableName";
117: private static final String POLLMILLI_SECONDS = "PollMilliSeconds";
118: private static final String OPERATION_TYPE = "operationType";
119:
120: // private static final String FLAGCOL_ATTR = "FlagColumn";
121: private static final String JNDI_NAME = "jndiname";
122: private static final String JNDI_ADD_ELE = "jdbc:address";
123:
124: private static final String SERVICE_NAME = "Service";
125: private static final String PORT_NAME = "Port";
126:
127: private DBTable mTable;
128: private String xsdTopEleName;
129: private String xsdName;
130: private static final String XSD_EXT = ".xsd";
131: private String mDBType;
132: private String mJNDIName;
133: private String mTableName = null;
134:
135: private DBConnectionDefinition dbinfo;
136:
137: private DBQueryModel dbDataAccessObject = null;
138: static {
139: WSDLGenerator.initFactory();
140: }
141:
142: /**
143: * Constructor
144: * @param dbmeta
145: * @param sqlFile
146: * @param wsdlFileLocation
147: */
148: public WSDLGenerator(final DBTable table,
149: final String wsdlFileName, final String wsdlFileLocation,
150: final String dbtype, final String jndiName) {
151: this .mWSDLFileName = wsdlFileName;
152: this .wsdlFileLocation = wsdlFileLocation;
153: this .mTable = table;
154: this .mDBType = dbtype;
155: this .mJNDIName = jndiName;
156: // this.setTopEleName();
157: // this.setXSDName();
158: }
159:
160: /**
161: * initialize the WSDLFactory
162: */
163: private static void initFactory() {
164: if (WSDLGenerator.factory == null) {
165: try {
166: WSDLGenerator.factory = WSDLFactory.newInstance();
167: WSDLGenerator.docBuilderfactory = DocumentBuilderFactory
168: .newInstance();
169: } catch (final WSDLException wsdle) {
170: WSDLGenerator.logger.log(Level.WARNING, wsdle
171: .getMessage(), wsdle);
172: }
173: }
174: }
175:
176: /**
177: *
178: *
179: */
180: public void setTopEleName() {
181: this .xsdTopEleName = "ns:"
182: + XMLCharUtil.makeValidNCName(this .mTable.getName());
183: }
184:
185: /**
186: *
187: *
188: */
189: public void setXSDName() {
190: this .xsdName = XMLCharUtil.makeValidNCName(this .mTable
191: .getName())
192: + WSDLGenerator.XSD_EXT;
193: }
194:
195: public void setDBInfo(DBConnectionDefinition dbinfo) {
196: this .dbinfo = dbinfo;
197: }
198:
199: /**
200: * reads an sqlpro wsdl template file and genarates the javax.wsdl.Definition
201: *
202: * @return Definition
203: * @throws WSDLException
204: */
205: private Definition getWsdlTemplate() throws WSDLException,
206: ParserConfigurationException, SAXException, IOException {
207: Definition def = null;
208: this .reader = WSDLGenerator.factory.newWSDLReader();
209:
210: try {
211: final URL u = WSDLGenerator.class
212: .getResource("jdbc.wsdl.template");
213: if (u != null) {
214: final String wsdlURI = u.getFile().indexOf(".jar") > 0 ? "jar:"
215: + u.getFile()
216: : u.getFile();
217: WSDLGenerator.docBuilderfactory.setNamespaceAware(true);
218: WSDLGenerator.docBuilderfactory.setValidating(false);
219: this .doc = WSDLGenerator.docBuilderfactory
220: .newDocumentBuilder().parse(wsdlURI);
221: def = this .reader.readWSDL(wsdlURI, this .doc);
222:
223: } else {
224: WSDLGenerator.logger.log(Level.WARNING,
225: "Unable to locate the wsdl template");
226: }
227: } catch (final WSDLException e) {
228: WSDLGenerator.logger.log(Level.SEVERE, e.getMessage(), e);
229: throw e;
230: } catch (final ParserConfigurationException e) {
231: WSDLGenerator.logger.log(Level.SEVERE, e.getMessage(), e);
232: throw e;
233: } catch (final SAXException e) {
234: WSDLGenerator.logger.log(Level.SEVERE, e.getMessage(), e);
235: throw e;
236: } catch (final IOException e) {
237: WSDLGenerator.logger.log(Level.SEVERE, e.getMessage(), e);
238: throw e;
239: }
240: return def;
241: }
242:
243: /**
244: * Generates the WSDL
245: * @return
246: */
247: public Definition generateWSDL() {
248: try {
249: this .def = this .getWsdlTemplate();
250: this .modifyWSDL();
251: this .writeWsdl();
252: } catch (final WSDLException wsdle) {
253: WSDLGenerator.logger.log(Level.SEVERE, wsdle.getMessage(),
254: wsdle);
255: } catch (final ParserConfigurationException e) {
256: WSDLGenerator.logger.log(Level.SEVERE, e.getMessage(), e);
257: } catch (final SAXException e) {
258: WSDLGenerator.logger.log(Level.SEVERE, e.getMessage(), e);
259: } catch (final IOException e) {
260: WSDLGenerator.logger.log(Level.SEVERE, e.getMessage(), e);
261: } catch (final Exception e) {
262: WSDLGenerator.logger.log(Level.SEVERE, e.getMessage(), e);
263: }
264: return this .def;
265: }
266:
267: /**
268: * Modify the WSDL Template
269: * @throws WSDLException
270: * @throws Exception
271: */
272: private void modifyWSDL() throws WSDLException, Exception {
273: this .modifyName();
274: this .modifyTargetNamespace();
275: this .modifySchEle();
276: this .modiyMessageEles();
277: this .modifyBindings();
278: this .modifyServiceAndPortNames();
279: this .modifyJNDIAddress();
280: }
281:
282: /**
283: * Modify the WSDL name
284: *
285: */
286: private void modifyName() {
287: /* QName q = this.def.getQName();
288: q = new QName(q.getNamespaceURI(), this.mWSDLFileName);
289: this.def.setQName(q); */
290: Element rootEle = this .doc.getDocumentElement();
291: Attr attr = rootEle.getAttributeNode(NAME);
292: attr.setNodeValue(this .mWSDLFileName);
293: }
294:
295: /**
296: * Modify the WSDL TargetNamespace
297: *
298: */
299: private void modifyTargetNamespace() {
300: Element rootEle = this .doc.getDocumentElement();
301: Attr attr = rootEle.getAttributeNode(TARGET_NS);
302: attr.setNodeValue(TARGET_NS_PREFIX_STRING + this .mWSDLFileName);
303: attr = rootEle.getAttributeNode(TNS_STRING);
304: attr.setNodeValue(TARGET_NS_PREFIX_STRING + this .mWSDLFileName);
305: }
306:
307: /**
308: *
309: * @throws WSDLException
310: * @throws Exception
311: */
312: public void modifySchEle() throws WSDLException, Exception {
313: Element rootEle = this .doc.getDocumentElement();
314: Element scheEle = null;
315: final NodeList childNodes = rootEle
316: .getElementsByTagName(WSDLGenerator.SCHEMA_ELE);
317: for (int i = 0; i < childNodes.getLength(); i++) {
318: final Node child = childNodes.item(i);
319: if (child instanceof Element) {
320: scheEle = (Element) child;
321: break;
322: }
323: }
324: //Change schema element targetnamespace
325: Attr attr = scheEle.getAttributeNode(WSDLGenerator.TARGET_NS);
326: attr.setNodeValue(WSDLGenerator.JDBC_NAMESPACE);
327:
328: Element importEle = null;
329: final NodeList childNodesTmp = rootEle
330: .getElementsByTagName(WSDLGenerator.IMPORT_ELEMENT);
331: for (int i = 0; i < childNodesTmp.getLength(); i++) {
332: final Node childTmp = childNodesTmp.item(i);
333: if (childTmp instanceof Element) {
334: importEle = (Element) childTmp;
335: break;
336: }
337: }
338: //Change import element namespace and schemalocation
339: Attr attrimp = importEle
340: .getAttributeNode(WSDLGenerator.NAMESPACE_ATTR);
341: attrimp.setNodeValue(WSDLGenerator.TARGETNAMESPACE);
342: Attr schloc = importEle
343: .getAttributeNode(WSDLGenerator.SCHEMALOCATION_ATTR);
344: schloc.setNodeValue(this .xsdName);
345: }
346:
347: public void modiyMessageEles() throws WSDLException, Exception {
348: Element rootEle = this .doc.getDocumentElement();
349: final Element inputMsgEle = this .getElementByName(rootEle,
350: WSDLGenerator.INPUTMESSAGE_NAME);
351: final NodeList partNodes = inputMsgEle.getChildNodes();
352: Element partEle = null;
353: for (int i = 0; i < partNodes.getLength(); i++) {
354: final Node child = partNodes.item(i);
355: if (child instanceof Element) {
356: partEle = (Element) child;
357: break;
358: }
359: }
360: Attr attrInput = partEle
361: .getAttributeNode(WSDLGenerator.ELEMENT_ATTR);
362: attrInput.setNodeValue(this .xsdTopEleName);
363:
364: Element partOutEle = null;
365: final Element outputMsgEle = this .getElementByName(rootEle,
366: WSDLGenerator.OUTPUTMESSAGE_NAME);
367: final NodeList outPartNodes = outputMsgEle.getChildNodes();
368: for (int i = 0; i < outPartNodes.getLength(); i++) {
369: final Node child = outPartNodes.item(i);
370: if (child instanceof Element) {
371: partOutEle = (Element) child;
372: break;
373: }
374: }
375: Attr attroutput = partOutEle
376: .getAttributeNode(WSDLGenerator.ELEMENT_ATTR);
377: attroutput.setNodeValue(this .xsdTopEleName);
378: this .def = this .reader.readWSDL(this .wsdlFileLocation, rootEle);
379: }
380:
381: /**
382: *
383: * @throws WSDLException
384: */
385: public void modifyBindings() throws Exception {
386: try {
387: String insertQuery = null;
388: String updateQuery = null;
389: String deleteQuery = null;
390: String findQuery = null;
391: String poolQuery = null;
392:
393: this .dbDataAccessObject = this .getQueryGenerator();
394: this .dbDataAccessObject.init(this .mTable);
395:
396: this .mTableName = this .mTable.getSchema() + "."
397: + this .mTable.getName();
398:
399: // Generate Queries
400: insertQuery = this .dbDataAccessObject.createInsertQuery();
401: updateQuery = this .dbDataAccessObject.createUpdateQuery();
402: deleteQuery = this .dbDataAccessObject.createDeleteQuery();
403: findQuery = this .dbDataAccessObject.createFindQuery();
404: poolQuery = this .dbDataAccessObject.createPoolQuery();
405:
406: this .modifyInsert(this .dbDataAccessObject, insertQuery);
407: this .modifyUpdate(this .dbDataAccessObject, updateQuery);
408: this .modifyDelete(this .dbDataAccessObject, deleteQuery);
409: this .modifyFind(this .dbDataAccessObject, findQuery);
410: this .modifyPoll(this .dbDataAccessObject, poolQuery);
411: } catch (final Exception e) {
412: throw new WSDLException(WSDLException.OTHER_ERROR,
413: "Could not generate the WSDL");
414: }
415: }
416:
417: /**
418: *
419: * @param dao
420: * @param query
421: * @throws WSDLException
422: * @throws Exception
423: */
424: public void modifyInsert(final DBQueryModel dao, final String query)
425: throws WSDLException, Exception {
426: try {
427: String primaryKey = null;
428: final Element rootEle = this .doc.getDocumentElement();
429:
430: final Element insSqlEle = this .getElementBySQL(rootEle,
431: WSDLGenerator.INSERT_QUERY);
432: primaryKey = dao.getPrimaryKey();
433:
434: Attr optType = insSqlEle
435: .getAttributeNode(WSDLGenerator.OPERATION_TYPE);
436: optType.setNodeValue("insert");
437:
438: Attr attrParamOrder = insSqlEle
439: .getAttributeNode(WSDLGenerator.PARAM_ORDER);
440: attrParamOrder.setNodeValue(dao
441: .getParamOrder(WSDLGenerator.INSERT_QUERY));
442:
443: Attr attrSqlEle = insSqlEle
444: .getAttributeNode(WSDLGenerator.SQL_ELEMENT);
445: attrSqlEle.setNodeValue(query);
446:
447: Attr attrPrmKey = insSqlEle
448: .getAttributeNode(WSDLGenerator.PRIMARYKEY_ATTR);
449: attrPrmKey.setNodeValue(primaryKey);
450:
451: Attr attrTrans = insSqlEle
452: .getAttributeNode(WSDLGenerator.TRANSACTION);
453: attrTrans.setNodeValue("NOTransaction");
454:
455: Attr attrTableName = insSqlEle
456: .getAttributeNode(WSDLGenerator.TABLE_NAME);
457: attrTableName.setNodeValue(this .mTableName);
458:
459: Attr attrNumOfRecs = insSqlEle
460: .getAttributeNode(WSDLGenerator.NUMNBER_OF_RECORDS);
461: attrNumOfRecs.setNodeValue("");
462:
463: Attr attrMarkColVal = insSqlEle
464: .getAttributeNode(WSDLGenerator.MARK_COLUMN_VALUE);
465: attrMarkColVal.setNodeValue("");
466:
467: Attr attrMovRowname = insSqlEle
468: .getAttributeNode(WSDLGenerator.MOVEROW_TABLE_NAME);
469: attrMovRowname.setNodeValue("");
470:
471: Attr attrMarkColName = insSqlEle
472: .getAttributeNode(WSDLGenerator.MARK_COLUMN_NAME);
473: attrMarkColName.setNodeValue("");
474:
475: Attr attrPollMilli = insSqlEle
476: .getAttributeNode(WSDLGenerator.POLLMILLI_SECONDS);
477: attrPollMilli.setNodeValue("5000");
478: } catch (final Exception e) {
479: throw new WSDLException(WSDLException.OTHER_ERROR,
480: "Could not generate the WSDL");
481: }
482: }
483:
484: /**
485: *
486: * @param dao
487: * @param query
488: * @throws WSDLException
489: * @throws Exception
490: */
491: public void modifyUpdate(final DBQueryModel dao, final String query)
492: throws WSDLException, Exception {
493: try {
494: String primaryKey = null;
495: final Element rootEle = this .doc.getDocumentElement();
496:
497: final Element updateSqlEle = this .getElementBySQL(rootEle,
498: WSDLGenerator.UPDATE_QUERY);
499: primaryKey = dao.getPrimaryKey();
500:
501: Attr optType = updateSqlEle
502: .getAttributeNode(WSDLGenerator.OPERATION_TYPE);
503: optType.setNodeValue("update");
504:
505: Attr attrParamOrder = updateSqlEle
506: .getAttributeNode(WSDLGenerator.PARAM_ORDER);
507: attrParamOrder.setNodeValue(dao
508: .getParamOrder(WSDLGenerator.UPDATE_QUERY));
509:
510: Attr attrSqlEle = updateSqlEle
511: .getAttributeNode(WSDLGenerator.SQL_ELEMENT);
512: attrSqlEle.setNodeValue(query);
513:
514: Attr attrPrmKey = updateSqlEle
515: .getAttributeNode(WSDLGenerator.PRIMARYKEY_ATTR);
516: attrPrmKey.setNodeValue(primaryKey);
517:
518: Attr attrTrans = updateSqlEle
519: .getAttributeNode(WSDLGenerator.TRANSACTION);
520: attrTrans.setNodeValue("NOTransaction");
521:
522: Attr attrTableName = updateSqlEle
523: .getAttributeNode(WSDLGenerator.TABLE_NAME);
524: attrTableName.setNodeValue(this .mTableName);
525:
526: Attr attrNumOfRecs = updateSqlEle
527: .getAttributeNode(WSDLGenerator.NUMNBER_OF_RECORDS);
528: attrNumOfRecs.setNodeValue("");
529:
530: Attr attrMarkColVal = updateSqlEle
531: .getAttributeNode(WSDLGenerator.MARK_COLUMN_VALUE);
532: attrMarkColVal.setNodeValue("");
533:
534: Attr attrMovRowname = updateSqlEle
535: .getAttributeNode(WSDLGenerator.MOVEROW_TABLE_NAME);
536: attrMovRowname.setNodeValue("");
537:
538: Attr attrMarkColName = updateSqlEle
539: .getAttributeNode(WSDLGenerator.MARK_COLUMN_NAME);
540: attrMarkColName.setNodeValue("");
541:
542: Attr attrPollMilli = updateSqlEle
543: .getAttributeNode(WSDLGenerator.POLLMILLI_SECONDS);
544: attrPollMilli.setNodeValue("5000");
545: } catch (final Exception e) {
546: throw new WSDLException(WSDLException.OTHER_ERROR,
547: "Could not generate the WSDL");
548: }
549: }
550:
551: /**
552: *
553: * @param dao
554: * @param query
555: * @throws WSDLException
556: * @throws Exception
557: */
558: public void modifyDelete(final DBQueryModel dao, final String query)
559: throws WSDLException, Exception {
560: try {
561: String primaryKey = null;
562: final Element rootEle = this .doc.getDocumentElement();
563:
564: final Element delSqlEle = this .getElementBySQL(rootEle,
565: WSDLGenerator.DELETE_QUERY);
566: primaryKey = dao.getPrimaryKey();
567:
568: Attr optType = delSqlEle
569: .getAttributeNode(WSDLGenerator.OPERATION_TYPE);
570: optType.setNodeValue("delete");
571:
572: Attr attrParamOrder = delSqlEle
573: .getAttributeNode(WSDLGenerator.PARAM_ORDER);
574: attrParamOrder.setNodeValue("");
575:
576: Attr attrSqlEle = delSqlEle
577: .getAttributeNode(WSDLGenerator.SQL_ELEMENT);
578: attrSqlEle.setNodeValue(query);
579:
580: Attr attrPrmKey = delSqlEle
581: .getAttributeNode(WSDLGenerator.PRIMARYKEY_ATTR);
582: attrPrmKey.setNodeValue(primaryKey);
583:
584: Attr attrTrans = delSqlEle
585: .getAttributeNode(WSDLGenerator.TRANSACTION);
586: attrTrans.setNodeValue("NOTransaction");
587:
588: Attr attrTableName = delSqlEle
589: .getAttributeNode(WSDLGenerator.TABLE_NAME);
590: attrTableName.setNodeValue(this .mTableName);
591:
592: Attr attrNumOfRecs = delSqlEle
593: .getAttributeNode(WSDLGenerator.NUMNBER_OF_RECORDS);
594: attrNumOfRecs.setNodeValue("");
595:
596: Attr attrMarkColVal = delSqlEle
597: .getAttributeNode(WSDLGenerator.MARK_COLUMN_VALUE);
598: attrMarkColVal.setNodeValue("");
599:
600: Attr attrMovRowname = delSqlEle
601: .getAttributeNode(WSDLGenerator.MOVEROW_TABLE_NAME);
602: attrMovRowname.setNodeValue("");
603:
604: Attr attrMarkColName = delSqlEle
605: .getAttributeNode(WSDLGenerator.MARK_COLUMN_NAME);
606: attrMarkColName.setNodeValue("");
607:
608: Attr attrPollMilli = delSqlEle
609: .getAttributeNode(WSDLGenerator.POLLMILLI_SECONDS);
610: attrPollMilli.setNodeValue("5000");
611: } catch (final Exception e) {
612: throw new WSDLException(WSDLException.OTHER_ERROR,
613: "Could not generate the WSDL");
614: }
615: }
616:
617: /**
618: *
619: * @param dao
620: * @param query
621: * @throws WSDLException
622: * @throws Exception
623: */
624: public void modifyFind(final DBQueryModel dao, final String query)
625: throws WSDLException, Exception {
626: try {
627: String primaryKey = null;
628: final Element rootEle = this .doc.getDocumentElement();
629: final Element findSqlEle = this .getElementBySQL(rootEle,
630: WSDLGenerator.FIND_QUERY);
631: primaryKey = dao.getPrimaryKey();
632:
633: Attr optType = findSqlEle
634: .getAttributeNode(WSDLGenerator.OPERATION_TYPE);
635: optType.setNodeValue("find");
636:
637: Attr attrParamOrder = findSqlEle
638: .getAttributeNode(WSDLGenerator.PARAM_ORDER);
639: attrParamOrder.setNodeValue("");
640:
641: Attr attrSqlEle = findSqlEle
642: .getAttributeNode(WSDLGenerator.SQL_ELEMENT);
643: attrSqlEle.setNodeValue(query);
644:
645: Attr attrPrmKey = findSqlEle
646: .getAttributeNode(WSDLGenerator.PRIMARYKEY_ATTR);
647: attrPrmKey.setNodeValue(primaryKey);
648:
649: Attr attrTrans = findSqlEle
650: .getAttributeNode(WSDLGenerator.TRANSACTION);
651: attrTrans.setNodeValue("NOTransaction");
652:
653: Attr attrTableName = findSqlEle
654: .getAttributeNode(WSDLGenerator.TABLE_NAME);
655: attrTableName.setNodeValue(this .mTableName);
656:
657: Attr attrNumOfRecs = findSqlEle
658: .getAttributeNode(WSDLGenerator.NUMNBER_OF_RECORDS);
659: attrNumOfRecs.setNodeValue("");
660:
661: Attr attrMarkColVal = findSqlEle
662: .getAttributeNode(WSDLGenerator.MARK_COLUMN_VALUE);
663: attrMarkColVal.setNodeValue("");
664:
665: Attr attrMovRowname = findSqlEle
666: .getAttributeNode(WSDLGenerator.MOVEROW_TABLE_NAME);
667: attrMovRowname.setNodeValue("");
668:
669: Attr attrMarkColName = findSqlEle
670: .getAttributeNode(WSDLGenerator.MARK_COLUMN_NAME);
671: attrMarkColName.setNodeValue("");
672:
673: Attr attrPollMilli = findSqlEle
674: .getAttributeNode(WSDLGenerator.POLLMILLI_SECONDS);
675: attrPollMilli.setNodeValue("5000");
676: } catch (final Exception e) {
677: throw new WSDLException(WSDLException.OTHER_ERROR,
678: "Could not generate the WSDL");
679: }
680: }
681:
682: /**
683: *
684: * @param dao
685: * @param query
686: * @throws WSDLException
687: * @throws Exception
688: */
689: public void modifyPoll(final DBQueryModel dao, final String query)
690: throws WSDLException, Exception {
691: try {
692: final Element rootEle = this .doc.getDocumentElement();
693: String primaryKey = null;
694: final Element poolSqlEle = this .getElementBySQL(rootEle,
695: WSDLGenerator.POLL_QUERY);
696:
697: // Create sql element to replace
698: primaryKey = dao.getPrimaryKey();
699:
700: Attr optType = poolSqlEle
701: .getAttributeNode(WSDLGenerator.OPERATION_TYPE);
702: optType.setNodeValue("poll");
703:
704: Attr attrParamOrder = poolSqlEle
705: .getAttributeNode(WSDLGenerator.PARAM_ORDER);
706: attrParamOrder.setNodeValue("");
707:
708: Attr attrSqlEle = poolSqlEle
709: .getAttributeNode(WSDLGenerator.SQL_ELEMENT);
710: attrSqlEle.setNodeValue(query);
711:
712: Attr attrPrmKey = poolSqlEle
713: .getAttributeNode(WSDLGenerator.PRIMARYKEY_ATTR);
714: attrPrmKey.setNodeValue(primaryKey);
715:
716: Attr attrTrans = poolSqlEle
717: .getAttributeNode(WSDLGenerator.TRANSACTION);
718: attrTrans.setNodeValue("NOTransaction");
719:
720: Attr attrTableName = poolSqlEle
721: .getAttributeNode(WSDLGenerator.TABLE_NAME);
722: attrTableName.setNodeValue(this .mTableName);
723:
724: Attr attrNumOfRecs = poolSqlEle
725: .getAttributeNode(WSDLGenerator.NUMNBER_OF_RECORDS);
726: attrNumOfRecs.setNodeValue("");
727:
728: Attr attrMarkColVal = poolSqlEle
729: .getAttributeNode(WSDLGenerator.MARK_COLUMN_VALUE);
730: attrMarkColVal.setNodeValue("");
731:
732: Attr attrMovRowname = poolSqlEle
733: .getAttributeNode(WSDLGenerator.MOVEROW_TABLE_NAME);
734: attrMovRowname.setNodeValue("");
735:
736: Attr attrMarkColName = poolSqlEle
737: .getAttributeNode(WSDLGenerator.MARK_COLUMN_NAME);
738: attrMarkColName.setNodeValue("");
739:
740: Attr attrPollPost = poolSqlEle
741: .getAttributeNode(WSDLGenerator.POLL_POST_PROCESS);
742: attrPollPost.setNodeValue("Delete");
743:
744: Attr attrPollMilli = poolSqlEle
745: .getAttributeNode(WSDLGenerator.POLLMILLI_SECONDS);
746: attrPollMilli.setNodeValue("5000");
747:
748: } catch (final Exception e) {
749: throw new WSDLException(WSDLException.OTHER_ERROR,
750: "Could not generate the WSDL");
751: }
752: }
753:
754: /**
755: * @throws WSDLException
756: * @throws Exception
757: */
758: public void modifyServiceAndPortNames() throws WSDLException,
759: Exception {
760: try {
761: final Element rootEle = this .doc.getDocumentElement();
762: final NodeList list = rootEle.getChildNodes();
763: for (int i = 0; i < list.getLength(); i++) {
764: final Node n = list.item(i);
765: if (n.getNodeType() == Node.ELEMENT_NODE) {
766: if (n.getLocalName().equalsIgnoreCase(
767: WSDLGenerator.SERVICE_NAME)) {
768: Element serEle = (Element) n;
769: Attr attrSer = serEle.getAttributeNode("name");
770: attrSer.setNodeValue(this .mWSDLFileName
771: + WSDLGenerator.SERVICE_NAME);
772:
773: final NodeList childList = n.getChildNodes();
774: for (int j = 0; j < childList.getLength(); j++) {
775: final Node childNode = childList.item(j);
776: if (childNode.getNodeType() == Node.ELEMENT_NODE) {
777: if (childNode.getLocalName()
778: .equalsIgnoreCase(PORT_NAME)) {
779: Element portEle = (Element) childNode;
780: Attr attrPort = portEle
781: .getAttributeNode("name");
782: attrPort
783: .setNodeValue(this .mWSDLFileName
784: + WSDLGenerator.PORT_NAME);
785: }
786: }
787: }
788: }
789: }
790: }
791: } catch (final Exception e) {
792: throw new WSDLException(WSDLException.OTHER_ERROR,
793: "Could not generate the WSDL");
794: }
795: }
796:
797: /**
798: *
799: * @throws WSDLException
800: * @throws Exception
801: */
802: public void modifyJNDIAddress() throws WSDLException, Exception {
803: try {
804: final Element rootEle = this .doc.getDocumentElement();
805: final Element jndiEle = this .getElementByAddress(rootEle,
806: WSDLGenerator.JNDI_NAME);
807:
808: Attr attrJndi = jndiEle.getAttributeNode("jndiName");
809: attrJndi.setNodeValue(this .mJNDIName);
810: } catch (final Exception e) {
811: throw new WSDLException(WSDLException.OTHER_ERROR,
812: "Could not generate the WSDL");
813: }
814: }
815:
816: /**
817: *
818: * @return
819: */
820: public DBQueryModel getQueryGenerator() {
821: DBQueryModel objDataAccess = null;
822: if (this .mDBType.equalsIgnoreCase("DERBY")) {
823: objDataAccess = DerbyQueryGenerator.getInstance();
824: } else if (this .mDBType.equalsIgnoreCase("ORACLE")) {
825: objDataAccess = OracleQueryGenerator.getInstance();
826: } else if (this .mDBType.equalsIgnoreCase("DB2")) {
827: objDataAccess = DB2QueryGenerator.getInstance();
828: } else if (this .mDBType.equalsIgnoreCase("SQLServer")) {
829: objDataAccess = SQLServerQueryGenerator.getInstance();
830: } else if (this .mDBType.equalsIgnoreCase("MYSQL")) {
831: objDataAccess = MySQLQueryGenerator.getInstance();
832: } else {
833: objDataAccess = JdbcQueryGenerator.getInstance();
834: }
835:
836: return objDataAccess;
837: }
838:
839: /**
840: * Helper method to return the Element with the name elementName from a
841: * top level element e. The method recursively looks thru sub elements and
842: * returns it once it is found. or a null.
843: * @param e
844: * @param elementName
845: * @return
846: */
847: private Element getElementByName(final Element e,
848: final String elementName) {
849: if (e.getAttribute("name").equalsIgnoreCase(elementName)) {
850: return e;
851: }
852: final NodeList list = e.getChildNodes();
853: Element el = null;
854: Element e2 = null;
855: for (int i = 0; i < list.getLength(); i++) {
856: if (e2 == null) {
857: final Node n = list.item(i);
858: if (n.getNodeType() == Node.ELEMENT_NODE) {
859: el = (Element) n;
860: if (el.getAttribute("name").equalsIgnoreCase(
861: elementName)) {
862: e2 = el;
863: break;
864: } else {
865: e2 = this .getElementByName(el, elementName);
866: if (e2 != null
867: && e2.getAttribute("name")
868: .equalsIgnoreCase(elementName)) {
869: return e2;
870: }
871: }
872: }
873: } else {
874: break;
875: }
876: }
877: return e2;
878: }
879:
880: /**
881: *
882: * @param e
883: * @param elementName
884: * @return
885: */
886: private Element getElementBySQL(final Element e,
887: final String elementName) {
888: if (e.getAttribute("sql").equalsIgnoreCase(elementName)) {
889: return e;
890: }
891: final NodeList list = e.getChildNodes();
892: Element el = null;
893: Element e2 = null;
894: for (int i = 0; i < list.getLength(); i++) {
895: if (e2 == null) {
896: final Node n = list.item(i);
897: if (n.getNodeType() == Node.ELEMENT_NODE) {
898: el = (Element) n;
899: if (el.getAttribute("input").equalsIgnoreCase(
900: elementName)) {
901: e2 = el;
902: break;
903: } else {
904: e2 = this .getElementBySQL(el, elementName);
905: if (e2 != null
906: && e2.getAttribute("sql")
907: .equalsIgnoreCase(elementName)) {
908: return e2;
909: }
910: }
911: }
912: } else {
913: break;
914: }
915: }
916: return e2;
917: }
918:
919: /**
920: *
921: * @param e
922: * @param elementName
923: * @return
924: */
925: private Element getElementByAddress(final Element e,
926: final String elementName) {
927: if (e.getAttribute("jndiName").equalsIgnoreCase(elementName)) {
928: return e;
929: }
930: final NodeList list = e.getChildNodes();
931: Element el = null;
932: Element e2 = null;
933: for (int i = 0; i < list.getLength(); i++) {
934: if (e2 == null) {
935: final Node n = list.item(i);
936: if (n.getNodeType() == Node.ELEMENT_NODE) {
937: el = (Element) n;
938: if (el.getAttribute("jndiName").equalsIgnoreCase(
939: elementName)) {
940: e2 = el;
941: break;
942: } else {
943: e2 = this .getElementByAddress(el, elementName);
944: if (e2 != null
945: && e2.getAttribute("jndiName")
946: .equalsIgnoreCase(elementName)) {
947: return e2;
948: }
949: }
950: }
951: } else {
952: break;
953: }
954: }
955: return e2;
956: }
957:
958: /**
959: * persist the wsdl file to disk
960: *
961: * @throws WSDLException
962: */
963: private void writeWsdl() throws WSDLException {
964: try {
965: final WSDLWriter writer = WSDLGenerator.factory
966: .newWSDLWriter();
967: final String outputFileName = this .wsdlFileLocation + "/"
968: + this .mWSDLFileName + ".wsdl";
969: final Writer sink = new FileWriter(outputFileName);
970: writer.writeWSDL(this .def, sink);
971: WSDLGenerator.logger.log(Level.INFO,
972: "Successfully generated wsdl file :"
973: + outputFileName);
974: } catch (final Exception e) {
975: throw new WSDLException(WSDLException.OTHER_ERROR, e
976: .getMessage());
977: }
978:
979: }
980: }
|