Skip to content
Snippets Groups Projects
contentProcess.js 4.16 KiB
import("system.datetime");
import("JditoFilter_lib");
import("KeywordRegistry_basic");
import("Keyword_lib");
import("system.db");
import("system.vars");
import("system.result");
import("Sql_lib");
import("Attribute_lib");

var sqlSelect = "select AB_ATTRIBUTEID, ATTRIBUTE_ACTIVE, " 
    + "ATTRIBUTE_NAME, ATTRIBUTE_PARENT_ID, ATTRIBUTE_TYPE, " 
    + KeywordUtils.getResolvedTitleSqlPart($KeywordRegistry.attributeType(), "ATTRIBUTE_TYPE")
    + ", KEYWORD_CONTAINER from AB_ATTRIBUTE";

var condition = new SqlCondition();

var getGroups = vars.exists("$param.GetGroups_param") && vars.get("$param.GetGroups_param");
var objectType = vars.exists("$param.ObjectType_param") && vars.get("$param.ObjectType_param");

if (vars.exists("$local.idvalues") && vars.get("$local.idvalues"))
    condition.and("AB_ATTRIBUTEID in ('" + vars.get("$local.idvalues").join("','") + "')");
else if (getGroups)
{
    //this is for the selection of the superordinate attribute, this condition
    //filters out the own id and the children to prevent loops
    
    var isGroupCondition = new SqlCondition();
    for (let type in $AttributeTypes)
        if ($AttributeTypes[type].isGroup && $AttributeTypes[type] != $AttributeTypes.COMBO)
            isGroupCondition.orPrepare("AB_ATTRIBUTE.ATTRIBUTE_TYPE", $AttributeTypes[type]);
    condition.andSqlCondition(SqlCondition.begin()
        .andSqlCondition(isGroupCondition)
        .andPrepareVars("AB_ATTRIBUTE.AB_ATTRIBUTEID", "$param.AttrParentId_param", "# != ?")
        .and("AB_ATTRIBUTE.AB_ATTRIBUTEID not in ('" + AttributeUtil.getAllChildren(vars.getString("$param.AttrParentId_param")).join("','") + "')")
    );
}
else if (objectType)  //if there's an objectType, it comes from the AttributeRelation entity
{
    var filteredAttributes = [];
    if (vars.exists("$param.FilteredAttributeIds_param") && vars.get("$param.FilteredAttributeIds_param"))
        filteredAttributes = JSON.parse(vars.get("$param.FilteredAttributeIds_param"));
    
    var ids = AttributeUtil.getPossibleAttributes(objectType, false, filteredAttributes);
    condition.and("AB_ATTRIBUTE.AB_ATTRIBUTEID in ('" + ids.join("','") + "')");
} 
else 
{
    var type = vars.exists("$param.AttrParentType_param") && vars.get("$param.AttrParentType_param");
    if (type == $AttributeTypes.COMBO)
        condition.andPrepareVars("AB_ATTRIBUTE.ATTRIBUTE_PARENT_ID", "$param.AttrParentId_param");        
    else if (type == $AttributeTypes.GROUP)
    {
        var parentId = vars.exists("$param.AttrParentId_param") && vars.get("$param.AttrParentId_param");
        if (parentId)
            condition.and("AB_ATTRIBUTE.AB_ATTRIBUTEID in ('" + AttributeUtil.getAllChildren(vars.getString("$param.AttrParentId_param")).join("','") + "')");
    }
    else if (type)
        condition.and("1=2");
}

//when there are filters selected, add them to the conditon
if (vars.exists("$local.filter") && vars.get("$local.filter"))
{
    var filter = vars.get("$local.filter");
    condition.andSqlCondition((JditoFilterUtils.getSqlCondition(filter, "AB_ATTRIBUTE")));
}

var attributes = db.table(condition.buildSql(sqlSelect, "1=1"));
if (attributes.length > 1)
    attributes = _sortArrayForTree(attributes);

result.object(attributes);

//sorts the records in a way that a tree can be built
function _sortArrayForTree (pArray) 
{
    var rows = {};
    var allIds = {};
    var idIndex = 0;
    var parentIdIndex = 3;
    
    pArray.forEach(function (row) {allIds[row[idIndex]] = true;});
    
    var index = 0;
    
    for (let itemsAdded = true; itemsAdded; itemsAdded = oldIndex != index)
    {
        var oldIndex = index;
        pArray.forEach(function (row)
        {
            //item will be added if the id is not already in the object and
            //the parent is already added (or the parent is not in the array)
            if (!(row[idIndex] in this) && (row[parentIdIndex] in this || !allIds[row[parentIdIndex]]))
                this[row[idIndex]] = {
                    data : row,
                    index : index++
                };
        }, rows);
    }
    var sortedArray = new Array(Object.keys(rows).length);
    for (let i in rows)
        sortedArray[rows[i].index] = rows[i].data;
    
    return sortedArray;
}