001/*
002 * Copyright 2011-2016 UnboundID Corp.
003 *
004 * This program is free software; you can redistribute it and/or modify
005 * it under the terms of the GNU General Public License (GPLv2 only)
006 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
007 * as published by the Free Software Foundation.
008 *
009 * This program is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012 * GNU General Public License for more details.
013 *
014 * You should have received a copy of the GNU General Public License
015 * along with this program; if not, see <http://www.gnu.org/licenses>.
016 */
017
018package com.unboundid.scim.data;
019
020import com.unboundid.scim.schema.AttributeDescriptor;
021import com.unboundid.scim.sdk.InvalidResourceException;
022import com.unboundid.scim.sdk.SCIMAttribute;
023import com.unboundid.scim.sdk.SCIMAttributeValue;
024
025import java.util.ArrayList;
026import java.util.List;
027
028/**
029 * The User's manager. A complex type that optionally allows Service Providers
030 * to represent organizational hierarchy by referencing the "id" attribute of
031 * another User
032 */
033public class Manager
034{
035  /**
036   * The <code>AttributeValueResolver</code> that resolves SCIM attribute values
037   * to/from <code>Manager</code> instances.
038   */
039  public static final AttributeValueResolver<Manager> MANAGER_RESOLVER =
040      new AttributeValueResolver<Manager>() {
041        /**
042         * {@inheritDoc}
043         */
044        public Manager toInstance(final SCIMAttributeValue value) {
045          return new Manager(
046              value.getSubAttributeValue("managerId",
047                  STRING_RESOLVER),
048              value.getSubAttributeValue("displayName",
049                  STRING_RESOLVER));
050        }
051
052        /**
053         * {@inheritDoc}
054         */
055        public SCIMAttributeValue fromInstance(
056            final AttributeDescriptor attributeDescriptor,
057            final Manager value) throws InvalidResourceException {
058          final List<SCIMAttribute> subAttributes =
059              new ArrayList<SCIMAttribute>(2);
060
061          if (value.managerId != null)
062          {
063            subAttributes.add(
064                SCIMAttribute.create(
065                    attributeDescriptor.getSubAttribute("managerId"),
066                    SCIMAttributeValue.createStringValue(value.managerId)));
067          }
068
069          if (value.displayName != null)
070          {
071            subAttributes.add(
072                SCIMAttribute.create(
073                    attributeDescriptor.getSubAttribute("displayName"),
074                    SCIMAttributeValue.createStringValue(value.displayName)));
075          }
076          return SCIMAttributeValue.createComplexValue(subAttributes);
077        }
078      };
079
080  private String managerId;
081
082  private String displayName;
083
084  /**
085   * Creates a SCIM enterprise user extension 'manager' attribute. Any of the
086   * arguments may be {@code null} if they are not to be included.
087   *
088   * @param managerId        The id of the SCIM resource representing the User's
089   *                         manager.
090   * @param displayName      The displayName of the User's manager.
091   */
092  public Manager(final String managerId, final String displayName) {
093    this.displayName = displayName;
094    this.managerId = managerId;
095  }
096
097  /**
098   * Retrieves the displayName of the User's manager.
099   *
100   * @return The displayName of the User's manager.
101   */
102  public String getDisplayName() {
103    return displayName;
104  }
105
106  /**
107   * Sets the displayName of the User's manager.
108   *
109   * @param displayName The displayName of the User's manager.
110   */
111  public void setDisplayName(final String displayName) {
112    this.displayName = displayName;
113  }
114
115  /**
116   * Retrieves the id of the SCIM resource representing the User's manager.
117   *
118   * @return The id of the SCIM resource representing the User's manager.
119   */
120  public String getManagerId() {
121    return managerId;
122  }
123
124  /**
125   * Sets the id of the SCIM resource representing the User's manager.
126   *
127   * @param managerId The id of the SCIM resource representing the User's
128   *                  manager.
129   */
130  public void setManagerId(final String managerId) {
131    this.managerId = managerId;
132  }
133
134  /**
135   * {@inheritDoc}
136   */
137  @Override
138  public boolean equals(final Object o) {
139    if (this == o) {
140      return true;
141    }
142    if (o == null || getClass() != o.getClass()) {
143      return false;
144    }
145
146    Manager manager = (Manager) o;
147
148    if (displayName != null ? !displayName.equals(manager.displayName) :
149        manager.displayName != null) {
150      return false;
151    }
152    if (managerId != null ? !managerId.equals(manager.managerId) :
153        manager.managerId != null) {
154      return false;
155    }
156
157    return true;
158  }
159
160  /**
161   * {@inheritDoc}
162   */
163  @Override
164  public int hashCode() {
165    int result = managerId != null ? managerId.hashCode() : 0;
166    result = 31 * result + (displayName != null ? displayName.hashCode() : 0);
167    return result;
168  }
169
170  /**
171   * {@inheritDoc}
172   */
173  @Override
174  public String toString() {
175    return "Manager{" +
176        "displayName='" + displayName + '\'' +
177        ", managerId='" + managerId + '\'' +
178        '}';
179  }
180}