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

var objectType = vars.exists("$param.ObjectType_param") && vars.get("$param.ObjectType_param");
S.Listl's avatar
S.Listl committed
var objectRowId = vars.exists("$param.ObjectRowId_param") && vars.get("$param.ObjectRowId_param");
S.Listl's avatar
S.Listl committed
var idvalues = vars.exists("$local.idvalues") && vars.get("$local.idvalues");
S.Listl's avatar
S.Listl committed
var getTree = vars.exists("$param.GetTree_param") && vars.getString("$param.GetTree_param") == "true";
var getTheme = vars.exists("$param.IsTheme_param") && vars.getString("$param.IsTheme_param") == "true";
var showEmpty = vars.exists("$param.ShowEmpty_param") && vars.getString("$param.ShowEmpty_param") == "true";
S.Listl's avatar
S.Listl committed
var displaySimpleName = vars.exists("$param.DisplaySimpleName_param") && vars.getString("$param.DisplaySimpleName_param") == "true";

var defaultFields = [
    "AB_ATTRIBUTERELATIONID",
    "AB_ATTRIBUTE.AB_ATTRIBUTEID", 
    "AB_ATTRIBUTE.ATTRIBUTE_PARENT_ID", 
    "AB_ATTRIBUTE.ATTRIBUTE_TYPE", 
    "AB_ATTRIBUTE.ATTRIBUTE_NAME", 
    "AB_ATTRIBUTE.DROPDOWNDEFINITION", 
    "COMBOVAL.ATTRIBUTE_NAME",
    "AB_ATTRIBUTERELATION.DATE_NEW",
    "AB_ATTRIBUTERELATION.USER_NEW",
    "AB_ATTRIBUTERELATION.DATE_EDIT",
    "AB_ATTRIBUTERELATION.USER_EDIT"
];
S.Listl's avatar
S.Listl committed
//these fields hold the attributeRelation value, depending on the attribute type
var valueFields = AttributeTypeUtil.getAllDatabaseFields();
var attributeSql = SqlBuilder.begin()
    .select(defaultFields.concat(valueFields))
    .from("AB_ATTRIBUTE");
S.Listl's avatar
S.Listl committed
var attributeCond = SqlCondition.begin(); //where-condition (condition for the Attribute)
var attributeRelationCond = SqlCondition.begin()
    .and("AB_ATTRIBUTERELATION.AB_ATTRIBUTE_ID = AB_ATTRIBUTE.AB_ATTRIBUTEID"); //condition for the joined values (for AttributeRelation)
S.Listl's avatar
S.Listl committed
var possibleAttributes;
S.Listl's avatar
S.Listl committed
if (idvalues)
S.Listl's avatar
S.Listl committed
    let attrId = idvalues.length === 1 && idvalues[0].split(",")[1];
    if (!attrId)
        showEmpty = false;
    
    if (showEmpty)
S.Listl's avatar
S.Listl committed
        attributeCond.andPrepare("AB_ATTRIBUTE.AB_ATTRIBUTEID", attrId);
        attributeCond.andIn("AB_ATTRIBUTERELATION.AB_ATTRIBUTERELATIONID", idvalues.map(function(pId) 
        {
            return pId[0] == "," ? pId.split(",")[1] : pId;
        }));
