Skip to content

Modularize itoolkit.js see issue #20 #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions lib/icmd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) International Business Machines Corp. 2019
// All Rights Reserved

// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
// associated documentation files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge, publish, distribute,
// sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
// NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

const { iXmlNodeCmdOpen, iXmlNodeCmdClose } = require('./ixml');

// iCmd() return a CL command object

/**
* @description creates cmd XML
* @param {string} cmd
* @param {object} [options]
* @returns {string} - the generated XML for the CL command
*/

const iCmd = (cmd, options) => {
let xexec = 'cmd';
let xml;

if (cmd.indexOf('?') > 0) { // If need return value
xexec = 'rexx';
}

if (options) {
xml = iXmlNodeCmdOpen(options.exec, options.hex, options.before, options.after, options.error)
+ cmd + iXmlNodeCmdClose();
} else {
xml = iXmlNodeCmdOpen(xexec, '', '', '', '', '') + cmd + iXmlNodeCmdClose();
}

return xml;
};

module.exports = iCmd;
183 changes: 183 additions & 0 deletions lib/iconn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
// Copyright (c) International Business Machines Corp. 2019
// All Rights Reserved

// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
// associated documentation files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge, publish, distribute,
// sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
// NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


const iXml = require('./ixml');
const { iRestHttp } = require('./irest');
const { db2Call } = require('./istoredp');

const I_TRANSPORT_REST = 'REST';
const I_TRANSPORT_DB2 = 'DB2';

class iConn {
/**
* @description Creates a new iConn object
* @constructor
* @param {string} db
* @param {string} user
* @param {string} pwd
* @param {object} [option]
*/
constructor(db, user, pwd, option) {
this.conn = {};
this.cmds = [];
this.timeout = 5000; // Default timeout is 5 seconds for sync mode.
this.I_DEBUG_VERBOSE = false;
this.conn.I_TRANSPORT_DB2_DATABASE = db; // Required Field
this.conn.I_TRANSPORT_DB2_USER = user; // Required Field
this.conn.I_TRANSPORT_DB2_PASSWORD = pwd; // Required Field
this.conn.iXml_SERVICE_LIB = 'QXMLSERV'; // Default XML Service library
this.conn.I_TRANSPORT_CTL = '*here';
this.conn.I_TRANSPORT_IPC = '*NA';

if (typeof db === 'object') {
this.conn.I_TRANSPORT = I_TRANSPORT_DB2;
return;
}

if (option && typeof option === 'object') {
if (option.host) {
this.conn.I_TRANSPORT = I_TRANSPORT_REST;
this.conn.I_TRANSPORT_REST_HOST = option.host;
if (option.port) {
this.conn.I_TRANSPORT_REST_PORT = option.port;
} else {
this.conn.I_TRANSPORT_REST_PORT = 80;
}
if (option.path) {
this.conn.I_TRANSPORT_REST_PATH = option.path;
} else {
this.conn.I_TRANSPORT_REST_PATH = '/';
}
} else {
this.conn.I_TRANSPORT = I_TRANSPORT_DB2;
if (option.xslib && option.xslib.length > 0) { this.conn.iXml_SERVICE_LIB = option.xslib; }
}

if (option.ctl && option.ctl.length > 0) {
this.conn.I_TRANSPORT_CTL = option.ctl;
}
if (option.ipc && option.ipc.length > 0) {
this.conn.I_TRANSPORT_IPC = option.ipc;
}
if (Number.isNaN(option.buf)) {
this.conn.I_TRANSPORT_REST_XML_OUT_SIZE = '500000';
} else {
this.conn.I_TRANSPORT_REST_XML_OUT_SIZE = option.buf.toString();
}
} else { this.conn.I_TRANSPORT = I_TRANSPORT_DB2; }
}

/**
* @description override the default timeout value for sync mode.
* @param {number} seconds
*/
setTimeout(seconds) {
if (typeof seconds === 'number') {
this.timeout = seconds * 1000;
}
}

/**
* @description
* enables or disables the verbose mode for debugging
* returns the current state of debug flag
* @param {boolean} [flag]
* @returns {boolean} the current state of the debug flag
*/
debug(flag) {
if (typeof flag === 'boolean') {
this.I_DEBUG_VERBOSE = flag;
}
return this.I_DEBUG_VERBOSE;
}


/**
* @description returns conn property from iConn object.
* @returns {object} the conn property from iConn object.
*/
getConnection() {
return this.conn;
}

/**
* @description adds XML request to command list
* @param {string | object} xml
*/
add(xml) {
if (typeof xml === 'object') {
this.cmds.push(xml.toXML());
} else if (xml && typeof xml === 'string') {
this.cmds.push(xml);
}
}

/**
* @description
* Invokes transport with XML input from command list
* Calls user provided callback with the XML output
* @param {fuction} callback
* @param {boolean} sync
*/
run(callback, sync) {
// Build the XML document.
let xml = iXml.iXmlNodeHead() + iXml.iXmlNodeScriptOpen();
for (let i = 0; i < this.cmds.length; i += 1) { xml += this.cmds[i]; }
xml += iXml.iXmlNodeScriptClose();
this.cmds = [];
// Print the XML document if in debug mode.
if (this.I_DEBUG_VERBOSE) {
console.log('============\nINPUT XML\n============');
console.log(xml);
console.log('============\nOUTPUT XML\n============');
}
// Post the XML document to XML service program.
if (this.conn.I_TRANSPORT === I_TRANSPORT_REST) {
// const iCall = require('./irest'); // TODO: Moved to top level, make sure it doesnt break
iRestHttp(callback,
this.conn.I_TRANSPORT_REST_HOST,
this.conn.I_TRANSPORT_REST_PORT,
this.conn.I_TRANSPORT_REST_PATH,
this.conn.I_TRANSPORT_DB2_DATABASE,
this.conn.I_TRANSPORT_DB2_USER,
this.conn.I_TRANSPORT_DB2_PASSWORD,
this.conn.I_TRANSPORT_IPC,
this.conn.I_TRANSPORT_CTL,
xml,
this.conn.I_TRANSPORT_REST_XML_OUT_SIZE);
} else if (this.conn.I_TRANSPORT === I_TRANSPORT_DB2) {
// const db = require('./istoredp'); // TODO: Moved to top level, make sure it doesnt break
db2Call(callback,
this.conn.iXml_SERVICE_LIB,
this.conn.I_TRANSPORT_DB2_DATABASE,
this.conn.I_TRANSPORT_DB2_USER,
this.conn.I_TRANSPORT_DB2_PASSWORD,
this.conn.I_TRANSPORT_IPC,
this.conn.I_TRANSPORT_CTL,
xml,
this.conn.I_TRANSPORT_REST_XML_OUT_SIZE,
this.I_DEBUG_VERBOSE,
sync);
} else {
callback(iXml.iXmlNodeHead() + iXml.iXmlNodeScriptOpen() + iXml.iXmlNodeError('***Transport error no data***') + iXml.iXmlNodeScriptClose());
}
}
}

