View Javadoc
1   /**
2    * Copyright 2005-2015 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.core.api.criteria;
17  
18  import java.util.Collections;
19  import java.util.HashSet;
20  import java.util.Set;
21  
22  import javax.xml.bind.annotation.XmlAccessType;
23  import javax.xml.bind.annotation.XmlAccessorType;
24  import javax.xml.bind.annotation.XmlElement;
25  import javax.xml.bind.annotation.XmlElements;
26  import javax.xml.bind.annotation.XmlType;
27  
28  /**
29   * An abstract implementation of a {@link CompositePredicate}.  This class defines all of the JAXB
30   * annotations such that sub-classes should not have to.
31   *
32   * <p>If a class subclasses this class then it *MUST* be sure to add itself to the JAXB
33   * annotations for {@link #predicates}
34   *
35   * @author Kuali Rice Team (rice.collab@kuali.org)
36   */
37  @XmlAccessorType(XmlAccessType.NONE)
38  @XmlType(name = AbstractCompositePredicate.Constants.TYPE_NAME)
39  abstract class AbstractCompositePredicate extends AbstractPredicate implements CompositePredicate {
40  
41      private static final long serialVersionUID = 6164560054223588779L;
42      private static final String LINE_SEPARATOR = System.getProperty("line.separator");
43  
44      /**
45       * Defines the JAXB annotations for the List of predicates.  All supported predicates *MUST* be
46       * included in this List in order for them to be supported in the XML schema.
47       *
48       * If a new type of predicate is created it *MUST* be added to this list.
49       */
50      @XmlElements(value = {
51              @XmlElement(name = AndPredicate.Constants.ROOT_ELEMENT_NAME, type = AndPredicate.class, required = false),
52              @XmlElement(name = EqualPredicate.Constants.ROOT_ELEMENT_NAME, type = EqualPredicate.class, required = false),
53              @XmlElement(name = EqualIgnoreCasePredicate.Constants.ROOT_ELEMENT_NAME, type = EqualIgnoreCasePredicate.class, required = false),
54              @XmlElement(name = ExistsSubQueryPredicate.Constants.ROOT_ELEMENT_NAME, type = ExistsSubQueryPredicate.class, required = false),
55              @XmlElement(name = GreaterThanPredicate.Constants.ROOT_ELEMENT_NAME, type = GreaterThanPredicate.class, required = false),
56              @XmlElement(name = GreaterThanOrEqualPredicate.Constants.ROOT_ELEMENT_NAME, type = GreaterThanOrEqualPredicate.class, required = false),
57              @XmlElement(name = InPredicate.Constants.ROOT_ELEMENT_NAME, type = InPredicate.class, required = false),
58              @XmlElement(name = InIgnoreCasePredicate.Constants.ROOT_ELEMENT_NAME, type = InIgnoreCasePredicate.class, required = false),
59              @XmlElement(name = LessThanPredicate.Constants.ROOT_ELEMENT_NAME, type = LessThanPredicate.class, required = false),
60              @XmlElement(name = LessThanOrEqualPredicate.Constants.ROOT_ELEMENT_NAME, type = LessThanOrEqualPredicate.class, required = false),
61              @XmlElement(name = LikePredicate.Constants.ROOT_ELEMENT_NAME, type = LikePredicate.class, required = false),
62              @XmlElement(name = LikeIgnoreCasePredicate.Constants.ROOT_ELEMENT_NAME, type = LikeIgnoreCasePredicate.class, required = false),
63              @XmlElement(name = NotEqualPredicate.Constants.ROOT_ELEMENT_NAME, type = NotEqualPredicate.class, required = false),
64              @XmlElement(name = NotEqualIgnoreCasePredicate.Constants.ROOT_ELEMENT_NAME, type = NotEqualIgnoreCasePredicate.class, required = false),
65              @XmlElement(name = NotInPredicate.Constants.ROOT_ELEMENT_NAME, type = NotInPredicate.class, required = false),
66              @XmlElement(name = NotInIgnoreCasePredicate.Constants.ROOT_ELEMENT_NAME, type = NotInIgnoreCasePredicate.class, required = false),
67              @XmlElement(name = NotLikeIgnoreCasePredicate.Constants.ROOT_ELEMENT_NAME, type = NotLikeIgnoreCasePredicate.class, required = false),
68              @XmlElement(name = NotLikePredicate.Constants.ROOT_ELEMENT_NAME, type = NotLikePredicate.class, required = false),
69              @XmlElement(name = NotNullPredicate.Constants.ROOT_ELEMENT_NAME, type = NotNullPredicate.class, required = false),
70              @XmlElement(name = NullPredicate.Constants.ROOT_ELEMENT_NAME, type = NullPredicate.class, required = false),
71              @XmlElement(name = OrPredicate.Constants.ROOT_ELEMENT_NAME, type = OrPredicate.class, required = false)
72      })
73      private final Set<Predicate> predicates;
74  
75  	/**
76  	 * This default constructor exists only to be invoked by sub-classes
77  	 * in their default constructors which is used by JAXB.
78  	 */
79      AbstractCompositePredicate() {
80          this.predicates = null;
81      }
82  
83      /**
84       * When invoked by a subclass, this constructor will set the predicates
85       * to the given set. If the set is null then it will be translated
86       * internally to an empty set.
87       *
88       * @param predicates the list of predicates to set
89       */
90      AbstractCompositePredicate(final Set<Predicate> predicates) {
91          if (predicates == null) {
92              this.predicates = Collections.emptySet();
93          } else {
94              final Set<Predicate> temp = new HashSet<Predicate>();
95              for (Predicate predicate: predicates) {
96                  if (predicate != null) {
97                      temp.add(predicate);
98                  }
99              }
100             this.predicates = Collections.unmodifiableSet(temp);
101         }
102     }
103 
104     @Override
105     public Set<Predicate> getPredicates() {
106         return Collections.unmodifiableSet(predicates);
107     }
108 
109     /**
110      * Defines some internal constants used on this class.
111      */
112     static class Constants {
113         final static String TYPE_NAME = "CompositePredicateType";
114     }
115 
116     @Override
117     public final String toString() {
118         StringBuilder b = new StringBuilder(CriteriaSupportUtils.findDynName(this.getClass().getSimpleName()));
119         b.append("(");
120         if (!predicates.isEmpty()) {
121             for (Predicate p : predicates) {
122                 b.append(LINE_SEPARATOR);
123                 //b.append("\t");
124                 b.append(p);
125                 b.append(", ");
126             }
127             b.deleteCharAt(b.lastIndexOf(", "));
128             b.append(LINE_SEPARATOR);
129         }
130         b.append(')');
131         return  b.toString();
132     }
133 }