Coverage Report - org.kuali.rice.core.api.criteria.CriteriaSupportUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
CriteriaSupportUtils
0%
0/105
0%
0/82
7.818
CriteriaSupportUtils$PropertyConstants
0%
0/1
N/A
7.818
 
 1  
 /**
 2  
  * Copyright 2005-2011 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.lang.reflect.Field;
 19  
 import java.lang.reflect.Method;
 20  
 import java.math.BigDecimal;
 21  
 import java.math.BigInteger;
 22  
 import java.util.ArrayList;
 23  
 import java.util.Calendar;
 24  
 import java.util.Collections;
 25  
 import java.util.Date;
 26  
 import java.util.HashSet;
 27  
 import java.util.List;
 28  
 import java.util.Set;
 29  
 import java.util.concurrent.atomic.AtomicInteger;
 30  
 import java.util.concurrent.atomic.AtomicLong;
 31  
 
 32  
 import javax.xml.bind.annotation.XmlElement;
 33  
 import javax.xml.bind.annotation.XmlElements;
 34  
 
 35  
 import org.apache.commons.lang.StringUtils;
 36  
 import org.joda.time.DateTime;
 37  
 
 38  
 /**
 39  
  * A class which includes various utilities and constants for use within the criteria API.
 40  
  * This class is intended to be for internal use only within the criteria API.
 41  
  * 
 42  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 43  
  */
 44  
 final class CriteriaSupportUtils {
 45  
         
 46  0
         private CriteriaSupportUtils () {}
 47  
         
 48  
     /**
 49  
      * Defines various property constants for internal use within the criteria package.
 50  
      */
 51  0
     static class PropertyConstants {
 52  
             
 53  
             /**
 54  
              * A constant representing the property name for {@link PropertyPathPredicate#getPropertyPath()}
 55  
              */
 56  
         final static String PROPERTY_PATH = "propertyPath";
 57  
         
 58  
             /**
 59  
              * A constant representing the property name for {@link SingleValuedPredicate#getValue()}
 60  
              */
 61  
         final static String VALUE = "value";
 62  
         
 63  
         /**
 64  
          * A constant representing the method name for {@link SingleValuedPredicate#getValue()}
 65  
          */
 66  
         final static String GET_VALUE_METHOD_NAME = "getValue";
 67  
 
 68  
         /**
 69  
              * A constant representing the property name for {@link MultiValuedPredicate#getValues()}
 70  
              */
 71  
         final static String VALUES = "values";
 72  
 
 73  
         /**
 74  
          * A constant representing the method name for {@link MultiValuedPredicate#getValues()}
 75  
          */
 76  
         final static String GET_VALUES_METHOD_NAME = "getValues";
 77  
     }
 78  
 
 79  
     /**
 80  
      * Validates the various properties of a {@link SingleValuedPredicate}.
 81  
      * 
 82  
      * @param valuedPredicateClass the type of the predicate
 83  
      * @param propertyPath the propertyPath which is being configured on the predicate
 84  
      * @param value the value which is being configured on the predicate
 85  
      * 
 86  
      * @throws IllegalArgumentException if the propertPath is null or blank
 87  
      * @throws IllegalArgumentException if the value is null
 88  
      * @throws IllegalArgumentException if the given {@link SingleValuedPredicate} class does not support the {@link CriteriaValue}
 89  
      */
 90  
     static void validateValuedConstruction(Class<? extends SingleValuedPredicate> valuedPredicateClass, String propertyPath, CriteriaValue<?> value) {
 91  0
             if (StringUtils.isBlank(propertyPath)) {
 92  0
                         throw new IllegalArgumentException("Property path cannot be null or blank.");
 93  
                 }
 94  0
                 if (value == null) {
 95  0
                     throw new IllegalArgumentException("CriteriaValue cannot be null.");
 96  
                 }
 97  0
                 if (!CriteriaSupportUtils.supportsCriteriaValue(valuedPredicateClass, value)) {
 98  0
                     throw new IllegalArgumentException(valuedPredicateClass.getSimpleName() + " does not support the given CriteriaValue");
 99  
                 }
 100  0
     }
 101  
 
 102  
         static boolean supportsCriteriaValue(Class<? extends SingleValuedPredicate> simplePredicateClass, CriteriaValue<?> value) {
 103  0
             if (simplePredicateClass == null) {
 104  0
                 throw new IllegalArgumentException("simplePredicateClass was null");
 105  
             }
 106  0
             if (value == null) {
 107  0
                 throw new IllegalArgumentException("valueClass was null");
 108  
             }
 109  0
             XmlElements elementsAnnotation = CriteriaSupportUtils.findXmlElementsAnnotation(simplePredicateClass);
 110  0
             if (elementsAnnotation != null) {
 111  0
                 XmlElement[] elements = elementsAnnotation.value();
 112  0
                 for (XmlElement element : elements) {
 113  0
                     if (value.getClass().equals(element.type())) {
 114  0
                         return true;
 115  
                     }
 116  
                 }
 117  
             }
 118  0
             return false;
 119  
         }
 120  
         
 121  
         private static XmlElements findXmlElementsAnnotation(Class<?> simplePredicateClass) {
 122  0
                 if (simplePredicateClass != null) {
 123  
                         try{
 124  0
                                 Field valueField = simplePredicateClass.getDeclaredField(PropertyConstants.VALUE);
 125  0
                                 XmlElements elementsAnnotation = valueField.getAnnotation(XmlElements.class);
 126  0
                                 if (elementsAnnotation != null) {
 127  0
                                         return elementsAnnotation;
 128  
                                 }
 129  0
                         } catch (NoSuchFieldException e) {
 130  
                                 // ignore, try the method
 131  0
                         }
 132  
                         try {
 133  0
                                 Method valueMethod = simplePredicateClass.getDeclaredMethod(PropertyConstants.GET_VALUE_METHOD_NAME, (Class<?>[])null);
 134  0
                                 XmlElements elementsAnnotation = valueMethod.getAnnotation(XmlElements.class);
 135  0
                                 if (elementsAnnotation == null) {
 136  0
                                         return CriteriaSupportUtils.findXmlElementsAnnotation(simplePredicateClass.getSuperclass());
 137  
                                 }
 138  0
                                 return elementsAnnotation;
 139  0
                         } catch (NoSuchMethodException e) {
 140  0
                                 return CriteriaSupportUtils.findXmlElementsAnnotation(simplePredicateClass.getSuperclass());
 141  
                         }
 142  
                 }
 143  0
                 return null;
 144  
         }
 145  
         
 146  
         static CriteriaValue<?> determineCriteriaValue(Object object) {
 147  0
                 if (object == null) {
 148  0
                         throw new IllegalArgumentException("Given criteria value cannot be null.");
 149  0
                 } else if (object instanceof CharSequence) {
 150  0
                         return new CriteriaStringValue((CharSequence)object);
 151  0
                 } else if (object instanceof DateTime) {
 152  0
             return new CriteriaDateTimeValue((DateTime)object);
 153  0
                 } else if (object instanceof Calendar) {
 154  0
                         return new CriteriaDateTimeValue((Calendar)object);
 155  0
                 } else if (object instanceof Date) {
 156  0
                         return new CriteriaDateTimeValue((Date)object);
 157  0
                 } else if (object instanceof BigInteger) {
 158  0
                         return new CriteriaIntegerValue((BigInteger)object);
 159  0
                 } else if (object instanceof Short) {
 160  0
                         return new CriteriaIntegerValue((Short)object);
 161  0
                 } else if (object instanceof Integer) {
 162  0
                         return new CriteriaIntegerValue((Integer)object);
 163  0
                 } else if (object instanceof AtomicInteger) {
 164  0
                         return new CriteriaIntegerValue((AtomicInteger)object);
 165  0
                 } else if (object instanceof Long) {
 166  0
                         return new CriteriaIntegerValue((Long)object);
 167  0
                 } else if (object instanceof AtomicLong) {
 168  0
                         return new CriteriaIntegerValue((AtomicLong)object);
 169  0
                 } else if (object instanceof BigDecimal) {
 170  0
                         return new CriteriaDecimalValue((BigDecimal)object);
 171  0
                 } else if (object instanceof Float) {
 172  0
                         return new CriteriaDecimalValue((Float)object);
 173  0
                 } else if (object instanceof Double) {
 174  0
                         return new CriteriaDecimalValue((Double)object);
 175  
                 }
 176  0
                 throw new IllegalArgumentException("Failed to translate the given object to a CriteriaValue: " + object);
 177  
         }
 178  
         
 179  
         static Set<CriteriaValue<?>> determineCriteriaValueList(Object[] values) {
 180  0
                 if (values == null) {
 181  0
                         return null;
 182  0
                 } else if (values.length == 0) {
 183  0
                         return Collections.emptySet();
 184  
                 }
 185  0
                 Set<CriteriaValue<?>> criteriaValues = new HashSet<CriteriaValue<?>>();
 186  0
                 for (Object value : values) {
 187  0
                         if (value != null) {
 188  0
                 criteriaValues.add(determineCriteriaValue(value));
 189  
             }
 190  
                 }
 191  0
                 return criteriaValues;
 192  
         }
 193  
 
 194  
     static Set<CriteriaStringValue> createCriteriaStringValueList(CharSequence[] values) {
 195  0
                 if (values == null) {
 196  0
                         return null;
 197  0
                 } else if (values.length == 0) {
 198  0
                         return Collections.emptySet();
 199  
                 }
 200  0
                 Set<CriteriaStringValue> criteriaValues = new HashSet<CriteriaStringValue>();
 201  0
                 for (CharSequence value : values) {
 202  0
                         if (value != null) {
 203  0
                 criteriaValues.add(new CriteriaStringValue(value));
 204  
             }
 205  
                 }
 206  0
                 return criteriaValues;
 207  
         }
 208  
         
 209  
         /**
 210  
      * Validates the incoming list of CriteriaValue to ensure they are valid for a
 211  
      * {@link MultiValuedPredicate}.  To be valid, the following must be true:
 212  
      * 
 213  
      * <ol>
 214  
      *   <li>The list of values must not be null.</li>
 215  
      *   <li>The list of values must not be empty.</li>
 216  
      *   <li>The list of values must all be of the same parameterized {@link CriteriaValue} type.</li>
 217  
      * </ol>
 218  
      */
 219  
     static void validateValuesForMultiValuedPredicate(Set<? extends CriteriaValue<?>> values) {
 220  0
             if (values == null) {
 221  0
                     throw new IllegalArgumentException("Criteria values cannot be null.");
 222  0
             } else if (values.isEmpty()) {
 223  0
                     throw new IllegalArgumentException("Criteria values cannot be empty.");
 224  
             }
 225  0
             Class<?> previousType = null;
 226  0
             for (CriteriaValue<?> value : values) {
 227  0
                     Class<?> currentType = value.getClass();
 228  0
                     if (previousType != null) {
 229  0
                             if (!currentType.equals(previousType)) {
 230  0
                                     throw new IllegalArgumentException("Encountered criteria values which do not match.  One was: " + previousType + " the other was: " + currentType);
 231  
                             }
 232  
                     }
 233  0
                     previousType = currentType;
 234  0
             }
 235  0
     }
 236  
 
 237  
     static String findDynName(String name) {
 238  0
         String correctedName = StringUtils.uncapitalize(name).replace("Predicate", "");
 239  
         //null is a keyword therefore they are called isNull & isNotNull
 240  0
         if (correctedName.equals("null")) {
 241  0
             correctedName = "isNull";
 242  0
         } else if (correctedName.equals("notNull")) {
 243  0
             correctedName = "isNotNull";
 244  
         }
 245  0
         return correctedName;
 246  
     }
 247  
 
 248  
     public static String toString(SingleValuedPredicate p) {
 249  0
         return new StringBuilder(CriteriaSupportUtils.findDynName(p.getClass().getSimpleName())).append("(")
 250  
                 .append(p.getPropertyPath()).append(", ").append(p.getValue().getValue()).append(")").toString();
 251  
     }
 252  
 
 253  
     public static String toString(MultiValuedPredicate p) {
 254  0
         final List<String> values = new ArrayList<String>();
 255  0
         for (CriteriaValue<?> value : p.getValues()) {
 256  0
             values.add(value.getValue().toString());
 257  
         }
 258  
 
 259  0
         return new StringBuilder(CriteriaSupportUtils.findDynName(p.getClass().getSimpleName())).append("(")
 260  
                 .append(p.getPropertyPath()).append(", ").append("[").append(StringUtils.join(values, ", ")).append("]").append(")").toString();
 261  
     }
 262  
 }