org.kuali.rice.kns.datadictionary.validation
Class AttributeValidatingTypeServiceBase

java.lang.Object
  extended by org.kuali.rice.kns.datadictionary.validation.AttributeValidatingTypeServiceBase
Direct Known Subclasses:
KrmsTypeServiceBase

public abstract class AttributeValidatingTypeServiceBase
extends Object

An abstract base class for type service implementations which provides default validation of attributes from the Data Dictionary. It attempts to remain module independent by requiring the translation of the attribute definitions to a generic format that includes the required RemotableAttributeFields as an unimplemented template method, seegetTypeAttributeDefinitions(String).

Note that any RemotableAttributeErrors returned from here should be fully resolved to the messages to be displayed to the user (in other words, they should not contain error keys). The same approach should be taken by subclasses since the message resources may not be present on the remote server that is invoking this service. There is a createErrorString(String, String...) utility method that can be used to resolve errorKeys and format them appropriately.

Author:
Kuali Rice Team (rice.collab@kuali.org)

Nested Class Summary
protected static class AttributeValidatingTypeServiceBase.TypeAttributeDefinition
          A module-independent representation of a type attribute containing all the information that we need in order to validate data dictionary-based attributes.
protected static class AttributeValidatingTypeServiceBase.TypeAttributeValidationException
           
 
Constructor Summary
AttributeValidatingTypeServiceBase()
           
 
Method Summary
protected  String createErrorString(String errorKey, String... params)
          creates an error String from the given errorKey and parameters.
protected  List<String> extractErrorsFromGlobalVariablesErrorMap(String attributeName)
          Removes all errors for the given attributeName from the global error map, transforms them as appropriate and returns them as a List of Strings.
protected static String getAttributeErrorLabel(RemotableAttributeField definition)
          Returns a String suitable for use in error messages to represent the given attribute.
protected  Formatter getAttributeFormatter(RemotableAttributeField field)
          Gets a Formatter appropriate for the data type of the given field.
protected  Pattern getAttributeValidatingExpression(RemotableAttributeField field)
          Gets the validation Pattern for the given RemotableAttributeField.
protected  ConfigurationService getConfigurationService()
           
protected  DataDictionaryRemoteFieldService getDataDictionaryRemoteFieldService()
           
protected  DataDictionaryService getDataDictionaryService()
           
protected  DictionaryValidationService getDictionaryValidationService()
           
protected abstract  List<AttributeValidatingTypeServiceBase.TypeAttributeDefinition> getTypeAttributeDefinitions(String typeId)
          Retrieves active type attribute definitions and translates them into a module-independent representation.
protected  List<RemotableAttributeError> validateAttributeFormat(RemotableAttributeField field, String objectClassName, String attributeName, String attributeValue, String errorKey)
          Validates the format of the value for the given attribute field.
protected  List<RemotableAttributeError> validateAttributeRequired(RemotableAttributeField field, String objectClassName, String attributeName, Object attributeValue, String errorKey)
          Validates required-ness of an attribute against its corresponding value
 List<RemotableAttributeError> validateAttributes(String typeId, Map<String,String> attributes)
          This is the default implementation.
protected  List<RemotableAttributeError> validateDataDictionaryAttribute(AttributeValidatingTypeServiceBase.TypeAttributeDefinition typeAttributeDefinition, String attributeName, String value)
          Validates the attribute value for the given AttributeValidatingTypeServiceBase.TypeAttributeDefinition having a componentName.
protected abstract  List<RemotableAttributeError> validateNonDataDictionaryAttribute(RemotableAttributeField attr, String key, String value)
          Validates an attribute that is *not* mapped to a data dictionary component via AttributeValidatingTypeServiceBase.TypeAttributeDefinition.componentName and AttributeValidatingTypeServiceBase.TypeAttributeDefinition.name.
protected  List<RemotableAttributeError> validatePrimitiveAttributeFromDescriptor(AttributeValidatingTypeServiceBase.TypeAttributeDefinition typeAttributeDefinition, String componentName, Object object, PropertyDescriptor propertyDescriptor)
          Validates a data dictionary mapped attribute for a primitive property.
protected  List<RemotableAttributeError> validateReferencesExistAndActive(Map<String,AttributeValidatingTypeServiceBase.TypeAttributeDefinition> typeAttributeDefinitionMap, Map<String,String> attributes, List<RemotableAttributeError> previousValidationErrors)
          Cross-validates referenced components amongst attributes to ensure they refer to existing and active business objects.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AttributeValidatingTypeServiceBase

public AttributeValidatingTypeServiceBase()
Method Detail

getTypeAttributeDefinitions

protected abstract List<AttributeValidatingTypeServiceBase.TypeAttributeDefinition> getTypeAttributeDefinitions(String typeId)
Retrieves active type attribute definitions and translates them into a module-independent representation. Note that they should be returned in the order desired for display.

