PageRenderTime 26ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/jwalton/metadata-confluence-plugin
Java | 114 lines | 66 code | 15 blank | 33 comment | 24 complexity | 9bd4297c120dead8eff3843c1172ecfa 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.user.User;
  30. import com.atlassian.confluence.spaces.Space;
  31. import java.util.Map;
  32. import java.util.HashMap;
  33. import java.util.List;
  34. import org.andya.confluence.utils.ContentService;
  35. /**
  36. * This class represents all of the information about the spaces in Confluence.
  37. */
  38. public class SpaceMap {
  39. private final SpaceHierarchy rootHierarchy;
  40. private SpaceHierarchy usersHierarchy;
  41. private final Map<Space, SpaceHierarchy> map = new HashMap<Space, SpaceHierarchy>();
  42. private SpaceMap(User user, ContentService contentService) {
  43. rootHierarchy = new SpaceHierarchy(null, null);
  44. populate(user, contentService);
  45. }
  46. public static SpaceMap get(User user, ContentService contentService) {
  47. // todo: is there a safe way to cache this given the possibility of running in a cluster?
  48. return new SpaceMap(user, contentService);
  49. }
  50. public SpaceHierarchy getRootHierarchy() {
  51. return rootHierarchy;
  52. }
  53. public SpaceHierarchy getUsersHierarchy() {
  54. return usersHierarchy;
  55. }
  56. private Map<Space, SpaceHierarchy> getMap() {
  57. return map;
  58. }
  59. public SpaceHierarchy getSpaceHierarchy(Space space) {
  60. return (SpaceHierarchy)getMap().get(space);
  61. }
  62. public void populate(User user, ContentService contentService) {
  63. Map<Space, SpaceHierarchy> map = getMap();
  64. List<Space> spaces = contentService.getSpaceManager().getPermittedSpaces(user);
  65. // First add the regular spaces
  66. for (Space space : spaces) {
  67. if (space.getKey() != null && getSpaceHierarchy(space) == null && !space.isPersonal())
  68. addSpaceHierarchy(map, contentService, space);
  69. }
  70. // Now add the personal spaces
  71. for (Space space : spaces) {
  72. if (space.getKey() != null && getSpaceHierarchy(space) == null && space.isPersonal())
  73. addSpaceHierarchy(map, contentService, space);
  74. }
  75. }
  76. private SpaceHierarchy addSpaceHierarchy(Map<Space, SpaceHierarchy> spaceMap, ContentService contentService, Space space) {
  77. Space parent = SpaceUtils.getParentSpace(contentService, space);
  78. if (parent == null && space.isPersonal()) {
  79. SpaceHierarchy usersHierarchy = getUsersHierarchy();
  80. if (usersHierarchy != null)
  81. parent = usersHierarchy.getSpace();
  82. }
  83. SpaceHierarchy parentHierarchy = getRootHierarchy();
  84. if (parent != null) {
  85. parentHierarchy = (SpaceHierarchy)spaceMap.get(parent);
  86. if (parentHierarchy == null)
  87. parentHierarchy = addSpaceHierarchy(spaceMap, contentService, parent);
  88. }
  89. SpaceHierarchy spaceHierarchy = (SpaceHierarchy)spaceMap.get(space);
  90. if (spaceHierarchy == null) {
  91. spaceHierarchy = new SpaceHierarchy(space, parentHierarchy);
  92. spaceMap.put(space, spaceHierarchy);
  93. }
  94. boolean isUsersSpace = SpaceUtils.isUsersSpace(contentService, space);
  95. if (isUsersSpace)
  96. usersHierarchy = spaceHierarchy;
  97. return spaceHierarchy;
  98. }
  99. }