From a198e2233a4d493032efcc4115bcf9d428a6d15e Mon Sep 17 00:00:00 2001
From: "S.Listl" <S.Listl@SLISTL.aditosoftware.local>
Date: Tue, 7 Jan 2020 09:26:30 +0100
Subject: [PATCH] AttributeRelationQuery insert function added

---
 .../recordcontainers/db/onDBInsert.js         |   4 +-
 process/Attribute_lib/process.js              | 112 +++++++++++-------
 .../SetAttribute_workflowService/process.js   |   2 +-
 3 files changed, 70 insertions(+), 48 deletions(-)

diff --git a/entity/Salesproject_entity/recordcontainers/db/onDBInsert.js b/entity/Salesproject_entity/recordcontainers/db/onDBInsert.js
index a1f0b77b21..3e831c35c9 100644
--- a/entity/Salesproject_entity/recordcontainers/db/onDBInsert.js
+++ b/entity/Salesproject_entity/recordcontainers/db/onDBInsert.js
@@ -11,6 +11,6 @@ Salesproject.insertMilestone(vars.getString("$local.uid"), "SalesprojectState",
 
 if (vars.get("$field.PROJECTTYPE"))
 {
-    AttributeRelationUtils.insertAttribute(rowdata["SALESPROJECT.SALESPROJECTID"], ContextUtils.getCurrentContextId(), 
-        $AttributeRegistry.salesprojectType(), vars.get("$field.PROJECTTYPE"))
+    new AttributeRelationQuery(rowdata["SALESPROJECT.SALESPROJECTID"], $AttributeRegistry.salesprojectType(), ContextUtils.getCurrentContextId())
+        .insertAttribute(vars.get("$field.PROJECTTYPE"), true);
 }
\ No newline at end of file
diff --git a/process/Attribute_lib/process.js b/process/Attribute_lib/process.js
index 94ddda7c76..0e9e19553c 100644
--- a/process/Attribute_lib/process.js
+++ b/process/Attribute_lib/process.js
@@ -476,6 +476,8 @@ AttributeRelationUtils.selectAttributeValue = function (pAttributeId, pValueMap,
 }
 
 /**
+ * @deprecated use AttributeRelationQuery.prototype.insertAttribute
+ * 
  * Inserts an attribute relation and validates if it can be used for the context and 
  * it also heeds the max usage count.
  * 
@@ -483,58 +485,19 @@ AttributeRelationUtils.selectAttributeValue = function (pAttributeId, pValueMap,
  */
 AttributeRelationUtils.setAttribute = function (pRowId, pObjectType, pAttributeId, pValue)
 {
-    var maxCount = newSelect("MAX_COUNT")
-        .from("AB_ATTRIBUTEUSAGE")
-        .where("AB_ATTRIBUTEUSAGE.AB_ATTRIBUTE_ID", pAttributeId)
-        .and("AB_ATTRIBUTEUSAGE.OBJECT_TYPE", pObjectType)
-        .arrayColumn();
-        
-    if (maxCount.length == 0)
-        return false;
-    
-    maxCount = maxCount[0];
-    if (maxCount && maxCount != 0)
-    {
-        let timesUsed = new AttributeRelationQuery(pRowId, pObjectType, pAttributeId).getAttributeCount();
-        if (timesUsed >= maxCount)
-            return false;
-    }
-    
-    AttributeRelationUtils.insertAttribute(pRowId, pObjectType, pAttributeId, pValue);
-    return true;
+    return new AttributeRelationQuery(pRowId, pAttributeId, pObjectType)
+        .insertAttribute(pValue, false);
 }
 
 /**
+ * @deprecated use AttributeRelationQuery.prototype.insertAttribute
+ * 
  * inserts an attribute without validating the count
  */
 AttributeRelationUtils.insertAttribute = function (pRowId, pObjectType, pAttributeId, pValue)
 {
-    //TODO: use write-entity when available
-    
-    var columns = [
-        "AB_ATTRIBUTERELATIONID",
-        "AB_ATTRIBUTE_ID",
-        "OBJECT_ROWID",
-        "OBJECT_TYPE",
-        "DATE_NEW",
-        "USER_NEW"
-    ];
-    var values = [
-        util.getNewUUID(),
-        pAttributeId,
-        pRowId,
-        pObjectType,
-        vars.get("$sys.date"),
-        vars.get("$sys.user")
-    ];
-    var type = AttributeUtil.getAttributeType(pAttributeId);
-    var valueField = AttributeTypeUtil.getDatabaseField(type);
-    if (valueField)
-    {
-        columns.push(valueField);
-        values.push(pValue);
-    }
-    db.insertData("AB_ATTRIBUTERELATION", columns, null, values);
+    return new AttributeRelationQuery(pRowId, pAttributeId, pObjectType)
+        .insertAttribute(pValue, true);
 }
 
 /**
@@ -1408,3 +1371,62 @@ AttributeRelationQuery.prototype.getAttributeCount = function ()
         .andIfSet("AB_ATTRIBUTERELATION.AB_ATTRIBUTE_ID", this._attributeId)
         .cell() || 0);
 }
+
+/**
+ * 
+ */
+AttributeRelationQuery.prototype.insertAttribute = function (pValue, pOmitValidation)
+{
+    if (this._attributeIds.length !== 1)
+        throw new Error("AttributeRelationQuery: You have to specify a single attribute id for insert");
+    if (!this._objectType || !this._rowId)
+        throw new Error("AttributeRelationQuery: Object type and row id are required for insert");
+    
+    var attributeId = this._attributeIds[0];
+    
+    if (!pOmitValidation)
+    {
+        var maxCount = newSelect("MAX_COUNT")
+            .from("AB_ATTRIBUTEUSAGE")
+            .where("AB_ATTRIBUTEUSAGE.AB_ATTRIBUTE_ID", attributeId)
+            .and("AB_ATTRIBUTEUSAGE.OBJECT_TYPE", this._objectType)
+            .arrayColumn();
+
+        if (maxCount.length == 0)
+            return false;
+
+        maxCount = maxCount[0];
+        if (maxCount && maxCount != 0)
+        {
+            let timesUsed = new AttributeRelationQuery(this._rowId, this._objectType, attributeId).getAttributeCount();
+            if (timesUsed >= maxCount)
+                return false;
+        }
+    }
+
+    var columns = [
+        "AB_ATTRIBUTERELATIONID",
+        "AB_ATTRIBUTE_ID",
+        "OBJECT_ROWID",
+        "OBJECT_TYPE",
+        "DATE_NEW",
+        "USER_NEW"
+    ];
+    var values = [
+        util.getNewUUID(),
+        attributeId,
+        this._rowId,
+        this._objectType,
+        vars.get("$sys.date"),
+        vars.get("$sys.user")
+    ];
+    var type = AttributeUtil.getAttributeType(attributeId);
+    var valueField = AttributeTypeUtil.getDatabaseField(type);
+    if (valueField)
+    {
+        columns.push(valueField);
+        values.push(pValue);
+    }
+    db.insertData("AB_ATTRIBUTERELATION", columns, null, values);
+    return true;
+}
\ No newline at end of file
diff --git a/process/SetAttribute_workflowService/process.js b/process/SetAttribute_workflowService/process.js
index d9db7dd2d6..1bc687a73c 100644
--- a/process/SetAttribute_workflowService/process.js
+++ b/process/SetAttribute_workflowService/process.js
@@ -9,4 +9,4 @@ var value = variables.attributeValue;
 var objectRowId = variables.targetId;
 var objectType = variables.targetContext;
 
-result.string(AttributeRelationUtils.setAttribute(objectRowId, objectType, attributeId, value));
\ No newline at end of file
+result.string(new AttributeRelationQuery(objectRowId, attributeId, objectType).insertAttribute(value));
\ No newline at end of file
-- 
GitLab