From 83123d5adf6a1bc1f5556698eecf57a46704cebb Mon Sep 17 00:00:00 2001
From: "j.goderbauer" <j.goderbauer@adito.de>
Date: Mon, 29 Apr 2019 09:39:50 +0200
Subject: [PATCH] =?UTF-8?q?[Projekt:=20Entwicklung=20-=20Neon][TicketNr.:?=
 =?UTF-8?q?=201036415][=C3=9Cbernahme=20AUDIT=20und=20Logging=20aus=20best?=
 =?UTF-8?q?ehendem=20xRM-Basic=20f=C3=BCr=20ADITO=202019]?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 process/Keyword_lib/process.js    |  5 +-
 process/Loghistory_lib/process.js | 92 +++++++++++++++++++++++++------
 process/process_audit/process.js  |  2 +-
 3 files changed, 78 insertions(+), 21 deletions(-)

diff --git a/process/Keyword_lib/process.js b/process/Keyword_lib/process.js
index a74c939dcf..2403beea88 100644
--- a/process/Keyword_lib/process.js
+++ b/process/Keyword_lib/process.js
@@ -34,6 +34,7 @@ KeywordUtils.getResolvedTitleSqlPart = function(pContainerName, pDbFieldName, pL
  * <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] Language-value for translations
  * @return {String} representation of the translated name of the keyword-key
  * @example
  * var histMedium;
@@ -42,7 +43,7 @@ KeywordUtils.getResolvedTitleSqlPart = function(pContainerName, pDbFieldName, pL
  *     result.string(vars.get("$field.SUBJECT") + " (" + LegacyKeywordUtils.getViewValue("ACTIVITY.MEDIUM", histMedium) + ")");
  * }
  */
-KeywordUtils.getViewValue = function(keywordContainer, key)
+KeywordUtils.getViewValue = function(keywordContainer, key, locale)
 {
     if (!key)
         return "";
@@ -54,7 +55,7 @@ KeywordUtils.getViewValue = function(keywordContainer, key)
     var originalTitle = db.cell(sql);
     if (originalTitle == "")
         return "";
-    var translatedTitle = translate.text(originalTitle);
+    var translatedTitle = locale ? translate.text(originalTitle, locale) : translate.text(originalTitle);
     return translatedTitle;
 };
 
diff --git a/process/Loghistory_lib/process.js b/process/Loghistory_lib/process.js
index 7ebb50f3be..396fb3eb19 100644
--- a/process/Loghistory_lib/process.js
+++ b/process/Loghistory_lib/process.js
@@ -17,6 +17,21 @@ import("AddressEntity_lib");
 import("Util_lib");
 import("KeywordRegistry_basic");
 
+/**
+ * object for writing the LogHistory based on audit-changes
+ * this object will be probably only usefull within the "process_audit"-process
+ * 
+ * @param {String} pTable name of logged-table
+ * @param {String} pUser name of the user that performed that change
+ * @param {String[]} pColumns columns that have been changed
+ * @param {String[]} pNewValues if available the new values that correspond to pColumns
+ * @param {String[]} pOldValues if available the existing values that correspond to pColumns
+ * @param {String} pTimeStamp timestamp when the change has been audited
+ * @param {String} pAction the SQL-action of what was done (insert/update/delete)
+ * @param {String} pIdValue primaryKey value of the reocrd that has been audited
+ * 
+ * @class 
+ */
 function LogHistoryExecutor(pTable, pUser, pColumns, pNewValues, pOldValues, pTimeStamp, pAction, pIdValue)
 {
     this.structureDefinitionAlias = "Data_alias";//alias where the definition of what shall be logged is stored
@@ -33,11 +48,23 @@ function LogHistoryExecutor(pTable, pUser, pColumns, pNewValues, pOldValues, pTi
     this.translationLanguage = LogHistoryExecutor.TRANSLATION_LANGUAGE();
 }
 
+/*
+* returns a constant value in which language the logging shall be done; 
+* if you change that value this will affect only new entries not existing ones
+*
+* @return {String} locale value that can be passed to translate.* methods
+*/
 LogHistoryExecutor.TRANSLATION_LANGUAGE = function()
 {
     return "DE_de";
 };
 
+/*
+* executes the logging action into the AB_LOGHIST table
+* this is the main function to use
+*
+* @return null
+*/
 LogHistoryExecutor.prototype.execute = function ()
 {
     var references = {};
@@ -66,6 +93,9 @@ LogHistoryExecutor.prototype.execute = function ()
     if (!somethingTolog)
         return null;
     
+    if (this.affectedTable == "AB_LOGHISTORY")//this would cause an endless logging-loop otherwise
+        throw new Error(translate.withArguments("the \"%0\" table is configured to get logged but this is the logging-stoarge itself and cannot be monitored", ["AB_LOGHISTORY"]));
+    
     var idvalue = this.idValue;
     var description = [];
     var extra = [];
@@ -112,20 +142,20 @@ LogHistoryExecutor.prototype.execute = function ()
                 else
                     description.push(translate.withArguments("%0 from \"%1\" to %2 \"%3\"", [conf.Description + " " + olddata[0], olddata[1], conf.Description + " " + newdata[0] , newdata[1]], this.translationLanguage));
             } 
-            else if(this.affectedTable == "COMMUNICATION")
+            else if(this.affectedTable == "COMMUNICATION" && olddata[0] != newdata[0])//fyi: no change of the "Standard"-flag will be logged
             {
-                description.push(translate.withArguments("%0 medium from \"%1\" to \"%2\"", [conf.Description, olddata[0], newdata[0]], this.translationLanguage));
+                description.push(translate.withArguments("%0 from \"%1\" to \"%2\"", [conf.Description, olddata[0], newdata[0]], this.translationLanguage));
             }
         }
         if (conf.RefTable) this.affectedTable = conf.RefTable;
     } //no extra tables
