From e41a22f1bcd43429cc11a689f37229213ad6d013 Mon Sep 17 00:00:00 2001
From: Sebastian Listl <s.listl@adito.de>
Date: Tue, 6 Oct 2020 10:51:36 +0200
Subject: [PATCH] AttributeTypes in Attribute_lib improved

---
 process/Attribute_lib/process.js | 245 +++++++++++++++++--------------
 1 file changed, 134 insertions(+), 111 deletions(-)

diff --git a/process/Attribute_lib/process.js b/process/Attribute_lib/process.js
index 5e0df4bb1e..a8f5e70540 100644
--- a/process/Attribute_lib/process.js
+++ b/process/Attribute_lib/process.js
@@ -1,4 +1,4 @@
-import("system.logging");
+import("Util_lib");
 import("Employee_lib");
 import("KeywordData_lib");
 import("Context_lib");
@@ -72,7 +72,7 @@ AttributeUtil.getPossibleAttributes = function (pObjectType, pIncludeGroups, pFi
                         .from("AB_ATTRIBUTE")
                         .join("AB_ATTRIBUTEUSAGE", "AB_ATTRIBUTEID = AB_ATTRIBUTE_ID")
                         .where("AB_ATTRIBUTEUSAGE.OBJECT_TYPE", pObjectType)
-                        .and("AB_ATTRIBUTE.ATTRIBUTE_TYPE", $AttributeTypes.COMBOVALUE, SqlBuilder.NOT_EQUAL())
+                        .and("AB_ATTRIBUTE.ATTRIBUTE_TYPE", AttributeTypes.COMBOVALUE, SqlBuilder.NOT_EQUAL())
                         .and("ATTRIBUTE_ACTIVE = 1");
 
     if (pAttributeCount)
@@ -96,7 +96,7 @@ AttributeUtil.getPossibleAttributes = function (pObjectType, pIncludeGroups, pFi
     }
 
     if (!pIncludeGroups)
-        attrSelect.and("AB_ATTRIBUTE.ATTRIBUTE_TYPE", $AttributeTypes.GROUP, SqlBuilder.NOT_EQUAL());
+        attrSelect.and("AB_ATTRIBUTE.ATTRIBUTE_TYPE", AttributeTypes.GROUP, SqlBuilder.NOT_EQUAL());
     
     var attributes = attrSelect.table();
     
@@ -110,7 +110,7 @@ AttributeUtil.getPossibleAttributes = function (pObjectType, pIncludeGroups, pFi
         }, parentIds);
         attributes = attributes.filter(function (attribute)
         {
-            return attribute[2].trim() != $AttributeTypes.GROUP || this[attribute[0]];
+            return attribute[2].trim() != AttributeTypes.GROUP || this[attribute[0]];
         }, parentIds);
     }
     
