Skip to content
Snippets Groups Projects
Commit 81fd1f89 authored by S.Listl's avatar S.Listl Committed by Sophia Leierseder
Browse files

Attribute jdito recordcontainer

parent 8b642ce0
No related branches found
No related tags found
No related merge requests found
Showing
with 292 additions and 337 deletions
import("system.vars");
import("system.result");
import("system.db");
import("Attribute_lib");
import("Sql_lib");
var objectType = vars.get("$param.ObjectType_param");
var rowId = vars.get("$param.ObjectRowId_param");
var attributeObj = {};
var allAttributes = [];
var sqlSelect = "select AB_ATTRIBUTEID, ATTRIBUTE_PARENT_ID, ATTRIBUTE_NAME, ATTRIBUTE_LEVEL from AB_ATTRIBUTE";
var attributeValues = AttributeRelationUtils.getAllAttributes(rowId, objectType, false, true);
_fetchAttributes(attributeValues.map(function (row) {return row[1]}));
allAttributes = allAttributes
.sort(function (a, b) {return a[3] - b[3];}) //sort by level to make sure parents are added first
.map(function (row) {return [row[0], row[1], row[2]];}) //remove level from array
.concat(attributeValues);
result.object(allAttributes);
function _fetchAttributes (pAttributeIds)
{
var condition = SqlCondition.begin();
var nextIds = [];
pAttributeIds.forEach(function (id)
{
if (!(id in this))
condition.orPrepare("AB_ATTRIBUTE.AB_ATTRIBUTEID", id);
}, attributeObj);
db.table(condition.buildSql(sqlSelect, "1=2"))
.forEach(function (row)
{
this[row[0]] = true;
if (row[1])
nextIds.push(row[1]);
else
row[1] = null;
allAttributes.push(row);
}, attributeObj);
if (nextIds.length)
_fetchAttributes(nextIds);
import("system.vars");
import("system.result");
import("system.db");
import("Attribute_lib");
import("Sql_lib");
var objectType = vars.get("$param.ObjectType_param");
var rowId = vars.get("$param.ObjectRowId_param");
var attributeObj = {};
var allAttributes = [];
var sqlSelect = "select AB_ATTRIBUTEID, ATTRIBUTE_PARENT_ID, ATTRIBUTE_NAME from AB_ATTRIBUTE";
var attributeValues = AttributeRelationUtils.getAllAttributes(rowId, objectType, false, true);
_fetchAttributes(attributeValues.map(function (row) {return row[1]}));
allAttributes = _sortArrayForTree(allAttributes).concat(attributeValues);
result.object(allAttributes);
function _fetchAttributes (pAttributeIds)
{
var condition = SqlCondition.begin();
var nextIds = [];
pAttributeIds.forEach(function (id)
{
if (!(id in this))
condition.orPrepare("AB_ATTRIBUTE.AB_ATTRIBUTEID", id);
}, attributeObj);
db.table(condition.buildSql(sqlSelect, "1=2"))
.forEach(function (row)
{
this[row[0]] = true;
if (row[1])
nextIds.push(row[1]);
else
row[1] = null;
allAttributes.push(row);
}, attributeObj);
if (nextIds.length)
_fetchAttributes(nextIds);
}
function _sortArrayForTree (pArray)
{
var rows = {};
var allIds = {};
var idIndex = 1;
var parentIdIndex = 4;
pArray.forEach(function (row) {allIds[row[idIndex]] = true;});
var index = 0;
for (let itemsAdded = true; itemsAdded; itemsAdded = oldIndex != index)
{
var oldIndex = index;
pArray.forEach(function (row)
{
if (!(row[idIndex] in this) && (row[parentIdIndex] in this || !allIds[row[parentIdIndex]]))
this[row[idIndex]] = {
data : row,
index : index++
};
}, rows);
}
var sortedArray = new Array(Object.keys(rows).length);
for (let i in rows)
sortedArray[rows[i].index] = rows[i].data;
return sortedArray;
}
\ No newline at end of file
......@@ -7,7 +7,7 @@
<afterUiInit>%aditoprj%/entity/Attribute_entity/afterUiInit.js</afterUiInit>
<iconId>VAADIN:TAG</iconId>
<titleProcess>%aditoprj%/entity/Attribute_entity/titleProcess.js</titleProcess>
<recordContainer>db</recordContainer>
<recordContainer>jdito</recordContainer>
<entityFields>
<entityProvider>
<name>#PROVIDER</name>
......@@ -282,57 +282,28 @@
<name>IMAGE</name>
<contentType>IMAGE</contentType>
</entityField>
<entityField>
<name>UID</name>
</entityField>
</entityFields>
<recordContainers>
<dbRecordContainer>
<name>db</name>
<alias>Data_alias</alias>
<conditionProcess>%aditoprj%/entity/Attribute_entity/recordcontainers/db/conditionProcess.js</conditionProcess>
<orderClauseProcess>%aditoprj%/entity/Attribute_entity/recordcontainers/db/orderClauseProcess.js</orderClauseProcess>
<onDBUpdate>%aditoprj%/entity/Attribute_entity/recordcontainers/db/onDBUpdate.js</onDBUpdate>
<onDBDelete>%aditoprj%/entity/Attribute_entity/recordcontainers/db/onDBDelete.js</onDBDelete>
<linkInformation>
<linkInformation>
<name>b5fd38e1-b315-4e66-bd68-569dfc04710c</name>
<tableName>AB_ATTRIBUTE</tableName>
<primaryKey>AB_ATTRIBUTEID</primaryKey>
<isUIDTable v="true" />
</linkInformation>
</linkInformation>
<recordFieldMappings>
<dbRecordFieldMapping>
<name>AB_ATTRIBUTEID.value</name>
<recordfield>AB_ATTRIBUTE.AB_ATTRIBUTEID</recordfield>
</dbRecordFieldMapping>
<dbRecordFieldMapping>
<name>ATTRIBUTE_PARENT_ID.value</name>
<recordfield>AB_ATTRIBUTE.ATTRIBUTE_PARENT_ID</recordfield>
</dbRecordFieldMapping>
<dbRecordFieldMapping>
<name>ATTRIBUTE_TYPE.value</name>
<recordfield>AB_ATTRIBUTE.ATTRIBUTE_TYPE</recordfield>
</dbRecordFieldMapping>
<dbRecordFieldMapping>
<name>ATTRIBUTE_ACTIVE.value</name>
<recordfield>AB_ATTRIBUTE.ATTRIBUTE_ACTIVE</recordfield>
</dbRecordFieldMapping>
<dbRecordFieldMapping>
<name>ATTRIBUTE_TYPE.displayValue</name>
<expression>%aditoprj%/entity/Attribute_entity/recordcontainers/db/recordfieldmappings/attribute_type.displayvalue/expression.js</expression>
</dbRecordFieldMapping>
<dbRecordFieldMapping>
<name>ATTRIBUTE_NAME.value</name>
<recordfield>AB_ATTRIBUTE.ATTRIBUTE_NAME</recordfield>
</dbRecordFieldMapping>
<dbRecordFieldMapping>
<name>ATTRIBUTE_LEVEL.value</name>
<recordfield>AB_ATTRIBUTE.ATTRIBUTE_LEVEL</recordfield>
</dbRecordFieldMapping>
<dbRecordFieldMapping>
<name>KEYWORD_CONTAINER.value</name>
<recordfield>AB_ATTRIBUTE.KEYWORD_CONTAINER</recordfield>
</dbRecordFieldMapping>
</recordFieldMappings>
</dbRecordContainer>
<jDitoRecordContainer>
<name>jdito</name>
<jDitoRecordAlias>Data_alias</jDitoRecordAlias>
<contentProcess>%aditoprj%/entity/Attribute_entity/recordcontainers/jdito/contentProcess.js</contentProcess>
<onInsert>%aditoprj%/entity/Attribute_entity/recordcontainers/jdito/onInsert.js</onInsert>
<onUpdate>%aditoprj%/entity/Attribute_entity/recordcontainers/jdito/onUpdate.js</onUpdate>
<onDelete>%aditoprj%/entity/Attribute_entity/recordcontainers/jdito/onDelete.js</onDelete>
<recordFields>
<element>UID.value</element>
<element>AB_ATTRIBUTEID.value</element>
<element>ATTRIBUTE_ACTIVE.value</element>
<element>ATTRIBUTE_NAME.value</element>
<element>ATTRIBUTE_PARENT_ID.value</element>
<element>ATTRIBUTE_TYPE.value</element>
<element>ATTRIBUTE_TYPE.displayValue</element>
<element>KEYWORD_CONTAINER.value</element>
</recordFields>
</jDitoRecordContainer>
</recordContainers>
</entity>
import("system.db");
import("system.neon");
import("system.result");
import("system.vars");
import("Sql_lib");
if (vars.get("$sys.recordstate") == neon.OPERATINGSTATE_NEW || vars.get("$sys.recordstate") == neon.OPERATINGSTATE_EDIT)
if (vars.get("$field.ATTRIBUTE_PARENT_ID") != "")
{
var level = db.cell(SqlCondition.begin()
.andPrepare("AB_ATTRIBUTE.AB_ATTRIBUTEID", vars.get("$field.ATTRIBUTE_PARENT_ID"))
.buildSql("select ATTRIBUTE_LEVEL from AB_ATTRIBUTE"));
result.string(parseInt(level) + 1);
}
else
result.string("0");
import("system.db");
import("system.vars");
import("system.result");
import("Sql_lib");
import("Attribute_lib");
var condition = "1 = 2";
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 (getGroups)
{
//this is for the selection of the superordinate attribute, this condition
//filters out the own id and the children to prevent loops
condition = db.translateCondition(SqlCondition.begin()
.andPrepare("AB_ATTRIBUTE.ATTRIBUTE_TYPE", $AttributeTypes.GROUP)
.andPrepareVars("AB_ATTRIBUTE.AB_ATTRIBUTEID", "$param.AttrParentId_param", "# != ?")
.and("AB_ATTRIBUTE.AB_ATTRIBUTEID not in ('" + AttributeUtil.getAllChildren(vars.getString("$param.AttrParentId_param")).join("','") + "')")
.build());
}
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 ids = AttributeUtil.getPossibleAttributes(objectType, false, filteredAttributes);
condition = "AB_ATTRIBUTE.AB_ATTRIBUTEID in ('" + ids.join("','") + "')";
}
else
{
var type = vars.exists("$param.AttrParentType_param") && vars.get("$param.AttrParentType_param");
if (type == $AttributeTypes.COMBO)
condition = SqlCondition.begin()
.andPrepareVars("AB_ATTRIBUTE.ATTRIBUTE_PARENT_ID", "$param.AttrParentId_param")
.build();
var parentId = vars.exists("$param.AttrParentId_param") && vars.get("$param.AttrParentId_param");
if (parentId)
condition = "AB_ATTRIBUTE.AB_ATTRIBUTEID in ('" + AttributeUtil.getAllChildren(vars.getString("$param.AttrParentId_param")).join("','") + "')";
else if (!type)
condition = "";
}
result.string(condition);
import("system.vars");
import("system.db");
import("Sql_lib");
import("Attribute_lib");
var attributeId = vars.get("$field.AB_ATTRIBUTEID");
var childIds = AttributeUtil.getAllChildren(attributeId).concat(attributeId);
var condition = SqlCondition.begin()
.and("AB_ATTRIBUTEUSAGE.AB_ATTRIBUTE_ID in ('" + childIds.join("','") + "')")
.build();
//delete all entries in AB_ATTRIBUTEUSAGE belonging to the attribute to avoid unrelated entries
db.deleteData("AB_ATTRIBUTEUSAGE", condition);
condition = SqlCondition.begin()
.and("AB_ATTRIBUTERELATION.AB_ATTRIBUTE_ID in ('" + childIds.join("','") + "')")
.build();
//delete all entries in AB_ATTRIBUTERELATION for the attributes
db.deleteData("AB_ATTRIBUTERELATION", condition);
condition = SqlCondition.begin()
.and("AB_ATTRIBUTE.AB_ATTRIBUTEID in ('" + childIds.join("','") + "')")
.build();
//delete all attribute children
db.deleteData("AB_ATTRIBUTE", condition);
import("system.db");
import("system.neon");
import("system.vars");
import("Attribute_lib");
var level = parseInt(vars.get("$field.ATTRIBUTE_LEVEL"));
var table = "AB_ATTRIBUTE";
var columns = ["ATTRIBUTE_LEVEL"];
var types = db.getColumnTypes(table, columns);
var toUpdate = [];
var attributes = [vars.get("$field.AB_ATTRIBUTEID")];
while (attributes.length > 0)
{
var condition = SqlCondition.begin()
.and("AB_ATTRIBUTE.ATTRIBUTE_PARENT_ID in ('" + attributes.join("','") + "')");
toUpdate.push([table, columns, types, [String(++level)], condition.build()]);
attributes = db.array(db.COLUMN, SqlCondition.begin()
.and("AB_ATTRIBUTE.ATTRIBUTE_PARENT_ID in ('" + attributes.join("','") + "')")
.buildSql("select AB_ATTRIBUTEID from AB_ATTRIBUTE")
);
}
db.updates(toUpdate);
import("system.db");
import("system.result");
result.object({
"AB_ATTRIBUTE.ATTRIBUTE_LEVEL" : db.ASCENDING,
"AB_ATTRIBUTE.ATTRIBUTE_NAME" : db.ASCENDING
});
\ No newline at end of file
import("system.result");
import("Keyword_lib");
import("KeywordRegistry_basic");
var sql = KeywordUtils.getResolvedTitleSqlPart($KeywordRegistry.attributeType(), "AB_ATTRIBUTE.ATTRIBUTE_TYPE");
result.string(sql);
\ No newline at end of file
......@@ -50,8 +50,8 @@ AttributeUtil.getPossibleAttributes = function (pObjectType, pIncludeGroups, pFi
attrCond.andSqlCondition(filteredIdsCondition);
}
// if (!pIncludeGroups)
// attrCond.and("ATTRIBUTE_TYPE != '" + $AttributeTypes.GROUP + "'");
if (!pIncludeGroups)
attrCond.and("ATTRIBUTE_TYPE != '" + $AttributeTypes.GROUP + "'");
var attributes = db.array(db.COLUMN, attrCond.buildSql(attrSql));
......
/**
* object for filtering records
*
* @param {Array} pColumns the column names
* @param {String|Object} pFilter the filter object
*/
function JditoFilter (pColumns, pFilter)
{
var columnMap = {};
pColumns.forEach(function (row, i)
{
columnMap[row] = i;
});
this._columnMap = columnMap;
if (pFilter.length) //check if pFilter is a string
pFilter = JSON.parse(pFilter);
this._operator = pFilter.operator;
this._filters = pFilter.childs;
}
/**
* tests the given row if it matches the filter
*
* @param {Array} pRow one record
*
* @return {boolean} true, if it matches the condition
*/
JditoFilter.prototype.checkRecord = function (pRow)
{
if (this._filters.length == 0)
return true;
if (this._operator == "AND")
return this._filters.every(_testFn, this);
return this._filters.some(_testFn, this);
function _testFn (pFilter)
{
let value = pRow[this._columnMap[pFilter.name]];
return this._testValue(value, (pFilter.key || pFilter.value), pFilter.operator);
}
}
/**
* compares two values with the given operator
*/
JditoFilter.prototype._testValue = function (pRowValue, pFilterValue, pOperator)
{
switch (pOperator)
{
case "CONTAINS":
return (new RegExp(pFilterValue)).test(pRowValue);
case "CONTAINSNOT":
return !(new RegExp(pFilterValue)).test(pRowValue);
case "STARTSWITH":
return (new RegExp("^" + pFilterValue)).test(pRowValue);
case "ENDSWITH":
return (new RegExp(pFilterValue + "$")).test(pRowValue);
case "EQUAL":
return (new RegExp("^" + pFilterValue + "$")).test(pRowValue);
case "NOT_EQUAL":
return !(new RegExp("^" + pFilterValue + "$")).test(pRowValue);
case "LESS":
return pRowValue < pFilterValue;
case "LESS_OR_EQUAL":
return pRowValue <= pFilterValue;
case "GREATER":
return pRowValue > pFilterValue;
case "GREATER_OR_EQUAL":
return pRowValue >= pFilterValue;
case "ISNULL":
return pRowValue == "";
case "ISNOTNULL":
return pRowValue != "";
}
}
/**
* Provides functions for using the filter with jdito recordcontainers. You should only use this
* if there is no other, faster way to filter the records
*
* Do not instanciate this!
*
* @class
*/
function JditoFilterUtils () {}
/**
* Filters the given records
*
* @param {Array} pColumns one dimensional array with all column names, the order has to match the columns of pRecords
* @param {Array} pRecords two dimensional array with all records
* @param {String|Object} pFilter the value of $local.filter
*
* @return {Array} the filtered records
*/
JditoFilterUtils.filterRecords = function (pColumns, pRecords, pFilter)
{
if (!pFilter)
return pRecords;
var filter = new JditoFilter(pColumns, pFilter);
return pRecords.filter(function (row)
{
return this.checkRecord(row);
}, filter);
import("Sql_lib");
/**
* object for filtering records
*
* @param {Array} pColumns the column names
* @param {String|Object} pFilter the filter object
*/
function JditoFilter (pColumns, pFilter)
{
var columnMap = {};
pColumns.forEach(function (row, i)
{
columnMap[row] = i;
});
this._columnMap = columnMap;
if (pFilter.length) //check if pFilter is a string
pFilter = JSON.parse(pFilter);
this._operator = pFilter.operator;
this._filters = pFilter.childs;
}
/**
* tests the given row if it matches the filter
*
* @param {Array} pRow one record
*
* @return {boolean} true, if it matches the condition
*/
JditoFilter.prototype.checkRecord = function (pRow)
{
if (this._filters.length == 0)
return true;
if (this._operator == "AND")
return this._filters.every(_testFn, this);
return this._filters.some(_testFn, this);
function _testFn (pFilter)
{
let value = pRow[this._columnMap[pFilter.name]];
return this._testValue(value, (pFilter.key || pFilter.value), pFilter.operator);
}
}
/**
* compares two values with the given operator
*/
JditoFilter.prototype._testValue = function (pRowValue, pFilterValue, pOperator)
{
switch (pOperator)
{
case "CONTAINS":
return (new RegExp(pFilterValue)).test(pRowValue);
case "CONTAINSNOT":
return !(new RegExp(pFilterValue)).test(pRowValue);
case "STARTSWITH":
return (new RegExp("^" + pFilterValue)).test(pRowValue);
case "ENDSWITH":
return (new RegExp(pFilterValue + "$")).test(pRowValue);
case "EQUAL":
return (new RegExp("^" + pFilterValue + "$")).test(pRowValue);
case "NOT_EQUAL":
return !(new RegExp("^" + pFilterValue + "$")).test(pRowValue);
case "LESS":
return pRowValue < pFilterValue;
case "LESS_OR_EQUAL":
return pRowValue <= pFilterValue;
case "GREATER":
return pRowValue > pFilterValue;
case "GREATER_OR_EQUAL":
return pRowValue >= pFilterValue;
case "ISNULL":
return pRowValue == "";
case "ISNOTNULL":
return pRowValue != "";
}
}
/**
* Provides functions for using the filter with jdito recordcontainers. You should only use this
* if there is no other, faster way to filter the records
*
* Do not instanciate this!
*
* @class
*/
function JditoFilterUtils () {}
/**
* Filters the given records
*
* @param {Array} pColumns one dimensional array with all column names, the order has to match the columns of pRecords
* @param {Array} pRecords two dimensional array with all records
* @param {String|Object} pFilter the value of $local.filter
*
* @return {Array} the filtered records
*/
JditoFilterUtils.filterRecords = function (pColumns, pRecords, pFilter)
{
if (!pFilter)
return pRecords;
var filter = new JditoFilter(pColumns, pFilter);
return pRecords.filter(function (row)
{
return this.checkRecord(row);
}, filter);
}
JditoFilterUtils.getSqlCondition = function (pFilter, pTable, pColumnMap)
{
var condition = new SqlCondition();
if (!pFilter)
return condition;
if (pFilter.length) //check if pFilter is a string
pFilter = JSON.parse(pFilter);
if (!pColumnMap)
pColumnMap = {};
_addCondition.call(condition, pFilter, pFilter.operator);
return condition;
//recursive function (for compatibility with a condition tree) that
//builds the SqlCondition
function _addCondition (pCondition, pOperator)
{
if (pCondition.type == "row")
{
if (pCondition.name in pColumnMap)
pCondition.name = pColumnMap[pCondition.name];
else if (pTable)
pCondition.name = pTable + "." + pCondition.name;
pCondition.value = (pCondition.key || pCondition.value);
var condition = _getCondition.call(pCondition);
if (pOperator == "AND")
this.andPrepare(pCondition.name, pCondition.value, condition);
else if (pOperator == "OR")
this.orPrepare(pCondition.name, pCondition.value, condition);
}
else if (pCondition.type == "group")
{
let subCondition = new SqlCondition();
let operator = pCondition.operator;
pCondition.childs.forEach(function (cond)
{
_addCondition.call(subCondition, cond, operator);
});
if (pOperator == "AND")
this.andSqlCondition(subCondition);
else if (pOperator == "OR")
this.orSqlCondition(subCondition);
}
}
//returns the condition depending on the operator and
//adds wildcards to the value if necessary
function _getCondition ()
{
switch (this.operator)
{
case "CONTAINS":
this.value = "%" + this.value + "%";
return "# like ?";
case "CONTAINSNOT":
this.value = "%" + this.value + "%";
return "# not like ?";
case "STARTSWITH":
this.value = this.value + "%";
return "# like ?";
case "ENDSWITH":
this.value = "%" + this.value;
return "# like ?";
case "EQUAL":
return "# = ?";
case "NOT_EQUAL":
return "# != ?";
case "LESS":
return "# < ?";
case "LESS_OR_EQUAL":
return "# <= ?";
case "GREATER":
return "# > ?";
case "GREATER_OR_EQUAL":
return "# >= ?";
case "ISNULL":
return "# is null";
case "ISNOTNULL":
return "# is not null";
}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment