From b9df76c148015188332175d3790d3981a4a3d520 Mon Sep 17 00:00:00 2001 From: "j.goderbauer" <j.goderbauer@adito.de> Date: Mon, 26 Nov 2018 15:57:05 +0100 Subject: [PATCH] [Projekt: Entwicklung - Neon][TicketNr.: 1027750][JDito - libray Standard ist komplett definiert und wird in allen libs verwendet] --- others/guide/how to write JDito code.adoc | 137 ++++++++++++++++++++++ process/Comm_lib/process.js | 132 +++++++++++---------- process/Sql_lib/process.js | 5 +- 3 files changed, 207 insertions(+), 67 deletions(-) create mode 100644 others/guide/how to write JDito code.adoc diff --git a/others/guide/how to write JDito code.adoc b/others/guide/how to write JDito code.adoc new file mode 100644 index 00000000000..7e71a092f62 --- /dev/null +++ b/others/guide/how to write JDito code.adoc @@ -0,0 +1,137 @@ +How to write JDito code +============================ +:toc2: left +:numbered: + +== code structure == +=== vars and others (var, let) === +* declaration of variables go on top (what actually happens in JS anyway by default -> hoisting) +* you can define them later +* avoid `let` as much as possible because you cannot debug these variables + +Example: +[source,javascript] +---- +import("system.result"); +import("system.vars"); + +var data, cond, i, resStr; + +data = vars.get....... +---- + +=== brackets === +* `{` are placed at the end of the expression + +Example: +[source,javascript] +---- +for (i = 0, i < dataLen; i++){ + //code here +} +---- + +== defining different types of functions n libraries == + +=== by using static methods === +This will be mostly utility functions and so on, where there is no need to instanciate an object. You'll need this probably the most time. + + +Definition: +[source,javascript] +---- +/** + * provides static methods for validation of communication data + * do not create an instance of this + * @static + * @class + */ +function CommValidationUtil(){<1> +} + +/** + * returns a blueprint for validation extensions; these extensions are needed for validating comm data and can be passed to other functions + * @return {object} a object with properties that have a specific default value; normally you want to overwrite that value + */ +CommValidationUtil.getExtensionsBlueprint = function(){<2> + return { + countryCode: null + }; +} +---- +<1> the function-object that keeps everything together - this function should never be actually called (no direct call, no indirect call) +<2> an actual function that can be called + +And how to use it: +[source,javascript] +---- +import("Comm_lib"); + +var additionals = CommValidationUtil.getExtensionsBlueprint(); +---- + +=== by creating an object with functions === + +You may want to hold data and create objects where methods share that data. + +Definition: +[source,javascript] +---- +/** + * object for easier handling of conditions; <1> + * With this object you do not have to check if the string is empty or not; + * you don't need to append a "1=1" condition or similar; + * this objects gains most benefit if you have a lot of conditions that are added (or not) depending on tons of JDito-conditions + * @class + * @param {String} [alias=the current alias] the database alias where the condition shall be executed later (important for column types of preparedStatements) <2> + * @example //TODO: add missing example <3> + */ +function SqlCondition(alias){<4> + //setting null is only needed to provide autocomplete for the ADITO-designer + this.preparedValues = null; + this._init();//the properties are initalized in an extra function because init is nearly the same as resetting (clearing) the SqlConditions + this.alias = alias; +} +/** + * append with SQL-and; no paranthesize of existing conditions is done + * @param {String} cond the condition string which shall be appended + * @return {Object} current SqlCondition-object + */ +SqlCondition.prototype.and = function(cond){<5> + if (!cond) + return this; + if (this._sqlStorage) + this._sqlStorage += " and "; + this._sqlStorage += cond; + return this; +} +---- + +<1> JS-Doc comment: http://usejsdoc.org/ +<2> use the correct form for optional/required parameters: http://usejsdoc.org/tags-param.html +<3> examples are usefull on more complex functions +<4> constructor function; init propiertes (do not set functions ("methods") here!) +<5> add functions ("methods") to the prototype, they are available through the prototype chain + +And how to use it (normally you'd want to use preparedStatements but for the sake of an easy example it's a bit shorter here) +[source,javascript] +---- +import("system.vars"); +import("system.result"); +import("Sql_lib"); +import("Comm_lib"); + +var cond, mediumIds, idVal; + +cond = new SqlCondition(); + +mediumIds = CommExtensions.getContextualMediumIds(); +if (mediumIds.length > 0) + cond.and("COMM.MEDIUM_ID in (" + mediumIds.join(", ") + ")"); + +idVal = vars.get("$local.idvalue"); +if (uids.length > 0) + cond.and("COMM.COMMID = '" + idVal + "' "); + +result.string(cond.toString("COMM.OPTIONAL = 't'")); +---- \ No newline at end of file diff --git a/process/Comm_lib/process.js b/process/Comm_lib/process.js index 84b969d635c..7445c6f7df3 100644 --- a/process/Comm_lib/process.js +++ b/process/Comm_lib/process.js @@ -1,65 +1,67 @@ -import("system.translate"); -import("system.net"); -import("system.mail"); -import("system.cti"); - - -/** - * provides somehow static methods for validation of communication data - * do not create an instance of this - */ -function CommValidationUtil(){ -} - -/** - * creates a function depending on a given COMM-category (like PHONE, EMAIL, etc.) or null if no validation-function was defined for the given category - * @param {string} commCategory category that determines which function shall be created, e.g. "EMAIL" - * @return {function} function that receives the following arguments: - * <br/> - commAddress - * <br/> - extensions - * <br/>the function has to return null if everything is OK or a value with details if validation failed - */ -CommValidationUtil.makeValidationFn = function (commCategory){ - var callbackFn; - - switch (commCategory) { - case "EMAIL": - callbackFn = function (addrValue){ -// if (!mail.isValidMailAddress(addrValue)) //TODO: enable JDito-methods -// return translate.text("no valid mail-address format"); - return null; - } - break; - case "LINK": - callbackFn = function (addrValue){ -// if (!net.isValidUrl(addrValue, ["http", "https"]))//TODO: enable JDito-methods -// return translate.text("no valid format"); - return null; - } - break; - case "TELEPHONE": - callbackFn = function (addrValue, ext){ - var country = null; - if (addrValue[0] != "+") //if the number starts with a country-identifier (e.g. +49) no country needs to be specified - country = ext.countryCode; -// if (!cti.isValidPhoneNumber(addrValue, country))//TODO: enable JDito-methods -// return translate.text("no valid phone number"); - return null; - } - break; - default: - callbackFn = null; - break; - } - return callbackFn; -} - -/** - * returns a blueprint for validation extensions; these extensions are needed for validating comm data and can be passed to other functions - * @return {object} a object with properties that have a specific default value; normally you want to overwrite that value - */ -CommValidationUtil.getExtensionsBlueprint = function(){ - return { - countryCode: null - }; -} +import("system.translate"); +import("system.net"); +import("system.mail"); +import("system.cti"); + + +/** + * provides static methods for validation of communication data + * do not create an instance of this + * @static + * @class + */ +function CommValidationUtil(){ +} + +/** + * creates a function depending on a given COMM-category (like PHONE, EMAIL, etc.) or null if no validation-function was defined for the given category + * @param {string} commCategory category that determines which function shall be created, e.g. "EMAIL" + * @return {function} function that receives the following arguments: + * <br/> - commAddress + * <br/> - extensions + * <br/>the function has to return null if everything is OK or a value with details if validation failed + */ +CommValidationUtil.makeValidationFn = function (commCategory){ + var callbackFn; + + switch (commCategory) { + case "EMAIL": + callbackFn = function (addrValue){ +// if (!mail.isValidMailAddress(addrValue)) //TODO: enable JDito-methods +// return translate.text("no valid mail-address format"); + return null; + } + break; + case "LINK": + callbackFn = function (addrValue){ +// if (!net.isValidUrl(addrValue, ["http", "https"]))//TODO: enable JDito-methods +// return translate.text("no valid format"); + return null; + } + break; + case "TELEPHONE": + callbackFn = function (addrValue, ext){ + var country = null; + if (addrValue[0] != "+") //if the number starts with a country-identifier (e.g. +49) no country needs to be specified + country = ext.countryCode; +// if (!cti.isValidPhoneNumber(addrValue, country))//TODO: enable JDito-methods +// return translate.text("no valid phone number"); + return null; + } + break; + default: + callbackFn = null; + break; + } + return callbackFn; +} + +/** + * returns a blueprint for validation extensions; these extensions are needed for validating comm data and can be passed to other functions + * @return {object} a object with properties that have a specific default value; normally you want to overwrite that value + */ +CommValidationUtil.getExtensionsBlueprint = function(){ + return { + countryCode: null + }; +} diff --git a/process/Sql_lib/process.js b/process/Sql_lib/process.js index 7c93fb1bc28..61a7c941e30 100644 --- a/process/Sql_lib/process.js +++ b/process/Sql_lib/process.js @@ -11,8 +11,9 @@ import("Util_lib"); * object for easier handling of conditions; * With this object you do not have to check if the string is empty or not; * you don't need to append a "1=1" condition or similar; - * this objects gains most benefit if you have a lot of conditions that are added (or not) depending on tons of conditions - * @class + * this objects gains most benefit if you have a lot of conditions that are added (or not) depending on tons of JDito-conditions + * @class + * @param {String} [alias=the current alias] the database alias where the condition shall be executed later (important for column types of preparedStatements) * @example //TODO: add missing example */ function SqlCondition(alias){ -- GitLab