Skip to content
Snippets Groups Projects
process.js 6.72 KiB
import("Util_lib");
import("JditoFilter_lib");
import("system.vars");
import("Sql_lib");
import("KeywordRegistry_basic");

/**
 * Object for building communication settings sql conditions.
 */
function CommunicationSettingsCondition ()
{
    this._contactIdSql = "CONTACT.CONTACTID";
    this._contactId = null;
    this._channelType = null;
    this._medium = null;
    this._channelIdSql = null;
    this._settingStatus = null;
    this._existsOperator = SqlBuilder.EXISTS();
    this._useAnyMedium = false;
}

/**
 * Sets a sql expression for the contact id
 * 
 * @param {String|SqlBuilder} pContactIdSql     sql field or expression to get the contactId
 * @return {CommunicationSettingsCondition}     current object
 */
CommunicationSettingsCondition.prototype.contactIdField = function (pContactIdSql)
{
    this._contactIdSql = pContactIdSql;
    this._contactId = null;
    return this;
}

/**
 * Sets the contactId to get the communication settings from
 * 
 * @param {String} pContactId                   contact id
 * @return {CommunicationSettingsCondition}     current object
 */
CommunicationSettingsCondition.prototype.contactId = function (pContactId)
{
    this._contactId = pContactId;
    this._contactIdSql = null;
    return this;
}

/**
 * Makes the condition yield true if no communication setting exists
 * 
 * @return {CommunicationSettingsCondition}     current object
 */
CommunicationSettingsCondition.prototype.existNoSettings = function ()
{
    this._existsOperator = SqlBuilder.NOT_EXISTS();
    return this;
}

/**
 * Makes the condition yield true if at least one communication setting exists
 * 
 * @return {CommunicationSettingsCondition}     current object
 */
CommunicationSettingsCondition.prototype.existSettings = function ()
{
    this._existsOperator = SqlBuilder.EXISTS();
    return this;
}

/**
 * Sets the medium and the source of the communication address for filtering the communication settings.
 * 
 * @param {String} pMedium      communication medium
 * @param {String} pAddressSql  sql field or expression for the communication address
 * @return {CommunicationSettingsCondition} current object
 */
CommunicationSettingsCondition.prototype.medium = function (pMedium, pAddressSql)
{
    this._channelType = $KeywordRegistry.communicationChannelType$communication();
    this._medium = pMedium;
    if (pAddressSql)
        this._channelIdSql = "( "+ pAddressSql + " )";
    return this;
}

/**
 * Sets the medium to email and the source of the email address for filtering the communication settings.
 * 
 * @param {String} pAddressSql  sql field or expression for the email address
 * @return {CommunicationSettingsCondition} current object
 */
CommunicationSettingsCondition.prototype.emails = function (pAddressSql)
{
    return this.medium($KeywordRegistry.communicationMediumCampaign$mail(), pAddressSql);
}

/**
 * Sets the source of the address to filter the communication settings by address.
 * 
 * @param {String} pAddressSql  sql field or expression for the address id
 * @return {CommunicationSettingsCondition} current object
 */
CommunicationSettingsCondition.prototype.address = function (pAddressSql)
{
    this._channelType = $KeywordRegistry.communicationChannelType$address();
    if (pAddressSql)
    {
        this._channelIdSql = "( "+ pAddressSql + " )";
    }
    return this;
}

/**
 * Only fetch communication settings with the status 'rejected'
 * 
 * @return {CommunicationSettingsCondition} current object
 */
CommunicationSettingsCondition.prototype.rejected = function ()
{
    this._settingStatus = $KeywordRegistry.communicationSettingStatus$rejected();
    return this;
}

/**
 * Only fetch communication settings with the status 'allowed'
 * 
 * @return {CommunicationSettingsCondition} current object
 */
CommunicationSettingsCondition.prototype.allowed = function ()
{
    this._settingStatus = $KeywordRegistry.communicationSettingStatus$allowed();
    return this;
}

/**
 * Only fetch communication settings with the status 'pending'
 * 
 * @return {CommunicationSettingsCondition} current object
 */
CommunicationSettingsCondition.prototype.pending = function ()
{
    this._settingStatus = $KeywordRegistry.communicationSettingStatus$pending();
    return this;
}

/**
 * Only fetch settings with the given status
 * 
 * @param {String} pStatus                      staus that the communication settings need to have
 * @return {CommunicationSettingsCondition}     current object
 */
CommunicationSettingsCondition.prototype.status = function (pStatus)
{
    this._settingStatus = pStatus;
    return this;
}

/**
 * Fetch communication settings of any kind, regardless of medium or channel.
 * 
 * @return {CommunicationSettingsCondition}     current object
 */
CommunicationSettingsCondition.prototype.anyMedium = function ()
{
    this._useAnyMedium = true;
    this._medium = null;
    this._channelType = null;
    this._channelIdSql = null;
    return this;
}

/**
 * Builds a sub select for fetching the communication settings with the properies as configured.
 * 
 * @param {String|Array} [pSelectFields=COMMUNICATIONSETTINGSID]  columns to select
 * @return {SqlBuilder} SqlBuilder object with the full sql select
 */
CommunicationSettingsCondition.prototype.buildSelect = function (pSelectFields)
{
    if (pSelectFields == undefined)
        pSelectFields = "COMMUNICATIONSETTINGS.COMMUNICATIONSETTINGSID";
    var sql = newSelect(pSelectFields)
        .from("COMMUNICATIONSETTINGS")
        .whereIfSet("COMMUNICATIONSETTINGS.STATUS", this._settingStatus);
    
    if (this._contactIdSql)
    {
        sql.and("COMMUNICATIONSETTINGS.CONTACT_ID = " + this._contactIdSql)
    }
    else if (this._contactId)
    {
        sql.and("COMMUNICATIONSETTINGS.CONTACT_ID", this._contactId);
    }
    
    if (!this._useAnyMedium)
    {
        var channelCondition = newWhere("COMMUNICATIONSETTINGS.CHANNEL_TYPE", $KeywordRegistry.communicationChannelType$global());
        if (this._channelType)
        {
            var specificChannelCondition = newWhere("COMMUNICATIONSETTINGS.CHANNEL_TYPE", this._channelType)
                .andIfSet("COMMUNICATIONSETTINGS.MEDIUM", this._medium);
            if (this._channelIdSql)
            {
                specificChannelCondition.and(newWhere("COMMUNICATIONSETTINGS.CHANNEL_ID = " + this._channelIdSql)
                    .or("COMMUNICATIONSETTINGS.CHANNEL_ID is null"));
            }
            channelCondition.or(specificChannelCondition);
        }
        sql.and(channelCondition);
    }
    
    return sql;
}

/**
 * Builds a sql condition for fetching only contacts with communication settings that have the configured properties.
 * 
 * @return {SqlBuilder} sql condition
 */
CommunicationSettingsCondition.prototype.buildCondition = function ()
{
    return newWhere(null, this.buildSelect(), this._existsOperator);
}