001package com.identityworksllc.iiq.common;
002
003import sailpoint.api.SailPointContext;
004import sailpoint.object.Link;
005import sailpoint.tools.GeneralException;
006import sailpoint.tools.Pair;
007
008import java.util.HashMap;
009import java.util.Map;
010import java.util.Objects;
011
012/**
013 * Implements a type-safe Link reference object, containing the application
014 * name and native identity of the Link.
015 */
016public final class LinkRef extends Pair<String, String> {
017
018    /**
019     * Constructs a LinkRef object with the specified application name and native identity.
020     * @param applicationName the name of the application
021     * @param nativeIdentity the native identity of the link
022     */
023    public LinkRef(String applicationName, String nativeIdentity) {
024        super(applicationName, nativeIdentity);
025    }
026
027    /**
028     * Constructs a LinkRef object from a map containing application name and native identity.
029     * @param map a map containing the application name and native identity
030     */
031    public LinkRef(Map<String, ?> map) {
032        this((String) map.get("applicationName"), (String) map.get("nativeIdentity"));
033    }
034
035    /**
036     * Constructs a LinkRef object from a {@link Link} object.
037     * @param link the Link object
038     */
039    public LinkRef(Link link) {
040        this(link.getApplicationName(), link.getNativeIdentity());
041    }
042
043    /**
044     * Returns true if this LinkRef is equal to another object.
045     * @param o the object to compare with
046     * @return true if the objects are equal, false otherwise
047     */
048    @Override
049    public boolean equals(Object o) {
050        if (this == o) return true;
051        if (!(o instanceof LinkRef)) return false;
052        LinkRef linkRef = (LinkRef) o;
053        return getFirst().equals(linkRef.getFirst()) && getSecond().equals(linkRef.getSecond());
054    }
055
056    /**
057     * Gets the application name.
058     * @return the application name
059     */
060    public String getApplicationName() {
061        return getFirst();
062    }
063
064    /**
065     * Gets the native identity.
066     * @return the native identity
067     */
068    public String getNativeIdentity() {
069        return getSecond();
070    }
071
072    /**
073     * Returns the hash code of this LinkRef.
074     * @return the hash code
075     */
076    @Override
077    public int hashCode() {
078        return Objects.hash(getFirst(), getSecond());
079    }
080
081    /**
082     * Resolves the link reference to a Link object.
083     *
084     * @param context the SailPointContext
085     * @return the resolved Link object
086     * @throws GeneralException if an error occurs during resolution
087     */
088    public Link resolve(SailPointContext context) throws GeneralException {
089        return IdentityLinkUtil.getUniqueLink(context, getApplicationName(), getNativeIdentity());
090    }
091
092    /**
093     * Converts the LinkRef object to a map representation.
094     *
095     * @return a map containing the application name and native identity
096     */
097    public Map<String, String> toMap() {
098        Map<String, String> map = new HashMap<>();
099        map.put("applicationName", getApplicationName());
100        map.put("nativeIdentity", getNativeIdentity());
101        return map;
102    }
103
104    /**
105     * Converts the LinkRef object to a string representation.
106     * @return a string representation of the LinkRef object
107     */
108    @Override
109    public String toString() {
110        return "LinkRef{" +
111                "applicationName='" + getApplicationName() + '\'' +
112                ", nativeIdentity='" + getNativeIdentity() + '\'' +
113                '}';
114    }
115}