From 883f5074b544aa951fa95e45b751edc10580e429 Mon Sep 17 00:00:00 2001
From: "j.goderbauer" <j.goderbauer@adito.de>
Date: Tue, 25 Aug 2020 16:03:02 +0200
Subject: [PATCH] Countries_entity: added record container caching

---
 entity/Countries_Entity/Countries_Entity.aod  |  2 +
 .../recordcontainers/db/cacheKeyProcess.js    |  5 ++
 process/CountryInfoData_lib/process.js        | 62 ++++++++++---------
 process/CountryInfo_lib/process.js            |  2 -
 4 files changed, 40 insertions(+), 31 deletions(-)
 create mode 100644 entity/Countries_Entity/recordcontainers/db/cacheKeyProcess.js

diff --git a/entity/Countries_Entity/Countries_Entity.aod b/entity/Countries_Entity/Countries_Entity.aod
index 41d1741997..35e3e6c3c5 100644
--- a/entity/Countries_Entity/Countries_Entity.aod
+++ b/entity/Countries_Entity/Countries_Entity.aod
@@ -98,6 +98,8 @@
       <alias>Data_alias</alias>
       <isPageable v="false" />
       <orderClauseProcess>%aditoprj%/entity/Countries_Entity/recordcontainers/db/orderClauseProcess.js</orderClauseProcess>
+      <cacheType>GLOBAL</cacheType>
+      <cacheKeyProcess>%aditoprj%/entity/Countries_Entity/recordcontainers/db/cacheKeyProcess.js</cacheKeyProcess>
       <linkInformation>
         <linkInformation>
           <name>cc219004-43fd-4a3f-bee0-be5e732c61f1</name>
diff --git a/entity/Countries_Entity/recordcontainers/db/cacheKeyProcess.js b/entity/Countries_Entity/recordcontainers/db/cacheKeyProcess.js
new file mode 100644
index 0000000000..a332cb19c8
--- /dev/null
+++ b/entity/Countries_Entity/recordcontainers/db/cacheKeyProcess.js
@@ -0,0 +1,5 @@
+import("CachedRecordContainer_lib");
+import("system.result")
+
+var res = CachedRecordContainerUtils.getCommonKey();
+result.string(res);
\ No newline at end of file
diff --git a/process/CountryInfoData_lib/process.js b/process/CountryInfoData_lib/process.js
index dbd7ab94a6..d071f7d601 100644
--- a/process/CountryInfoData_lib/process.js
+++ b/process/CountryInfoData_lib/process.js
@@ -1,38 +1,42 @@
 import("system.entities");
 import("system.translate");
-import("system.db");
-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.
-*/
 
+/**
+ * provides functions for retrieving common forms of countrydata
+ * Do not create an instance of this!
+ *
+ * @class
+ * @static
+ */
 function CountryInfoData(){}
 
-CountryInfoData.getMainData = function (pLocale)
-{
-    var cache = new CachedData("CountryInfoData.getMainData", true, pLocale);
-    return cache.load(function (pTranslationNecessary, pLocale){
-        var loadingConfig = entities.createConfigForLoadingRows().entity("Countries_Entity").fields(["ISO2", "NAME_TRANSLATED"]);//TODO: into cachedDatalib
-        var countryData = entities.getRows(loadingConfig);//TODO: ignore grants?
-        return countryData;
-    });
-};
-
+/**
+* loads the ISO2 code and name of the countries_entity
+*
+* @param {String} [pLocale=current language]    <p/>specifies the locale for translating the name, if not given the locale of the current image-context 
+*                                                     (server or client) is used
+*
+* @return {Array} 2D array where each row is an array of: [<<iso2-code>>, <<country-name>>]
+*
+*/
 CountryInfoData.getIso2NameData = function (pLocale)
 {
-    var cache = new CachedData("CountryInfoData.getIso2NameData", true, pLocale);
-    return cache.load(function (pTranslationNecessary, pLocale){
-        var countryData = CountryInfoData.getMainData(pLocale);
-        countryData = countryData.map(function (row){
-                return [row["ISO2"], row["NAME_TRANSLATED"]];
-            });
-        return countryData;
+    var fieldsToLoad = ["ISO2"];
+    if (pLocale)
+        fieldsToLoad.push("NAME");
+    else
+        fieldsToLoad.push("NAME_TRANSLATED");
+
+    var loadingConfig = entities.createConfigForLoadingRows()
+        .ignorePermissions(true)
+        .entity("Countries_Entity")//default record container is cached
+        .fields(fieldsToLoad);
+    var countryData = entities.getRows(loadingConfig);
+    
+    var res = countryData.map(function (entityRow){
+        var name = pLocale ? translate.text(entityRow["NAME"], pLocale) : entityRow["NAME_TRANSLATED"];
+        return [entityRow["ISO2"], name];
     });
+    return res;
 };
-    
+    
\ No newline at end of file
diff --git a/process/CountryInfo_lib/process.js b/process/CountryInfo_lib/process.js
index 999d00d5d2..2db23f676c 100644
--- a/process/CountryInfo_lib/process.js
+++ b/process/CountryInfo_lib/process.js
@@ -34,8 +34,6 @@ CountryUtils.getLatinNameByIso2 = function(pIso2, pLocale)
     return countryName;
 };
 
-//SqlUtils.getResolvingCaseWhen(keywordData, pDbFieldName, pLocale);
-
 /**
 * resolves ISO2 codes of countries to the translated name
 * 
-- 
GitLab