Skip to content
Snippets Groups Projects
affectedIds.js 3.00 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 "PERSON":
        //if a contact person is created or deleted there is no need to determine the index-uid for the PERSON and CONTACT-table, since everytime a 
        //entry in PERSON is done, there is also  one mande in the CONTACT table which is handled in the switch case above 
        //=> skip then the PERSON-part (also on delete the record does not exist anymore)
        if (action == "U") 
        {
            res = newSelect("CONTACT.CONTACTID")
                .from("CONTACT")
                .where("CONTACT.PERSON_ID", idValue)
                .arrayColumn();
        }
        break;
    case "ORGANISATION":
        //an organisation can only be deleted if there are no persons left and if  an organisation is created there is no entry for person that has
        //to be indexed => only in the update-case necessary
        if (action == "U") 
        {
            res = newSelect("CONTACT.CONTACTID")
                        .from("CONTACT")
                        .where("CONTACT.ORGANISATION_ID", idValue)
                        .and("CONTACT.PERSON_ID is not null")
                        .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,
        //person, 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, person, 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([]);