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