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");
var objectRowId = vars.exists("$param.ObjectRowId_param") && vars.get("$param.ObjectRowId_param");
var idvalues = vars.exists("$local.idvalues") && vars.get("$local.idvalues");
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";
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"
];
//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");
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)
let attrId = idvalues.length === 1 && idvalues[0].split(",")[1];
if (!attrId)
showEmpty = false;
if (showEmpty)
attributeCond.andPrepare("AB_ATTRIBUTE.AB_ATTRIBUTEID", attrId);
attributeCond.andIn("AB_ATTRIBUTERELATION.AB_ATTRIBUTERELATIONID", idvalues.map(function(pId)
{
return pId[0] == "," ? pId.split(",")[1] : pId;
}));
if (getTheme)
attributeCond.andPrepare("AB_ATTRIBUTE.ATTRIBUTE_TYPE", $AttributeTypes.THEME);
else
attributeCond.andPrepare("AB_ATTRIBUTE.ATTRIBUTE_TYPE", $AttributeTypes.THEME, "# != ?");
possibleAttributes = AttributeUtil.getPossibleAttributes(objectType);
let filtered = vars.exists("$param.FilteredAttributeIds_param") && vars.getString("$param.FilteredAttributeIds_param");
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)
attributeRelationCond.andPrepare("AB_ATTRIBUTERELATION.OBJECT_ROWID", objectRowId);
if (objectType)
attributeRelationCond.andPrepare("AB_ATTRIBUTERELATION.OBJECT_TYPE", objectType);
attributeSql.leftJoin("AB_ATTRIBUTERELATION", attributeRelationCond);
attributeSql.join("AB_ATTRIBUTERELATION", attributeRelationCond);
attributeSql.leftJoin("AB_ATTRIBUTE", "COMBOVAL.AB_ATTRIBUTEID = " + $AttributeTypes.COMBO.databaseField, "COMBOVAL")
//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 (!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]
};
var attrRelations = db.table(attributeSql.build()).map(
function (row)
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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
];
//object of attribute ids to avoid duplicates (more than one attribute can have the same parent)
var attrCatalog = {};
/*
* 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
*/
function _fetchParentAttributes (pAttributeIds, pParentAttributes)
var nextIds = [];
pAttributeIds.forEach(function (id)
{
if (!(id in this))
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])
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);
_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";
}