Newer
Older
import("system.vars");
import("system.result");
import("system.db");
import("Attribute_lib");
import("Sql_lib");
var objectType = vars.exists("$param.ObjectType_param") && vars.get("$param.ObjectType_param");
var rowId = vars.exists("$param.ObjectRowId_param") && vars.get("$param.ObjectRowId_param");
var getTree = vars.exists("$param.GetTree_param") && vars.getString("$param.GetTree_param") == "true";
var displaySimpleName = vars.exists("$param.DisplaySimpleName_param") && vars.get("$param.DisplaySimpleName_param");
var attributeObj = {};
var sqlSelect = "select AB_ATTRIBUTEID, ATTRIBUTE_PARENT_ID, '', '', '', ATTRIBUTE_NAME from AB_ATTRIBUTE";
if (vars.exists("$local.idvalues") && vars.get("$local.idvalues"))
{
var idVals = vars.get("$local.idvalues");
var idCond = SqlCondition.begin();
idVals.forEach(function (id)
{
idCond.orPrepare("AB_ATTRIBUTERELATION.AB_ATTRIBUTERELATIONID", id);
});
attrCond.andSqlCondition(idCond);
}
else if (rowId)
{
attrCond.andPrepare("AB_ATTRIBUTERELATION.OBJECT_ROWID", rowId);
if (objectType != null)
attrCond.andPrepare("AB_ATTRIBUTERELATION.OBJECT_TYPE", objectType);
if (vars.exists("$param.FilteredAttributeIds_param") && vars.get("$param.FilteredAttributeIds_param"))
{
var filteredIds = JSON.parse(vars.get("$param.FilteredAttributeIds_param"));
var filteredIdsCondition = new SqlCondition();
filteredIds.forEach(function(id)
{
this.orPrepare("AB_ATTRIBUTERELATION.AB_ATTRIBUTE_ID", id);
}, filteredIdsCondition);
attrCond.andSqlCondition(filteredIdsCondition);
}
var defaultFields = [
"AB_ATTRIBUTERELATIONID",
"AB_ATTRIBUTE.AB_ATTRIBUTEID",
"AB_ATTRIBUTE.ATTRIBUTE_PARENT_ID",
"AB_ATTRIBUTE.KEYWORD_CONTAINER",
"COMBOVAL.ATTRIBUTE_NAME"
];
var valueFields = AttributeTypeUtil.getAllDatabaseFields();
var attributeSql = attrCond.buildSql("select " + defaultFields.join(", ") + ", " + valueFields.join(", ")
+ " from AB_ATTRIBUTERELATION join AB_ATTRIBUTE on AB_ATTRIBUTE_ID = AB_ATTRIBUTE.AB_ATTRIBUTEID"
+ " left join AB_ATTRIBUTE COMBOVAL on " + $AttributeTypes.COMBO.databaseField + " = COMBOVAL.AB_ATTRIBUTEID");
var attributeNameMap = {};
var attributeValues = db.table(attributeSql).map(function (row)
{
let attributeId = row[1];
if (!getTree && !displaySimpleName && row[2])
{
let parentName = AttributeUtil.getFullAttributeName(row[2]);
attributeName = (parentName ? parentName + " / " : "") + attributeName;
}
let value = row[AttributeTypeUtil.getTypeColumnIndex(row[3]) + defaultFields.length];
if (row[3].trim() == $AttributeTypes.COMBO)
viewValue = row[6];
viewValue = AttributeTypeUtil.getAttributeViewValue(row[3].trim(), value, row[5]);
return [row[0], row[2], value, viewValue, attributeId, attributeName];
if (getTree)
_fetchAttributes(attributeValues.map(function (row) {return row[1]}));
allAttributes = _sortArrayForTree(allAttributes).concat(attributeValues);
result.object(allAttributes);
function _fetchAttributes (pAttributeIds)
{
var condition = SqlCondition.begin();
var nextIds = [];
pAttributeIds.forEach(function (id)
{
if (!(id in this))
condition.orPrepare("AB_ATTRIBUTE.AB_ATTRIBUTEID", id);
}, attributeObj);
db.table(condition.buildSql(sqlSelect, "1=2"))
.forEach(function (row)
{
this[row[0]] = true;
if (row[1])
nextIds.push(row[1]);
else
row[1] = null;
allAttributes.push(row);
}, attributeObj);
if (nextIds.length)
_fetchAttributes(nextIds);
}
function _sortArrayForTree (pArray)
{
if (pArray.length <= 1)
return pArray;
pArray.forEach(function (row) {allIds[row[idIndex]] = true;});
var index = 0;
var oldIndex = index;
pArray.forEach(function (row)
{
if (!(row[idIndex] in this) && (row[parentIdIndex] in this || !allIds[row[parentIdIndex]]))
this[row[idIndex]] = {
data : row,
index : index++
};
}, rows);
for (let i in rows)
sortedArray[rows[i].index] = rows[i].data;
return sortedArray;