Skip to content
Snippets Groups Projects
contentProcess.js 8.01 KiB
Newer Older
S.Listl's avatar
S.Listl committed
import("system.translate");
import("Util_lib");
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 getGroups = vars.exists("$param.GetGroups_param") && vars.get("$param.GetGroups_param");
var objectType = vars.exists("$param.ObjectType_param") && vars.get("$param.ObjectType_param");
var parentType = vars.exists("$param.AttrParentType_param") && vars.get("$param.AttrParentType_param");
var fetchUsages = true;
S.Listl's avatar
S.Listl committed
var translateName = false;
S.Listl's avatar
S.Listl committed

var uidTableAlias = "UIDROW";
S.Listl's avatar
S.Listl committed
var sqlSelect = "select UIDROW.AB_ATTRIBUTEID, UIDROW.ATTRIBUTE_PARENT_ID, UIDROW.ATTRIBUTE_ACTIVE, UIDROW.DROPDOWNDEFINITION, UIDROW.SORTING, UIDROW.ATTRIBUTE_TYPE, " 
    + KeywordUtils.getResolvedTitleSqlPart($KeywordRegistry.attributeType(), "UIDROW.ATTRIBUTE_TYPE") //3
    + ", '', UIDROW.ATTRIBUTE_NAME, PARENT1.ATTRIBUTE_NAME, PARENT2.ATTRIBUTE_NAME, PARENT3.ATTRIBUTE_NAME, PARENT3.ATTRIBUTE_PARENT_ID \n\
    from AB_ATTRIBUTE UIDROW \n\
    left join AB_ATTRIBUTE PARENT1 on UIDROW.ATTRIBUTE_PARENT_ID = PARENT1.AB_ATTRIBUTEID \n\
    left join AB_ATTRIBUTE PARENT2 ON PARENT1.ATTRIBUTE_PARENT_ID = PARENT2.AB_ATTRIBUTEID \n\
    left join AB_ATTRIBUTE PARENT3 ON PARENT2.ATTRIBUTE_PARENT_ID = PARENT3.AB_ATTRIBUTEID"; 

/* always select the names of the next 3 parents so that less queries
   are required later when buildung the full name */

var sqlOrder = " order by UIDROW.ATTRIBUTE_PARENT_ID asc, UIDROW.SORTING asc";

var condition = SqlCondition.begin();

