diff --git a/process/Comm_lib/process.js b/process/Comm_lib/process.js index 1fc1ecff2447239d14eea4d220c0933c40a8272c..c47d8449e7c70c684583b4b046554732e2319050 100644 --- a/process/Comm_lib/process.js +++ b/process/Comm_lib/process.js @@ -1,67 +1,156 @@ -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 - * - * @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 - }; -} +import("system.db"); +import("system.vars"); +import("system.datetime"); +import("system.translate"); +import("system.net"); +import("system.mail"); +import("system.cti"); + +/** + * provides static methods for miscellaneous tasks + * do not create an instance of this + * + * @class + */ +function CommUtil(){} + +/** + * returns the ids of COMM.MEDIUM that are clustered unter a specific category + * @param {String} pCategory value of the keyword "COMM.MEDIUM" custom.category; e.g. "PHONE" + * @return {String[]} array of the medium-ids + */ +CommUtil.getMediumIdsByCategory = function (pCategory) +{ + var kwd = KeywordUtils.createKeyword("COMM.MEDIUM"); + kwd.filter(function(id, name, customs){ + return customs.category == pCategory; + }); + var mediumIds = kwd.toArray("id"); + return mediumIds; +}; + +/** + * sets the standard address for COMM entries for a specific category; + * does not verify if the COMMID you provide has the given category + * @param {String} pAffectedRowId a refencial ID whoes COMMs should be modified (a RELATIONID) + * @param {String} pNewStandardCommId COMMID of the new STANDARD comm-entry + * @param {String} pCategory value of the keyword "COMM.MEDIUM" custom.category; e.g. "PHONE"; the STANDARD of this category is set + * @return {null} currently returns always null; reserved for future + */ +CommUtil.setStandardForCategory = function(pAffectedRowId, pNewStandardCommId, pCategory) +{ + if (!pAffectedRowId || !pNewStandardCommId || !pCategory) + return null; + var mediumIds = CommUtil.getMediumIdsByCategory(pCategory); + if (mediumIds.length == 0)//none found? since mediumIds affects the update later there is no reason to continue + return null; + + var statements = []; + var timestamp = datetime.date(); + var login = vars.get("$sys.user"); + var cols = ["STANDARD", "DATE_EDIT", "USER_EDIT"]; + var types = db.getColumnTypes("COMM", cols); + + //set current standard comm-record as non-standard + var cond = SqlCondition.begin().and("STANDARD = 1").andPrepare("COMM.RELATION_ID", pAffectedRowId); + //mediumIds cannot be empty so no need to do further checks (this is checked above) + cond.and("MEDIUM_ID in (" + mediumIds.join(", ") + ")"); + statements.push(["COMM", cols, types, ["0", timestamp, login], cond.build()]); + + //set the new standard comm-record + cond.clear(); + //check commid, relId and medium to prevent data-inconsistency when bad function params are passed by (e.g commid of a different category) + cond.andPrepare("COMM.COMMID", pNewStandardCommId).andPrepare("COMM.RELATION_ID", pAffectedRowId); + cond.and("MEDIUM_ID in (" + mediumIds.join(", ") + ")"); + statements.push(["COMM", cols, types, ["1", timestamp, login], cond.build()]); + + count = db.updates(statements); + return null; +}; + +/** + * sets the standard PHONE-entry for COMM + * does not verify if the COMMID you provide is actually PHONE-COMM + * @param {String} pAffectedRowId a refencial ID whoes COMMs should be modified (a RELATIONID) + * @param {String} pNewStandardCommId COMMID of the new STANDARD comm-entry + * @return {null} currently returns always null; reserved for future + */ +CommUtil.setStandardPhone = function(pAffectedRowId, pNewStandardCommId) +{ + return CommUtil.setStandardForCategory(pAffectedRowId, pNewStandardCommId, "PHONE"); +}; + +/** + * sets the standard EMAIL-entry for COMM + * does not verify if the COMMID you provide is actually EMAIL-COMM + * @param {String} pAffectedRowId a refencial ID whoes COMMs should be modified (a RELATIONID) + * @param {String} pNewStandardCommId COMMID of the new STANDARD comm-entry + * @return {null} currently returns always null; reserved for future + */ +CommUtil.setStandardMail = function(pAffectedRowId, pNewStandardCommId) +{ + return CommUtil.setStandardForCategory(pAffectedRowId, pNewStandardCommId, "EMAIL"); +}; + +/** + * provides static methods for validation of communication data + * do not create an instance of this + * + * @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 + }; +};