<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE Rule PUBLIC "sailpoint.dtd" "sailpoint.dtd">
<Rule name="EnterpriseHRManagerCorrelation" type="ManagerCorrelation">
  <Description>
    Resolves manager correlation for authoritative sources with multiple accounts correlated to one identity:
    1. Prioritizes MANAGER_ID from an active Employee account (ACTIVE == "1" and WORKER_TYPE == "Employee").
    2. Falls back to MANAGER_ID from any active account (ACTIVE == "1").
    3. Falls back to MANAGER_ID from an inactive/terminated account (ACTIVE == "0") only if no active account exists.
    Matches the resolved MANAGER_ID against the manager identity's identificationNumber attribute.
  </Description>
  <Source><![CDATA[
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import sailpoint.rule.Account;
import sailpoint.rule.Identity;
import org.apache.commons.lang.StringUtils;

Map returnMap = new HashMap();

String currentActive = null;
String currentWorkerType = null;
String currentManagerId = null;
String immutablePersonId = null;
String workEmail = null;
String workerId = null;

if (link != null) {
    Object activeObj = link.getAttribute("ACTIVE");
    if (activeObj != null) {
        currentActive = String.valueOf(activeObj).trim();
    }

    Object typeObj = link.getAttribute("WORKER_TYPE");
    if (typeObj != null) {
        currentWorkerType = String.valueOf(typeObj).trim();
    }

    Object mgrObj = link.getAttribute("MANAGER_ID");
    if (mgrObj != null) {
        currentManagerId = String.valueOf(mgrObj).trim();
    }

    Object upidObj = link.getAttribute("IMMUTABLE_PERSON_ID");
    if (upidObj != null) {
        immutablePersonId = String.valueOf(upidObj).trim();
    }

    Object emailObj = link.getAttribute("EMAIL_ADDRESS_WORK");
    if (emailObj != null) {
        workEmail = String.valueOf(emailObj).trim();
    }

    Object widObj = link.getAttribute("WORKER_ID");
    if (widObj != null) {
        workerId = String.valueOf(widObj).trim();
    } else if (link.getNativeIdentity() != null) {
        workerId = link.getNativeIdentity().trim();
    }
}

String resolvedManagerId = null;

// 1. Fast path: An active Employee row (ACTIVE == "1" and WORKER_TYPE == "Employee") wins immediately.
if ("1".equals(currentActive) && "Employee".equalsIgnoreCase(currentWorkerType) && StringUtils.isNotBlank(currentManagerId)) {
    resolvedManagerId = currentManagerId;
} else {
    // 2. The current row is inactive (ACTIVE == "0") or a Contingent row that may overlap with an active Employee row.
    //    Look up the owning identity and check all authoritative accounts before choosing a manager.
    Identity ownerIdentity = null;

    if (StringUtils.isNotBlank(immutablePersonId)) {
        List upidMatches = idn.findIdentitiesBySearchableIdentityAttribute("universalPersonId", "Equals", immutablePersonId, "universalPersonId");
        if (upidMatches != null && !upidMatches.isEmpty()) {
            ownerIdentity = (Identity) upidMatches.get(0);
        }
    }

    if (ownerIdentity == null && StringUtils.isNotBlank(workEmail)) {
        List emailMatches = idn.findIdentitiesBySearchableIdentityAttribute("email", "Equals", workEmail, "email");
        if (emailMatches != null && !emailMatches.isEmpty()) {
            ownerIdentity = (Identity) emailMatches.get(0);
        }
    }

    if (ownerIdentity == null && StringUtils.isNotBlank(workerId)) {
        List idMatches = idn.findIdentitiesBySearchableIdentityAttribute("identificationNumber", "Equals", workerId, "identificationNumber");
        if (idMatches != null && !idMatches.isEmpty()) {
            ownerIdentity = (Identity) idMatches.get(0);
        }
    }

    if (ownerIdentity != null && StringUtils.isNotBlank(ownerIdentity.getName())) {
        String appName = (link != null && StringUtils.isNotBlank(link.getApplicationName()))
            ? link.getApplicationName()
            : "Enterprise HR [source]";

        List hrAccounts = idn.getAllAccounts(appName, ownerIdentity.getName());
        String activeEmployeeMgrId = null;
        String activeFallbackMgrId = null;
        String inactiveAcctMgrId = null;

        if (hrAccounts != null && !hrAccounts.isEmpty()) {
            for (Account acct : hrAccounts) {
                if (acct != null) {
                    String acctActive = idn.getAccountAttribute(acct, "ACTIVE");
                    String acctWorkerType = idn.getAccountAttribute(acct, "WORKER_TYPE");
                    String acctMgrId = idn.getAccountAttribute(acct, "MANAGER_ID");

                    if (StringUtils.isNotBlank(acctMgrId) && !"NULL".equalsIgnoreCase(acctMgrId.trim())) {
                        String trimmedMgrId = acctMgrId.trim();
                        if ("1".equals(acctActive) && "Employee".equalsIgnoreCase(acctWorkerType) && StringUtils.isBlank(activeEmployeeMgrId)) {
                            activeEmployeeMgrId = trimmedMgrId;
                        } else if ("1".equals(acctActive) && StringUtils.isBlank(activeFallbackMgrId)) {
                            activeFallbackMgrId = trimmedMgrId;
                        } else if ("0".equals(acctActive) && StringUtils.isBlank(inactiveAcctMgrId)) {
                            inactiveAcctMgrId = trimmedMgrId;
                        }
                    }
                }
            }
        }

        if (StringUtils.isNotBlank(activeEmployeeMgrId)) {
            resolvedManagerId = activeEmployeeMgrId;
        } else if (StringUtils.isNotBlank(activeFallbackMgrId)) {
            resolvedManagerId = activeFallbackMgrId;
        } else if (StringUtils.isNotBlank(inactiveAcctMgrId)) {
            resolvedManagerId = inactiveAcctMgrId;
        } else {
            resolvedManagerId = currentManagerId;
        }
    } else {
        // First-time aggregation before the identity cube exists
        resolvedManagerId = currentManagerId;
    }
}

if (StringUtils.isNotBlank(resolvedManagerId) && !"NULL".equalsIgnoreCase(resolvedManagerId)) {
    returnMap.put("identityAttributeName", "identificationNumber");
    returnMap.put("identityAttributeValue", resolvedManagerId);
}

return returnMap;
  ]]></Source>
</Rule>
