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

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

https://bitbucket.org/jwalton/metadata-confluence-plugin
Java | 128 lines | 80 code | 10 blank | 38 comment | 8 complexity | 263781fdb72b0da174c2e935adaf945b 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.SpaceManager;
  30. import com.atlassian.confluence.spaces.Space;
  31. import com.atlassian.confluence.spaces.SpaceDescription;
  32. import com.atlassian.confluence.core.ConfluenceEntityObject;
  33. import com.atlassian.confluence.labels.ParsedLabelName;
  34. import com.atlassian.confluence.labels.LabelParser;
  35. import com.atlassian.confluence.labels.Label;
  36. import com.atlassian.confluence.labels.LabelManager;
  37. import com.atlassian.renderer.RenderContext;
  38. import com.atlassian.renderer.v2.RenderMode;
  39. import com.atlassian.renderer.v2.macro.MacroException;
  40. import org.andya.confluence.plugins.metadata.model.MetadataContent;
  41. import org.andya.confluence.plugins.metadata.renderers.MetadataRenderer;
  42. import org.andya.confluence.utils.MacroUtils;
  43. import org.andya.confluence.utils.ContentService;
  44. import java.util.*;
  45. /**
  46. * Macro allowing metadata from a space to be shown on the current page.
  47. */
  48. public class SpacesReportMacro extends AbstractSpaceMetadataMacro {
  49. public SpacesReportMacro() {
  50. super("space-report");
  51. }
  52. public boolean isInline() {
  53. return true;
  54. }
  55. public boolean hasBody() {
  56. return true;
  57. }
  58. public RenderMode getBodyRenderMode()
  59. {
  60. return RenderMode.NO_RENDER;
  61. }
  62. @SuppressWarnings("unchecked")
  63. public String execute(Map parameters, String body, RenderContext renderContext) throws MacroException {
  64. try {
  65. String commaDelimitedColumns = MacroUtils.getStringParameter(parameters, "0", true);
  66. int maxResults = MacroUtils.getIntParameter(parameters, MAX_RESULTS_PARAMETER, DEFAULT_MAX_RESULTS);
  67. List<MetadataContent> contents = getMatchingSpaces(parameters, renderContext, true);
  68. String[] columns = commaDelimitedColumns.split(",");
  69. String commaDelimitedTotals = MacroUtils.getStringParameter(parameters, TOTALS_PARAMETER, "");
  70. MetadataRenderer renderer = getMetadataRenderer(parameters);
  71. return renderer.render(renderContext, contents, maxResults, columns, commaDelimitedTotals, "Space");
  72. }
  73. catch (MacroException e) {
  74. return MacroUtils.showRenderedExceptionString(parameters, "Unable to render macro", e.getMessage());
  75. }
  76. catch (Exception e) {
  77. return MacroUtils.showRenderedExceptionString(parameters, "Unable to render macro", e);
  78. }
  79. }
  80. /**
  81. * Returns a list of matching spaces based on the parameters to the macro.
  82. * @param parameters The macro's parameters.
  83. * @param renderContext The render context.
  84. * @param ordered If true then the results should be ordered according to the sort parameters.
  85. * @return A list of matching spaces
  86. * @throws MacroException Thrown if any of the parameters are inappropriate.
  87. */
  88. @SuppressWarnings("unchecked")
  89. public List<MetadataContent> getMatchingSpaces(Map parameters, RenderContext renderContext, boolean ordered) throws MacroException {
  90. String type = MacroUtils.getStringParameter(parameters, TYPE_PARAMETER, null);
  91. String labelName = MacroUtils.getStringParameter(parameters, LABEL_PARAMETER, null);
  92. boolean personalOnly = PERSONAL_SPACE_KEY.equalsIgnoreCase(type);
  93. SpaceManager spaceManager = getSpaceManager();
  94. List<Space> spaces;
  95. ParsedLabelName ref = labelName != null ? LabelParser.parse(labelName) : null;
  96. if (ref != null) {
  97. LabelManager labelManager = getLabelManager();
  98. Label label = labelManager.getLabel(ref);
  99. spaces = labelManager.getSpacesWithLabel(label);
  100. } else {
  101. spaces = spaceManager.getPermittedSpaces(getUser());
  102. }
  103. if (personalOnly) {
  104. List<Space> filteredSpaces = new ArrayList<Space>();
  105. for (Space space : spaces) {
  106. if (space.isPersonal())
  107. filteredSpaces.add(space);
  108. }
  109. spaces = filteredSpaces;
  110. }
  111. List<ConfluenceEntityObject> spaceDescriptions = new ArrayList<ConfluenceEntityObject>();
  112. for (Space space : spaces) {
  113. SpaceDescription spaceDescription = space.getDescription();
  114. spaceDescriptions.add(spaceDescription);
  115. }
  116. return getSortedContents(parameters, spaceDescriptions, ordered ? ContentService.SPACE_KEY : null);
  117. }
  118. }