-    else	
+   else	
     {
         if (this.affectedTable == "ASYS_CALENDARBACKEND") 
         {
             var entrytypePosition = this.columns.indexOf("ENTRYTYPE");
             if (entrytypePosition > -1 && this.newValues[entrytypePosition] == calendars.VEVENT)
-                return null;//TODO: wyh return?
+                return null;
         }
 
         for (var i = 0; i < this.columns.length; i++)
@@ -142,8 +172,8 @@ LogHistoryExecutor.prototype.execute = function ()
             var logfield = columnStructure[this.columns[i]];
             if (logfield && logfield.log)
             {
-                if (this.sqlAction != 'I' && this.oldValues[i] != "") this.oldValues[i] = LogHistoryExecutor._getFormattedValue(logfield, this.oldValues[i], this.translationLanguage);
-                if (this.sqlAction != 'D' && this.newValues[i] != "") this.newValues[i] = LogHistoryExecutor._getFormattedValue(logfield, this.newValues[i], this.translationLanguage);
+                if (this.sqlAction != 'I' && this.oldValues[i] != "") this.oldValues[i] = this._getFormattedValue(logfield, this.oldValues[i], this.translationLanguage);
+                if (this.sqlAction != 'D' && this.newValues[i] != "") this.newValues[i] = this._getFormattedValue(logfield, this.newValues[i], this.translationLanguage);
                 
                 var logfieldTitle = (logfield.title ? translate.text(logfield.title, this.translationLanguage) : translate.text("Value", this.translationLanguage));
                 if (this.sqlAction == 'U' && this.oldValues[i] != this.newValues[i])
@@ -186,6 +216,18 @@ LogHistoryExecutor.prototype.execute = function ()
     return null;
 };
 
+/*
+* adds an entry that shall be inserted into the AB_LOGHIST-table
+* that information is collected and later inserted within one transaction via the "_insertLoghistEntry"-function
+* 
+* @param {String} pTablename name of the table that is logged
+* @param {String} pTablenameId UID-value of the entry that is logged
+* @param {String} pSourceTablename reserved for future | specifies if the origin of a change was somewhere else (which table)
+* @param {String} pSourceTablenameId reserved for future | specifies if the origin of a change was somewhere else  (which id in that ref-table)
+* @param {String} pDescription text that describes the changes that have been done
+*
+* @return void
+*/
 LogHistoryExecutor.prototype._addEntryForInsert = function (pTablename, pTablenameId, pSourceTablename, pSourceTablenameId, pDescription)
 {
     if (this.toInsert == undefined)
@@ -203,6 +245,12 @@ LogHistoryExecutor.prototype._addEntryForInsert = function (pTablename, pTablena
     this.toInsert.statements.push(["AB_LOGHISTORY", this.toInsert.cols, this.toInsert.types, vals]);
 };
 
+/*
+* inserts the pending entries into the AB_LOGHIST-table within one transaction
+* elements have to be added before this via the  "_addEntryForInsert"-function oterwise nothing will happen
+*
+* @return void
+*/
 LogHistoryExecutor.prototype._insertLoghistEntry = function ()
 {
     if (this.toInsert && this.toInsert.statements)
@@ -211,12 +259,12 @@ LogHistoryExecutor.prototype._insertLoghistEntry = function ()
 
 
 /*
-* Creates the data for the tables with special cases
+* collects data for the tables with "extra"-cases
 *
 * @param {String} pId the table id
-* @param {[]} pValues the values 
+* @param {Object} pValues key-value maps of values
 *
-* @return {[]}	table
+* @return {Array} has either zero (tabledefinition not found) or 2 elements: first ist somehow a title while the second element is a resolved value
 */
 LogHistoryExecutor.prototype._getDataForExtras = function(pId, pValues)
 {
@@ -257,12 +305,19 @@ LogHistoryExecutor.prototype._getCalendarDescription = function (pIndex)
 };
 
 /*
-* Creates an Array of AB_LOGHISTORY data for display in a view
-*
+* determines if a value has to be formatted and if so, performs that formatting strategy
+* this depends mostly on the configuration; if a:
+* - keyword is set, the view value of the keyword is retrieved
+* - translate4Log process is set, that process will be executed
+* - autoMapTrueFalse4Log is set, the raw value will be tried to translate to a "yes" or "no" text based on common cases
+* - the field is a TIMESTAMP field the raw long-value will be formatted
+* 
+* @param {Object} pDescription the structureConfig of one specific field which is logged
+* @param {String} pValue raw value that shall be formatted
 *
-* @return {[]}	table
+* @return {String} if necessary the formattedValue otherwise the original value
 */
-LogHistoryExecutor._getFormattedValue = function (pDescription, pValue, pLocale)
+LogHistoryExecutor.prototype._getFormattedValue = function (pDescription, pValue)
 {
     if (pDescription.keyword != null && pDescription.keyword != "")
     {
@@ -275,20 +330,21 @@ LogHistoryExecutor._getFormattedValue = function (pDescription, pValue, pLocale)
             }
             pValue = pValue.join(", ");
         }
-        else pValue = KeywordUtils.getViewValue(pDescription.keyword, pValue);
+        else pValue = KeywordUtils.getViewValue(pDescription.keyword, pValue, this.translationLanguage);
     }
     else if (pDescription.translate4Log != null && pDescription.translate4Log != "") 
     {
         var params = {
             rowId: this.idValue,
             value: pValue.toString(),
-            action: this.sqlAction
+            action: this.sqlAction,
+            locale: this.translationLanguage
         };
         
         pValue = process.executeScript("LogHistoryExecutor._getFormattedValue ", pDescription.translate4Log, params);
     }
     else if (pDescription.columnType == String(SQLTYPES.TIMESTAMP)) 
-        pValue = datetime.toDate(pValue, translate.text("dd.MM.yyyy", pLocale), "Europe/Berlin");//TODO: timezone?
+        pValue = datetime.toDate(pValue, translate.text("dd.MM.yyyy", this.translationLanguage), "Europe/Berlin");//TODO: timezone?
     else if (pDescription.autoMapTrueFalse4Log) 
     {
         switch (pValue.toLowerCase())
@@ -297,14 +353,14 @@ LogHistoryExecutor._getFormattedValue = function (pDescription, pValue, pLocale)
             case "t":
             case "y":
             case "1":
-                pValue = translate.text("Yes", pLocale);
+                pValue = translate.text("Yes", this.translationLanguage);
                 break;
             case "false":
             case "f":
             case "n":
             case "0":
             case "2":
-                pValue = translate.text("No", pLocale);
+                pValue = translate.text("No", this.translationLanguage);
                 break;
             default:
                 break;
diff --git a/process/process_audit/process.js b/process/process_audit/process.js
index 3b971f41bb..4b1e795542 100644
--- a/process/process_audit/process.js
+++ b/process/process_audit/process.js
@@ -1,6 +1,7 @@
 import("system.logging");
 import("Loghistory_lib");
 import("system.vars");
+import("system.process");
 
 var tableName = vars.get("$local.table");
 var id = vars.get("$local.idvalue");
@@ -13,6 +14,5 @@ var userLogin = vars.get("$local.user");
 
 if (sqlAction != 'X')
 {
-//    logHistory(tableName, userLogin, columns, newvalues, oldvalues, timestamp, sqlAction, id);
     (new LogHistoryExecutor(tableName, userLogin, columns, newvalues, oldvalues, timestamp, sqlAction, id)).execute();
 }
-- 
GitLab