Skip to content
Snippets Groups Projects
contentProcess.js 4.65 KiB
Newer Older
import("system.vars");
import("system.result");
import("system.db");
import("Attribute_lib");
import("Sql_lib");

S.Listl's avatar
S.Listl committed
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 allAttributes = [];
S.Listl's avatar
S.Listl committed
var attributeObj = {};
var sqlSelect = "select AB_ATTRIBUTEID, ATTRIBUTE_PARENT_ID, '', '', '', ATTRIBUTE_NAME from AB_ATTRIBUTE";
S.Listl's avatar
S.Listl committed
var attrCond = SqlCondition.begin();
S.Listl's avatar
S.Listl committed
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);
S.Listl's avatar
S.Listl committed
}
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);
    }
S.Listl's avatar
S.Listl committed
}
var defaultFields = [
    "AB_ATTRIBUTERELATIONID",
S.Listl's avatar
S.Listl committed
    "AB_ATTRIBUTE.AB_ATTRIBUTEID", 
    "AB_ATTRIBUTE.ATTRIBUTE_PARENT_ID", 
    "AB_ATTRIBUTE.ATTRIBUTE_TYPE", 
S.Listl's avatar
S.Listl committed
    "AB_ATTRIBUTE.ATTRIBUTE_NAME", 
    "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];
S.Listl's avatar
S.Listl committed
    let attributeName = row[4];
    if (!getTree && !displaySimpleName && row[2])
    {
        let parentName = AttributeUtil.getFullAttributeName(row[2]);
        attributeName = (parentName ? parentName + " / " : "") + attributeName;
    }
S.Listl's avatar
S.Listl committed
    let value = row[AttributeTypeUtil.getTypeColumnIndex(row[3]) + defaultFields.length];
    let viewValue;
S.Listl's avatar
S.Listl committed
    if (row[3].trim() == $AttributeTypes.COMBO)
        viewValue = row[6];
S.Listl's avatar
S.Listl committed
        viewValue = AttributeTypeUtil.getAttributeViewValue(row[3].trim(), value, row[5]);
S.Listl's avatar
S.Listl committed
    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;
    var rows = {};
    var allIds = {};
S.Listl's avatar
S.Listl committed
    var idIndex = 0;
    var parentIdIndex = 1;
    
    pArray.forEach(function (row) {allIds[row[idIndex]] = true;});
    var index = 0;
    
S.Listl's avatar
S.Listl committed
    do {
        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);
S.Listl's avatar
S.Listl committed
    } while (oldIndex != index);
S.Listl's avatar
S.Listl committed
    var sortedArray = new Array(index);
    for (let i in rows)
        sortedArray[rows[i].index] = rows[i].data;
    return sortedArray;
S.Listl's avatar
S.Listl committed
}