PageRenderTime 67ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/org/andya/confluence/plugins/metadata/MetadataMatchesMacro.java

https://bitbucket.org/jwalton/metadata-confluence-plugin
Java | 97 lines | 56 code | 9 blank | 32 comment | 10 complexity | 17e45beb0d4d644f42651e0f9568c081 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;
  29. import com.atlassian.renderer.RenderContext;
  30. import com.atlassian.renderer.v2.macro.MacroException;
  31. import com.atlassian.renderer.v2.RenderMode;
  32. import java.util.Map;
  33. import java.util.List;
  34. import java.util.ArrayList;
  35. import org.andya.confluence.utils.MacroUtils;
  36. import org.andya.confluence.plugins.metadata.model.MetadataContent;
  37. import org.andya.confluence.plugins.metadata.model.MetadataValue;
  38. /**
  39. * This macro returns the total number of pages whose values for a particular piece of metadata
  40. * match its body. It is primarily useful for charting, e.g. how many pages match 'complete' vs
  41. * how many match 'in progress'.
  42. */
  43. public class MetadataMatchesMacro extends MetadataUsingMacro {
  44. public MetadataMatchesMacro() {
  45. super("metadata-matches");
  46. }
  47. public boolean isInline() {
  48. return true;
  49. }
  50. public boolean hasBody() {
  51. return true;
  52. }
  53. public RenderMode getBodyRenderMode() {
  54. return null;
  55. }
  56. @SuppressWarnings("unchecked")
  57. public String execute(Map parameters, String body, RenderContext renderContext) throws MacroException {
  58. try {
  59. String valueName = MacroUtils.getStringParameter(parameters, "0", true);
  60. String functionName = MacroUtils.getStringParameter(parameters, FUNCTION_PARAMETER, DEFAULT_FUNCTION);
  61. String calculationValueName = MacroUtils.getStringParameter(parameters, VALUE_NAME_PARAMETER, null);
  62. List<MetadataContent> contents = getMatchingPages(parameters, getContentService(), renderContext, false);
  63. List<Number> values = new ArrayList<Number>();
  64. int total = 0;
  65. for (MetadataContent content : contents) {
  66. MetadataValue value = content.getMetadataValue(valueName);
  67. String wikiSnippet = value != null ? value.getWikiSnippet() : "";
  68. if (body.equals(wikiSnippet)) {
  69. if (calculationValueName != null) {
  70. MetadataValue calculationValue = content.getMetadataValue(calculationValueName);
  71. Number number = calculationValue.getValueAsNumber();
  72. if (number != null)
  73. values.add(number);
  74. } else {
  75. total++;
  76. }
  77. }
  78. }
  79. if (values.isEmpty()) {
  80. return Integer.toString(total);
  81. } else {
  82. return renderCalculation(functionName, values);
  83. }
  84. } catch (Exception e) {
  85. return showUnrenderedExceptionString(parameters, e);
  86. }
  87. }
  88. }