Skip to content
Snippets Groups Projects
contentProcess.js 6.58 KiB
Newer Older
import("system.logging");
S.Listl's avatar
S.Listl committed
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");

S.Listl's avatar
S.Listl committed
var uidTableAlias = "SELF";
var sqlSelect = "select SELF.AB_ATTRIBUTEID, SELF.ATTRIBUTE_PARENT_ID, SELF.ATTRIBUTE_ACTIVE, SELF.KEYWORD_CONTAINER, SELF.SORTING, SELF.ATTRIBUTE_TYPE, " 
    + KeywordUtils.getResolvedTitleSqlPart($KeywordRegistry.attributeType(), "SELF.ATTRIBUTE_TYPE") //3
    + ", '', SELF.ATTRIBUTE_NAME, PARENT1.ATTRIBUTE_NAME, PARENT2.ATTRIBUTE_NAME, PARENT3.ATTRIBUTE_NAME, PARENT3.ATTRIBUTE_PARENT_ID " 
S.Listl's avatar
S.Listl committed
    + "from AB_ATTRIBUTE SELF "
    + "left join AB_ATTRIBUTE PARENT1 on SELF.ATTRIBUTE_PARENT_ID = PARENT1.AB_ATTRIBUTEID "    //always select the names of the next 3 parents so that less queries
    + "left join AB_ATTRIBUTE PARENT2 ON PARENT1.ATTRIBUTE_PARENT_ID = PARENT2.AB_ATTRIBUTEID " //are required later when buildung the full name
    + "left join AB_ATTRIBUTE PARENT3 ON PARENT2.ATTRIBUTE_PARENT_ID = PARENT3.AB_ATTRIBUTEID"; 
S.Listl's avatar
S.Listl committed

S.Listl's avatar
S.Listl committed
var condition = SqlCondition.begin();
S.Listl's avatar
S.Listl committed
var sqlOrder = " order by SELF.ATTRIBUTE_PARENT_ID asc, SELF.SORTING asc";

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
var parentType = vars.exists("$param.AttrParentType_param") && vars.get("$param.AttrParentType_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("SELF.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
S.Listl's avatar
S.Listl committed
    var isGroupCondition = SqlCondition.begin();
S.Listl's avatar
S.Listl committed
    for (let type in $AttributeTypes)
S.Listl's avatar
S.Listl committed
        if ($AttributeTypes[type].isGroup && $AttributeTypes[type] != $AttributeTypes.COMBO 
            && (parentType == $AttributeTypes.VOID || $AttributeTypes[type] != $AttributeTypes.VOID))
        {
S.Listl's avatar
S.Listl committed
            isGroupCondition.orPrepare(["AB_ATTRIBUTE", "ATTRIBUTE_TYPE", uidTableAlias], $AttributeTypes[type]);
S.Listl's avatar
S.Listl committed
        }
    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", uidTableAlias], "$param.AttrParentId_param", "# != ?")
        .and("SELF.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
{
    logging.log(vars.get("$param.FilteredAttributeIds_param"))
    if (vars.exists("$param.FilteredAttributeIds_param") && vars.getString("$param.FilteredAttributeIds_param")) {
        var 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 (filteredAttributes.length > 0)
            condition.and("SELF.AB_ATTRIBUTEID in ('" + ids.join("','") + "')");
        else // do not return anything, if parameter is there but an empty array
            condition.and("1=2");
    }
        
S.Listl's avatar
S.Listl committed
else if (parentType)
S.Listl's avatar
S.Listl committed
    if (AttributeTypeUtil.isGroupType(parentType)) //condition for all subordinate attributes of an attribute
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("SELF.AB_ATTRIBUTEID in ('" + AttributeUtil.getAllChildren(vars.getString("$param.AttrParentId_param")).join("','") + "')");
S.Listl's avatar
S.Listl committed
    else
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");
    if (filter.filter)
        condition.andSqlCondition(JditoFilterUtils.getSqlCondition(filter.filter, "AB_ATTRIBUTE", uidTableAlias));
S.Listl's avatar
S.Listl committed
var attributes = db.table(condition.buildSql(sqlSelect, "1=1", sqlOrder));
S.Listl's avatar
S.Listl committed
result.object(_buildAttributeTable(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
    do {
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[1] in this || !allIds[row[1]]))
S.Listl's avatar
S.Listl committed
                this[row[0]] = {
                    data : row,
S.Listl's avatar
S.Listl committed
                    index : arrayIndex++
                };
        }, rows);
S.Listl's avatar
S.Listl committed
    } while (oldIndex != arrayIndex); //stops the loop when no new items were added so that recursive relations between attributes don't cause an infinite loop
    
S.Listl's avatar
S.Listl committed
    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)
S.Listl's avatar
S.Listl committed
    {
        let rowData = rows[i].data;
S.Listl's avatar
S.Listl committed
        if (rowData[5].trim() != $AttributeTypes.COMBOVALUE)
S.Listl's avatar
S.Listl committed
        {
            let usages = db.array(db.COLUMN, SqlCondition.begin()
                .andPrepare("AB_ATTRIBUTEUSAGE.AB_ATTRIBUTE_ID", rowData[0])
S.Listl's avatar
S.Listl committed
                .buildSql("select OBJECT_TYPE from AB_ATTRIBUTEUSAGE")
            );
S.Listl's avatar
S.Listl committed
            rowData[7] = usages.join(", ");
S.Listl's avatar
S.Listl committed
        }
S.Listl's avatar
S.Listl committed
        var fullName = displaySimpleName 
            ? rowData[8]
            : _getFullName(rowData[8], rowData[9], rowData[10], rowData[11], rowData[12]);
        rowData.splice(9, 4, fullName);
S.Listl's avatar
S.Listl committed
        sortedArray[rows[i].index] = rowData;
    }
    
    return sortedArray;
function _getFullName (pAttributeName, pParent1Name, pParent2Name, pParent3Name, pParent4Id)
S.Listl's avatar
S.Listl committed
{
    var parent4FullName = pParent4Id ? AttributeUtil.getFullAttributeName(pParent4Id) : null;
    pAttributeName = ArrayUtils.joinNonEmptyFields([parent4FullName, pParent3Name, pParent2Name, pParent1Name, pAttributeName], " / ");
S.Listl's avatar
S.Listl committed
    return pAttributeName;