From b877de6f16d8563945a84caca9a69728acab64d6 Mon Sep 17 00:00:00 2001 From: "j.goderbauer" <j.goderbauer@adito.de> Date: Mon, 1 Apr 2019 14:56:08 +0200 Subject: [PATCH] refactoring Context_lib (2) --- process/Context_lib/process.js | 133 +++++++++++++++++++++++++-------- process/Proto_lib/process.js | 15 ++++ 2 files changed, 115 insertions(+), 33 deletions(-) diff --git a/process/Context_lib/process.js b/process/Context_lib/process.js index 82a5ac3b5b..1abab8d76e 100644 --- a/process/Context_lib/process.js +++ b/process/Context_lib/process.js @@ -112,44 +112,109 @@ ContextUtils._contextDataMapping = function(pContext) return [pContext[0], contextName, (pContext[1] ? pContext[1] : contextName)]; } -function ContextSelector(pSqlNameSelect, pTableName, pIdField) +/** + * represents a single context selection for one context + * this is usefull for objectlinks and 360° definition + * most properties are read only and can only be written with a setter + * + * @param {String} [pTableName] presets the matching property of the object + * @param {String} [pIdField] presets the matching property of the object + * @param {String} [pTitleExpression] presets the matching property of the object + * + * TODO: mostly temporary function until you can get fields from another Entity + * + * @class + */ +function ContextSelector(pTableName, pIdField, pTitleExpression) { - //the >>this.property = null;<< is for autocomplete in the designer - this.sqlNameSelect = null; ProtoPropertyUtils.makeSemiReadOnly(this, "sqlNameSelect"); - this.setSqlNameSelect(pSqlNameSelect); + //the >>this.propertyX = null;<< is for autocomplete in the designer + + /** + * title-definition; db-column or another sql-expression (like concating fields) as long as it returns one field + * read-only property; set it with a matching setter + * @property + */ + this.titleExpression = null; ProtoPropertyUtils.makeSemiReadOnly(this, "titleExpression"); + this.setTitleExpression(pTitleExpression); + /** + * name of the database-table + * read-only property; set it with a matching setter + * @property + */ this.tableName = null; ProtoPropertyUtils.makeSemiReadOnly(this, "tableName"); this.setTableName(pTableName); + /** + * db-field for the ID of one record (UID of matching context) + * read-only property; set it with a matching setter + * @property + */ this.idField = null; ProtoPropertyUtils.makeSemiReadOnly(this, "idField"); this.setIdField(pIdField); - this.sqlJoin = null; ProtoPropertyUtils.makeSemiReadOnly(this, "sqlJoin"); + /** + * expression for additional joins to be made (addotopmaö pt table-name) + * read-only property; set it with a matching setter + * @property + */ + this.joinExpression = null; ProtoPropertyUtils.makeSemiReadOnly(this, "joinExpression"); + /** + * db-field for the ID of the relation to a CONTACT-record + * read-only property; set it with a matching setter + * @property + */ this.contactIdField = null; ProtoPropertyUtils.makeSemiReadOnly(this, "contactIdField"); + /** + * db-field that represents the date of creation + * read-only property; set it with a matching setter + * @property + */ this.creationDateField = null; ProtoPropertyUtils.makeSemiReadOnly(this, "creationDateField"); + /** + * db-field where the STATE-information (active/inactive) is stored (see the activeStates-property) + * read-only property; set it with a matching setter + * @property + */ this.stateField = null; ProtoPropertyUtils.makeSemiReadOnly(this, "stateField"); + /** + * array that contains IDs of states that represent an "active"-state + * read-only property; set it with a matching setter + * @property + */ this.activeStates = null; ProtoPropertyUtils.makeSemiReadOnly(this, "activeStates"); } -ContextSelector.create = function(pSqlNameSelect, pTableName, pIdField) +/** + * creates a new instance of a ContextSelector and returns it + * if given it also sets some properties (property names with matching function-parameters) + * @static + */ +ContextSelector.create = function(pTableName, pIdField, pTitleExpression) { - return new ContextSelector(pSqlNameSelect, pTableName, pIdField); + return new ContextSelector(pTableName, pIdField, pTitleExpression); }; +/** + * @return {String} full id field containing tablename and column + */ ContextSelector.prototype.getFullIdField = function() { return this.tableName + "." + this.idField; }; - +/** + * @return {String} full from-expression with tablename and join-part + */ ContextSelector.prototype.getFullFromClause = function() { - if (this.sqlJoin) - return " " + this.tableName + " " + this.sqlJoin + " "; + if (this.joinExpression) + return " " + this.tableName + " " + this.joinExpression + " "; else return this.tableName; }; -ContextSelector.prototype.setSqlNameSelect = function(pValue) +//setters which to nothing special; no need to document them +ContextSelector.prototype.setTitleExpression = function(pValue) { - this._sqlNameSelect = pValue; + this._titleExpression = pValue; return this; }; ContextSelector.prototype.setTableName = function(pValue) @@ -162,9 +227,9 @@ ContextSelector.prototype.setIdField = function(pValue) this._idField = pValue; return this; }; -ContextSelector.prototype.setSqlJoin = function(pValue) +ContextSelector.prototype.setJoinExpression = function(pValue) { - this._sqlJoin = pValue; + this._joinExpression = pValue; return this; }; ContextSelector.prototype.setContactIdField = function(pValue) @@ -187,6 +252,7 @@ ContextSelector.prototype.setActiveStates = function(pValue) this._activeStates = pValue; return this; }; + /** * TODO: !!!temporary function until you can get fields from another Entity!!! */ @@ -194,13 +260,14 @@ ContextUtils.getSelectMap = function() { var maskingUtils = new SqlMaskingUtils(); return { - "Organisation": ContextSelector.create("NAME", "ORGANISATION", "ORGANISATIONID") - ,"Person": ContextSelector.create(null, "PERSON", "CONTACTID") - .setSqlNameSelect(new ContactTitleRenderer(Contact.createWithColumnPreset()).asSql()) - .setSqlJoin("join CONTACT on PERSON.PERSONID = CONTACT.PERSON_ID join ORGANISATION on ORGANISATION.ORGANISATIONID = CONTACT.ORGANISATION_ID") - ,"Activity": ContextSelector.create("SUBJECT", "ACTIVITY", "ACTIVITYID") - ,"Salesproject": ContextSelector.create(null, "SALESPROJECT", "SALESPROJECTID") - .setSqlNameSelect(maskingUtils.concat([ + "Organisation": ContextSelector.create("ORGANISATION", "ORGANISATIONID", "NAME") + ,"Person": ContextSelector.create("CONTACT", "CONTACTID") + .setTitleExpression(new ContactTitleRenderer(Contact.createWithColumnPreset()).asSql()) + .setJoinExpression("join PERSON on PERSON.PERSONID = CONTACT.PERSON_ID \n\ + join ORGANISATION on ORGANISATION.ORGANISATIONID = CONTACT.ORGANISATION_ID") + ,"Activity": ContextSelector.create("ACTIVITY", "ACTIVITYID", "SUBJECT") + ,"Salesproject": ContextSelector.create("SALESPROJECT", "SALESPROJECTID") + .setTitleExpression(maskingUtils.concat([ "'" + translate.text("Salesproject") + "'", "' '", maskingUtils.cast("PROJECTCODE", SQLTYPES.VARCHAR, 10), @@ -211,8 +278,8 @@ ContextUtils.getSelectMap = function() .setCreationDateField("STARTDATE") .setStateField("STATE") .setActiveStates(["25b0ac77-ef92-4809-802e-bb9d8782f865", "23d38486-4cce-41ce-a8df-164ad44df706"]) - ,"Contract": ContextSelector.create(null, "CONTRACT", "CONTRACTID") - .setSqlNameSelect(maskingUtils.concat([ + ,"Contract": ContextSelector.create("CONTRACT", "CONTRACTID") + .setTitleExpression(maskingUtils.concat([ KeywordUtils.getResolvedTitleSqlPart("ContractType", "CONTRACTTYPE"), maskingUtils.cast("CONTRACTCODE", SQLTYPES.VARCHAR, 10) ], " ")) @@ -220,8 +287,8 @@ ContextUtils.getSelectMap = function() .setCreationDateField("CONTRACTSTART") .setStateField("CONTRACTSTATUS") .setActiveStates(["e12d37e9-3429-40b5-973b-c1569843ca46", "3579eb0c-d8ca-4b6b-85ee-f1800a9301eb", "4c63c82d-0276-4c12-9937-13fd361ad786"]) - ,"Offer": ContextSelector.create(null, "OFFER", "OFFERID") - .setSqlNameSelect(maskingUtils.concat([ + ,"Offer": ContextSelector.create("OFFER", "OFFERID") + .setTitleExpression(maskingUtils.concat([ "'" + translate.text("Offer") + "'", "' '", maskingUtils.cast("OFFERCODE", SQLTYPES.VARCHAR, 10), @@ -231,8 +298,8 @@ ContextUtils.getSelectMap = function() .setContactIdField("CONTACT_ID") .setStateField("STATUS") .setActiveStates(["5134153d-2e18-452f-ab35-7a52f1aee7d1", "e5d6b5a4-7576-440f-8332-bc40147c0335"]) - ,"Order": ContextSelector.create(null, "SALESORDER", "") - .setSqlNameSelect(maskingUtils.concat([ + ,"Order": ContextSelector.create("SALESORDER", "SALESORDERID") + .setTitleExpression(maskingUtils.concat([ "'" + translate.text("Order") + "'", "' '", maskingUtils.cast("SALESORDERCODE", SQLTYPES.VARCHAR, 10), @@ -241,13 +308,13 @@ ContextUtils.getSelectMap = function() ], "", false)) .setContactIdField("CONTACT_ID") .setCreationDateField("ORDERDATE") - ,"Product": ContextSelector.create(null, "PRODUCT", "PRODUCTID") - .setSqlNameSelect(maskingUtils.concat([ + ,"Product": ContextSelector.create("PRODUCT", "PRODUCTID") + .setTitleExpression(maskingUtils.concat([ "PRODUCTCODE", "' | '", "PRODUCTNAME" ], "", false)) - ,"Task": ContextSelector.create("SUBJECT", "TASK", "TASKID") + ,"Task": ContextSelector.create("TASK", "TASKID", "SUBJECT") .setContactIdField(translate.text("Task"))//wait, what? } } @@ -262,7 +329,7 @@ ContextUtils.getNameSubselectSql = function(pContextIdDbField, pRowIdDbField) var selectMap = ContextUtils.getSelectMap () for (let contextId in selectMap) { - select += "when '" + contextId + "' then (select " + selectMap[contextId].sqlNameSelect + " from " + selectMap[contextId].getFullFromClause() + (pRowIdDbField ? " where " + selectMap[contextId].idField + " = " + pRowIdDbField : " ") + ") "; + select += "when '" + contextId + "' then (select " + selectMap[contextId].titleExpression + " from " + selectMap[contextId].getFullFromClause() + (pRowIdDbField ? " where " + selectMap[contextId].idField + " = " + pRowIdDbField : " ") + ") "; } select += "else 'Not defined in ContextUtils.getNameSql()!'"; @@ -279,7 +346,7 @@ ContextUtils.getNameSql = function(pContextId, pRowId) var selectMap = ContextUtils.getSelectMap () if (selectMap[pContextId] != undefined) { - return SqlCondition.begin().andPrepare(selectMap[pContextId].getFullIdField(), pRowId).buildSql("select " + selectMap[pContextId].sqlNameSelect + " from " + selectMap[pContextId].getFullFromClause(), "1 = 2"); + return SqlCondition.begin().andPrepare(selectMap[pContextId].getFullIdField(), pRowId).buildSql("select " + selectMap[pContextId].titleExpression + " from " + selectMap[pContextId].getFullFromClause(), "1 = 2"); } else return "select 1 from person where 1=2"; @@ -321,5 +388,5 @@ ContextUtils.getContextDataSql = function(pContextId, pRowId, pWithDate, pActive if (pWithState === true) stateColumn = ", " + (selectMap[pContextId].stateField || "''"); - return cond.buildSql("select " + selectMap[pContextId].getFullIdField() + ", " + selectMap[pContextId].sqlNameSelect + dateColumn + stateColumn + " from " + selectMap[pContextId].getFullFromClause(), "1=1"); + return cond.buildSql("select " + selectMap[pContextId].getFullIdField() + ", " + selectMap[pContextId].titleExpression + dateColumn + stateColumn + " from " + selectMap[pContextId].getFullFromClause(), "1=1"); } diff --git a/process/Proto_lib/process.js b/process/Proto_lib/process.js index 04d65e8fcb..a13e079380 100644 --- a/process/Proto_lib/process.js +++ b/process/Proto_lib/process.js @@ -1,5 +1,20 @@ +/** + * Methods to manage properties of objects that can be instanciated + * Do not create an instance of this itself! + * + * @class + */ function ProtoPropertyUtils(){} +/** + * makes an property of a object semi read-only + * this means a property with a undersocre-prefix "_" holds the actual value and the origin prop cannot be written + * + * @param {Object} pObj the object that holds the property + * @param {String} pPropName name of the property within pObj to mark as readonly + * + * @return void + */ ProtoPropertyUtils.makeSemiReadOnly = function(pObj, pPropName) { Object.defineProperty(pObj, pPropName, { -- GitLab