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

/src/main/java/org/randombits/filtering/confluence/param/criteria/CriterionParameterGroup.java

https://bitbucket.org/ianchiu/filtering-confluence
Java | 141 lines | 64 code | 16 blank | 61 comment | 6 complexity | b373bbc5f0902e11d6858490b68b9971 MD5 | raw file
  1. /*
  2. * Copyright (c) 2006, David Peterson
  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
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * * Neither the name of "randombits.org" nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. * POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. package org.randombits.filtering.confluence.param.criteria;
  30. import java.util.Collection;
  31. import java.util.List;
  32. import org.randombits.filtering.confluence.param.ListQueryParameter;
  33. import org.randombits.filtering.confluence.param.ParameterException;
  34. import org.randombits.filtering.confluence.param.criteria.content.ContentScopeParameter;
  35. import org.randombits.filtering.confluence.param.criteria.content.ContentTypeParameter;
  36. import org.randombits.filtering.confluence.param.criteria.content.LabelParameter;
  37. import org.randombits.filtering.confluence.param.criteria.content.SpaceKeyParameter;
  38. import org.randombits.filtering.core.criteria.Criteria;
  39. import org.randombits.filtering.core.criteria.Criterion;
  40. import org.randombits.filtering.core.criteria.GroupCriteria;
  41. import org.randombits.filtering.core.criteria.SourceCriterion;
  42. import org.randombits.support.confluence.LinkAssistant;
  43. import org.randombits.support.confluence.MacroInfo;
  44. import org.randombits.support.core.convert.ConversionAssistant;
  45. import com.atlassian.confluence.core.ListQuery;
  46. /**
  47. * A support class for grouping CriterionParameters together and producing a
  48. * single Criteria group for them.
  49. *
  50. * @author David Peterson
  51. */
  52. public class CriterionParameterGroup {
  53. private List<CriterionParameter> params;
  54. public CriterionParameterGroup() {
  55. params = new java.util.ArrayList<CriterionParameter>();
  56. }
  57. public void addParameter( CriterionParameter parameter ) {
  58. params.add( parameter );
  59. }
  60. public Criteria createCriteria( MacroInfo info ) throws ParameterException {
  61. return createCriteria( info, true );
  62. }
  63. public Criteria createCriteria( MacroInfo info, boolean matchAll ) throws ParameterException {
  64. Criteria criteria = new GroupCriteria( matchAll );
  65. return addCriteria( criteria, info );
  66. }
  67. /**
  68. * Adds the criteria from this group to the specified criteria.
  69. *
  70. * @param criteria The criteria to add.
  71. * @param info The Macro info.
  72. * @return
  73. */
  74. public Criteria addCriteria( Criteria criteria, MacroInfo info ) throws ParameterException {
  75. for ( CriterionParameter parameter : params ) {
  76. Criterion criterion = parameter.createCriterion( info );
  77. if ( criterion != null )
  78. criteria.addCriterion( criterion );
  79. }
  80. return criteria;
  81. }
  82. /**
  83. * Checks the criteria to see if they support {@link SourceCriterion} and
  84. * returns the set of values that match all the provided criteria. If an
  85. * <code>null</code> is returned, some of the criteria do not support
  86. * finding values, so the caller must apply the criteria themselves.
  87. *
  88. * @param info The macro info.
  89. * @param matchAll If true, all criteria from all parameters must match.
  90. * Otherwise, any may match.
  91. * @return the matching values.
  92. * @throws ParameterException if there were any issues with the parameters in the group.
  93. */
  94. public Collection<?> getMatchingValues( MacroInfo info, boolean matchAll ) throws ParameterException {
  95. Criteria criteria = createCriteria( info, matchAll );
  96. if ( criteria instanceof SourceCriterion<?> ) {
  97. SourceCriterion<?> source = (SourceCriterion<?>) criteria;
  98. return source.getMatchingValues();
  99. }
  100. return null;
  101. }
  102. /**
  103. * Prepares the {@link com.atlassian.confluence.core.ListQuery} by passing it into all parameters that
  104. * implement the {@link ListQueryParameter} interface.
  105. *
  106. * @param query The query.
  107. * @param info The macro info.
  108. * @throws ParameterException if there is a problem.
  109. */
  110. public void prepareListQuery( ListQuery query, MacroInfo info ) throws ParameterException {
  111. for ( CriterionParameter parameter : params ) {
  112. if ( parameter instanceof ListQueryParameter ) {
  113. ( (ListQueryParameter<?>) parameter ).prepareListQuery( query, info );
  114. }
  115. }
  116. }
  117. public static CriterionParameterGroup standardPageFilters( LinkAssistant linkAssistant, ConversionAssistant conversionAssistant ) {
  118. CriterionParameterGroup params = new CriterionParameterGroup();
  119. params.addParameter( new ContentTypeParameter() );
  120. params.addParameter( new LabelParameter() );
  121. params.addParameter( new ContentScopeParameter( conversionAssistant, linkAssistant ) );
  122. params.addParameter( new SpaceKeyParameter() );
  123. return params;
  124. }
  125. }