001package com.identityworksllc.iiq.common;
002
003import org.apache.commons.logging.Log;
004import org.apache.commons.logging.LogFactory;
005import sailpoint.api.SailPointContext;
006import sailpoint.object.ApprovalItem;
007import sailpoint.object.ApprovalSet;
008import sailpoint.object.Comment;
009import sailpoint.object.Filter;
010import sailpoint.object.Workflow;
011import sailpoint.tools.Util;
012
013import java.util.ArrayList;
014import java.util.List;
015import java.util.function.Predicate;
016
017/**
018 * Utility class for dealing with Approvals, ApprovalSets, and ApprovalItems
019 */
020public class AccessRequestUtilities extends AbstractBaseUtility {
021    public static final String DEFAULT_APPROVER = "spadmin";
022    public static final String VAR_APPROVAL_SET = "approvalSet";
023    public static final String VAR_TEMP_APPROVAL_SET = "_tempSplitApprovalSet";
024    private static final Log log = LogFactory.getLog(AccessRequestUtilities.class);
025
026    public AccessRequestUtilities(SailPointContext context) {
027        super(context);
028    }
029
030    public void autoApprove(ApprovalSet set, Predicate<ApprovalItem> approvalFilter, String comment) {
031        if (set != null) {
032            for(ApprovalItem item : Util.safeIterable(set.getItems())) {
033                if (approvalFilter == null || approvalFilter.test(item)) {
034                    item.approve();
035                    item.setApprover(DEFAULT_APPROVER);
036                    if (Util.isNotNullOrEmpty(comment)) {
037                        item.add(new Comment(comment, DEFAULT_APPROVER));
038                    }
039                }
040            }
041        }
042    }
043
044    public void divideApprovalSet(Workflow workflow, Filter excludedItemMatcher) {
045        divideApprovalSet(workflow, Functions.matches(excludedItemMatcher, ApprovalItem.class));
046    }
047
048    public void divideApprovalSet(Workflow workflow, Predicate<ApprovalItem> excludedItemMatcher) {
049        ApprovalSet inputSet = (ApprovalSet) workflow.get(VAR_APPROVAL_SET);
050        if (inputSet == null) {
051            log.debug("divideApprovalSet() called with null approval set");
052            return;
053        }
054
055        List<ApprovalItem> removedItems = new ArrayList<>();
056        for(ApprovalItem item : Util.safeIterable(inputSet.getItems())) {
057            if (excludedItemMatcher != null && excludedItemMatcher.test(item)) {
058                removedItems.add(item);
059            }
060        }
061
062        if (!Util.isEmpty(removedItems)) {
063            ApprovalSet temporarySet = new ApprovalSet();
064            temporarySet.setItems(removedItems);
065
066            workflow.put(VAR_TEMP_APPROVAL_SET, temporarySet);
067
068            for(ApprovalItem item : removedItems) {
069                inputSet.remove(item);
070            }
071        } else {
072            workflow.put(VAR_TEMP_APPROVAL_SET, null);
073        }
074    }
075
076    public void mergeDividedApprovalSet(ApprovalSet approvalSet, ApprovalSet temporarySet, boolean autoApprove, String comment) {
077        if (temporarySet == null || temporarySet.isEmpty()) {
078            log.debug("Called mergeDividedApprovalSet() with a null or empty temporary set");
079        } else {
080            for (ApprovalItem item : Util.safeIterable(temporarySet.getItems())) {
081                if (!item.isRejected()) {
082                    if (autoApprove) {
083                        item.approve();
084                        if (Util.isNotNullOrEmpty(comment)) {
085                            item.add(new Comment(comment, DEFAULT_APPROVER));
086                        }
087                        item.setApprover(DEFAULT_APPROVER);
088                    } else {
089                        if (Util.isNotNullOrEmpty(comment)) {
090                            item.add(new Comment(comment, DEFAULT_APPROVER));
091                        }
092                    }
093                }
094                approvalSet.add(item);
095            }
096        }
097    }
098
099    public void mergeDividedApprovalSet(ApprovalSet approvalSet, ApprovalSet temporarySet) {
100        mergeDividedApprovalSet(approvalSet, temporarySet, true, null);
101    }
102
103    public void mergeDividedApprovalSet(ApprovalSet approvalSet, ApprovalSet temporarySet, boolean autoApprove) {
104        mergeDividedApprovalSet(approvalSet, temporarySet, autoApprove, null);
105    }
106}