if (vars.exists("$local.idvalues") && vars.get("$local.idvalues"))
{
    condition.and("UIDROW.AB_ATTRIBUTEID in ('" + vars.get("$local.idvalues").join("','") + "')");
}
else if (getGroups) //if getGroups == true, it is the lookup for selecting the superordinate attribute
{
    //this condition filters out the own id and the children to prevent loops
    
    var isGroupCondition = SqlCondition.begin();
    for (let type in $AttributeTypes)
        if ($AttributeTypes[type].isGroup)
        {
            isGroupCondition.orPrepare(["AB_ATTRIBUTE", "ATTRIBUTE_TYPE", uidTableAlias], $AttributeTypes[type]);
        }
    
    condition.andSqlCondition(SqlCondition.begin()
        .andSqlCondition(isGroupCondition)
        .andPrepareVars(["AB_ATTRIBUTE", "AB_ATTRIBUTEID", uidTableAlias], "$param.AttrParentId_param", "# != ?")
        .and("UIDROW.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 (lookup for the attribute selection)
{
    var filteredAttributes = null;
    
    if (vars.exists("$param.FilteredAttributeIds_param") && vars.getString("$param.FilteredAttributeIds_param")) {
        filteredAttributes = JSON.parse(vars.getString("$param.FilteredAttributeIds_param"));
    }
    
    var attributeCount;
    if (vars.exists("$param.AttributeCount_param") && vars.get("$param.AttributeCount_param"))
        attributeCount = JSON.parse(vars.getString("$param.AttributeCount_param"));
    var ids = AttributeUtil.getPossibleAttributes(objectType, false, filteredAttributes, attributeCount);

    if (ids.length > 0)
        condition.and("UIDROW.AB_ATTRIBUTEID in ('" + ids.join("','") + "')");
S.Listl's avatar
S.Listl committed
    else  // do not return anything, if parameter is there but an empty array
        condition.and("1=2");

S.Listl's avatar
S.Listl committed
     fetchUsages = false;
     translateName = true;
else if (parentType) //condition for all subordinate attributes of an attribute (for the tree of subordinate attributes in an attribute)
{
    if (AttributeTypeUtil.isGroupType(parentType))
    {
        var parentId = vars.exists("$param.AttrParentId_param") && vars.get("$param.AttrParentId_param");
        if (parentId)
            condition.and("UIDROW.AB_ATTRIBUTEID in ('" + AttributeUtil.getAllChildren(vars.getString("$param.AttrParentId_param")).join("','") + "')");
    }
    else
        condition.and("1=2");
    
    translateName = true;
}

//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");
    if (filter.filter)
        condition.andSqlCondition(JditoFilterUtils.getSqlCondition(filter.filter, "AB_ATTRIBUTE", uidTableAlias));
}

var usages;
if (fetchUsages) //this query is only necessary in Attribute, not in AttributeRelation
    var usagesSelect = "select AB_ATTRIBUTE_ID, OBJECT_TYPE from AB_ATTRIBUTEUSAGE \n\
                        join AB_ATTRIBUTE UIDROW on AB_ATTRIBUTEUSAGE.AB_ATTRIBUTE_ID = UIDROW.AB_ATTRIBUTEID";
    var usageTbl = db.table(condition.buildSql(usagesSelect, "1=1"));
    usages = {};
    for (let i = 0, l = usageTbl.length; i < l; i++)
    {
        let attrId = usageTbl[i][0];
        if (attrId in usages)
            usages[attrId].push(usageTbl[i][1]);
        else
            usages[attrId] = [usageTbl[i][1]];
    }
}

var attributes = db.table(condition.buildSql(sqlSelect, "1=1", sqlOrder));

var nameCache = {};
S.Listl's avatar
S.Listl committed
result.object(_buildAttributeTable(attributes, usages, translateName));


//sorts the records in a way that a tree can be built and adds values
S.Listl's avatar
S.Listl committed
function _buildAttributeTable (pAttributes, pUsages, pTranslate) 
{
    var rows = {};
    var allIds = {};
    
    //fills the allIds object, the object is used for checking if a parent exists in the array
    for (let i = 0, l = pAttributes.length; i < l; i++)
        allIds[pAttributes[i][0]] = true;
    
    var arrayIndex = 0;
    
    do {
        var oldIndex = arrayIndex;
        pAttributes.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[0] in this) && (row[1] in this || !allIds[row[1]]))
                this[row[0]] = {
                    data : row,
                    index : arrayIndex++
                };
        }, rows);
    } while (oldIndex != arrayIndex); //stops the loop when no new items were added so that recursive relations between attributes don't cause an infinite loop
    
    var displaySimpleName = vars.exists("$param.DisplaySimpleName_param") && vars.get("$param.DisplaySimpleName_param");
    var sortedArray = new Array(Object.keys(rows).length);
    for (let i in rows)
    {
        let rowData = rows[i].data;
        if (pUsages && rowData[5].trim() != $AttributeTypes.COMBOVALUE && i in pUsages)
S.Listl's avatar
S.Listl committed
            rowData[7] = pUsages[i].map(function (usage)
            {
                return translate.text(usage);
            }).join(", ");
S.Listl's avatar
S.Listl committed
        var fullName;
        if (displaySimpleName) 
            fullName = pTranslate 
                ? translate.text(rowData[8])
                : rowData[8];
        else
            fullName = _getFullName(rowData[8], rowData[9], rowData[10], rowData[11], rowData[12], pTranslate);
        rowData.splice(10, 3, fullName);
        sortedArray[rows[i].index] = rowData;
    }
    
    return sortedArray;
    /**
     * builds the full attribute name from the pre-loaded parent names and adds all parent names
     * if required
     */
    function _getFullName (pAttributeName, pParent1Name, pParent2Name, pParent3Name, pParent4Id)
S.Listl's avatar
S.Listl committed
    {
        var parent4FullName;
        if (pParent4Id && pParent4Id in nameCache)
            parent4FullName = nameCache[pParent4Id];
        else
        {
            parent4FullName = pParent4Id ? AttributeUtil.getFullAttributeName(pParent4Id, false, pTranslate) : null;
            nameCache[pParent4Id] = parent4FullName;
        }
        if (pTranslate)
        {
            pAttributeName = translate.text(pAttributeName);
            pParent1Name = translate.text(pParent1Name);
            pParent2Name = translate.text(pParent2Name);
            pParent3Name = translate.text(pParent3Name);
        }
        pAttributeName = ArrayUtils.joinNonEmptyFields([parent4FullName, pParent3Name, pParent2Name, pParent1Name, pAttributeName], " / ");

        return pAttributeName;
S.Listl's avatar
S.Listl committed
    }