<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE Rule PUBLIC "sailpoint.dtd" "sailpoint.dtd">
<Rule language="beanshell" name="TestDelimitedFileState" type="BuildMap">
  <Description>Validates common Delimited File BuildMap use cases (IIQDisabled, column combining, row filtering) and cross-row state patterns (lookup caching, row counter, deduplication).</Description>
  <Signature returnType="Map">
    <Inputs>
      <Argument name="cols" type="java.util.List">
        <Description>Ordered list of column names from the file header.</Description>
      </Argument>
      <Argument name="record" type="java.util.List">
        <Description>Ordered list of values for the current record.</Description>
      </Argument>
      <Argument name="application" type="sailpoint.object.Application">
        <Description>Application object.</Description>
      </Argument>
      <Argument name="schema" type="sailpoint.object.Schema">
        <Description>Schema reference for the delimited file source.</Description>
      </Argument>
      <Argument name="state" type="java.util.Map">
        <Description>Shared state map persisting across rows during aggregation.</Description>
      </Argument>
    </Inputs>
    <Returns>
      <Argument name="map" type="java.util.Map">
        <Description>Map of attributes representing the processed account row.</Description>
      </Argument>
    </Returns>
  </Signature>
  <Source><![CDATA[
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import java.util.HashSet;
import sailpoint.connector.DelimitedFileConnector;

Map map = DelimitedFileConnector.defaultBuildMap(cols, record);
if (map == null || map.isEmpty()) {
    return new HashMap();
}

// 1. Common Use Case: Skip non-contractor rows by returning an empty HashMap
String empType = (String) map.get("EmployeeType");
if (!"Contractor".equalsIgnoreCase(empType)) {
    return new HashMap();
}

// 2. Common Use Case: Set IIQDisabled based on Status
String status = (String) map.get("Status");
if ("Terminated".equalsIgnoreCase(status) || "Inactive".equalsIgnoreCase(status)) {
    map.put("IIQDisabled", true);
} else {
    map.put("IIQDisabled", false);
}

// 3. Common Use Case: Combine FirstName + LastName into DisplayName
String first = (String) map.get("FirstName");
String last = (String) map.get("LastName");
if (first != null && last != null) {
    map.put("DisplayName", first.trim() + " " + last.trim());
}

// 4. Diagnostics: Inspect application object type and "file" attribute
String appClass = "null";
Object fileVal = null;
if (application != null) {
    appClass = application.getClass().getName();
    fileVal = application.getAttributeValue("file");
}
map.put("AppClass", appClass);
map.put("FileAttrValue", String.valueOf(fileVal));

// 5. Cross-Row State Patterns: Deduplication, Lookup Cache, Row Counter
if (state != null) {
    map.put("StateAvailable", "true");

    // 5a. On-the-fly deduplication using a HashSet stored in state
    String id = (String) map.get("id");
    Set seen = (Set) state.get("seenIds");
    if (seen == null) {
        seen = new HashSet();
        state.put("seenIds", seen);
    }
    if (seen.contains(id)) {
        return new HashMap();
    }
    seen.add(id);

    // 5b. One-time lookup cache stored in state
    Map deptLookup = (Map) state.get("deptLookup");
    Integer initCount = (Integer) state.get("cacheInitCount");
    if (deptLookup == null) {
        deptLookup = new HashMap();
        deptLookup.put("ENG", "Engineering");
        deptLookup.put("FIN", "Finance");
        deptLookup.put("OPS", "Operations");
        deptLookup.put("HR", "Human Resources");
        state.put("deptLookup", deptLookup);
        initCount = (initCount == null ? 1 : initCount + 1);
        state.put("cacheInitCount", initCount);
    }
    String deptCode = (String) map.get("DeptCode");
    map.put("DeptLabel", deptLookup.get(deptCode));
    map.put("CacheInitCount", String.valueOf(state.get("cacheInitCount")));

    // 5c. Row counter stored in state
    Integer count = (Integer) state.get("rowCount");
    count = (count == null ? 1 : count + 1);
    state.put("rowCount", count);
    map.put("RowNumber", String.valueOf(count));
} else {
    map.put("StateAvailable", "null");
}

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