@@ -149,12 +149,12 @@ AttributeUtil.getPossibleListValues = function (pAttributeId, pAttributeType, pI
     var attributeId = pAttributeId;
     var attrType = pAttributeType.trim();
     var onlyActives = !pIncludeInactives;
-    if (attrType == $AttributeTypes.COMBO.toString())
+    if (attrType == AttributeTypes.COMBO())
     {
         var valuesSelect = newSelect("AB_ATTRIBUTEID, ATTRIBUTE_NAME")
                                 .from("AB_ATTRIBUTE")
                                 .where("AB_ATTRIBUTE.ATTRIBUTE_PARENT_ID", attributeId)
-                                .and("AB_ATTRIBUTE.ATTRIBUTE_TYPE", $AttributeTypes.COMBOVALUE);
+                                .and("AB_ATTRIBUTE.ATTRIBUTE_TYPE", AttributeTypes.COMBOVALUE);
                                 
         if (onlyActives)
             valuesSelect.and("AB_ATTRIBUTE.ATTRIBUTE_ACTIVE", "1");
@@ -168,14 +168,14 @@ AttributeUtil.getPossibleListValues = function (pAttributeId, pAttributeType, pI
         }
         return valueList;
     }
-    else if (attrType == $AttributeTypes.BOOLEAN.toString())
+    else if (attrType == AttributeTypes.BOOLEAN())
     {
         return [
             ["1", translate.text("Yes")],
             ["0", translate.text("No")]
             ];
     }
-    else if (attrType == $AttributeTypes.KEYWORD.toString())
+    else if (attrType == AttributeTypes.KEYWORD())
     {
         var attrKeyword = newSelect("DROPDOWNDEFINITION")
                                 .from("AB_ATTRIBUTE")
@@ -185,7 +185,7 @@ AttributeUtil.getPossibleListValues = function (pAttributeId, pAttributeType, pI
         var keywords = KeywordData.getSimpleData(attrKeyword, null, onlyActives);
         return keywords;
     }
-    else if (attrType == $AttributeTypes.OBJECTSELECTION)
+    else if (attrType == AttributeTypes.OBJECTSELECTION())
     {
         var [module, filter] = newSelect("DROPDOWNDEFINITION, DROPDOWNFILTER")
                                     .from("AB_ATTRIBUTE")
@@ -393,8 +393,8 @@ AttributeUtil.hasAttributes = function (pObjectType)
                 .join("AB_ATTRIBUTE", "AB_ATTRIBUTEUSAGE.AB_ATTRIBUTE_ID = AB_ATTRIBUTE.AB_ATTRIBUTEID")
                 .where("AB_ATTRIBUTEUSAGE.OBJECT_TYPE", pObjectType)
                 .and("AB_ATTRIBUTE.ATTRIBUTE_ACTIVE", "1")
-                .and("AB_ATTRIBUTE.ATTRIBUTE_TYPE", $AttributeTypes.COMBOVALUE, SqlBuilder.NOT_EQUAL())
-                .and("AB_ATTRIBUTE.ATTRIBUTE_TYPE", $AttributeTypes.GROUP, SqlBuilder.NOT_EQUAL())
+                .and("AB_ATTRIBUTE.ATTRIBUTE_TYPE", AttributeTypes.COMBOVALUE(), SqlBuilder.NOT_EQUAL())
+                .and("AB_ATTRIBUTE.ATTRIBUTE_TYPE", AttributeTypes.GROUP(), SqlBuilder.NOT_EQUAL())
                 .cell() != "0"; //TODO: is there a way exists could be used?
 }
 
@@ -462,7 +462,7 @@ AttributeRelationUtils.getAttributeSqlBuilder = function (pFields, pObjectRowId,
     return newSelect(pFields)
                 .from("AB_ATTRIBUTERELATION")
                 .join("AB_ATTRIBUTE", "AB_ATTRIBUTE_ID = AB_ATTRIBUTE.AB_ATTRIBUTEID")
-                .leftJoin("AB_ATTRIBUTE COMBOVAL", $AttributeTypes.COMBO.databaseField + " = COMBOVAL.AB_ATTRIBUTEID")
+                .leftJoin("AB_ATTRIBUTE COMBOVAL", AttributeTypes.COMBO.databaseField + " = COMBOVAL.AB_ATTRIBUTEID")
                 .whereIfSet("AB_ATTRIBUTERELATION.OBJECT_ROWID", pObjectRowId)
                 .andIfSet("AB_ATTRIBUTERELATION.OBJECT_TYPE", pObjectType);
 }
@@ -542,7 +542,7 @@ AttributeRelationUtils.selectAttributeValue = function (pAttributeId, pValueMap,
     var value = pValueMap[field];
     if(value == undefined)
         return "";
-    if (pGetViewValue && type[0] == $AttributeTypes.COMBO)
+    if (pGetViewValue && type[0] == AttributeTypes.COMBO())
     {
         value = newSelect("ATTRIBUTE_NAME")
                     .from("AB_ATTRIBUTE")
@@ -597,8 +597,8 @@ AttributeRelationUtils.presetMandatoryAttributes = function (pObjectType, pConsu
                                     .from("AB_ATTRIBUTEUSAGE")
                                     .join("AB_ATTRIBUTE", "AB_ATTRIBUTE_ID = AB_ATTRIBUTEID")
                                     .where("AB_ATTRIBUTEUSAGE.OBJECT_TYPE", pObjectType)
-                                    .and("AB_ATTRIBUTE.ATTRIBUTE_TYPE", $AttributeTypes.COMBOVALUE, SqlBuilder.NOT_EQUAL())
-                                    .and("AB_ATTRIBUTE.ATTRIBUTE_TYPE", $AttributeTypes.GROUP, SqlBuilder.NOT_EQUAL())
+                                    .and("AB_ATTRIBUTE.ATTRIBUTE_TYPE", AttributeTypes.COMBOVALUE(), SqlBuilder.NOT_EQUAL())
+                                    .and("AB_ATTRIBUTE.ATTRIBUTE_TYPE", AttributeTypes.GROUP(), SqlBuilder.NOT_EQUAL())
                                     .and("ATTRIBUTE_ACTIVE = 1")
                                     .and("MIN_COUNT > 0");
                                     
@@ -814,36 +814,48 @@ AttributeRelationUtils.countAttributeRelations = function (pRowId, pObjectType,
  * 
  * optional:
  * getViewValue = function that gets the display value for a given value
- * isGroup = if true, the attribute can have children
  * getDropDownDefinitions = function that returns an array of possible values
  *          for DROPDOWNDEFINITION
  * singleSelection = if true, the maximal usage count is always 1
  * 
  * The display name is controlled by the keyword 'AttributeType'
  */
-function $AttributeTypes () {}
-
-$AttributeTypes.TEXT = { 
-    toString : function () {return "TEXT";},
-    contentType : "TEXT", 
-    databaseField : "CHAR_VALUE"
-};
-$AttributeTypes.DATE = {
-    toString : function () {return "DATE";},
-    contentType : "DATE", 
-    databaseField : "DATE_VALUE",
-    getViewValue : function (pValue)
-        {
-            return datetime.toDate(pValue, translate.text("dd.MM.yyyy"));
-        }
-};
-$AttributeTypes.NUMBER = {
-    toString : function () {return  "NUMBER";},
+function AttributeTypes () {}
+
+AttributeTypes.TEXT = function () {return "TEXT";}
+AttributeTypes.DATE = function () {return "DATE";}
+AttributeTypes.NUMBER = function () {return "NUMBER";}
+AttributeTypes.BOOLEAN = function () {return "BOOLEAN";}
+AttributeTypes.COMBO = function () {return "COMBO";}
+AttributeTypes.COMBOVALUE = function () {return "COMBOVALUE";}
+AttributeTypes.GROUP = function () {return "GROUP";} 
+AttributeTypes.KEYWORD = function () {return "KEYWORD";}
+AttributeTypes.VOID = function () {return "VOID";}
+AttributeTypes.MEMO = function () {return "MEMO";}
+AttributeTypes.OBJECTSELECTION = function () {return "OBJECTSELECTION";}
+AttributeTypes.THEME = function () {return "THEME";}
+
+Object.assign(AttributeTypes.TEXT, {
+    toString: function () {return this();},
+    contentType: "TEXT",
+    databaseField: "CHAR_VALUE"
+});
+Object.assign(AttributeTypes.DATE, {
+    toString: function () {return this();},
+    contentType: "DATE", 
+    databaseField: "DATE_VALUE",
+    getViewValue: function (pValue)
+    {
+        return datetime.toDate(pValue, translate.text("dd.MM.yyyy"));
+    }
+});
+Object.assign(AttributeTypes.NUMBER, {
+    toString: function () {return this();},
     contentType : "NUMBER", 
     databaseField : "NUMBER_VALUE"
-};
-$AttributeTypes.BOOLEAN = {
-    toString : function () {return "BOOLEAN";},
+});
+Object.assign(AttributeTypes.BOOLEAN, {
+    toString: function () {return this();},
     contentType : "BOOLEAN", 
     databaseField : "INT_VALUE",
     singleSelection : true,
@@ -851,38 +863,48 @@ $AttributeTypes.BOOLEAN = {
         {
             return pValue == "1" ? translate.text("Yes") : translate.text("No");
         }
-};
-$AttributeTypes.COMBO = {
-    toString : function () {return "COMBO";},
+});
+Object.assign(AttributeTypes.COMBO, {
+    toString: function () {return this();},
     contentType : "UNKNOWN",
     databaseField : "ID_VALUE",
-    isGroup : true,
-    possibleChildren : ["COMBOVALUE"],
+    possibleChildren : [AttributeTypes.COMBOVALUE()],
     //in most cases the view value of this attribute type is loaded via a direct sql join for less queries and better performance
     getViewValue : function (pValue)
         {
             var viewValue = newSelect("AB_ATTRIBUTE.ATTRIBUTE_NAME")
                 .from("AB_ATTRIBUTE")
                 .where("AB_ATTRIBUTE.AB_ATTRIBUTEID", pValue)
-                .and("AB_ATTRIBUTE.ATTRIBUTE_TYPE", $AttributeTypes.COMBOVALUE)
+                .and("AB_ATTRIBUTE.ATTRIBUTE_TYPE", AttributeTypes.COMBOVALUE())
                 .cell();
             return viewValue ? translate.text(viewValue) : viewValue;
         }
-};
-$AttributeTypes.COMBOVALUE = {
-    toString : function () {return "COMBOVALUE";},
+});
+Object.assign(AttributeTypes.COMBOVALUE, {
+    toString: function () {return this();},
     contentType : null, 
     databaseField : null
-};
-$AttributeTypes.GROUP = {
-    toString : function () {return "GROUP";},
+});
+Object.assign(AttributeTypes.GROUP, {
+    toString: function () {return this();},
     contentType : null, 
     databaseField : null,
-    isGroup : true,
-    possibleChildren : ["GROUP", "TEXT", "NUMBER", "COMBO", "VOID", "THEME", "KEYWORD", "OBJECTSELECTION", "MEMO", "DATE", "BOOLEAN"]
-};
-$AttributeTypes.KEYWORD = {
-    toString : function () {return "KEYWORD";},
+    possibleChildren : [
+        AttributeTypes.GROUP(), 
+        AttributeTypes.TEXT(), 
+        AttributeTypes.NUMBER(), 
+        AttributeTypes.COMBO(), 
+        AttributeTypes.VOID(), 
+        AttributeTypes.THEME(), 
+        AttributeTypes.KEYWORD(), 
+        AttributeTypes.OBJECTSELECTION(), 
+        AttributeTypes.MEMO(), 
+        AttributeTypes.DATE(), 
+        AttributeTypes.BOOLEAN()
+    ]
+});
+Object.assign(AttributeTypes.KEYWORD, {
+    toString: function () {return this();},
     contentType : "UNKNOWN", 
     databaseField : "ID_VALUE", 
     getViewValue : function (pValue, pKeyword)
@@ -896,22 +918,21 @@ $AttributeTypes.KEYWORD = {
                 return [e, e];//currently the first column is ID, second view value - which is the same because there is no ID for keyword-containers
             });
         }
-};
-$AttributeTypes.VOID = {
-    toString : function () {return "VOID";},
+});
+Object.assign(AttributeTypes.VOID, {
+    toString: function () {return this();},
     contentType : null,
     databaseField : null,
-    isGroup : true,
-    possibleChildren : ["VOID"],
+    possibleChildren : [AttributeTypes.VOID()],
     singleSelection : true
-};
-$AttributeTypes.MEMO = { 
-    toString : function () {return "MEMO";},
+});
+Object.assign(AttributeTypes.MEMO, {
+    toString: function () {return this();},
     contentType : "LONG_TEXT", 
     databaseField : "CHAR_VALUE"
-};
-$AttributeTypes.OBJECTSELECTION = {
-    toString : function () {return "OBJECTSELECTION";},
+});
+Object.assign(AttributeTypes.OBJECTSELECTION, {
+    toString: function () {return this();},
     contentType : "UNKNOWN",
     databaseField : "ID_VALUE",
     getViewValue : function (pValue, pModule)
@@ -938,45 +959,47 @@ $AttributeTypes.OBJECTSELECTION = {
             project.getDataModels(project.DATAMODEL_KIND_ENTITY).forEach(
                 function (entity)
                 {
-                    if (entity[1] && $AttributeTypes.OBJECTSELECTION._selectableEntities[entity[0]])
+                    if (entity[1] && AttributeTypes.OBJECTSELECTION._selectableEntities.has(entity[0]))
                         dropDownList.push([entity[0], translate.text(entity[1])]);
                 }
             );
             return dropDownList;
         },
     /** @private */
-    _selectableEntities : {
-        "ObjectRelationType_entity" : true,
-        "DocumentTemplate_entity" : true,
-        "SupportTicket_entity" : true,
-        "Organisation_entity" : true,
-        "Salesproject_entity" : true,
-        "Productprice_entity" : true,
-        "SerialLetter_entity" : true,
-        "AnyContact_entity" : true,
-        "Salutation_entity" : true,
-        "Attribute_entity" : true,
-        "Activity_entity" : true,
-        "Contract_entity" : true,
-        "Campaign_entity" : true,
-        "BulkMail_entity" : true,
-        "Employee_entity" : true,
-        "Language_entity" : true,
-        "Product_entity" : true,
-        "Person_entity" : true,
-        "Offer_entity" : true,
-        "Order_entity" : true,
-        "Task_entity" : true,
-        "Role_entity" : true
-    }
-};
-$AttributeTypes.THEME = {
-    toString : function () {return "THEME";},
+    _selectableEntities : new Set([
+        "ObjectRelationType_entity",
+        "DocumentTemplate_entity",
+        "SupportTicket_entity",
+        "Organisation_entity",
+        "Salesproject_entity",
+        "Productprice_entity",
+        "SerialLetter_entity",
+        "AnyContact_entity",
+        "Salutation_entity",
+        "Attribute_entity",
+        "Activity_entity",
+        "Contract_entity",
+        "Campaign_entity",
+        "BulkMail_entity",
+        "Employee_entity",
+        "Language_entity",
+        "Product_entity",
+        "Person_entity",
+        "Offer_entity",
+        "Order_entity",
+        "Task_entity",
+        "Role_entity"
+    ])
+});
+Object.assign(AttributeTypes.THEME, {
+    toString: function () {return this();},
     contentType : "LONG_TEXT",
     databaseField : "CHAR_VALUE",
-    isGroup : true,
-    possibleChildren : ["THEME"]
-};
+    possibleChildren : [AttributeTypes.THEME()]
+});
+
+//reference for compatibility with old name
+var $AttributeTypes = AttributeTypes;
 
 function AttributeTypeUtil () {}
 
@@ -1005,7 +1028,7 @@ AttributeTypeUtil.getContentType = function (pAttributeType)
  */
 AttributeTypeUtil.isGroupType = function (pAttributeType)
 {
-    return AttributeTypeUtil._getProperty(pAttributeType, "isGroup", false);
+    return !Utils.isNullOrEmpty(AttributeTypeUtil.getPossibleChildren(pAttributeType));
 }
 
 /**
@@ -1069,7 +1092,7 @@ AttributeTypeUtil.isSingleSelection = function (pAttributeType)
  */
 AttributeTypeUtil.useLookup = function (pAttributeType)
 {
-    return pAttributeType.trim() == $AttributeTypes.OBJECTSELECTION.toString();
+    return pAttributeType.trim() == AttributeTypes.OBJECTSELECTION();
 }
 
 /**
@@ -1085,7 +1108,7 @@ AttributeTypeUtil.useLookup = function (pAttributeType)
 AttributeTypeUtil.getGroupTypes = function (pChildType)
 {
     var groupTypes = [];
-    for (let type in $AttributeTypes)
+    for (let type in AttributeTypes)
     {
         if (AttributeTypeUtil.isGroupType(type) && (!pChildType || (!AttributeTypeUtil.getPossibleChildren(type) || AttributeTypeUtil.getPossibleChildren(type).indexOf(pChildType) !== -1)) )
             groupTypes.push(type);
@@ -1106,7 +1129,7 @@ AttributeTypeUtil.getGroupTypes = function (pChildType)
  * @return                                  <p>
  *                                          Returns the property or null, when pAttributeType<br>
  *                                          isn't filled or the given attribute type isn't in<br>
- *                                          $AttributeTypes. Otherwise the pDefaultValue,<br>
+ *                                          AttributeTypes. Otherwise the pDefaultValue,<br>
  *                                          will be returned, case if it isn't undefined or null.<br>
  */
 AttributeTypeUtil._getProperty = function (pAttributeType, pPropertyName, pDefaultValue)
@@ -1115,9 +1138,9 @@ AttributeTypeUtil._getProperty = function (pAttributeType, pPropertyName, pDefau
         return null;
     
     pAttributeType = pAttributeType.trim();
-    if (pAttributeType in $AttributeTypes)
-        if (pPropertyName in $AttributeTypes[pAttributeType])
-            return $AttributeTypes[pAttributeType][pPropertyName];
+    if (pAttributeType in AttributeTypes)
+        if (pPropertyName in AttributeTypes[pAttributeType])
+            return AttributeTypes[pAttributeType][pPropertyName];
         else
             return pDefaultValue === undefined ? null : pDefaultValue;
         
@@ -1141,8 +1164,8 @@ AttributeTypeUtil._getProperty = function (pAttributeType, pPropertyName, pDefau
  */
 AttributeTypeUtil.getAttributeViewValue = function (pAttributeType, pValue, pKeyword)
 {
-    if (pAttributeType in $AttributeTypes && $AttributeTypes[pAttributeType].getViewValue)
-        return $AttributeTypes[pAttributeType].getViewValue(pValue, pKeyword);
+    if (pAttributeType in AttributeTypes && AttributeTypes[pAttributeType].getViewValue)
+        return AttributeTypes[pAttributeType].getViewValue(pValue, pKeyword);
     return pValue;
 }
 
@@ -1153,9 +1176,9 @@ AttributeTypeUtil._initTypeColumnData = function ()
 {
     var columns = [];
     var typeColumnMap = {};
-    for (let type in $AttributeTypes)
+    for (let type in AttributeTypes)
     {
-        type = $AttributeTypes[type];
+        type = AttributeTypes[type];
         if (type.databaseField)
         {
             var typeKey = type.toString();
@@ -1237,7 +1260,7 @@ AttributeUsageUtil.insertChildrenUsages = function (pAttributeId, pObjectType)
                                     .where("AB_ATTRIBUTEUSAGE.AB_ATTRIBUTE_ID = AB_ATTRIBUTE.AB_ATTRIBUTEID")
                                     .and("AB_ATTRIBUTEUSAGE.OBJECT_TYPE", pObjectType)])
                             .from("AB_ATTRIBUTE")
-                            .where("AB_ATTRIBUTE.ATTRIBUTE_TYPE", $AttributeTypes.COMBOVALUE, SqlBuilder.NOT_EQUAL())
+                            .where("AB_ATTRIBUTE.ATTRIBUTE_TYPE", AttributeTypes.COMBOVALUE, SqlBuilder.NOT_EQUAL())
                             .and("AB_ATTRIBUTE.ATTRIBUTE_PARENT_ID", pAttributeId)
                             .table();
 
@@ -1300,7 +1323,7 @@ AttributeUsageUtil.updateChildrenUsages = function (pAttributeId, pOldObjectType
     function _addUpdateIds (pAttributeId)
     {
         sqlSelect.clearWhere()
-                 .where("AB_ATTRIBUTE.ATTRIBUTE_TYPE", $AttributeTypes.COMBOVALUE, SqlBuilder.NOT_EQUAL())
+                 .where("AB_ATTRIBUTE.ATTRIBUTE_TYPE", AttributeTypes.COMBOVALUE, SqlBuilder.NOT_EQUAL())
                  .and("AB_ATTRIBUTE.ATTRIBUTE_PARENT_ID", pAttributeId);
 
         var attributes = sqlSelect.table();
@@ -1340,7 +1363,7 @@ AttributeUsageUtil.deleteChildrenUsages = function (pAttributeId, pObjectType)
     function _addDeleteIds (pAttributeId)
     {
         attributeSelect.clearWhere()
-                       .where("AB_ATTRIBUTE.ATTRIBUTE_TYPE", $AttributeTypes.COMBOVALUE, SqlBuilder.NOT_EQUAL())
+                       .where("AB_ATTRIBUTE.ATTRIBUTE_TYPE", AttributeTypes.COMBOVALUE, SqlBuilder.NOT_EQUAL())
                        .and("AB_ATTRIBUTE.ATTRIBUTE_PARENT_ID", pAttributeId);
         var attributes = attributeSelect.table();
         
@@ -1571,7 +1594,7 @@ AttributeRelationQuery.prototype.getAttributes = function ()
         
         if (this._includeDisplayValue)
         {
-            if (row[0].trim() == $AttributeTypes.COMBO)
+            if (row[0].trim() == AttributeTypes.COMBO)
                 attrObj.displayValue = translate.text(row[3]);
             else
                 attrObj.displayValue = AttributeTypeUtil.getAttributeViewValue(row[0].trim(), attrObj.value, row[1]);
-- 
GitLab