public class ERXEntityClassDescription
extends com.webobjects.eoaccess.EOEntityClassDescription
ERXValidationException
s instead of the
usual NSValidation.ValidationException
objects. See the
ERXValidationException and ERXValidationFactory class
for more information about localized and templatized
validation exceptions. This class is configured to
register itself as the class description by calling
the method registerDescription
. This method
is called when the principal class of this framework is
loaded. This happens really early so you shouldn't have
to worry about this at all.
Additionally, this class allows for model driven validations in a "poor-mans-Validity-way":
add a ERXValidation
user info entry on your entity.
This is an example:
"ERXValidation" = {
// these keys are evaluated on validateForSave, they don't correspond to properties
additionalValidationKeys = ("validateEmailPassword");
// This dictionary holds the keys to use for validating properties
validateForKey =
{
// these keys are evaluated on validateForSave, they don't correspond to properties
email =
(
{
// this is the message code into ValidationStrings.plist
// User.email.wrongLength = "The mail does not have the right size (5 to 50)";
message = "wrongLength";
// skip this rule if the value is null
ignoreIfNull = true;
// if there is a qualifier key, then a dictionary containing "object" and "value" is evaluated
// and an exception is thrown if the evaluation returns false
qualifier = "(value.length >= 5) AND (value.length < 50)";
},
{
// again, this is the message code into ValidationStrings.plist
message = "sampleTest";
// Given this key, an object of the corresponding EOQualifierEvaluation subclass is created
// and given this dictionary on creation. This object needs to be re-entrant.
className = "SampleTest";
// an example is:
// public class SampleTest implements EOQualifierEvaluation {
// int minLength, maxLength;
// public SampleTest(Object param) {
// NSDictionary dict = (NSDictionary)param;
// minLength = ERXValueUtilities.intValue(dict.objectForKey("minLength"));
// maxLength = ERXValueUtilities.intValue(dict.objectForKey("maxLength"));
// }
// public boolean evaluateObject(Object o) {
// ERXEntityClassDescription.ValidationObjectValue val
// = (ERXEntityClassDescription.ValidationObjectValue)o;
// EOEnterpriseObject eo = val.object();
// String value = (String)val.value();
// return value.length() >= minLength && value.length() <= maxLength;
// }
// }
minLength = "5";
maxLength = "10";
}
);
// This key does not correspond to any property, it get's evaluated in D2WApps where you have a
// multi-step page and need to do validation before validateForSave
"validateEmailPassword" =
(
{
message = "stupidTestWithEmailAndPassword";
// means to get D2W to highlight the fields involved instead of only displaying the message
// For this to work, your corresponding localized String should be
// User.email,password.stupidTestWithEmailAndPassword = "Stupid test failed";
keyPaths = "email,password";
qualifier = "(object.email.length >= object.password.length)";
}
);
};
// These get checked when the object gets saved, additionally to "additionalValidations"
// The structure of "validateForInsert", "validateForUpdate" and "validateForDelete" is the same.
validateForSave =
(
{
message = "cantBeBoth";
keyPaths = "isEditor,isAdmin";
qualifier = "(object.isEditor = 'Y' and object.isAdmin = 'Y')";
}
);
}
If you have validation methods in your EO classes (e.g. validateName(String name)
for attribute name)
be aware that those are executed first and that they possibly coerce the value to validate (e.g. making a string uppercase).
The validations done by this class will be executed on those potentially coerced values.
This code is mainly a quick-and-dirty rewrite from PRValidation by Proteon.
Additionally, this class adds a concept of "Default" values that get pushed into the object at creation time. Simply add a "ERXDefaultValues" key into the entity's userInfo dictionary that contains key-value-pairs for every default you want to set. Alternately, you can set a "default" key on each of the relationship or attribute's userInfo.
"ERXDefaultValues" = {
// Example for simple values.
isAdmin = N;
isEditor = Y;
// Example for a related object (->Languages(pk,displayName)). You need to enter the primary key value.
language = "de";
// Example for an NSArray of related objects
recommendingUser = "@threadStorage.actor";
// Example for an NSArray
articlesToRevisit = "@threadStorage.actor.articles";
// Example for a NSTimestamp. All static methods from ERXTimestampUtilities are supported.
created = "@now";
updatePassword = "@tomorrow";
}
If you wish to provide your own class description subclass
see the documentation associated with the Factory inner class.Modifier and Type | Class and Description |
---|---|
static class |
ERXEntityClassDescription.AttributeDefault |
static interface |
ERXEntityClassDescription.Default |
static class |
ERXEntityClassDescription.Factory
This factory inner class is registered as the observer
for three notifications: modelWasAdded, classDescriptionNeededForEntity
and classDescriptionNeededForClass.
|
static class |
ERXEntityClassDescription.QualiferValidation |
static class |
ERXEntityClassDescription.RelationshipDefault |
static class |
ERXEntityClassDescription.ValidationObjectValue |
Modifier and Type | Field and Description |
---|---|
protected NSMutableDictionary |
_initialDefaultValues
Holds default values
|
protected NSDictionary |
_validationInfo
Holds validation info from the entities user info dictionary
|
protected NSMutableDictionary |
_validationQualiferCache
Holds validation qualifiers
|
protected String |
defaultKey
default handling
|
static org.slf4j.Logger |
defaultLog
default logging support
|
static String |
ValidateEntityClassAvailability |
Constructor and Description |
---|
ERXEntityClassDescription(com.webobjects.eoaccess.EOEntity entity)
Public constructor
|
Modifier and Type | Method and Description |
---|---|
void |
_addPartialClass(Class<ERXPartial> partialClass)
Associates a partial entity class with this entity.
|
Class |
_enforcedKVCNumberClassForKey(String key) |
void |
awakeObjectFromInsertion(com.webobjects.eocontrol.EOEnterpriseObject eo,
com.webobjects.eocontrol.EOEditingContext ec) |
protected void |
checkEntity() |
com.webobjects.eocontrol.EOEnterpriseObject |
createInstanceWithEditingContext(com.webobjects.eocontrol.EOEditingContext ec,
com.webobjects.eocontrol.EOGlobalID gid) |
String |
displayNameForKey(String key)
Calculates a display name for a key using
localization of entityname.key if found
otherwise an improved method.
|
com.webobjects.eoaccess.EOEntity |
entity()
Overridden to perform a check if the entity is still in a model group.
|
static ERXEntityClassDescription.Factory |
factory()
getter for the factory
|
String |
inverseForRelationshipKey(String relationshipKey) |
String |
localizedKey(String key) |
void |
modelFileDidChange(com.webobjects.foundation.NSNotification n) |
NSArray<Class<ERXPartial>> |
partialClasses()
Returns the list of partial entity classes for this entity.
|
void |
readDefaultValues() |
static void |
registerDescription()
This method is called by the principal class
of the framework when the framework's NSBundle is
loaded.
|
void |
setDefaultAttributeValue(EOAttribute attr,
String defaultValue) |
void |
setDefaultRelationshipValue(com.webobjects.eoaccess.EORelationship rel,
String defaultValue) |
void |
setDefaultValuesInObject(com.webobjects.eocontrol.EOEnterpriseObject eo,
com.webobjects.eocontrol.EOEditingContext ec) |
void |
validateObjectForDelete(com.webobjects.eocontrol.EOEnterpriseObject obj)
This method is called when an object is
about to be deleted.
|
void |
validateObjectForInsert(com.webobjects.eocontrol.EOEnterpriseObject obj)
This method is called when an object is
about to be inserted.
|
void |
validateObjectForSave(com.webobjects.eocontrol.EOEnterpriseObject obj)
This method is called when an object is
about to be saved.
|
void |
validateObjectForUpdate(com.webobjects.eocontrol.EOEnterpriseObject obj)
This method is called when an object is
about to be updated.
|
protected boolean |
validateObjectValueDictWithInfo(ERXEntityClassDescription.ValidationObjectValue values,
NSDictionary info,
String cacheKey) |
void |
validateObjectWithUserInfo(com.webobjects.eocontrol.EOEnterpriseObject object,
Object value,
String validationTypeString,
String property)
Validates a specific property of an EO by applying the rules found in the userInfo
of the entity i.e.
|
Object |
validateValueForKey(Object obj,
String s)
This method is called to validate a value
for a particular key.
|
_awakeFromRegistration, _concreteSubClassDescriptionForDeferredFault, _globalIDWithEntityName, _newDictionaryForProperties, allAttributeKeys, allPropertyKeys, allToManyRelationshipKeys, allToOneRelationshipKeys, attributeKeys, classDescriptionForDestinationKey, classForAttributeKey, clientAttributeKeys, clientToManyRelationshipKeys, clientToOneRelationshipKeys, defaultFormatterForKey, deleteRuleForRelationshipKey, entityName, fetchSpecificationNamed, isEntityReadOnly, ownsDestinationObjectsForRelationshipKey, readResolve, superClassDescription, toManyRelationshipKeys, toOneRelationshipKeys, validateObjectForSave
_decimalFormatter, _existingClassDescriptionForClass, _integerFormatter, _timestampFormatter, awakeObjectFromFetch, classDelegate, classDescriptionForClass, classDescriptionForEntityName, classDescriptionForKeyPath, defaultFormatterForKeyPath, finalize, invalidateClassDescriptionCache, propagateDeleteForObject, registerClassDescription, setClassDelegate, toString, userPresentableDescriptionForObject
public static final org.slf4j.Logger defaultLog
protected NSDictionary _validationInfo
protected NSMutableDictionary _validationQualiferCache
protected NSMutableDictionary _initialDefaultValues
public static final String ValidateEntityClassAvailability
protected String defaultKey
public ERXEntityClassDescription(com.webobjects.eoaccess.EOEntity entity)
entity
- that this class description corresponds topublic static ERXEntityClassDescription.Factory factory()
public static void registerDescription()
public void modelFileDidChange(com.webobjects.foundation.NSNotification n)
public void validateObjectForDelete(com.webobjects.eocontrol.EOEnterpriseObject obj) throws com.webobjects.foundation.NSValidation.ValidationException
ERXValidationException
and that is
thrown.validateObjectForDelete
in class com.webobjects.eoaccess.EOEntityClassDescription
obj
- enterprise object to be deletedNSValidation.ValidationException
- validation exceptionpublic com.webobjects.eoaccess.EOEntity entity()
entity
in class com.webobjects.eoaccess.EOEntityClassDescription
protected void checkEntity()
public com.webobjects.eocontrol.EOEnterpriseObject createInstanceWithEditingContext(com.webobjects.eocontrol.EOEditingContext ec, com.webobjects.eocontrol.EOGlobalID gid)
createInstanceWithEditingContext
in class com.webobjects.eoaccess.EOEntityClassDescription
public void validateObjectForUpdate(com.webobjects.eocontrol.EOEnterpriseObject obj) throws com.webobjects.foundation.NSValidation.ValidationException
ERXValidationException
and that is
thrown.obj
- enterprise object to be deletedNSValidation.ValidationException
- validation exceptionpublic void validateObjectForInsert(com.webobjects.eocontrol.EOEnterpriseObject obj) throws com.webobjects.foundation.NSValidation.ValidationException
ERXValidationException
and that is
thrown.obj
- enterprise object to be deletedNSValidation.ValidationException
- validation exceptionpublic Object validateValueForKey(Object obj, String s) throws com.webobjects.foundation.NSValidation.ValidationException
ERXValidationException
and that is
thrown.validateValueForKey
in class com.webobjects.eoaccess.EOEntityClassDescription
obj
- value to be validateds
- property key to validate the value
against.NSValidation.ValidationException
- validation exceptionpublic void validateObjectForSave(com.webobjects.eocontrol.EOEnterpriseObject obj) throws com.webobjects.foundation.NSValidation.ValidationException
ERXValidation.additionalValidationKeys
. If any validation
exceptions occur they are converted to an
ERXValidationException
and that is
thrown.validateObjectForSave
in class com.webobjects.eocontrol.EOClassDescription
obj
- enterprise object to be savedNSValidation.ValidationException
- validation exceptionprotected boolean validateObjectValueDictWithInfo(ERXEntityClassDescription.ValidationObjectValue values, NSDictionary info, String cacheKey)
public void validateObjectWithUserInfo(com.webobjects.eocontrol.EOEnterpriseObject object, Object value, String validationTypeString, String property)
object
- the EO validation is done forvalue
- the value to validatevalidationTypeString
- the key for the validation info from userInfoproperty
- the property key to validatepublic String displayNameForKey(String key)
displayNameForKey
in class com.webobjects.eocontrol.EOClassDescription
key
- to be convertedpublic void readDefaultValues()
public void setDefaultAttributeValue(EOAttribute attr, String defaultValue)
public void setDefaultRelationshipValue(com.webobjects.eoaccess.EORelationship rel, String defaultValue)
public void setDefaultValuesInObject(com.webobjects.eocontrol.EOEnterpriseObject eo, com.webobjects.eocontrol.EOEditingContext ec)
public void awakeObjectFromInsertion(com.webobjects.eocontrol.EOEnterpriseObject eo, com.webobjects.eocontrol.EOEditingContext ec)
awakeObjectFromInsertion
in class com.webobjects.eoaccess.EOEntityClassDescription
public String inverseForRelationshipKey(String relationshipKey)
inverseForRelationshipKey
in class com.webobjects.eoaccess.EOEntityClassDescription
public Class _enforcedKVCNumberClassForKey(String key)
_enforcedKVCNumberClassForKey
in class com.webobjects.eoaccess.EOEntityClassDescription
public void _addPartialClass(Class<ERXPartial> partialClass)
partialClass
- the partial class to associateer.extensions.partials
public NSArray<Class<ERXPartial>> partialClasses()
er.extensions.partials
Copyright © 2002 – 2020 Project Wonder.