001package com.identityworksllc.iiq.common.logging;
002
003import sailpoint.object.Link;
004import sailpoint.tools.Util;
005import sailpoint.object.Configuration;
006import sailpoint.object.Identity;
007import sailpoint.tools.GeneralException;
008import sailpoint.tools.VelocityUtil;
009
010import java.util.HashMap;
011import java.util.Locale;
012import java.util.Map;
013import java.util.TimeZone;
014
015/**
016 * Describes objects in a human-readable format. This is intended
017 * to be used by both IIQCommon internal code and customer code.
018 */
019public class Describer {
020    /**
021     * The logger for this class
022     */
023    private static final SLogger log = new SLogger(Describer.class);
024
025    public static String describe(Link link) {
026        Configuration sysConfig = Configuration.getSystemConfig();
027        @SuppressWarnings("unchecked")
028        Map<String, Object> describerConfig = (Map<String, Object>) sysConfig.get("IIQCommon.Describer.Config");
029
030        var defaultDisplay = "Link[application =" + link.getApplicationName() + ", nativeIdentity = " + link.getNativeIdentity() + "]";
031
032        if (Util.isEmpty(describerConfig) || !(describerConfig.get("Link") instanceof String)) {
033            return defaultDisplay;
034        } else {
035            var template = describerConfig.get("Link").toString();
036
037            Map<String, Object> model = new HashMap<>();
038            model.put("link", link);
039            model.put("nativeIdentity", link.getNativeIdentity());
040            model.put("applicationName", link.getApplicationName());
041            model.putAll(link.getAttributes());
042
043            try {
044                var result = VelocityUtil.secureRender(template, model, Locale.getDefault(), TimeZone.getDefault());
045                if (Util.isNullOrEmpty(result)) {
046                    log.warn("Describer template returned null or empty for {0}", defaultDisplay);
047                    return defaultDisplay;
048                } else {
049                    return result;
050                }
051            } catch(GeneralException e) {
052                log.error("Error rendering describer template for " + defaultDisplay, e);
053                return defaultDisplay;
054            }
055        }
056    }
057
058    /**
059     * Gets the human-readable description of the given Identity.
060     * @param identity The Identity to describe
061     * @return The human-readable description
062     */
063    public static String describe(Identity identity) {
064        Configuration sysConfig = Configuration.getSystemConfig();
065        @SuppressWarnings("unchecked")
066        Map<String, Object> describerConfig = (Map<String, Object>) sysConfig.get("IIQCommon.Describer.Config");
067
068        var defaultDisplay = identity.getDisplayableName() + " (" + identity.getName() + ")";
069
070        if (Util.isEmpty(describerConfig) || !(describerConfig.get("Identity") instanceof String)) {
071            return defaultDisplay;
072        } else {
073            var template = describerConfig.get("Identity").toString();
074
075            Map<String, Object> model = new HashMap<>();
076            model.put("identity", identity);
077            model.put("displayName", identity.getDisplayableName());
078            model.put("name", identity.getName());
079            model.putAll(identity.getAttributes());
080
081            try {
082                var result = VelocityUtil.secureRender(template, model, Locale.getDefault(), TimeZone.getDefault());
083                if (Util.isNullOrEmpty(result)) {
084                    log.warn("Describer template returned null or empty for {0}", defaultDisplay);
085                    return defaultDisplay;
086                } else {
087                    return result;
088                }
089            } catch(GeneralException e) {
090                log.error("Error rendering describer template for " + defaultDisplay, e);
091                return defaultDisplay;
092            }
093        }
094    }
095}