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 public class DictionaryValidationServiceImpl extends org.kuali.rice.krad.service.impl.DictionaryValidationServiceImpl implements DictionaryValidationService {
44 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 validateDocument(document);
54
55
56 validateBusinessObjectsFromDescriptors(document, PropertyUtils.getPropertyDescriptors(document.getClass()),
57 depth);
58 }
59
60
61
62
63
64
65 @Deprecated
66 public void validateBusinessObjectOnMaintenanceDocument(BusinessObject businessObject, String docTypeName) {
67
68 MaintenanceDocumentEntry entry = (MaintenanceDocumentEntry)
69 KRADServiceLocatorWeb.getDocumentDictionaryService().getMaintenanceDocumentEntry(docTypeName);
70 validate(new MaintenanceDocumentAttributeValueReader(businessObject, docTypeName, entry,
71 persistenceStructureService), true);
72 }
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 String attributeDataType = null;
119 try {
120 attributeDataType = getWorkflowAttributePropertyResolutionService().determineFieldDataType(
121 (Class<? extends BusinessObject>) Class.forName(
122 getDataDictionaryService().getDataDictionary().getDictionaryObjectEntry(objectClassName)
123 .getFullClassName()), attributeName);
124 } catch (ClassNotFoundException e) {
125 attributeDataType = KRADConstants.DATA_TYPE_STRING;
126 } catch (NullPointerException e) {
127 attributeDataType = KRADConstants.DATA_TYPE_STRING;
128 }
129
130 validateAttributeFormat(objectClassName, attributeName, attributeInValue, attributeDataType, errorKey);
131 }
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 boolean checkDateBounds = false;
148 Class<?> formatterClass = null;
149
150 if (LOG.isDebugEnabled()) {
151 LOG.debug("(bo, attributeName, attributeValue) = (" + objectClassName + "," + attributeName + "," +
152 attributeInValue + ")");
153 }
154
155
156
157
158
159
160 final List<String> attributeValues = SQLUtils.getCleanedSearchableValues(attributeInValue, attributeDataType);
161
162 if (attributeValues == null || attributeValues.isEmpty()) {
163 return;
164 }
165
166 for (String attributeValue : attributeValues) {
167
168
169
170
171
172
173 if (StringUtils.isNotBlank(attributeValue)) {
174 Integer minLength = getDataDictionaryService().getAttributeMinLength(objectClassName, attributeName);
175 if ((minLength != null) && (minLength.intValue() > attributeValue.length())) {
176 String errorLabel =
177 getDataDictionaryService().getAttributeErrorLabel(objectClassName, attributeName);
178 GlobalVariables.getMessageMap().putError(errorKey, RiceKeyConstants.ERROR_MIN_LENGTH,
179 new String[]{errorLabel, minLength.toString()});
180 return;
181 }
182 Integer maxLength = getDataDictionaryService().getAttributeMaxLength(objectClassName, attributeName);
183 if ((maxLength != null) && (maxLength.intValue() < attributeValue.length())) {
184 String errorLabel =
185 getDataDictionaryService().getAttributeErrorLabel(objectClassName, attributeName);
186 GlobalVariables.getMessageMap().putError(errorKey, RiceKeyConstants.ERROR_MAX_LENGTH,
187 new String[]{errorLabel, maxLength.toString()});
188 return;
189 }
190 Pattern validationExpression =
191 getDataDictionaryService().getAttributeValidatingExpression(objectClassName, attributeName);
192 if (validationExpression != null && !validationExpression.pattern().equals(".*")) {
193 if (LOG.isDebugEnabled()) {
194 LOG.debug("(bo, attributeName, validationExpression) = (" + objectClassName + "," +
195 attributeName + "," + validationExpression + ")");
196 }
197
198 if (!validationExpression.matcher(attributeValue).matches()) {
199
200 if (formatterClass == null) {
201
202 formatterClass =
203 getDataDictionaryService().getAttributeFormatter(objectClassName, attributeName);
204 }
205
206 if (formatterClass != null) {
207 boolean valuesAreValid = true;
208 boolean isError = true;
209 String errorKeyPrefix = "";
210 try {
211
212
213 if (DateFormatter.class.isAssignableFrom(formatterClass)) {
214 String[] values = attributeInValue.split("\\.\\.");
215 if (values.length == 2 &&
216 attributeValues.size() == 2) {
217 checkDateBounds = true;
218 if (attributeValues.indexOf(attributeValue) ==
219 0) {
220 errorKeyPrefix = KRADConstants.LOOKUP_RANGE_LOWER_BOUND_PROPERTY_PREFIX;
221 }
222 }
223 }
224
225 Method validatorMethod =
226 formatterClass.getDeclaredMethod(VALIDATE_METHOD, new Class<?>[]{String.class});
227 Object o = validatorMethod.invoke(formatterClass.newInstance(), attributeValue);
228 if (o instanceof Boolean) {
229 isError = !((Boolean) o).booleanValue();
230 }
231 valuesAreValid &= !isError;
232 } catch (Exception e) {
233 if (LOG.isDebugEnabled()) {
234 LOG.debug(e.getMessage(), e);
235 }
236 isError = true;
237 valuesAreValid = false;
238 }
239 if (isError) {
240 checkDateBounds = false;
241 String errorMessageKey = getDataDictionaryService()
242 .getAttributeValidatingErrorMessageKey(objectClassName, attributeName);
243 String[] errorMessageParameters = getDataDictionaryService()
244 .getAttributeValidatingErrorMessageParameters(objectClassName, attributeName);
245 GlobalVariables.getMessageMap()
246 .putError(errorKeyPrefix + errorKey, errorMessageKey, errorMessageParameters);
247 }
248 } else {
249
250 String errorMessageKey = getDataDictionaryService()
251 .getAttributeValidatingErrorMessageKey(objectClassName, attributeName);
252 String[] errorMessageParameters = getDataDictionaryService()
253 .getAttributeValidatingErrorMessageParameters(objectClassName, attributeName);
254 GlobalVariables.getMessageMap().putError(errorKey, errorMessageKey, errorMessageParameters);
255 }
256 }
257 }
258
259 String exclusiveMin =
260 getDataDictionaryService().getAttributeExclusiveMin(objectClassName, attributeName);
261 if (exclusiveMin != null) {
262 try {
263 BigDecimal exclusiveMinBigDecimal = new BigDecimal(exclusiveMin);
264 if (exclusiveMinBigDecimal.compareTo(new BigDecimal(attributeValue)) >= 0) {
265 String errorLabel =
266 getDataDictionaryService().getAttributeErrorLabel(objectClassName, attributeName);
267 GlobalVariables.getMessageMap().putError(errorKey, RiceKeyConstants.ERROR_EXCLUSIVE_MIN,
268
269 new String[]{errorLabel, exclusiveMin.toString()});
270 return;
271 }
272 } catch (NumberFormatException e) {
273
274 }
275 }
276
277 String inclusiveMax =
278 getDataDictionaryService().getAttributeInclusiveMax(objectClassName, attributeName);
279 if (inclusiveMax != null) {
280 try {
281 BigDecimal inclusiveMaxBigDecimal = new BigDecimal(inclusiveMax);
282 if (inclusiveMaxBigDecimal.compareTo(new BigDecimal(attributeValue)) < 0) {
283 String errorLabel =
284 getDataDictionaryService().getAttributeErrorLabel(objectClassName, attributeName);
285 GlobalVariables.getMessageMap().putError(errorKey, RiceKeyConstants.ERROR_INCLUSIVE_MAX,
286
287 new String[]{errorLabel, inclusiveMax.toString()});
288 return;
289 }
290 } catch (NumberFormatException e) {
291
292 }
293 }
294 }
295 }
296
297 if (checkDateBounds) {
298
299 java.sql.Timestamp lVal = null;
300 java.sql.Timestamp uVal = null;
301 try {
302 lVal = CoreApiServiceLocator.getDateTimeService().convertToSqlTimestamp(attributeValues.get(0));
303 uVal = CoreApiServiceLocator.getDateTimeService().convertToSqlTimestamp(attributeValues.get(1));
304 } catch (Exception ex) {
305
306 String errorMessageKey = getDataDictionaryService()
307 .getAttributeValidatingErrorMessageKey(objectClassName, attributeName);
308 String[] errorMessageParameters = getDataDictionaryService()
309 .getAttributeValidatingErrorMessageParameters(objectClassName, attributeName);
310 GlobalVariables.getMessageMap()
311 .putError(KRADConstants.LOOKUP_RANGE_LOWER_BOUND_PROPERTY_PREFIX + errorKey, errorMessageKey,
312 errorMessageParameters);
313 }
314
315 if (lVal != null && lVal.compareTo(uVal) > 0) {
316 String errorMessageKey = getDataDictionaryService()
317 .getAttributeValidatingErrorMessageKey(objectClassName, attributeName);
318 String[] errorMessageParameters = getDataDictionaryService()
319 .getAttributeValidatingErrorMessageParameters(objectClassName, attributeName);
320 GlobalVariables.getMessageMap()
321 .putError(KRADConstants.LOOKUP_RANGE_LOWER_BOUND_PROPERTY_PREFIX + errorKey,
322 errorMessageKey + ".range", errorMessageParameters);
323 }
324 }
325 }
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 if (attributeValue == null ||
337 (attributeValue instanceof String && StringUtils.isBlank((String) attributeValue))) {
338 Boolean required = getDataDictionaryService().isAttributeRequired(objectClassName, attributeName);
339 ControlDefinition controlDef =
340 getDataDictionaryService().getAttributeControlDefinition(objectClassName, attributeName);
341
342 if (required != null && required.booleanValue() && !(controlDef != null && controlDef.isHidden())) {
343
344
345 String errorLabel = getDataDictionaryService().getAttributeErrorLabel(objectClassName, attributeName);
346 GlobalVariables.getMessageMap().putError(errorKey, RiceKeyConstants.ERROR_REQUIRED, errorLabel);
347 }
348 }
349 }
350 }