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

/src/main/java/org/andya/confluence/plugins/metadata/space/SpaceUtils.java

https://bitbucket.org/jwalton/metadata-confluence-plugin
Java | 93 lines | 49 code | 8 blank | 36 comment | 5 complexity | 8bcac5c16e31b2160f2108ed800963b6 MD5 | raw file
  1. /*
  2. * Copyright (c) 2006, 2007 Andy Armstrong, Kelsey Grant and other contributors.
  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 notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * * The names of contributors may not
  14. * be used to endorse or promote products derived from this software without
  15. * specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  21. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  24. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. package org.andya.confluence.plugins.metadata.space;
  29. import com.atlassian.confluence.spaces.Space;
  30. import com.atlassian.confluence.spaces.SpaceDescription;
  31. import com.atlassian.confluence.spaces.SpaceManager;
  32. import com.atlassian.confluence.core.ContentPropertyManager;
  33. import com.atlassian.user.User;
  34. import com.atlassian.user.UserManager;
  35. import com.atlassian.user.EntityException;
  36. import org.andya.confluence.plugins.metadata.MetadataUtils;
  37. import org.andya.confluence.plugins.metadata.model.MetadataValue;
  38. import org.andya.confluence.utils.ContentService;
  39. /**
  40. * A set of utilities for useful metadata-based management of spaces.
  41. */
  42. public class SpaceUtils {
  43. public static final String PARENT_SPACE_METADATA_NAME = "Parent";
  44. public static final String USERS_SPACE_METADATA_NAME = "UsersSpace";
  45. /** Returns the owner of the personal space, or null if none. */
  46. public static User getPersonalSpaceOwner(UserManager userManager, Space space) {
  47. if (!space.isPersonal())
  48. return null;
  49. // todo: is there ever a case where the creator is not the user themselves?
  50. String userName = space.getCreatorName();
  51. try {
  52. return userManager.getUser(userName);
  53. } catch (EntityException e) {
  54. return null;
  55. }
  56. }
  57. /** Returns the parent space of the specified space. */
  58. public static Space getParentSpace(ContentService contentService, Space space) {
  59. SpaceDescription description = space.getDescription();
  60. MetadataValue parentKey = MetadataUtils.getRawMetadataValue(contentService, description, PARENT_SPACE_METADATA_NAME, null);
  61. SpaceManager spaceManager = contentService.getSpaceManager();
  62. return parentKey != null ? spaceManager.getSpace(parentKey.getWikiSnippet()) : null;
  63. }
  64. /** Sets the parent space of the specified space. */
  65. public static void setParentSpace(ContentService contentService, Space space, Space parentSpace) {
  66. SpaceDescription spaceDescription = space.getDescription();
  67. ContentPropertyManager contentPropertyManager = contentService.getContentPropertyManager();
  68. String parentKey = parentSpace.getKey();
  69. MetadataUtils.setMetadataValue(contentPropertyManager, spaceDescription, PARENT_SPACE_METADATA_NAME, parentKey);
  70. }
  71. /** Returns true if the specified space is the root of all user spaces. */
  72. public static boolean isUsersSpace(ContentService contentService, Space space) {
  73. SpaceDescription description = space.getDescription();
  74. MetadataValue isUsersSpaceValue = MetadataUtils.getRawMetadataValue(contentService, description, USERS_SPACE_METADATA_NAME, null);
  75. Boolean isUsersSpace = isUsersSpaceValue != null ? isUsersSpaceValue.getValueAsBoolean() : null;
  76. return isUsersSpace != null && isUsersSpace.booleanValue();
  77. }
  78. /** Sets whether the specified space is the root of all user spaces. */
  79. public static void setUsersSpace(ContentService contentService, Space space, boolean isUsersSpace) {
  80. SpaceDescription spaceDescription = space.getDescription();
  81. ContentPropertyManager contentPropertyManager = contentService.getContentPropertyManager();
  82. MetadataUtils.setMetadataValue(contentPropertyManager, spaceDescription, USERS_SPACE_METADATA_NAME,
  83. isUsersSpace ? "true" : "false");
  84. }
  85. }