Parameters:
typeId - the identifier for the type
Returns:
a correctly ordered List of active, module-independent type attribute definitions

validateNonDataDictionaryAttribute

protected abstract List<RemotableAttributeError> validateNonDataDictionaryAttribute(RemotableAttributeField attr,
                                                                                    String key,
                                                                                    String value)
Validates an attribute that is *not* mapped to a data dictionary component via AttributeValidatingTypeServiceBase.TypeAttributeDefinition.componentName and AttributeValidatingTypeServiceBase.TypeAttributeDefinition.name.

Parameters:
attr - the RemotableAttributeField for which to validate.
key - the attribute name
value - the attribute value
Returns:
a List of RemotableAttributeErrors with fully resolved error messages (not error keys). May return null or an empty List if no errors are encountered.

validateAttributes

public List<RemotableAttributeError> validateAttributes(String typeId,
                                                        Map<String,String> attributes)

This is the default implementation. It calls into the service for each attribute to validate it there. No combination validation is done. That should be done by overriding this method.

This implementation calls getTypeAttributeDefinitions(String) to retrieve module-agnostic representations. It then iterates through the entry set of attributes, and calls validateNonDataDictionaryAttribute(org.kuali.rice.core.api.uif.RemotableAttributeField, String, String) or validateDataDictionaryAttribute(org.kuali.rice.kns.datadictionary.validation.AttributeValidatingTypeServiceBase.TypeAttributeDefinition, String, String) as appropriate. Lastly it calls validateReferencesExistAndActive(java.util.Map, java.util.Map, java.util.List).

Parameters:
typeId - the identifier for the type
attributes - the Map of attribute names to values
Returns:
the List of errors (RemotableAttributeErrors) encountered during validation.

validateReferencesExistAndActive

protected List<RemotableAttributeError> validateReferencesExistAndActive(Map<String,AttributeValidatingTypeServiceBase.TypeAttributeDefinition> typeAttributeDefinitionMap,
                                                                         Map<String,String> attributes,
                                                                         List<RemotableAttributeError> previousValidationErrors)

Cross-validates referenced components amongst attributes to ensure they refer to existing and active business objects.

This implementation instantiates any components mapped by attributes, populates them as best it can, and then uses the DataDictionaryService to get relationship information. Then, through the DictionaryValidationService it attempts to ensure that any referenced business objects mapped by other attributes exist and are active. It pulls any errors encountered out of the global error map via calls to extractErrorsFromGlobalVariablesErrorMap(String)

TODO: who can explain this? :-)

Parameters:
typeAttributeDefinitionMap - a Map from attribute name to AttributeValidatingTypeServiceBase.TypeAttributeDefinition containing all of the attribute definitions for this type.
attributes - the Map of attribute names to values
previousValidationErrors - a List of previously encountered errors used to short circuit testing on attributes that are already known to have errors.
Returns:
the List of errors encountered. Cannot return null.

getAttributeErrorLabel

protected static String getAttributeErrorLabel(RemotableAttributeField definition)

Returns a String suitable for use in error messages to represent the given attribute.

This implementation returns a String of the format "longLabel (shortLabel)" where those fields are pulled from the passed in definition.

Parameters:
definition - the definition for which to create an error label.
Returns:
the error label String.

createErrorString

protected String createErrorString(String errorKey,
                                   String... params)

creates an error String from the given errorKey and parameters.

This implementation will attempt to resolve the errorKey using the ConfigurationService, and format it with the provided params using MessageFormat.format(String, Object...). If the errorKey can't be resolved, it will return a string like the following: errorKey:param1;param2;param3;

Parameters:
errorKey - the errorKey
params - the error params
Returns:
error string

validatePrimitiveAttributeFromDescriptor

protected List<RemotableAttributeError> validatePrimitiveAttributeFromDescriptor(AttributeValidatingTypeServiceBase.TypeAttributeDefinition typeAttributeDefinition,
                                                                                 String componentName,
                                                                                 Object object,
                                                                                 PropertyDescriptor propertyDescriptor)

Validates a data dictionary mapped attribute for a primitive property.

This implementation checks that the attribute is defined using the DataDictionaryService if it is from a specific set of types defined in TypeUtils. Then, if the value is not blank, it checks for errors by calling validateAttributeFormat(org.kuali.rice.core.api.uif.RemotableAttributeField, String, String, String, String). If it is blank, it checks for errors by calling validateAttributeRequired(org.kuali.rice.core.api.uif.RemotableAttributeField, String, String, Object, String) .

Parameters:
typeAttributeDefinition - the definition for the attribute
componentName - the data dictionary component name
object - the instance of the component
propertyDescriptor - the descriptor for the property that the attribute maps to
Returns:
a List of errors (RemotableAttributeErrors) encountered during validation. Cannot return null.

