PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/org/andya/confluence/utils/page/PageLocator.java

https://bitbucket.org/jwalton/metadata-confluence-plugin
Java | 90 lines | 44 code | 7 blank | 39 comment | 21 complexity | 511b11fb5697d433233b2fa65c30ca02 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.utils.page;
  29. import java.util.List;
  30. import com.atlassian.confluence.pages.Page;
  31. import com.atlassian.renderer.v2.macro.MacroException;
  32. /**
  33. * Decides which page sources need to be used. Different combinations apply - see
  34. * http://confluence.atlassian.com/display/DISC/Unified+Page+Searching+Tags for more detail.
  35. *
  36. * @author kelsey
  37. *
  38. */
  39. public class PageLocator implements PageSource {
  40. public List<Page> getPages(PageSearchContext context) throws MacroException {
  41. // compatibility..
  42. if(context.hasParameter(LABEL_PARAMETER)) {
  43. context.setParameter(LABELS_PARAMETER, context.getStringParameter(LABEL_PARAMETER, true));
  44. }
  45. final PageSource total;
  46. //this is a nasty matrix of possibilities. Bear with me, and review
  47. //the wiki link above, as it makes each combination a little clearer.
  48. if(!context.hasParameter(ROOT_PARAMETER)) {
  49. context.setParameter(ROOT_PARAMETER, SELF_KEY); // not used in case 2, mind.
  50. //case 1:
  51. if(!context.hasParameter(PAGES_PARAMETER) && !context.hasParameter(LABELS_PARAMETER)) {
  52. context.setParameter(PAGES_PARAMETER, CHILDREN_KEY);
  53. total = new PagesPageSource(new RootPageSource());
  54. }//case 2
  55. else if(!context.hasParameter(PAGES_PARAMETER) && context.hasParameter(LABELS_PARAMETER)) {
  56. total = new LabelPageSource();
  57. }//case 3
  58. else if(context.hasParameter(PAGES_PARAMETER) && !context.hasParameter(LABELS_PARAMETER)) {
  59. total = new PagesPageSource(new RootPageSource());
  60. }//case 4
  61. else { // pages != null & labels != null...
  62. total = new LabelFilterPageSource(new PagesPageSource(new RootPageSource()));
  63. }
  64. } else {
  65. //case 5
  66. if(!context.hasParameter(PAGES_PARAMETER) && !context.hasParameter(LABELS_PARAMETER)) {
  67. context.setParameter(PAGES_PARAMETER, IDENTITY_KEY);
  68. total = new PagesPageSource(new RootPageSource());
  69. } //case 6
  70. else if(!context.hasParameter(PAGES_PARAMETER) && context.hasParameter(LABELS_PARAMETER)) {
  71. context.setParameter(PAGES_PARAMETER, IDENTITY_KEY);
  72. total = new LabelFilterPageSource(new PagesPageSource(new RootPageSource()));
  73. } //case 7
  74. else if(context.hasParameter(PAGES_PARAMETER) && !context.hasParameter(LABELS_PARAMETER)) {
  75. total = new PagesPageSource(new RootPageSource());
  76. } //case 8
  77. else {
  78. total = new LabelFilterPageSource(new PagesPageSource(new RootPageSource()));
  79. }
  80. }
  81. return total.getPages(context);
  82. }
  83. }