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 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"; var sqlOrder = " order by A.ATTRIBUTE_PARENT_ID asc, A.SORTING asc"; 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"); if (vars.exists("$local.idvalues") && vars.get("$local.idvalues")) condition.and("A.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 = SqlCondition.begin(); for (let type in $AttributeTypes) if ($AttributeTypes[type].isGroup && $AttributeTypes[type] != $AttributeTypes.COMBO) isGroupCondition.orPrepare(["AB_ATTRIBUTE", "ATTRIBUTE_TYPE", "A"], $AttributeTypes[type]); condition.andSqlCondition(SqlCondition.begin() .andSqlCondition(isGroupCondition) .andPrepareVars(["AB_ATTRIBUTE", "AB_ATTRIBUTEID", "A"], "$param.AttrParentId_param", "# != ?") .and("A.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 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); condition.and("A.AB_ATTRIBUTEID in ('" + ids.join("','") + "')"); } else { var parentType = vars.exists("$param.AttrParentType_param") && vars.get("$param.AttrParentType_param"); if (AttributeTypeUtil.isGroupType(parentType)) { var parentId = vars.exists("$param.AttrParentId_param") && vars.get("$param.AttrParentId_param"); if (parentId) condition.and("A.AB_ATTRIBUTEID in ('" + AttributeUtil.getAllChildren(vars.getString("$param.AttrParentId_param")).join("','") + "')"); } else if (parentType) 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", "A")); } var attributes = db.table(condition.buildSql(sqlSelect, "1=1", sqlOrder)); var usageIdType = SqlUtils.getSingleColumnType("AB_ATTRIBUTEUSAGE.AB_ATTRIBUTE_ID"); var attributeIdType = SqlUtils.getSingleColumnType("AB_ATTRIBUTE.AB_ATTRIBUTEID"); attributes = _buildAttributeTable(attributes); result.object(attributes); //sorts the records in a way that a tree can be built and adds values function _buildAttributeTable (pAttributes) { var rows = {}; var allIds = {}; //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;}); var arrayIndex = 0; //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) { 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[3] in this || !allIds[row[3]])) this[row[0]] = { data : row, index : arrayIndex++ }; }, rows); } var sortedArray = new Array(Object.keys(rows).length); for (let i in rows) { 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; } 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; }