diff --git a/process/Contact_lib/process.js b/process/Contact_lib/process.js index 7bbafdc693adac2eb70bf75da3d35f24790c6560..4a59839543915bbe0c16edfb2c56985efe935f15 100644 --- a/process/Contact_lib/process.js +++ b/process/Contact_lib/process.js @@ -528,10 +528,8 @@ ContactUtils.hasCommRestriction = function(pContactId, pMedium, pStartDate) */ ContactUtils.getActiveCommRestrictionsSubselect = function() { - var mediumList = KeywordUtils.getEntryNamesAndIdsByContainer($KeywordRegistry.communicationMediumCampaign()); - var sqlMasking = new SqlMaskingUtils(); - - var parts = []; + var mediumList = KeywordUtils.getResolvedTitleSqlPart($KeywordRegistry.communicationMediumCampaign(), "COMMRESTRICTION.MEDIUM"); + var mask = new SqlMaskingUtils() var orgContactSubselect = newSelect("orgContact.CONTACTID") .from("CONTACT anyContact") @@ -539,20 +537,19 @@ ContactUtils.getActiveCommRestrictionsSubselect = function() .and("orgContact.PERSON_ID IS NULL")) .where("anyContact.CONTACTID = CONTACT.CONTACTID") - mediumList.forEach(function(pMedium) + var group = mask.getGroupConcat("DISTINCT "+mediumList, "', '"); + var subselect = "''"; + if(group) { - var subquery = newSelect("COMMRESTRICTION.MEDIUM, COMMRESTRICTIONID") - .from("COMMRESTRICTION") - .where("COMMRESTRICTION.MEDIUM", pMedium[0]) - .and("COMMRESTRICTION.STARTDATE", vars.get("$sys.date"), SqlBuilder.LESS_OR_EQUAL()) - .and(newWhere() - .or("COMMRESTRICTION.CONTACT_ID = CONTACT.CONTACTID") - .or("COMMRESTRICTION.CONTACT_ID", orgContactSubselect)); - //!SqlBuilder - parts.push("case when exists(" + subquery.toString() + ") then '" + pMedium[1] + "' else '' end"); - }) - - return sqlMasking.concatWithSeparator(parts, " ", false); + var res = newSelect(group).from("COMMRESTRICTION") + .where("COMMRESTRICTION.CONTACT_ID = CONTACT.CONTACTID") + + var subres = newSelect(group).from("COMMRESTRICTION") + .where("COMMRESTRICTION.CONTACT_ID in ( "+orgContactSubselect.toString()+")") + + subselect = mask.concatWithSeparator(["("+res.toString()+")", "("+subres.toString()+")"], ", ", false); + } + return subselect; } /** diff --git a/process/Sql_lib/process.js b/process/Sql_lib/process.js index eecd2a9c83628dd909a5c182d53c53d38963a53b..8c981ecd4db360cd95a00663a447f1ebf3f8be5c 100644 --- a/process/Sql_lib/process.js +++ b/process/Sql_lib/process.js @@ -3292,6 +3292,45 @@ SqlMaskingUtils.prototype.getConcatSymbol = function() } } + +/** +* Returns the group_concat function, which groups <br> +* multiple Row Values into one Cell. Note: This function <br> +* does not work on Derby<br> +* +* @param {String} pField <p> +* Expression that shall be grouped.<br> +* @param {String} pSeperator <p> +* Character that shall be used as Seperator<br> +* @return {String} <p> +* Returns the field with groupConcat wrapped around<br> +*/ +SqlMaskingUtils.prototype.getGroupConcat = function(pField, pSeperator) +{ + var group; + if(pField == null || pSeperator == null || pField == null && pSeperator == null) + throw new Error(translate.withArguments("Field or Seperator were empty function: %0", ["SqlMaskingUtils.prototype.getGroupConcat"])); + + switch(this.dbType) + { + case db.DBTYPE_MARIADB10: + case db.DBTYPE_MYSQL4: + group = " GROUP_CONCAT("+pField+" SEPARATOR "+pSeperator+")"; + break; + case db.DBTYPE_ORACLE10_CLUSTER: + case db.DBTYPE_ORACLE10_THIN: + case db.DBTYPE_ORACLE10_OCI: + case db.DBTYPE_POSTGRESQL8: + case db.DBTYPE_SQLSERVER2000: + group = " STRING_AGG("+pField+", "+pSeperator+")"; + break; + case db.DBTYPE_DERBY10: + logging.log(translate.withArguments("${SQL_LIB_UNSUPPORTED_DBTYPE} function: %0", ["SqlMaskingUtils.prototype.getGroupConcat"]), logging.ERROR); + break; + } + return group; +} + /** * Returns the trim function, which removes the<br> * leading and trailing spaces in a string, depending<br>