Skip to content
Snippets Groups Projects
affectedIds.js 2.49 KiB
import("system.db");
import("system.result");
import("system.vars");
import("IndexSearch_lib");
import("Sql_lib");

var infoContainer, onUpdFn, tableName, res, action;

tableName = vars.get("$local.table");
idValue = vars.get("$local.idvalue");
action = vars.get("$local.action");

infoContainer =  IndexsearchUtils.createAffectedInfoContainer(idValue, null, action
    ,function (){return vars.get("$local.columns")}
    ,function (){return vars.get("$local.oldvalues")}
    ,function (){return vars.get("$local.values")});

switch (tableName)
{
    case "CONTACT":
        res = [idValue];
        break;    
    case "ORGANISATION":
        //if an organisation is created or deleted there is no need to do this for the ORGANISATION and CONTACT-table twice, since everytime a 
        //entry in ORGANISATION is done´, it is also done in CONTACT which is handled in the switch case above => skip then the ORGANISATION-part
        //(also on delete the record does not exist anymore)
        if (action == "U") 
        {
            res = newSelect("CONTACT.CONTACTID")
                .from("CONTACT")
                .where("CONTACT.PERSON_ID is null")
                .and("CONTACT.ORGANISATION_ID", idValue)
                .arrayColumn();
        }
        break;
    case "ADDRESS":
        //do not skip here if it's a delete action since it could be that only an address is removed and not a whole record-set of 
        //organisation, address, communication, etc.
        res = IndexsearchUtils.getAffectedIdValues("CONTACT_ID", infoContainer, function (id){
            return newSelect("ADDRESS.CONTACT_ID")
                    .from("ADDRESS")
                    .where("ADDRESS.ADDRESSID", id)
                    .arrayColumn();
        });
        break;
    case "COMMUNICATION":
        //do not skip here if it's a delete action since it could be that only a communication-entry is removed and not a whole record-set of 
        //organisation, address, communication, etc.
        res = IndexsearchUtils.getAffectedIdValues("CONTACT_ID", infoContainer, function (id){
            return newSelect("COMMUNICATION.CONTACT_ID")
                    .from("COMMUNICATION")
                    .where("COMMUNICATION.COMMUNICATIONID", id)
                    .arrayColumn();
        });
        break;
}

//needed, because the complete index (fullIndexer) is being rebuilt if nothing gets returned here
if (res)
    result.object(res);
else
    result.object([]);