PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/com/atlassian/crowd/model/user/UserTemplate.java

https://bitbucket.org/atlassian/crowd-rest-client
Java | 225 lines | 147 code | 33 blank | 45 comment | 1 complexity | c0e2b00a864e203d017e9b16630fb4b7 MD5 | raw file
  1. /*
  2. * Copyright © 2010 - 2015 Atlassian Corporation Pty Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * https://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.atlassian.crowd.model.user;
  17. import com.atlassian.crowd.embedded.api.User;
  18. import com.atlassian.crowd.embedded.api.UserComparator;
  19. import org.apache.commons.lang3.StringUtils;
  20. import org.apache.commons.lang3.Validate;
  21. import org.apache.commons.lang3.builder.ToStringBuilder;
  22. import java.io.Serializable;
  23. /**
  24. * A publicly mutable User implementation.
  25. * <p/>
  26. * Used to create or update a user.
  27. */
  28. public class UserTemplate implements com.atlassian.crowd.model.user.User, Serializable
  29. {
  30. private long directoryId;
  31. private String name;
  32. private boolean active;
  33. private String emailAddress;
  34. private String firstName;
  35. private String lastName;
  36. private String displayName;
  37. private String externalId;
  38. /**
  39. * Build a template for a new user.
  40. * <p/>
  41. * Used to create a user.
  42. *
  43. * @param username username of new user.
  44. * @param directoryId ID of the directory in which to store the new user.
  45. */
  46. public UserTemplate(String username, long directoryId)
  47. {
  48. Validate.isTrue(StringUtils.isNotBlank(username), "username argument cannot be null or blank");
  49. // lowercasing not enforced, only on the Internal User, since an LDAP user can handle both
  50. this.name = username;
  51. this.directoryId = directoryId;
  52. }
  53. public UserTemplate(final String name)
  54. {
  55. this(name, -1L);
  56. }
  57. /**
  58. * Build a template from an existing user.
  59. * <p/>
  60. * Used to update a user.
  61. *
  62. * @param user user to build template from.
  63. */
  64. public UserTemplate(final com.atlassian.crowd.model.user.User user)
  65. {
  66. Validate.notNull(user, "user argument cannot be null");
  67. Validate.isTrue(StringUtils.isNotBlank(user.getName()), "user.name argument cannot be null or blank");
  68. // lowercasing not enforced, only on the Internal User, since an LDAP user can handle both
  69. this.name = user.getName();
  70. this.directoryId = user.getDirectoryId();
  71. this.active = user.isActive();
  72. this.emailAddress = user.getEmailAddress();
  73. this.firstName = user.getFirstName();
  74. this.lastName = user.getLastName();
  75. this.displayName = user.getDisplayName();
  76. this.externalId = user.getExternalId();
  77. }
  78. public UserTemplate(String username, String firstName, String lastName, String displayName)
  79. {
  80. this(username);
  81. this.displayName = displayName;
  82. this.lastName = lastName;
  83. this.firstName = firstName;
  84. }
  85. /**
  86. * Build a template from an existing user.
  87. * <p/>
  88. * Used to update a user.
  89. *
  90. * @param user user to build template from.
  91. */
  92. public UserTemplate(final com.atlassian.crowd.embedded.api.User user)
  93. {
  94. Validate.notNull(user, "user argument cannot be null");
  95. Validate.isTrue(StringUtils.isNotBlank(user.getName()), "user.name argument cannot be null or blank");
  96. // lowercasing not enforced, only on the Internal User, since an LDAP user can handle both
  97. this.name = user.getName();
  98. this.directoryId = user.getDirectoryId();
  99. this.active = user.isActive();
  100. this.emailAddress = user.getEmailAddress();
  101. this.displayName = user.getDisplayName();
  102. }
  103. public void setDirectoryId(long directoryId)
  104. {
  105. this.directoryId = directoryId;
  106. }
  107. public void setName(final String name)
  108. {
  109. this.name = name;
  110. }
  111. public long getDirectoryId()
  112. {
  113. return directoryId;
  114. }
  115. public boolean isActive()
  116. {
  117. return active;
  118. }
  119. public void setActive(boolean active)
  120. {
  121. this.active = active;
  122. }
  123. public String getName()
  124. {
  125. return name;
  126. }
  127. public String getEmailAddress()
  128. {
  129. return emailAddress;
  130. }
  131. public void setEmailAddress(String emailAddress)
  132. {
  133. this.emailAddress = emailAddress;
  134. }
  135. public String getFirstName()
  136. {
  137. return firstName;
  138. }
  139. public void setFirstName(String firstName)
  140. {
  141. this.firstName = firstName;
  142. }
  143. public String getLastName()
  144. {
  145. return lastName;
  146. }
  147. public void setLastName(String lastName)
  148. {
  149. this.lastName = lastName;
  150. }
  151. public String getDisplayName()
  152. {
  153. return displayName;
  154. }
  155. public void setDisplayName(String displayName)
  156. {
  157. this.displayName = displayName == null ? "" : displayName;
  158. }
  159. public boolean equals(final Object o)
  160. {
  161. return UserComparator.equalsObject(this, o);
  162. }
  163. public int hashCode()
  164. {
  165. return UserComparator.hashCode(this);
  166. }
  167. public int compareTo(User other)
  168. {
  169. return UserComparator.compareTo(this, other);
  170. }
  171. public String getExternalId()
  172. {
  173. return externalId;
  174. }
  175. public void setExternalId(final String externalId)
  176. {
  177. this.externalId = externalId;
  178. }
  179. public String toString()
  180. {
  181. return new ToStringBuilder(this).
  182. append("name", name).
  183. append("directoryId", directoryId).
  184. append("active", active).
  185. append("emailAddress", emailAddress).
  186. append("firstName", firstName).
  187. append("lastName", lastName).
  188. append("displayName", displayName).
  189. append("externalId", externalId).
  190. toString();
  191. }
  192. }