S.Listl's avatar
S.Listl committed
        objectRowId = null;
    }
    getTree = false;
}
S.Listl's avatar
S.Listl committed
else if (showEmpty || objectRowId)
S.Listl's avatar
S.Listl committed
    if (getTheme)
        attributeCond.andPrepare("AB_ATTRIBUTE.ATTRIBUTE_TYPE", $AttributeTypes.THEME);
    else
        attributeCond.andPrepare("AB_ATTRIBUTE.ATTRIBUTE_TYPE", $AttributeTypes.THEME, "# != ?");
    
    if (showEmpty)
    {
S.Listl's avatar
S.Listl committed
        possibleAttributes = AttributeUtil.getPossibleAttributes(objectType);
        let filtered = vars.exists("$param.FilteredAttributeIds_param") && vars.getString("$param.FilteredAttributeIds_param");
        
S.Listl's avatar
S.Listl committed
        attributeCond.andIn("AB_ATTRIBUTE.AB_ATTRIBUTEID", possibleAttributes);
    }
    if (vars.exists("$param.FilteredAttributeIds_param") && vars.getString("$param.FilteredAttributeIds_param"))
    {
        let filteredIds = JSON.parse(vars.getString("$param.FilteredAttributeIds_param"));

        let filteredCondition = new SqlCondition();
        let filteredIdChildren = AttributeUtil.getAllChildren(filteredIds);
        
        filteredCondition.andIn("AB_ATTRIBUTE.AB_ATTRIBUTEID", filteredIdChildren);
        filteredCondition.andPrepare("AB_ATTRIBUTE.ATTRIBUTE_TYPE", $AttributeTypes.COMBOVALUE, "# != ?")
    
        // return nothing if filteredAttributeIds is an empty array. (--> and 1=2)
S.Listl's avatar
S.Listl committed
        attributeCond.andSqlCondition(filteredCondition, "1=2");
S.Listl's avatar
S.Listl committed
if (objectRowId)
S.Listl's avatar
S.Listl committed
    attributeRelationCond.andPrepare("AB_ATTRIBUTERELATION.OBJECT_ROWID", objectRowId);
    if (objectType)
        attributeRelationCond.andPrepare("AB_ATTRIBUTERELATION.OBJECT_TYPE", objectType);
S.Listl's avatar
S.Listl committed
attributeSql.where(attributeCond);
    
if (showEmpty)
S.Listl's avatar
S.Listl committed
    attributeSql.leftJoin("AB_ATTRIBUTERELATION", attributeRelationCond);
S.Listl's avatar
S.Listl committed
    attributeSql.join("AB_ATTRIBUTERELATION", attributeRelationCond);
S.Listl's avatar
S.Listl committed
attributeSql.leftJoin("AB_ATTRIBUTE", "COMBOVAL.AB_ATTRIBUTEID = " + $AttributeTypes.COMBO.databaseField, "COMBOVAL")
S.Listl's avatar
S.Listl committed
//Builds an object containing the minimal counts of the attributes, this is required for
//checking if an attribute is used not often enough or just often enough. When this is the case,
//deletion of this attributeRelation will be prohibited.
var minCountInsurance = {};
if (getTree)
{
S.Listl's avatar
S.Listl committed
    if (!possibleAttributes)
        possibleAttributes = AttributeUtil.getPossibleAttributes(objectType);
    
    let minUsages = db.table(SqlCondition.begin()
        .andIn("AB_ATTRIBUTEUSAGE.AB_ATTRIBUTE_ID", possibleAttributes)
        .andPrepare("AB_ATTRIBUTEUSAGE.OBJECT_TYPE", objectType)
        .buildSql("select AB_ATTRIBUTE_ID, MIN_COUNT from AB_ATTRIBUTEUSAGE", "1=2")
    );
    minUsages.forEach(function (usage)
    {
        this[usage[0]] = {
            count : 0,
            min : usage[1]
        };
S.Listl's avatar
S.Listl committed
    }, minCountInsurance);
S.Listl's avatar
S.Listl committed
var attrRelations = db.table(attributeSql.build()).map(
    function (row) 
S.Listl's avatar
S.Listl committed
        var [attrRelId, attrId, attrParentId, attrType, attrName, dropDownDef, comboViewVal, dateNew, userNew, dateEdit, userEdit] = row;
        attrName = translate.text(attrName);
        attrType = attrType.trim();
        if (!getTree && !displaySimpleName && attrParentId)
        {
            let parentName = AttributeUtil.getFullAttributeName(attrParentId);
            attrName = (parentName ? parentName + " / " : "") + attrName;
        }
        var value = row[AttributeTypeUtil.getTypeColumnIndex(attrType) + defaultFields.length];
        var viewValue;
        if (attrType == $AttributeTypes.COMBO)
            viewValue = translate.text(comboViewVal);
        else 
            viewValue = AttributeTypeUtil.getAttributeViewValue(attrType, value, dropDownDef);

        if (attrId in minCountInsurance)
            minCountInsurance[attrId].count++;
        
        //TODO: what should be the uid if showEmpty is true?
        //                 V-- set "," to mark this as new generated UUID
        return [
            attrRelId || util.getNewUUID() + "," + attrId, 
            attrParentId, 
            value, 
            viewValue, 
            attrId, 
            attrName, 
            "", 
            attrType.trim(), 
            dateNew, 
            userNew, 
            dateEdit, 
            userEdit
        ];
S.Listl's avatar
S.Listl committed
);
S.Listl's avatar
S.Listl committed
 _protectMinCountAttributes(attrRelations, minCountInsurance);
S.Listl's avatar
S.Listl committed
//object of attribute ids to avoid duplicates (more than one attribute can have the same parent)
var attrCatalog = {}; 
if (getTree)
S.Listl's avatar
S.Listl committed
    attrRelations = _buildAttributeTree(attrRelations);
S.Listl's avatar
S.Listl committed
result.object(attrRelations);
S.Listl's avatar
S.Listl committed
/*
 * loads the parents for a tree
 */
function _buildAttributeTree (pAttrRelations)
{
    var parentAttributes = [];
    _fetchParentAttributes(pAttrRelations.map(function (row) {return row[1]}), parentAttributes);
    return TreeUtils.sortArrayForTree(parentAttributes, 0, 1).concat(pAttrRelations);
}

/*
 * recursive function that loads all superordinate attributes for the tree
 */
S.Listl's avatar
S.Listl committed
function _fetchParentAttributes (pAttributeIds, pParentAttributes)
S.Listl's avatar
S.Listl committed
    var attributeCond = SqlCondition.begin();
    var nextIds = [];
    pAttributeIds.forEach(function (id)
    {
        if (!(id in this))
S.Listl's avatar
S.Listl committed
            attributeCond.orPrepare("AB_ATTRIBUTE.AB_ATTRIBUTEID", id);
    }, attrCatalog);
    db.table(attributeCond.buildSql("select AB_ATTRIBUTEID, ATTRIBUTE_PARENT_ID, ATTRIBUTE_NAME from AB_ATTRIBUTE", "1=2"))
        .forEach(function ([attrId, parentId, attrName])
S.Listl's avatar
S.Listl committed
                this[attrId] = true; //make entry in attrCatalog to avoid duplicates
                if (parentId)
                    nextIds.push(parentId);
                
                pParentAttributes.push([
                    attrId, 
                    parentId, 
                    "", 
                    "", 
                    "", 
                    translate.text(attrName),  //translate attribute name
                    "true", 
                    "", 
                    "", 
                    "", 
                    "", 
                    ""
                ]);
            }, attrCatalog);
            
    if (nextIds.length)
S.Listl's avatar
S.Listl committed
        _fetchParentAttributes(nextIds, pParentAttributes);
}

function _protectMinCountAttributes (pAttrRelations, pMinCountInsurance)
{
    for (let i = 0; i < pAttrRelations.length; i++)
    {
        let attrId = pAttrRelations[i][4];
        if (attrId in pMinCountInsurance && pMinCountInsurance[attrId].min >= pMinCountInsurance[attrId].count)
            pAttrRelations[i][6] = "true";
    }
S.Listl's avatar
S.Listl committed
}