Skip to content
Snippets Groups Projects
Commit a198e223 authored by S.Listl's avatar S.Listl
Browse files

AttributeRelationQuery insert function added

parent fc92a6c4
No related branches found
No related tags found
No related merge requests found
......@@ -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
......@@ -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
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment