| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.rice.kns.service.impl; |
| 17 | |
|
| 18 | |
import org.apache.commons.beanutils.PropertyUtils; |
| 19 | |
import org.apache.commons.lang.StringUtils; |
| 20 | |
import org.kuali.rice.core.api.CoreApiServiceLocator; |
| 21 | |
import org.kuali.rice.core.api.util.RiceKeyConstants; |
| 22 | |
import org.kuali.rice.core.framework.persistence.jdbc.sql.SQLUtils; |
| 23 | |
import org.kuali.rice.core.web.format.DateFormatter; |
| 24 | |
import org.kuali.rice.kns.datadictionary.MaintenanceDocumentEntry; |
| 25 | |
import org.kuali.rice.kns.datadictionary.validation.MaintenanceDocumentAttributeValueReader; |
| 26 | |
import org.kuali.rice.kns.service.DictionaryValidationService; |
| 27 | |
import org.kuali.rice.krad.bo.BusinessObject; |
| 28 | |
import org.kuali.rice.krad.datadictionary.control.ControlDefinition; |
| 29 | |
import org.kuali.rice.krad.document.Document; |
| 30 | |
import org.kuali.rice.krad.service.KRADServiceLocatorWeb; |
| 31 | |
import org.kuali.rice.krad.util.GlobalVariables; |
| 32 | |
import org.kuali.rice.krad.util.KRADConstants; |
| 33 | |
|
| 34 | |
import java.lang.reflect.Method; |
| 35 | |
import java.math.BigDecimal; |
| 36 | |
import java.util.List; |
| 37 | |
import java.util.regex.Pattern; |
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
@Deprecated |
| 43 | 0 | public class DictionaryValidationServiceImpl extends org.kuali.rice.krad.service.impl.DictionaryValidationServiceImpl implements DictionaryValidationService { |
| 44 | 0 | private static org.apache.log4j.Logger LOG = |
| 45 | |
org.apache.log4j.Logger.getLogger(DictionaryValidationServiceImpl.class); |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
@Deprecated |
| 51 | |
public void validateDocumentRecursively(Document document, int depth) { |
| 52 | |
|
| 53 | 0 | validateDocument(document); |
| 54 | |
|
| 55 | |
|
| 56 | 0 | validateBusinessObjectsFromDescriptors(document, PropertyUtils.getPropertyDescriptors(document.getClass()), |
| 57 | |
depth); |
| 58 | 0 | } |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
@Deprecated |
| 66 | |
public void validateBusinessObjectOnMaintenanceDocument(BusinessObject businessObject, String docTypeName) { |
| 67 | |
|
| 68 | 0 | MaintenanceDocumentEntry entry = (MaintenanceDocumentEntry) |
| 69 | |
KRADServiceLocatorWeb.getDocumentDictionaryService().getMaintenanceDocumentEntry(docTypeName); |
| 70 | 0 | validate(new MaintenanceDocumentAttributeValueReader(businessObject, docTypeName, entry, |
| 71 | |
persistenceStructureService), true); |
| 72 | 0 | } |
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
@Deprecated |
| 115 | |
public void validateAttributeFormat(String objectClassName, String attributeName, String attributeInValue, |
| 116 | |
String errorKey) { |
| 117 | |
|
| 118 | 0 | String attributeDataType = null; |
| 119 | |
try { |
| 120 | 0 | attributeDataType = getWorkflowAttributePropertyResolutionService().determineFieldDataType( |
| 121 | |
(Class<? extends BusinessObject>) Class.forName( |
| 122 | |
getDataDictionaryService().getDataDictionary().getDictionaryObjectEntry(objectClassName) |
| 123 | |
.getFullClassName()), attributeName); |
| 124 | 0 | } catch (ClassNotFoundException e) { |
| 125 | 0 | attributeDataType = KRADConstants.DATA_TYPE_STRING; |
| 126 | 0 | } catch (NullPointerException e) { |
| 127 | 0 | attributeDataType = KRADConstants.DATA_TYPE_STRING; |
| 128 | 0 | } |
| 129 | |
|
| 130 | 0 | validateAttributeFormat(objectClassName, attributeName, attributeInValue, attributeDataType, errorKey); |
| 131 | 0 | } |
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
@Deprecated |
| 145 | |
public void validateAttributeFormat(String objectClassName, String attributeName, String attributeInValue, |
| 146 | |
String attributeDataType, String errorKey) { |
| 147 | 0 | boolean checkDateBounds = false; |
| 148 | 0 | Class<?> formatterClass = null; |
| 149 | |
|
| 150 | 0 | if (LOG.isDebugEnabled()) { |
| 151 | 0 | LOG.debug("(bo, attributeName, attributeValue) = (" + objectClassName + "," + attributeName + "," + |
| 152 | |
attributeInValue + ")"); |
| 153 | |
} |
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | 0 | final List<String> attributeValues = SQLUtils.getCleanedSearchableValues(attributeInValue, attributeDataType); |
| 161 | |
|
| 162 | 0 | if (attributeValues == null || attributeValues.isEmpty()) { |
| 163 | 0 | return; |
| 164 | |
} |
| 165 | |
|
| 166 | 0 | for (String attributeValue : attributeValues) { |
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | 0 | if (StringUtils.isNotBlank(attributeValue)) { |
| 174 | 0 | Integer minLength = getDataDictionaryService().getAttributeMinLength(objectClassName, attributeName); |
| 175 | 0 | if ((minLength != null) && (minLength.intValue() > attributeValue.length())) { |
| 176 | 0 | String errorLabel = |
| 177 | |
getDataDictionaryService().getAttributeErrorLabel(objectClassName, attributeName); |
| 178 | 0 | GlobalVariables.getMessageMap().putError(errorKey, RiceKeyConstants.ERROR_MIN_LENGTH, |
| 179 | |
new String[]{errorLabel, minLength.toString()}); |
| 180 | 0 | return; |
| 181 | |
} |
| 182 | 0 | Integer maxLength = getDataDictionaryService().getAttributeMaxLength(objectClassName, attributeName); |
| 183 | 0 | if ((maxLength != null) && (maxLength.intValue() < attributeValue.length())) { |
| 184 | 0 | String errorLabel = |
| 185 | |
getDataDictionaryService().getAttributeErrorLabel(objectClassName, attributeName); |
| 186 | 0 | GlobalVariables.getMessageMap().putError(errorKey, RiceKeyConstants.ERROR_MAX_LENGTH, |
| 187 | |
new String[]{errorLabel, maxLength.toString()}); |
| 188 | 0 | return; |
| 189 | |
} |
| 190 | 0 | Pattern validationExpression = |
| 191 | |
getDataDictionaryService().getAttributeValidatingExpression(objectClassName, attributeName); |
| 192 | 0 | if (validationExpression != null && !validationExpression.pattern().equals(".*")) { |
| 193 | 0 | if (LOG.isDebugEnabled()) { |
| 194 | 0 | LOG.debug("(bo, attributeName, validationExpression) = (" + objectClassName + "," + |
| 195 | |
attributeName + "," + validationExpression + ")"); |
| 196 | |
} |
| 197 | |
|
| 198 | 0 | if (!validationExpression.matcher(attributeValue).matches()) { |
| 199 | |
|
| 200 | 0 | if (formatterClass == null) { |
| 201 | |
|
| 202 | 0 | formatterClass = |
| 203 | |
getDataDictionaryService().getAttributeFormatter(objectClassName, attributeName); |
| 204 | |
} |
| 205 | |
|
| 206 | 0 | if (formatterClass != null) { |
| 207 | 0 | boolean valuesAreValid = true; |
| 208 | 0 | boolean isError = true; |
| 209 | 0 | String errorKeyPrefix = ""; |
| 210 | |
try { |
| 211 | |
|
| 212 | |
|
| 213 | 0 | if (DateFormatter.class.isAssignableFrom(formatterClass)) { |
| 214 | 0 | String[] values = attributeInValue.split("\\.\\."); |
| 215 | 0 | if (values.length == 2 && |
| 216 | |
attributeValues.size() == 2) { |
| 217 | 0 | checkDateBounds = true; |
| 218 | 0 | if (attributeValues.indexOf(attributeValue) == |
| 219 | |
0) { |
| 220 | 0 | errorKeyPrefix = KRADConstants.LOOKUP_RANGE_LOWER_BOUND_PROPERTY_PREFIX; |
| 221 | |
} |
| 222 | |
} |
| 223 | |
} |
| 224 | |
|
| 225 | 0 | Method validatorMethod = |
| 226 | |
formatterClass.getDeclaredMethod(VALIDATE_METHOD, new Class<?>[]{String.class}); |
| 227 | 0 | Object o = validatorMethod.invoke(formatterClass.newInstance(), attributeValue); |
| 228 | 0 | if (o instanceof Boolean) { |
| 229 | 0 | isError = !((Boolean) o).booleanValue(); |
| 230 | |
} |
| 231 | 0 | valuesAreValid &= !isError; |
| 232 | 0 | } catch (Exception e) { |
| 233 | 0 | if (LOG.isDebugEnabled()) { |
| 234 | 0 | LOG.debug(e.getMessage(), e); |
| 235 | |
} |
| 236 | 0 | isError = true; |
| 237 | 0 | valuesAreValid = false; |
| 238 | 0 | } |
| 239 | 0 | if (isError) { |
| 240 | 0 | checkDateBounds = false; |
| 241 | 0 | String errorMessageKey = getDataDictionaryService() |
| 242 | |
.getAttributeValidatingErrorMessageKey(objectClassName, attributeName); |
| 243 | 0 | String[] errorMessageParameters = getDataDictionaryService() |
| 244 | |
.getAttributeValidatingErrorMessageParameters(objectClassName, attributeName); |
| 245 | 0 | GlobalVariables.getMessageMap() |
| 246 | |
.putError(errorKeyPrefix + errorKey, errorMessageKey, errorMessageParameters); |
| 247 | |
} |
| 248 | 0 | } else { |
| 249 | |
|
| 250 | 0 | String errorMessageKey = getDataDictionaryService() |
| 251 | |
.getAttributeValidatingErrorMessageKey(objectClassName, attributeName); |
| 252 | 0 | String[] errorMessageParameters = getDataDictionaryService() |
| 253 | |
.getAttributeValidatingErrorMessageParameters(objectClassName, attributeName); |
| 254 | 0 | GlobalVariables.getMessageMap().putError(errorKey, errorMessageKey, errorMessageParameters); |
| 255 | |
} |
| 256 | |
} |
| 257 | |
} |
| 258 | |
|
| 259 | 0 | String exclusiveMin = |
| 260 | |
getDataDictionaryService().getAttributeExclusiveMin(objectClassName, attributeName); |
| 261 | 0 | if (exclusiveMin != null) { |
| 262 | |
try { |
| 263 | 0 | BigDecimal exclusiveMinBigDecimal = new BigDecimal(exclusiveMin); |
| 264 | 0 | if (exclusiveMinBigDecimal.compareTo(new BigDecimal(attributeValue)) >= 0) { |
| 265 | 0 | String errorLabel = |
| 266 | |
getDataDictionaryService().getAttributeErrorLabel(objectClassName, attributeName); |
| 267 | 0 | GlobalVariables.getMessageMap().putError(errorKey, RiceKeyConstants.ERROR_EXCLUSIVE_MIN, |
| 268 | |
|
| 269 | |
new String[]{errorLabel, exclusiveMin.toString()}); |
| 270 | 0 | return; |
| 271 | |
} |
| 272 | 0 | } catch (NumberFormatException e) { |
| 273 | |
|
| 274 | 0 | } |
| 275 | |
} |
| 276 | |
|
| 277 | 0 | String inclusiveMax = |
| 278 | |
getDataDictionaryService().getAttributeInclusiveMax(objectClassName, attributeName); |
| 279 | 0 | if (inclusiveMax != null) { |
| 280 | |
try { |
| 281 | 0 | BigDecimal inclusiveMaxBigDecimal = new BigDecimal(inclusiveMax); |
| 282 | 0 | if (inclusiveMaxBigDecimal.compareTo(new BigDecimal(attributeValue)) < 0) { |
| 283 | 0 | String errorLabel = |
| 284 | |
getDataDictionaryService().getAttributeErrorLabel(objectClassName, attributeName); |
| 285 | 0 | GlobalVariables.getMessageMap().putError(errorKey, RiceKeyConstants.ERROR_INCLUSIVE_MAX, |
| 286 | |
|
| 287 | |
new String[]{errorLabel, inclusiveMax.toString()}); |
| 288 | 0 | return; |
| 289 | |
} |
| 290 | 0 | } catch (NumberFormatException e) { |
| 291 | |
|
| 292 | 0 | } |
| 293 | |
} |
| 294 | 0 | } |
| 295 | |
} |
| 296 | |
|
| 297 | 0 | if (checkDateBounds) { |
| 298 | |
|
| 299 | 0 | java.sql.Timestamp lVal = null; |
| 300 | 0 | java.sql.Timestamp uVal = null; |
| 301 | |
try { |
| 302 | 0 | lVal = CoreApiServiceLocator.getDateTimeService().convertToSqlTimestamp(attributeValues.get(0)); |
| 303 | 0 | uVal = CoreApiServiceLocator.getDateTimeService().convertToSqlTimestamp(attributeValues.get(1)); |
| 304 | 0 | } catch (Exception ex) { |
| 305 | |
|
| 306 | 0 | String errorMessageKey = getDataDictionaryService() |
| 307 | |
.getAttributeValidatingErrorMessageKey(objectClassName, attributeName); |
| 308 | 0 | String[] errorMessageParameters = getDataDictionaryService() |
| 309 | |
.getAttributeValidatingErrorMessageParameters(objectClassName, attributeName); |
| 310 | 0 | GlobalVariables.getMessageMap() |
| 311 | |
.putError(KRADConstants.LOOKUP_RANGE_LOWER_BOUND_PROPERTY_PREFIX + errorKey, errorMessageKey, |
| 312 | |
errorMessageParameters); |
| 313 | 0 | } |
| 314 | |
|
| 315 | 0 | if (lVal != null && lVal.compareTo(uVal) > 0) { |
| 316 | 0 | String errorMessageKey = getDataDictionaryService() |
| 317 | |
.getAttributeValidatingErrorMessageKey(objectClassName, attributeName); |
| 318 | 0 | String[] errorMessageParameters = getDataDictionaryService() |
| 319 | |
.getAttributeValidatingErrorMessageParameters(objectClassName, attributeName); |
| 320 | 0 | GlobalVariables.getMessageMap() |
| 321 | |
.putError(KRADConstants.LOOKUP_RANGE_LOWER_BOUND_PROPERTY_PREFIX + errorKey, |
| 322 | |
errorMessageKey + ".range", errorMessageParameters); |
| 323 | |
} |
| 324 | |
} |
| 325 | 0 | } |
| 326 | |
|
| 327 | |
|
| 328 | |
|
| 329 | |
|
| 330 | |
|
| 331 | |
|
| 332 | |
@Deprecated |
| 333 | |
public void validateAttributeRequired(String objectClassName, String attributeName, Object attributeValue, |
| 334 | |
Boolean forMaintenance, String errorKey) { |
| 335 | |
|
| 336 | 0 | if (attributeValue == null || |
| 337 | |
(attributeValue instanceof String && StringUtils.isBlank((String) attributeValue))) { |
| 338 | 0 | Boolean required = getDataDictionaryService().isAttributeRequired(objectClassName, attributeName); |
| 339 | 0 | ControlDefinition controlDef = |
| 340 | |
getDataDictionaryService().getAttributeControlDefinition(objectClassName, attributeName); |
| 341 | |
|
| 342 | 0 | if (required != null && required.booleanValue() && !(controlDef != null && controlDef.isHidden())) { |
| 343 | |
|
| 344 | |
|
| 345 | 0 | String errorLabel = getDataDictionaryService().getAttributeErrorLabel(objectClassName, attributeName); |
| 346 | 0 | GlobalVariables.getMessageMap().putError(errorKey, RiceKeyConstants.ERROR_REQUIRED, errorLabel); |
| 347 | |
} |
| 348 | |
} |
| 349 | 0 | } |
| 350 | |
} |