001package com.identityworksllc.iiq.common.plugin.vo;
002
003import com.fasterxml.jackson.annotation.JsonAutoDetect;
004import com.fasterxml.jackson.annotation.JsonProperty;
005import sailpoint.api.ObjectUtil;
006import sailpoint.object.SailPointObject;
007
008import java.lang.reflect.Method;
009import java.util.HashMap;
010import java.util.List;
011import java.util.Map;
012import java.util.StringJoiner;
013
014/**
015 * A default implementation of an object summary VO object
016 */
017@JsonAutoDetect
018public class ObjectSummary extends RestObject {
019
020    /**
021     * Extra attributes for this object
022     */
023    private Map<String, Object> attributes;
024
025    /**
026     * The object's display name
027     */
028    private String displayName;
029
030    /**
031     * The object ID
032     */
033    private String id;
034
035    /**
036     * The object name
037     */
038    private String name;
039
040    /**
041     * The object type (e.g., a class or short name like Identity)
042     */
043    private String type;
044
045    /**
046     * Creates a new ObjectSummary with empty details
047     */
048    public ObjectSummary() {
049        attributes = new HashMap<>();
050    }
051
052    /**
053     * Constructs a new summary of the given SailPointObject
054     * @param spo The SailPointObject to copy
055     */
056    public ObjectSummary(SailPointObject spo) {
057        this();
058        this.id = spo.getId();
059        this.name = spo.getName();
060        this.type = ObjectUtil.getTheRealClass(spo).getSimpleName();
061        try {
062            Method getDisplayName = spo.getClass().getMethod("getDisplayName");
063            this.displayName = (String) getDisplayName.invoke(spo);
064        } catch(Exception ignored) {
065            /* Do nothing, this might be fine */
066        }
067    }
068
069    /**
070     * Adds a new attribute to this object summary
071     * @param name The name of the attribute
072     * @param value The string value of the attribute
073     */
074    public void addAttribute(String name, String value) {
075        if (attributes == null) {
076                attributes = new HashMap<>();                   
077        }
078        attributes.put(name, value);
079    }
080
081    /**
082     * Adds a new attribute to this object summary
083     * @param name The name of the attribute
084     * @param value The list value of the attribute
085     */
086    public void addAttribute(String name, List<?> value) {
087        if (attributes == null) {
088            attributes = new HashMap<>();
089        }
090        attributes.put(name, value);
091    }
092
093    @JsonProperty
094    public Map<String, Object> getAttributes() {
095        return attributes;
096    }
097
098    @JsonProperty
099    public String getDisplayName() {
100        return displayName;
101    }
102
103    @JsonProperty
104    public String getId() {
105        return id;
106    }
107
108    @JsonProperty
109    public String getName() {
110        return name;
111    }
112
113    @JsonProperty
114    public String getType() {
115        return type;
116    }
117
118    public void setAttributes(Map<String, Object> attributes) {
119        this.attributes = attributes;
120    }
121
122    public void setDisplayName(String displayName) {
123        this.displayName = displayName;
124    }
125
126    public void setId(String id) {
127        this.id = id;
128    }
129
130    public void setName(String name) {
131        this.name = name;
132    }
133
134    public void setType(String type) {
135        this.type = type;
136    }
137
138    @Override
139    public String toString() {
140        StringJoiner joiner = new StringJoiner(", ", ObjectSummary.class.getSimpleName() + "[", "]");
141        if ((attributes) != null) {
142            joiner.add("attributes=" + attributes);
143        }
144        if ((displayName) != null) {
145            joiner.add("displayName='" + displayName + "'");
146        }
147        if ((id) != null) {
148            joiner.add("id='" + id + "'");
149        }
150        if ((name) != null) {
151            joiner.add("name='" + name + "'");
152        }
153        if ((type) != null) {
154            joiner.add("type='" + type + "'");
155        }
156        return joiner.toString();
157    }
158}