PageRenderTime 21ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/jwalton/metadata-confluence-plugin
Java | 91 lines | 48 code | 12 blank | 31 comment | 10 complexity | 2fdeb7f7b1b915815fa9d80507d3213c 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.renderer.RenderContext;
  30. import com.atlassian.renderer.v2.macro.MacroException;
  31. import com.atlassian.renderer.v2.RenderMode;
  32. import com.atlassian.confluence.spaces.SpaceDescription;
  33. import java.util.Map;
  34. import org.andya.confluence.utils.MacroUtils;
  35. import org.andya.confluence.plugins.metadata.model.MetadataValue;
  36. /**
  37. * Macro allowing metadata from a space to be shown on the current page.
  38. */
  39. public class SpaceMetadataFromMacro extends AbstractSpaceMetadataMacro {
  40. public SpaceMetadataFromMacro() {
  41. super("space-metadata-from");
  42. }
  43. public boolean isInline() {
  44. return true;
  45. }
  46. public boolean hasBody() {
  47. return true;
  48. }
  49. public RenderMode getBodyRenderMode() {
  50. return RenderMode.NO_RENDER;
  51. }
  52. @SuppressWarnings("unchecked")
  53. public String execute(Map parameters, String qualifiedName, RenderContext renderContext) throws MacroException {
  54. try {
  55. String spaceKey = MacroUtils.getStringParameter(parameters, "0", null);
  56. String valueName = MacroUtils.getStringParameter(parameters, "1", null);
  57. if (spaceKey == null && valueName == null) {
  58. throw new MacroException("Must specify the space and the metadata to fetch");
  59. }
  60. if (valueName == null) {
  61. //shift along..
  62. valueName = spaceKey;
  63. spaceKey = SELF_KEY;
  64. }
  65. if ("".equals(spaceKey)) {
  66. spaceKey = SELF_KEY;
  67. }
  68. SpaceDescription spaceDescription;
  69. if (spaceKey != null && !SELF_KEY.equals(spaceKey))
  70. spaceDescription = getSpaceDescription(spaceKey);
  71. else
  72. spaceDescription = getSpaceDescription(renderContext);
  73. MetadataValue value = getMetadataValue(spaceDescription, valueName);
  74. return renderValue(renderContext, value);
  75. } catch (Exception e) {
  76. return showRenderedExceptionString(parameters, e);
  77. }
  78. }
  79. }