diff --git a/process/KeywordData_lib/process.js b/process/KeywordData_lib/process.js index a4fbd6bd4e44b0b1068e110f60e27107bceb47ee..2377e227e109e2fb9625c52c14662c3d8869ef02 100644 --- a/process/KeywordData_lib/process.js +++ b/process/KeywordData_lib/process.js @@ -1,25 +1,26 @@ -import("system.translate"); import("system.db"); +import("system.translate"); import("DataCaching_lib"); import("Sql_lib"); -/* -This is a experimental library. -The code will definetly change in the future while not keeping the same functionparameters and calls. You should not use this except you excatly know what you're doing. -Since the functions will change, comments are written for the future functions. - -The idea behind this is to maintain data in cached form (client-context-side)) for various keyword-constructs. +/** + * This is an experimental library. + * The code will definitely change in the future while not keeping the same function parameters and calls. You should not use this except you exactly know what you're doing. + * Since the functions will change, comments are written for the future functions. + * + * The idea behind this is to maintain data in cached form (client-context-side)) for various keyword-constructs. */ -function KeywordData(){} +function KeywordData() {} -KeywordData.getSimpleData = function (pKeywordContainer, pLocale, pOnlyActives) +KeywordData.getSimpleData = function(pKeywordContainer, pLocale, pOnlyActives) { - var onlyActives = (pOnlyActives == undefined ? true : pOnlyActives); + var onlyActives = pOnlyActives == undefined ? true : pOnlyActives; var flags = "#" + (onlyActives ? "1" : "0"); var identifier = "KeywordSimpleData_" + pKeywordContainer + flags; var cache = new CachedData(identifier, true, pLocale); - return cache.load(function (pTranslationNecessary, pLocale){ + + return cache.load(function(pTranslationNecessary, pLocale) { var keywordEntrySelect = newSelect("AB_KEYWORD_ENTRY.KEYID, AB_KEYWORD_ENTRY.TITLE", "Data_alias") .from("AB_KEYWORD_ENTRY") .orderBy("AB_KEYWORD_ENTRY.SORTING asc, AB_KEYWORD_ENTRY.TITLE asc") @@ -34,29 +35,33 @@ KeywordData.getSimpleData = function (pKeywordContainer, pLocale, pOnlyActives) { keywordData[i][1] = translate.text(keywordData[i][1], pLocale); } + return keywordData.slice(0); }); }; -KeywordData.getKeyIdMap = function (pKeywordContainer, pLocale) +KeywordData.getKeyIdMap = function(pKeywordContainer, pLocale) { var cache = new CachedData("KeywordKeyidMap_" + pKeywordContainer, true, pLocale); - return cache.load(function (pTranslationNecessary, pLocale){ + + return cache.load(function(pTranslationNecessary, pLocale) { var keywordData = KeywordData.getSimpleData(pKeywordContainer, pLocale) var res = {}; var keyid, title; + for (var i = 0, l = keywordData.length; i < l; i++) { [keyid, title] = keywordData[i]; - res[keyid] = title;//title comes already translated through the getData-function + res[keyid] = title; // title comes already translated through the getData-function } + return res; }); }; -KeywordData.getKeywordAttributeRelations = function (pKeywordContainer) +KeywordData.getKeywordAttributeRelations = function(pKeywordContainer) { - return CachedData.make("KeywordAttributeRelationObj_" + pKeywordContainer, false, function(){ + return CachedData.make("KeywordAttributeRelationObj_" + pKeywordContainer, false, function() { var data = newSelect("AB_KEYWORD_ENTRY.KEYID, AB_KEYWORD_ATTRIBUTE.NAME, AB_KEYWORD_ATTRIBUTE.KIND, \n\ AB_KEYWORD_ATTRIBUTERELATION.CHAR_VALUE, AB_KEYWORD_ATTRIBUTERELATION.LONG_CHAR_VALUE, AB_KEYWORD_ATTRIBUTERELATION.NUMBER_VALUE, AB_KEYWORD_ATTRIBUTERELATION.BOOL_VALUE") .from("AB_KEYWORD_ENTRY") @@ -73,11 +78,13 @@ KeywordData.getKeywordAttributeRelations = function (pKeywordContainer) { [keyId, name, type, charVal, longCharVal, numVal, boolVal] = data[i]; name = name.trim(); - type = type.trim(); + type = type.trim(); + if (res[keyId] == undefined) res[keyId] = {}; var parsedValue = null; + switch(type) { case "NUMBER_VALUE": @@ -102,12 +109,14 @@ KeywordData.getKeywordAttributeRelations = function (pKeywordContainer) return data; } -function LanguageData(){} + +function LanguageData() {} LanguageData.getData = function() { var cache = new CachedData("LanguagesData", false); - return cache.load(function (pTranslationNecessary, pLocale){ + + return cache.load(function(pTranslationNecessary, pLocale) { var data = db.table("select AB_LANGUAGE.ISO3, AB_LANGUAGE.NAME_LATIN from AB_LANGUAGE"); return data; }); @@ -117,16 +126,19 @@ LanguageData.getIso3Map = function(pLocale, pAlias) { var dbAlias = pAlias || db.getCurrentAlias(); var cache = new CachedData("LanguagesISO3Map", true, pLocale, dbAlias); - var cachedData = cache.load(function (pTranslationNecessary, pLocale, pAlias){ + var cachedData = cache.load(function(pTranslationNecessary, pLocale, pAlias) { var data = db.table("select AB_LANGUAGE.ISO3, AB_LANGUAGE.ISO2, AB_LANGUAGE.NAME_LATIN from AB_LANGUAGE", pAlias); var res = {}; var iso3, iso2, countryName; + for (var i = 0, l = data.length; i < l; i++) { [iso3, iso2, countryName] = data[i]; res[iso3] = {name: translate.text(countryName, pLocale), iso2: iso2}; } + return res; }); + return cachedData; }; \ No newline at end of file diff --git a/process/Keyword_lib/process.js b/process/Keyword_lib/process.js index 994fd55c07d07fda603f24db728838663dd844f2..70242a24743ba1dc889abaea542f91018f914572 100644 --- a/process/Keyword_lib/process.js +++ b/process/Keyword_lib/process.js @@ -1,10 +1,10 @@ -import("KeywordRegistry_basic"); -import("KeywordData_lib"); -import("system.vars"); -import("system.SQLTYPES"); import("system.db"); -import("system.translate"); import("system.neon"); +import("system.SQLTYPES"); +import("system.translate"); +import("system.vars"); +import("KeywordData_lib"); +import("KeywordRegistry_basic"); import("Sql_lib"); /** @@ -12,14 +12,14 @@ import("Sql_lib"); * * @class */ -function KeywordUtils(){} +function KeywordUtils() {} /** -* resolves KEYIDs of a keywordentry into the specific title +* resolves KEYIDs of a KeywordEntry into the specific title * * @param {String} pContainerName name of the keyword container that shall be resolved * @param {String} pDbFieldName name fo the database field where the KEYID-value is stored -* @param {String} [pLocale=current client language] specifies the locale for translating the title; can be false if nothing shalle be translated +* @param {String} [pLocale=current client language] specifies the locale for translating the title; can be false if nothing shall be translated * * @return {String} a SQL-expression (case-when-statement) that resolves the KEYID into the title */ @@ -27,20 +27,24 @@ KeywordUtils.getResolvedTitleSqlPart = function(pContainerName, pDbFieldName, pL { var keywordData = KeywordData.getSimpleData(pContainerName, pLocale); var resSql = SqlUtils.getResolvingCaseWhen(keywordData, pDbFieldName, pLocale); + return SqlUtils.translateStatementWithQuotes(resSql); }; /** * returns a specific name (translated) - this is normally the view-value - of a given keyword; * <br/>if the key could not be found an empty string "" is returned + * * @param {String} keywordContainer specifies the type of the keyword and therefore the list elements; e.g. "COUNTRY" * @param {String} key id value of the keyword where the view-value shall be searched * @param {String} [locale=locale depending on current client/servercontext] Language-value for translations + * * @return {String} representation of the translated name of the keyword-key + * * @example * var histMedium; * histMedium = vars.get("$field.MEDIUM"); - * if (histMedium){ + * if (histMedium) { * result.string(vars.get("$field.SUBJECT") + " (" + LegacyKeywordUtils.getViewValue("ACTIVITY.MEDIUM", histMedium) + ")"); * } */ @@ -50,9 +54,8 @@ KeywordUtils.getViewValue = function(keywordContainer, key, locale) return ""; var data = KeywordData.getKeyIdMap(keywordContainer, locale); - if (data[key] == undefined) - return ""; - return data[key]; + + return data[key] == undefined ? "" : data[key]; }; /** @@ -70,10 +73,8 @@ KeywordUtils.getAttributeRelationsByKey = function(pKeyId, pContainerName) return ""; var data = KeywordData.getKeywordAttributeRelations(pContainerName); - if (data[pKeyId] == undefined) - return {}; - else - return data[pKeyId]; + + return data[pKeyId] == undefined ? {} : data[pKeyId]; }; /** @@ -90,10 +91,12 @@ KeywordUtils.getAttributeRelationsByKey = function(pKeyId, pContainerName) KeywordUtils.getAttributeRelation = function(pKeyId, pContainerName, pAttrName, pDefault) { var attributes = KeywordUtils.getAttributeRelationsByKey(pKeyId, pContainerName); + if (attributes && attributes[pAttrName] != null) { return attributes[pAttrName]; } + return pDefault !== undefined ? pDefault : null; }; @@ -105,7 +108,7 @@ KeywordUtils.getAttributeRelation = function(pKeyId, pContainerName, pAttrName, KeywordUtils.getContainerNames = function() { //do not cache this list since - // a) the list can easly change when a new container is created + // a) the list can easily change when a new container is created // b) where this is called it's not relevant in terms of performance //!SqlBuilder var list = db.array(db.COLUMN, "select distinct AB_KEYWORD_ENTRY.CONTAINER from AB_KEYWORD_ENTRY order by AB_KEYWORD_ENTRY.CONTAINER asc"); @@ -114,7 +117,7 @@ KeywordUtils.getContainerNames = function() /** * provides a translated list of keyword-entry-titles in the system filtered by a containerName; -* usefull for lists where the key is the name which is then a editable displayValue +* useful for lists where the key is the name which is then a editable displayValue * * @param {String} pContainerName name of the keyword container for filtering * @param {String} [pLocale=locale depending on current client/servercontext] Language-value for translations @@ -124,19 +127,20 @@ KeywordUtils.getContainerNames = function() KeywordUtils.getEntryNamesByContainer = function(pContainerName, pLocale) { var data = KeywordData.getSimpleData(pContainerName, pLocale); - data = data.map(function (v){ + data = data.map(function(v) { return v[1]; }); + return data; }; /** * provides a translated list of keyword-entry-titles and its ids in the system filtered by a containerName; -* usefull for lists where the key is the name which is then a editable displayValue +* useful for lists where the key is the name which is then a editable displayValue * * @param {String} pContainerName name of the keyword container for filtering -* * @param {String} [pLocale=locale depending on current client/servercontext] Language-value for translations +* @param {String} [pLocale=locale depending on current client/servercontext] Language-value for translations * * @return {String[]} 2D-Array in the form of [[id1, translatedTitle1], [idN, translatedTitleN]] */ @@ -149,10 +153,11 @@ KeywordUtils.getEntryNamesAndIdsByContainer = function(pContainerName, pLocale) .orderBy("AB_KEYWORD_ENTRY.SORTING asc, AB_KEYWORD_ENTRY.TITLE asc") .table(); - list = list.map(function (elem){ + list = list.map(function(elem) { elem[1] = pLocale ? translate.text(elem[1], pLocale) : translate.text(elem[1]); return elem; }); + return list; }; @@ -168,20 +173,20 @@ KeywordUtils.getEntryNamesAndIdsByContainer = function(pContainerName, pLocale) KeywordUtils.exists = function(pKeyId, pContainerName) { //a check if a keyword exists should always be on the origin data and not the cache, so do not cache here - return parseInt( - newSelect("count(*)") - .from("AB_KEYWORD_ENTRY") - .where("AB_KEYWORD_ENTRY.KEYID", pKeyId) - .and("AB_KEYWORD_ENTRY.CONTAINER", pContainerName) - .cell(true, "0") - ) > 0; + var countValue = newSelect("count(*)") + .from("AB_KEYWORD_ENTRY") + .where("AB_KEYWORD_ENTRY.KEYID", pKeyId) + .and("AB_KEYWORD_ENTRY.CONTAINER", pContainerName) + .cell(true, "0"); + + return parseInt(countValue) > 0; }; /** * Get the first keyword Keyid from a container * * @param {String} pContainerName name of the keyword container that shall be resolved - * @param {String} [pLocale=current client language] specifies the locale for translating the title; can be false if nothing shalle be translated + * @param {String} [pLocale=current client language] specifies the locale for translating the title; can be false if nothing shall be translated * * @return {String} the keyid or "" */ @@ -197,7 +202,8 @@ KeywordUtils.getFirst = function(pContainerName, pLocale) * * @param {String} pContainerName specifies the type of the keyword and therefore the list elements; * e.g. "COUNTRY"; use an entry of the $KeywordRegistry here - * @return translated name, if it exists in the switch case + * + * @return {String} translated name, if it exists in the switch case */ KeywordUtils.getTranslatedContainer = function(pContainerName) { @@ -207,19 +213,18 @@ KeywordUtils.getTranslatedContainer = function(pContainerName) return translate.text("Phase"); case "SalesprojectState": return translate.text("State"); - break; default: - return "Please add " + pContainerName + " to the switch case in Salesproject_lib"; + return "Please add '" + pContainerName + "' to the switch case in Salesproject_lib"; } } /** - * object that provides featrues for a single keyword attribute; initalizes itself on creation with a specific keyword-attribute + * object that provides features for a single keyword attribute; initializes itself on creation with a specific keyword-attribute * * @param {String} pContainerName specifies the type of the keyword and therefore the list elements; * e.g. "COUNTRY"; use an entry of the $KeywordRegistry here - * @param {String} pAttributeName the name of the keyword attribute that shall be initalized + * @param {String} pAttributeName the name of the keyword attribute that shall be initialized * @param {String} [pDefault=undefined] the default value -> Does not throw an error, if default value exists. * * @class @@ -286,21 +291,21 @@ KeywordAttribute.prototype.getValue = function(pKeyId) /** * get a SqlBuilder object for this keyword attribute. You can easily add additional conditions to it. * - * @return {SqlBuilder} a SqlBuilder which contains a select for the entry-id's, joins to entry and attribute - * and conditions for the container and the attribute-name. + * @return {SqlBuilder} a SqlBuilder which contains a select for the entry-id's, joins to entry and attribute and conditions for the container and the attribute-name. */ KeywordAttribute.prototype.getSqlBuilderSelect = function() { return newSelect("AB_KEYWORD_ATTRIBUTERELATION.AB_KEYWORD_ENTRY_ID") - .from("AB_KEYWORD_ATTRIBUTERELATION") - .join("AB_KEYWORD_ENTRY", "AB_KEYWORD_ENTRYID = AB_KEYWORD_ENTRY_ID", "attrEntry") - .join("AB_KEYWORD_ATTRIBUTE", "AB_KEYWORD_ATTRIBUTEID = AB_KEYWORD_ATTRIBUTE_ID") - .where(["AB_KEYWORD_ENTRY", "CONTAINER", "attrEntry"], this.container) - .and("AB_KEYWORD_ATTRIBUTE.NAME", this.attribute) + .from("AB_KEYWORD_ATTRIBUTERELATION") + .join("AB_KEYWORD_ENTRY", "AB_KEYWORD_ENTRYID = AB_KEYWORD_ENTRY_ID", "attrEntry") + .join("AB_KEYWORD_ATTRIBUTE", "AB_KEYWORD_ATTRIBUTEID = AB_KEYWORD_ATTRIBUTE_ID") + .where(["AB_KEYWORD_ENTRY", "CONTAINER", "attrEntry"], this.container) + .and("AB_KEYWORD_ATTRIBUTE.NAME", this.attribute) } /** * check if the Container can have the attribute. + * * @return {Boolean} true if it exists, false if not */ KeywordAttribute.prototype.exists = function() @@ -308,18 +313,19 @@ KeywordAttribute.prototype.exists = function() return this.id != undefined && this.type != undefined && this.dbField != undefined; } + /** - * provides methods for interactions with the sepcial-keywords "LANGUAGE" + * provides methods for interactions with the special-keywords "LANGUAGE" * * @class */ -function LanguageKeywordUtils(){} +function LanguageKeywordUtils() {} /** -* resolves Languagecode into the latin name +* resolves LanguageCode into the latin name * * @param {String} pDbFieldName name fo the database field where the ISO3-value is stored -* @param {String} [pLocale=current client language] specifies the locale for translating the title; can be false if nothing shalle be translated +* @param {String} [pLocale=current client language] specifies the locale for translating the title; can be false if nothing shall be translated * * @return {String} a SQL-expression (case-when-statement) */ @@ -346,16 +352,17 @@ LanguageKeywordUtils.getViewValue = function(key, locale) var languageMap = LanguageData.getIso3Map(locale); var languageInfo = languageMap[key]; + if (languageInfo == undefined) return ""; + var title = languageInfo.name; - if (title == undefined) - return ""; - return title; + + return title == undefined ? "" : title; }; /** - * deterines on server- and clientside the matching iso2-code to a iso3 by generating a map and then returning the matched result; + * determines on server- and clientside the matching iso2-code to a iso3 by generating a map and then returning the matched result; * on clientside the map is cached * * @param {String} key id value (iso3) of the language where the value shall be searched @@ -368,13 +375,15 @@ LanguageKeywordUtils.Iso2FromIso3 = function(key, pAlias) { if (!key) return ""; + //maybe this should be retrieved by a simple select on the serverside since LanguageData-methods are only cached on the client side var languageMap = LanguageData.getIso3Map(null, pAlias); var languageInfo = languageMap[key]; + if (languageInfo == undefined) return ""; + var res = languageInfo.iso2; - if (res == undefined) - return ""; - return res; + + return res == undefined ? "" : res; }; \ No newline at end of file