validateAttributeRequired

protected List<RemotableAttributeError> validateAttributeRequired(RemotableAttributeField field,
                                                                  String objectClassName,
                                                                  String attributeName,
                                                                  Object attributeValue,
                                                                  String errorKey)

Validates required-ness of an attribute against its corresponding value

This implementation checks if an attribute value is null or blank, and if so checks if the RemotableAttributeField is required. If it is, a RemotableAttributeError is created with the message populated by a call to createErrorString(String, String...).

Parameters:
field - the field for the attribute being tested
objectClassName - the class name for the component
attributeName - the name of the attribute
attributeValue - the value of the attribute
errorKey - the errorKey used to identify the field
Returns:
the List of errors (RemotableAttributeErrors) encountered during validation. Cannot return null.

getAttributeValidatingExpression

protected Pattern getAttributeValidatingExpression(RemotableAttributeField field)

Gets the validation Pattern for the given RemotableAttributeField.

This implementation checks if there is a regexConstraint set on the field, and if so it compiles a Pattern (with no special flags) using it. Otherwise, it returns a pattern that always matches.

Parameters:
field - the field for which to return a validation Pattern.
Returns:
the compiled Pattern to use in validation the given field.

getAttributeFormatter

protected Formatter getAttributeFormatter(RemotableAttributeField field)

Gets a Formatter appropriate for the data type of the given field.

This implementation returns null if RemotableAttributeField.getDataType() returns null. Otherwise, it returns the result of calling Formatter.getFormatter(Class) on the DataType's type

Parameters:
field - the field for which to provide a Formatter.
Returns:
an applicable Formatter, or null if one can't be found.

validateAttributeFormat

protected List<RemotableAttributeError> validateAttributeFormat(RemotableAttributeField field,
                                                                String objectClassName,
                                                                String attributeName,
                                                                String attributeValue,
                                                                String errorKey)

Validates the format of the value for the given attribute field.

This implementation checks if the attribute value is not blank, in which case it checks (as applicable) the max length, min length, min value, max value, and format (using the Pattern returned by getAttributeValidatingExpression(org.kuali.rice.core.api.uif.RemotableAttributeField)). If that doesn't match, it will use the Formatter returned by getAttributeFormatter(org.kuali.rice.core.api.uif.RemotableAttributeField) to format the value and try matching against it again. For each format error that is found, createErrorString(String, String...) is called to prepare the text for the RemotableAttributeError that is generated.

Parameters:
field - the field for the attribute whose value we are validating
objectClassName - the name of the class to which the attribute belongs
attributeName - the name of the attribute
attributeValue - the String value whose format we are validating
errorKey - the name of the property on the object class that this attribute maps to
Returns:
a List containing any errors (RemotableAttributeErrors) that are detected.

extractErrorsFromGlobalVariablesErrorMap

protected List<String> extractErrorsFromGlobalVariablesErrorMap(String attributeName)

Removes all errors for the given attributeName from the global error map, transforms them as appropriate and returns them as a List of Strings.

This implementation iterates through any errors found in the error map, transforms them by calling createErrorString(String, String...) and adds them to the List that is then returned

Parameters:
attributeName - the attribute name for which to extract errors from the global error map.
Returns:
a List of error Strings

validateDataDictionaryAttribute

protected List<RemotableAttributeError> validateDataDictionaryAttribute(AttributeValidatingTypeServiceBase.TypeAttributeDefinition typeAttributeDefinition,
                                                                        String attributeName,
                                                                        String value)

Validates the attribute value for the given AttributeValidatingTypeServiceBase.TypeAttributeDefinition having a componentName.

This implementation instantiates a component object using reflection on the class name specified in the AttributeValidatingTypeServiceBase.TypeAttributeDefinitions componentName, gets a PropertyDescriptor for the attribute of the component object, hydrates the attribute's value from it's String form, sets that value on the component object, and then delegates to validatePrimitiveAttributeFromDescriptor(org.kuali.rice.kns.datadictionary.validation.AttributeValidatingTypeServiceBase.TypeAttributeDefinition, String, Object, java.beans.PropertyDescriptor).

Parameters:
typeAttributeDefinition -
attributeName -
value -
Returns:

getDictionaryValidationService

protected DictionaryValidationService getDictionaryValidationService()

getDataDictionaryService

protected DataDictionaryService getDataDictionaryService()

getDataDictionaryRemoteFieldService

protected DataDictionaryRemoteFieldService getDataDictionaryRemoteFieldService()

getConfigurationService

protected ConfigurationService getConfigurationService()


Copyright © 2005-2013 The Kuali Foundation. All Rights Reserved.