PageRenderTime 56ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/org/randombits/confluence/metadata/reference/UserReference.java

https://bitbucket.org/servicerocket/confluence-metadata
Java | 147 lines | 62 code | 18 blank | 67 comment | 8 complexity | abd60bc9b211467d8897241a4de4fbac MD5 | raw file
  1. /*
  2. * Copyright (c) 2005, David Peterson
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * * Neither the name of "randombits.org" nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. * POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * v1.0: Created on 16/05/2005 by David Peterson
  30. */
  31. package org.randombits.confluence.metadata.reference;
  32. import com.atlassian.confluence.user.ConfluenceUser;
  33. import com.atlassian.sal.api.user.UserKey;
  34. import com.atlassian.user.User;
  35. import org.apache.commons.lang.StringUtils;
  36. import org.apache.commons.lang.builder.HashCodeBuilder;
  37. import static com.atlassian.confluence.user.persistence.dao.compatibility.FindUserHelper.getUserByUsername;
  38. /**
  39. * @author randomeizer (David Peterson)
  40. * @author yclian
  41. */
  42. public class UserReference implements Reference<User> {
  43. private static final long serialVersionUID = 1934030029425758042L;
  44. private String username;
  45. private String userKey;
  46. /**
  47. * Constructs a new, blank user option. For use when defrosting.
  48. */
  49. protected UserReference() {
  50. }
  51. public UserReference(ConfluenceUser user) {
  52. this(user.getKey());
  53. }
  54. /**
  55. * @deprecated Since 6.0.0 as Confluence 5.3 now uses userKey, not username.
  56. * @param username
  57. */
  58. @Deprecated
  59. public UserReference(String username) {
  60. this.username = username;
  61. // This won't happen when UserReference is being constructed through reflection as field injection is used.
  62. this.userKey = getUserKeyFromUsername(username);
  63. }
  64. /**
  65. * Constructs a new user option.
  66. *
  67. * @param userKey the user key of the user
  68. */
  69. public UserReference(UserKey userKey) {
  70. this.userKey = userKey.toString();
  71. }
  72. private String getUserKeyFromUsername(String username) {
  73. ConfluenceUser user = getUserByUsername(username);
  74. return null != user ? user.getKey().getStringValue() : null;
  75. }
  76. /**
  77. * @deprecated Since 6.0.0. Use {@link #getUserKey()} instead.
  78. * @return
  79. */
  80. @Deprecated
  81. public String getUsername() {
  82. return username;
  83. }
  84. public String getUserKey() {
  85. return userKey;
  86. }
  87. /**
  88. * @param obj The object to compare.
  89. * @return <code>true</code> if the object is a UserOption with the same
  90. * username.
  91. * @see java.lang.Object#equals(java.lang.Object)
  92. */
  93. @Override
  94. public boolean equals( Object obj ) {
  95. if (obj instanceof UserReference) {
  96. UserReference uo = (UserReference) obj;
  97. return StringUtils.equals(userKey, uo.userKey);
  98. }
  99. return false;
  100. }
  101. /**
  102. * @return the hashcode for the object.
  103. * @see java.lang.Object#hashCode()
  104. */
  105. @Override
  106. public int hashCode() {
  107. return new HashCodeBuilder().append(userKey).toHashCode();
  108. }
  109. /**
  110. * @return the String version of the list option.
  111. * @see java.lang.Object#toString()
  112. */
  113. @Override
  114. public String toString() {
  115. StringBuilder r = new StringBuilder("user: ");
  116. if (null == userKey) {
  117. // HACK [20140208 YCL] Return the legacy username value if userKey is null (to make troubleshooting easier).
  118. // I don't know what would be the impact, no client should use toString() I suppose?
  119. if (null != username) {
  120. r.append(username);
  121. } else {
  122. r.append("null");
  123. }
  124. } else {
  125. r.append(userKey.toString());
  126. }
  127. return r.toString();
  128. }
  129. }