module.exports = iConn;
66 changes: 66 additions & 0 deletions lib/ipgm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (c) International Business Machines Corp. 2019
// All Rights Reserved

// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
// associated documentation files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge, publish, distribute,
// sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
// NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

const ProgramCaller = require('./programCaller');

class iPgm {
/**
* @description Creates a new iPgm object
* @constructor
* @param {string} pgm
* @param {object} [options]
*/
constructor(pgm, options) {
console.warn('As of v1.0, class \'iPgm\' is deprecated. Please use \'ProgramCaller\' instead.');
this.programCaller = new ProgramCaller(pgm, options);
}

/**
* @description adds a parameter to the program XML
* @param {string | array} data
* @param {string} [type]
* @param {object} options
* @param {*} inDs
*/
addParam(data, type, options, inDs) {
console.warn('As of v1.0, class \'iPgm.addParam(data, type, options, inDs)\' is deprecated. Please use \'ProgramCaller.addParam(data, type, options, inDs)\' instead.');
this.programCaller.addParam(data, type, options, inDs);
}

/**
* @description adds a return element to the program XML
* @param {string} data
* @param {string} type
* @param {object} [options]
*/
addReturn(data, type, options) {
console.warn('As of v1.0, class \'iPgm.addReturn(data, type, options)\' is deprecated. Please use \'ProgramCaller.addParam(data, type, options)\' instead.');
this.programCaller.addReturn(data, type, options);
}

/**
* @description returns the current program XML
* @returns {string} the generated program XML
*/
toXML() {
console.warn('As of v1.0, class \'iPgm.toXML()\' is deprecated. Please use \'ProgramCaller.toXML()\' instead.');
return this.programCaller.toXML();
}
}

module.exports = iPgm;
39 changes: 39 additions & 0 deletions lib/iqsh.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) International Business Machines Corp. 2019
// All Rights Reserved

// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
// associated documentation files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge, publish, distribute,
// sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
// NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


const { iXmlNodeQshOpen, iXmlNodeQshClose } = require('./ixml');

/**
* @description creates qsh XML
* @param {string} qsh
* @param {object} [options]
* @returns {string} - the generated XML for the qsh command
*/
const iQsh = (qsh, options) => {
let xml;
if (options) {
xml = iXmlNodeQshOpen(options.rows, options.hex, options.before, options.after, options.error)
+ qsh + iXmlNodeQshClose();
} else {
xml = iXmlNodeQshOpen('', '', '', '', '') + qsh + iXmlNodeQshClose();
}
return xml;
};

module.exports = iQsh;
38 changes: 38 additions & 0 deletions lib/ish.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) International Business Machines Corp. 2019
// All Rights Reserved

// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
// associated documentation files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge, publish, distribute,
// sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
// NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

const { iXmlNodeShOpen, iXmlNodeShClose } = require('./ixml');

/**
* @description creates sh XML
* @param {string} sh
* @param {object} [options]
* @returns {string} - the generated XML for the sh command
*/
const iSh = (sh, options) => {
let xml;
if (options) {
xml = iXmlNodeShOpen(options.rows, options.hex, options.before, options.after, options.error)
+ sh + iXmlNodeShClose();
} else {
xml = iXmlNodeShOpen('', '', '', '', '') + sh + iXmlNodeShClose();
}
return xml;
};

module.exports = iSh;
Loading