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 * A complex type containing the components of the User's real name.
030 */
031public class Name
032{
033  /**
034   * The <code>AttributeValueResolver</code> that resolves SCIM attribute values
035   * to/from <code>Name</code> instances.
036   */
037  public static final AttributeValueResolver<Name> NAME_RESOLVER =
038      new AttributeValueResolver<Name>()
039      {
040        /**
041         * {@inheritDoc}
042         */
043        @Override
044        public Name toInstance(final SCIMAttributeValue value) {
045          return new Name(
046              value.getSubAttributeValue("formatted",
047                  STRING_RESOLVER),
048              value.getSubAttributeValue("familyName",
049                  STRING_RESOLVER),
050              value.getSubAttributeValue("middleName",
051                  STRING_RESOLVER),
052              value.getSubAttributeValue("givenName",
053                  STRING_RESOLVER),
054              value.getSubAttributeValue("honorificPrefix",
055                  STRING_RESOLVER),
056              value.getSubAttributeValue("honorificSuffix",
057                  STRING_RESOLVER));
058        }
059
060        /**
061         * {@inheritDoc}
062         */
063        @Override
064        public SCIMAttributeValue fromInstance(
065            final AttributeDescriptor attributeDescriptor, final Name value)
066            throws InvalidResourceException {
067          final List<SCIMAttribute> subAttributes =
068              new ArrayList<SCIMAttribute>(6);
069
070          if (value.formatted != null)
071          {
072            subAttributes.add(
073                SCIMAttribute.create(
074                    attributeDescriptor.getSubAttribute("formatted"),
075                    SCIMAttributeValue.createStringValue(value.formatted)));
076          }
077
078          if (value.givenName != null)
079          {
080            subAttributes.add(
081                SCIMAttribute.create(
082                    attributeDescriptor.getSubAttribute("givenName"),
083                    SCIMAttributeValue.createStringValue(value.givenName)));
084          }
085
086          if (value.familyName != null)
087          {
088            subAttributes.add(
089                SCIMAttribute.create(
090                    attributeDescriptor.getSubAttribute("familyName"),
091                    SCIMAttributeValue.createStringValue(value.familyName)));
092          }
093
094          if (value.middleName != null)
095          {
096            subAttributes.add(
097                SCIMAttribute.create(
098                    attributeDescriptor.getSubAttribute("middleName"),
099                    SCIMAttributeValue.createStringValue(value.middleName)));
100          }
101
102          if (value.honorificPrefix != null)
103          {
104            subAttributes.add(
105                SCIMAttribute.create(
106                    attributeDescriptor.getSubAttribute("honorificPrefix"),
107                    SCIMAttributeValue.createStringValue(
108                        value.honorificPrefix)));
109          }
110
111          if (value.honorificSuffix != null)
112          {
113            subAttributes.add(
114                SCIMAttribute.create(
115                    attributeDescriptor.getSubAttribute("honorificSuffix"),
116                    SCIMAttributeValue.createStringValue(
117                        value.honorificSuffix)));
118          }
119
120          return SCIMAttributeValue.createComplexValue(subAttributes);
121        }
122      };
123
124  private String formatted;
125  private String familyName;
126  private String middleName;
127  private String givenName;
128  private String honorificPrefix;
129  private String honorificSuffix;
130
131  /**
132   * Creates a SCIM core user 'name' attribute. Any of the arguments may be
133   * {@code null} if they are not to be included.
134   *
135   * @param formatted        The The full name, including all middle names,
136   *                         titles, and suffixes as appropriate, formatted
137   *                         for display.
138   * @param familyName       The family name of the User, or "Last Name" in
139   *                         most Western languages.
140   * @param givenName        The given name of the User, or "First Name" in
141   *                         most Western languages.
142   * @param middleName       The middle name(s) of the User.
143   * @param honorificPrefix  The honorific prefix(es) of the User, or "Title"
144   *                         in most Western languages.
145   * @param honorificSuffix  The honorifix suffix(es) of the User, or "Suffix"
146   *                         in most Western languages.
147   */
148  public Name(final String formatted, final String familyName,
149              final String middleName, final String givenName,
150              final String honorificPrefix, final String honorificSuffix) {
151    this.formatted = formatted;
152    this.familyName = familyName;
153    this.middleName = middleName;
154    this.givenName = givenName;
155    this.honorificPrefix = honorificPrefix;
156    this.honorificSuffix = honorificSuffix;
157  }
158
159  /**
160   * Retrieves the family name of the User, or "Last Name" in  most Western
161   * languages.
162   *
163   * @return The family name of the User, or "Last Name" in  most Western
164   * languages.
165   */
166  public String getFamilyName() {
167    return familyName;
168  }
169
170  /**
171   * Sets the family name of the User, or "Last Name" in  most Western
172   * languages.
173   *
174   * @param familyName The family name of the User, or "Last Name" in  most
175   * Western languages.
176   */
177  public void setFamilyName(final String familyName) {
178    this.familyName = familyName;
179  }
180
181  /**
182   * Retrieves the full name, including all middle names, titles, and
183   * suffixes as appropriate, formatted for display.
184   *
185   * @return The full name, including all middle names, titles, and
186   * suffixes as appropriate, formatted for display.
187   */
188  public String getFormatted() {
189    return formatted;
190  }
191
192  /**
193   * Sets the full name, including all middle names, titles, and
194   * suffixes as appropriate, formatted for display.
195   *
196   * @param formatted The full name, including all middle names, titles, and
197   * suffixes as appropriate, formatted for display.
198   */
199  public void setFormatted(final String formatted) {
200    this.formatted = formatted;
201  }
202
203  /**
204   * Retrieves the given name of the User, or "First Name" in most Western
205   * languages.
206   *
207   * @return The given name of the User, or "First Name" in most Western
208   * languages.
209   */
210  public String getGivenName() {
211    return givenName;
212  }
213
214  /**
215   * Sets the given name of the User, or "First Name" in most Western
216   * languages.
217   *
218   * @param givenName The given name of the User, or "First Name" in most
219   * Western languages.
220   */
221  public void setGivenName(final String givenName) {
222    this.givenName = givenName;
223  }
224
225  /**
226   * Retrieves the honorific prefix(es) of the User, or "Title" in most Western
227   * languages.
228   *
229   * @return The honorific prefix(es) of the User, or "Title" in most Western
230   * languages.
231   */
232  public String getHonorificPrefix() {
233    return honorificPrefix;
234  }
235
236  /**
237   * Sets the honorific prefix(es) of the User, or "Title" in most Western
238   * languages.
239   *
240   * @param honorificPrefix The honorific prefix(es) of the User, or "Title"
241   * in most Western languages.
242   */
243  public void setHonorificPrefix(final String honorificPrefix) {
244    this.honorificPrefix = honorificPrefix;
245  }
246
247  /**
248   * Retrieves the honorific suffix(es) of the User, or "Suffix" in most
249   * Western languages.
250   *
251   * @return The honorific suffix(es) of the User, or "Suffix" in most
252   * Western languages.
253   */
254  public String getHonorificSuffix() {
255    return honorificSuffix;
256  }
257
258  /**
259   * Sets the honorific suffix(es) of the User, or "Suffix" in most
260   * Western languages.
261   *
262   * @param honorificSuffix The honorific suffix(es) of the User, or "Suffix"
263   * in most Western languages.
264   */
265  public void setHonorificSuffix(final String honorificSuffix) {
266    this.honorificSuffix = honorificSuffix;
267  }
268
269  /**
270   * Retrieves the middle name(s) of the User.
271   *
272   * @return The middle name(s) of the User.
273   */
274  public String getMiddleName() {
275    return middleName;
276  }
277
278  /**
279   * Retrieves the middle name(s) of the User.
280   *
281   * @param middleName The middle name(s) of the User.
282   */
283  public void setMiddleName(final String middleName) {
284    this.middleName = middleName;
285  }
286
287  /**
288   * {@inheritDoc}
289   */
290  @Override
291  public boolean equals(final Object o) {
292    if (this == o) {
293      return true;
294    }
295    if (o == null || getClass() != o.getClass()) {
296      return false;
297    }
298
299    Name name = (Name) o;
300
301    if (familyName != null ? !familyName.equals(name.familyName) :
302        name.familyName != null) {
303      return false;
304    }
305    if (formatted != null ? !formatted.equals(name.formatted) :
306        name.formatted != null) {
307      return false;
308    }
309    if (givenName != null ? !givenName.equals(name.givenName) :
310        name.givenName != null) {
311      return false;
312    }
313    if (honorificPrefix != null ?
314        !honorificPrefix.equals(name.honorificPrefix) :
315        name.honorificPrefix != null) {
316      return false;
317    }
318    if (honorificSuffix != null ?
319        !honorificSuffix.equals(name.honorificSuffix) :
320        name.honorificSuffix != null) {
321      return false;
322    }
323    if (middleName != null ? !middleName.equals(name.middleName) :
324        name.middleName != null) {
325      return false;
326    }
327
328    return true;
329  }
330
331  /**
332   * {@inheritDoc}
333   */
334  @Override
335  public int hashCode() {
336    int result = formatted != null ? formatted.hashCode() : 0;
337    result = 31 * result + (familyName != null ? familyName.hashCode() : 0);
338    result = 31 * result + (middleName != null ? middleName.hashCode() : 0);
339    result = 31 * result + (givenName != null ? givenName.hashCode() : 0);
340    result = 31 * result + (honorificPrefix != null ?
341        honorificPrefix.hashCode() : 0);
342    result = 31 * result + (honorificSuffix != null ?
343        honorificSuffix.hashCode() : 0);
344    return result;
345  }
346
347  @Override
348  public String toString() {
349    return "Name{" +
350        "formatted='" + formatted + '\'' +
351        ", familyName='" + familyName + '\'' +
352        ", middleName='" + middleName + '\'' +
353        ", givenName='" + givenName + '\'' +
354        ", honorificPrefix='" + honorificPrefix + '\'' +
355        ", honorificSuffix='" + honorificSuffix + '\'' +
356        '}';
357  }
358}