001package org.gbif.api.model.common;
002
003import org.gbif.api.vocabulary.UserRole;
004
005import java.util.Date;
006import java.util.Map;
007import java.util.Set;
008import javax.validation.constraints.NotNull;
009import javax.validation.constraints.Pattern;
010import javax.validation.constraints.Size;
011
012import com.google.common.base.Objects;
013import com.google.common.collect.Maps;
014import com.google.common.collect.Sets;
015import org.codehaus.jackson.annotate.JsonIgnore;
016
017/**
018 * An abstract GBIF user account.
019 * The main purpose of this abstraction is to let subclasses handle key and password information only if required.
020 * By doing so, it is possible to have classes working for user information without having to carry those
021 * information around.
022 */
023public abstract class AbstractGbifUser {
024  protected static final String EMAIL_PATTERN =
025          "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z]{2,})$";
026
027  protected String userName;
028  protected String firstName;
029  protected String lastName;
030  protected String email;
031  protected Set<UserRole> roles = Sets.newHashSet();
032
033//  //country of the user (user for stats)
034//  protected Country country;
035
036  protected Map<String, String> settings = Maps.newHashMap();
037
038  //settings that the user will not set directly
039  protected Map<String, String> systemSettings = Maps.newHashMap();
040
041  protected Date deleted;
042
043  @NotNull
044  @Pattern(regexp = EMAIL_PATTERN)
045  public String getEmail() {
046    return email;
047  }
048
049  public void setEmail(String email) {
050    this.email = email;
051  }
052
053  /**
054   * The unique, immutable drupal user account name.
055   * This name should be used for referring to a user.
056   * The account name is made of ASCII lower case alphanumerics, underscore, dash or dots and is in particular void
057   * of whitespace.
058   */
059  @NotNull
060  @Pattern(regexp = "^[a-z0-9_.-]+$")
061  @Size(min = 3, max = 64)
062  public String getUserName() {
063    return userName;
064  }
065
066  public void setUserName(String userName) {
067    this.userName = userName;
068  }
069
070  /**
071   * @return the first name of a person
072   */
073  public String getFirstName() {
074    return firstName;
075  }
076
077  public void setFirstName(String firstName) {
078    this.firstName = firstName;
079  }
080
081  /**
082   * @return the last name of the user
083   */
084  public String getLastName() {
085    return lastName;
086  }
087
088  public void setLastName(String lastName) {
089    this.lastName = lastName;
090  }
091
092  /**
093   * @return the first and last name of the user concatenated with a space
094   */
095  @JsonIgnore
096  public String getName() {
097    return firstName + " " + lastName;
098  }
099
100  @NotNull
101  public Set<UserRole> getRoles() {
102    return roles;
103  }
104
105  public void setRoles(Set<UserRole> roles) {
106    this.roles = roles;
107  }
108
109  public void addRole(UserRole role) {
110    roles.add(role);
111  }
112
113  /**
114   * Checks if the user has the given user role.
115   * @param role
116   *
117   * @return true if the user has the requested role
118   */
119  public boolean hasRole(UserRole role) {
120    return role != null && roles.contains(role);
121  }
122
123  /**
124   * Sets the settings object, setting an empty map if null is provided.
125   */
126  public void setSettings(Map<String, String> settings) {
127    // safeguard against misuse to avoid NPE
128    this.settings = settings == null ? Maps.newHashMap() : settings;
129  }
130
131  /**
132   * Gets the settings which may be empty but never null.
133   * @return
134   */
135  @NotNull
136  public Map<String, String> getSettings() {
137    return settings;
138  }
139
140  /**
141   * Sets the settings object, setting an empty map if null is provided.
142   */
143  public void setSystemSettings(Map<String, String> systemSettings) {
144    // safeguard against misuse to avoid NPE
145    this.systemSettings = systemSettings == null ? Maps.newHashMap() : systemSettings;
146  }
147  /**
148   * Gets the settings which may be empty but never null.
149   * @return
150   */
151  @NotNull
152  public Map<String, String> getSystemSettings() {
153    return systemSettings;
154  }
155
156  public Date getDeleted() {
157    return deleted;
158  }
159
160  public void setDeleted(Date deleted) {
161    this.deleted = deleted;
162  }
163
164  @Override
165  public boolean equals(Object obj) {
166    if (this == obj) {
167      return true;
168    }
169    if (!(obj instanceof AbstractGbifUser)) {
170      return false;
171    }
172
173    AbstractGbifUser that = (AbstractGbifUser) obj;
174    return Objects.equal(this.userName, that.userName)
175            && Objects.equal(this.firstName, that.firstName)
176            && Objects.equal(this.lastName, that.lastName)
177            && Objects.equal(this.email, that.email)
178            && Objects.equal(this.roles, that.roles)
179            && Objects.equal(this.settings, that.settings)
180            && Objects.equal(this.systemSettings, that.systemSettings)
181            && Objects.equal(this.deleted, that.deleted);
182  }
183
184  @Override
185  public int hashCode() {
186    return Objects.hashCode(userName, firstName, lastName, email, roles, settings,
187            systemSettings, deleted);
188  }
189
190  @Override
191  public String toString() {
192    return Objects.toStringHelper(this)
193            .add("userName", userName)
194            .add("firstName", firstName)
195            .add("lastName", lastName)
196            .add("email", email)
197            .add("roles", roles)
198            .add("settings", settings)
199            .add("systemSettings", systemSettings)
200            .add("deleted", deleted)
201            .toString();
202  }
203
204}