| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 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.List; |
| 27 | |
import java.util.concurrent.atomic.AtomicInteger; |
| 28 | |
import java.util.concurrent.atomic.AtomicLong; |
| 29 | |
|
| 30 | |
import javax.xml.bind.annotation.XmlElement; |
| 31 | |
import javax.xml.bind.annotation.XmlElements; |
| 32 | |
|
| 33 | |
import org.apache.commons.lang.StringUtils; |
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
final class CriteriaSupportUtils { |
| 42 | |
|
| 43 | 0 | private CriteriaSupportUtils () {} |
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | 0 | static class PropertyConstants { |
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
final static String PROPERTY_PATH = "propertyPath"; |
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
final static String VALUE = "value"; |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
final static String GET_VALUE_METHOD_NAME = "getValue"; |
| 64 | |
} |
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
static void validateValuedExpressionConstruction(Class<? extends ValuedExpression> valuedExpressionClass, String propertyPath, CriteriaValue<?> value) { |
| 78 | 0 | if (StringUtils.isBlank(propertyPath)) { |
| 79 | 0 | throw new IllegalArgumentException("Property path cannot be null or blank."); |
| 80 | |
} |
| 81 | 0 | if (value == null) { |
| 82 | 0 | throw new IllegalArgumentException("CriteriaValue cannot be null."); |
| 83 | |
} |
| 84 | 0 | if (!CriteriaSupportUtils.supportsCriteriaValue(valuedExpressionClass, value)) { |
| 85 | 0 | throw new IllegalArgumentException(valuedExpressionClass.getSimpleName() + " does not support the given CriteriaValue"); |
| 86 | |
} |
| 87 | 0 | } |
| 88 | |
|
| 89 | |
static boolean supportsCriteriaValue(Class<? extends ValuedExpression> simpleExpressionClass, CriteriaValue<?> value) { |
| 90 | 0 | if (simpleExpressionClass == null) { |
| 91 | 0 | throw new IllegalArgumentException("simpleExpressionClass was null"); |
| 92 | |
} |
| 93 | 0 | if (value == null) { |
| 94 | 0 | throw new IllegalArgumentException("valueClass was null"); |
| 95 | |
} |
| 96 | 0 | XmlElements elementsAnnotation = CriteriaSupportUtils.findXmlElementsAnnotation(simpleExpressionClass); |
| 97 | 0 | if (elementsAnnotation != null) { |
| 98 | 0 | XmlElement[] elements = elementsAnnotation.value(); |
| 99 | 0 | for (XmlElement element : elements) { |
| 100 | 0 | if (value.getClass().equals(element.type())) { |
| 101 | 0 | return true; |
| 102 | |
} |
| 103 | |
} |
| 104 | |
} |
| 105 | 0 | return false; |
| 106 | |
} |
| 107 | |
|
| 108 | |
private static XmlElements findXmlElementsAnnotation(Class<?> simpleExpressionClass) { |
| 109 | 0 | if (simpleExpressionClass != null) { |
| 110 | |
try{ |
| 111 | 0 | Field valueField = simpleExpressionClass.getDeclaredField(PropertyConstants.VALUE); |
| 112 | 0 | XmlElements elementsAnnotation = valueField.getAnnotation(XmlElements.class); |
| 113 | 0 | if (elementsAnnotation != null) { |
| 114 | 0 | return elementsAnnotation; |
| 115 | |
} |
| 116 | 0 | } catch (NoSuchFieldException e) { |
| 117 | |
|
| 118 | 0 | } |
| 119 | |
try { |
| 120 | 0 | Method valueMethod = simpleExpressionClass.getDeclaredMethod(PropertyConstants.GET_VALUE_METHOD_NAME, (Class<?>[])null); |
| 121 | 0 | XmlElements elementsAnnotation = valueMethod.getAnnotation(XmlElements.class); |
| 122 | 0 | if (elementsAnnotation == null) { |
| 123 | 0 | return CriteriaSupportUtils.findXmlElementsAnnotation(simpleExpressionClass.getSuperclass()); |
| 124 | |
} |
| 125 | 0 | return elementsAnnotation; |
| 126 | 0 | } catch (NoSuchMethodException e) { |
| 127 | 0 | return CriteriaSupportUtils.findXmlElementsAnnotation(simpleExpressionClass.getSuperclass()); |
| 128 | |
} |
| 129 | |
} |
| 130 | 0 | return null; |
| 131 | |
} |
| 132 | |
|
| 133 | |
static CriteriaValue<?> determineCriteriaValue(Object object) { |
| 134 | 0 | if (object == null) { |
| 135 | 0 | throw new IllegalArgumentException("Given criteria value cannot be null."); |
| 136 | 0 | } else if (object instanceof CharSequence) { |
| 137 | 0 | return new CriteriaStringValue((CharSequence)object); |
| 138 | 0 | } else if (object instanceof Calendar) { |
| 139 | 0 | return new CriteriaDateTimeValue((Calendar)object); |
| 140 | 0 | } else if (object instanceof Date) { |
| 141 | 0 | return new CriteriaDateTimeValue((Date)object); |
| 142 | 0 | } else if (object instanceof BigInteger) { |
| 143 | 0 | return new CriteriaIntegerValue((BigInteger)object); |
| 144 | 0 | } else if (object instanceof Short) { |
| 145 | 0 | return new CriteriaIntegerValue((Short)object); |
| 146 | 0 | } else if (object instanceof Integer) { |
| 147 | 0 | return new CriteriaIntegerValue((Integer)object); |
| 148 | 0 | } else if (object instanceof AtomicInteger) { |
| 149 | 0 | return new CriteriaIntegerValue((AtomicInteger)object); |
| 150 | 0 | } else if (object instanceof Long) { |
| 151 | 0 | return new CriteriaIntegerValue((Long)object); |
| 152 | 0 | } else if (object instanceof AtomicLong) { |
| 153 | 0 | return new CriteriaIntegerValue((AtomicLong)object); |
| 154 | 0 | } else if (object instanceof BigDecimal) { |
| 155 | 0 | return new CriteriaDecimalValue((BigDecimal)object); |
| 156 | 0 | } else if (object instanceof Float) { |
| 157 | 0 | return new CriteriaDecimalValue((Float)object); |
| 158 | 0 | } else if (object instanceof Double) { |
| 159 | 0 | return new CriteriaDecimalValue((Double)object); |
| 160 | |
} |
| 161 | 0 | throw new IllegalArgumentException("Failed to translate the given object to a CriteriaValue: " + object); |
| 162 | |
} |
| 163 | |
|
| 164 | |
static List<CriteriaValue<?>> determineCriteriaValueList(List<? extends Object> values) { |
| 165 | 0 | if (values == null) { |
| 166 | 0 | return null; |
| 167 | 0 | } else if (values.isEmpty()) { |
| 168 | 0 | return Collections.emptyList(); |
| 169 | |
} |
| 170 | 0 | List<CriteriaValue<?>> criteriaValues = new ArrayList<CriteriaValue<?>>(); |
| 171 | 0 | for (Object value : values) { |
| 172 | 0 | criteriaValues.add(determineCriteriaValue(value)); |
| 173 | |
} |
| 174 | 0 | return criteriaValues; |
| 175 | |
} |
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
|
| 187 | |
static void validateValuesForMultiValuedExpression(List<? extends CriteriaValue<?>> values) { |
| 188 | 0 | if (values == null) { |
| 189 | 0 | throw new IllegalArgumentException("Criteria values cannot be null."); |
| 190 | 0 | } else if (values.isEmpty()) { |
| 191 | 0 | throw new IllegalArgumentException("Criteria values cannot be empty."); |
| 192 | |
} |
| 193 | 0 | Class<?> previousType = null; |
| 194 | 0 | for (CriteriaValue<?> value : values) { |
| 195 | 0 | Class<?> currentType = value.getClass(); |
| 196 | 0 | if (previousType != null) { |
| 197 | 0 | if (!currentType.equals(previousType)) { |
| 198 | 0 | throw new IllegalArgumentException("Encountered criteria values which do not match. One was: " + previousType + " the other was: " + currentType); |
| 199 | |
} |
| 200 | |
} |
| 201 | 0 | previousType = currentType; |
| 202 | 0 | } |
| 203 | 0 | } |
| 204 | |
|
| 205 | |
} |