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

S.Listl's avatar
S.Listl committed
var sqlSelect = "select A.AB_ATTRIBUTEID, A.ATTRIBUTE_ACTIVE, A.ATTRIBUTE_NAME, A.ATTRIBUTE_PARENT_ID, A.ATTRIBUTE_TYPE, " 
    + KeywordUtils.getResolvedTitleSqlPart($KeywordRegistry.attributeType(), "A.ATTRIBUTE_TYPE")
    + ", A.KEYWORD_CONTAINER, A.SORTING, P.ATTRIBUTE_PARENT_ID, P.ATTRIBUTE_NAME " 
    + "from AB_ATTRIBUTE A "
    + "left join AB_ATTRIBUTE P on A.ATTRIBUTE_PARENT_ID = P.AB_ATTRIBUTEID";
S.Listl's avatar
S.Listl committed

S.Listl's avatar
S.Listl committed
var sqlOrder = " order by A.ATTRIBUTE_PARENT_ID asc, A.SORTING asc";
S.Listl's avatar
S.Listl committed

S.Listl's avatar
S.Listl committed
var condition = SqlCondition.begin();

var getGroups = vars.exists("$param.GetGroups_param") && vars.get("$param.GetGroups_param");
var objectType = vars.exists("$param.ObjectType_param") && vars.get("$param.ObjectType_param");
S.Listl's avatar
S.Listl committed

if (vars.exists("$local.idvalues") && vars.get("$local.idvalues"))
S.Listl's avatar
S.Listl committed
    condition.and("A.AB_ATTRIBUTEID in ('" + vars.get("$local.idvalues").join("','") + "')");
S.Listl's avatar
S.Listl committed
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
S.Listl's avatar
S.Listl committed
    var isGroupCondition = SqlCondition.begin();
S.Listl's avatar
S.Listl committed
    for (let type in $AttributeTypes)
        if ($AttributeTypes[type].isGroup && $AttributeTypes[type] != $AttributeTypes.COMBO)
S.Listl's avatar
S.Listl committed
            isGroupCondition.orPrepare(["AB_ATTRIBUTE", "ATTRIBUTE_TYPE", "A"], $AttributeTypes[type]);
    
    condition.andSqlCondition(SqlCondition.begin()
S.Listl's avatar
S.Listl committed
        .andSqlCondition(isGroupCondition)
S.Listl's avatar
S.Listl committed
        .andPrepareVars(["AB_ATTRIBUTE", "AB_ATTRIBUTEID", "A"], "$param.AttrParentId_param", "# != ?")
        .and("A.AB_ATTRIBUTEID not in ('" + AttributeUtil.getAllChildren(vars.getString("$param.AttrParentId_param")).join("','") + "')")
S.Listl's avatar
S.Listl committed
    );
}
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"));
S.Listl's avatar
S.Listl committed
    var attributeCount;
    if (vars.exists("$param.AttributeCount_param") && vars.get("$param.AttributeCount_param"))
        attributeCount = JSON.parse(vars.get("$param.AttributeCount_param"));
    var ids = AttributeUtil.getPossibleAttributes(objectType, false, filteredAttributes, attributeCount);
S.Listl's avatar
S.Listl committed
    condition.and("A.AB_ATTRIBUTEID in ('" + ids.join("','") + "')");
S.Listl's avatar
S.Listl committed
    var parentType = vars.exists("$param.AttrParentType_param") && vars.get("$param.AttrParentType_param");   
    if (AttributeTypeUtil.isGroupType(parentType))
S.Listl's avatar
S.Listl committed
        var parentId = vars.exists("$param.AttrParentId_param") && vars.get("$param.AttrParentId_param");
        if (parentId)
S.Listl's avatar
S.Listl committed
            condition.and("A.AB_ATTRIBUTEID in ('" + AttributeUtil.getAllChildren(vars.getString("$param.AttrParentId_param")).join("','") + "')");
S.Listl's avatar
S.Listl committed
    else if (parentType)
S.Listl's avatar
S.Listl committed
        condition.and("1=2");
S.Listl's avatar
S.Listl committed
//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");
S.Listl's avatar
S.Listl committed
    condition.andSqlCondition(JditoFilterUtils.getSqlCondition(filter, "AB_ATTRIBUTE", "A"));
S.Listl's avatar
S.Listl committed
var attributes = db.table(condition.buildSql(sqlSelect, "1=1", sqlOrder));
S.Listl's avatar
S.Listl committed
var usageIdType = SqlUtils.getSingleColumnType("AB_ATTRIBUTEUSAGE.AB_ATTRIBUTE_ID");
var attributeIdType = SqlUtils.getSingleColumnType("AB_ATTRIBUTE.AB_ATTRIBUTEID");

attributes = _buildAttributeTable(attributes);

result.object(attributes);

S.Listl's avatar
S.Listl committed

//sorts the records in a way that a tree can be built and adds values
function _buildAttributeTable (pAttributes) 
{
    var rows = {};
    var allIds = {};
    
S.Listl's avatar
S.Listl committed
    //fills the allIds object, the object is used for checking if a parent exists in the array
    pAttributes.forEach(function (row) {allIds[row[0]] = true;});
S.Listl's avatar
S.Listl committed
    var arrayIndex = 0;
S.Listl's avatar
S.Listl committed
    //stops the loop when no new items were added so that recursive relations between attributes don't cause an infinite loop
    for (let itemsAdded = true; itemsAdded; itemsAdded = oldIndex != arrayIndex)
S.Listl's avatar
S.Listl committed
        var oldIndex = arrayIndex;
        pAttributes.forEach(function (row)
        {   
S.Listl's avatar
S.Listl committed
            //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)
S.Listl's avatar
S.Listl committed
            if (!(row[0] in this) && (row[3] in this || !allIds[row[3]]))
                this[row[0]] = {
                    data : row,
S.Listl's avatar
S.Listl committed
                    index : arrayIndex++
                };
        }, rows);
    }
    var sortedArray = new Array(Object.keys(rows).length);
    for (let i in rows)
S.Listl's avatar
S.Listl committed
    {
        let rowData = rows[i].data;
        if (!(vars.exists("$param.DisplaySimpleName_param") && vars.get("$param.DisplaySimpleName_param")))
            rowData[9] = _getFullName(rowData[2], rowData[9], rowData[8]);
        else
            rowData[9] = rowData[2];
        if (rowData[4].trim() != $AttributeTypes.COMBOVALUE)
        {
            let usages = db.array(db.COLUMN, SqlCondition.begin()
                .andPrepare("AB_ATTRIBUTEUSAGE.AB_ATTRIBUTE_ID", rowData[0], null, usageIdType)
                .buildSql("select OBJECT_TYPE from AB_ATTRIBUTEUSAGE")
            );
            if (usages.length)
                rowData[8] = usages.join(", ");
        }
        else
            rowData[8] = "";
        sortedArray[rows[i].index] = rowData;
    }
    
    return sortedArray;
S.Listl's avatar
S.Listl committed
}

function _getFullName (pAttributeName, pParentName, pGrandParentId)
{
    if (pParentName)
        pAttributeName = pParentName + " / " + pAttributeName;
    if (pGrandParentId)
    {
        var grandParentName = AttributeUtil.getFullAttributeName(pGrandParentId, null, attributeIdType);
        if (grandParentName)
            pAttributeName = grandParentName + " / " + pAttributeName;
    }
    return pAttributeName;