public class ERXEntityClassDescription extends 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 |
EOClassDescription.ClassDelegate
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 |
_entity
_CLASS, ClassDescriptionNeededForClassNotification, ClassDescriptionNeededForEntityNameNotification, DeleteRuleCascade, DeleteRuleDeny, DeleteRuleNoAction, DeleteRuleNullify
Constructor and Description |
---|
ERXEntityClassDescription(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(EOEnterpriseObject eo,
EOEditingContext ec) |
protected void |
checkEntity() |
EOEnterpriseObject |
createInstanceWithEditingContext(EOEditingContext ec,
EOGlobalID gid) |
String |
displayNameForKey(String key)
Calculates a display name for a key using
localization of entityname.key if found
otherwise an improved method.
|
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(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(EORelationship rel,
String defaultValue) |
void |
setDefaultValuesInObject(EOEnterpriseObject eo,
EOEditingContext ec) |
void |
validateObjectForDelete(EOEnterpriseObject obj)
This method is called when an object is
about to be deleted.
|
void |
validateObjectForInsert(EOEnterpriseObject obj)
This method is called when an object is
about to be inserted.
|
void |
validateObjectForSave(EOEnterpriseObject obj)
This method is called when an object is
about to be saved.
|
void |
validateObjectForUpdate(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(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(EOEntity entity)
entity
- that this class description corresponds topublic static ERXEntityClassDescription.Factory factory()
public static void registerDescription()
public void modelFileDidChange(NSNotification n)
public void validateObjectForDelete(EOEnterpriseObject obj) throws NSValidation.ValidationException
ERXValidationException
and that is
thrown.validateObjectForDelete
in class EOEntityClassDescription
obj
- enterprise object to be deletedNSValidation.ValidationException
- validation exceptionpublic EOEntity entity()
entity
in class EOEntityClassDescription
protected void checkEntity()
public EOEnterpriseObject createInstanceWithEditingContext(EOEditingContext ec, EOGlobalID gid)
createInstanceWithEditingContext
in class EOEntityClassDescription
public void validateObjectForUpdate(EOEnterpriseObject obj) throws NSValidation.ValidationException
ERXValidationException
and that is
thrown.obj
- enterprise object to be deletedNSValidation.ValidationException
- validation exceptionpublic void validateObjectForInsert(EOEnterpriseObject obj) throws NSValidation.ValidationException
ERXValidationException
and that is
thrown.obj
- enterprise object to be deletedNSValidation.ValidationException
- validation exceptionpublic Object validateValueForKey(Object obj, String s) throws NSValidation.ValidationException
ERXValidationException
and that is
thrown.validateValueForKey
in class EOEntityClassDescription
obj
- value to be validateds
- property key to validate the value
against.NSValidation.ValidationException
- validation exceptionpublic void validateObjectForSave(EOEnterpriseObject obj) throws NSValidation.ValidationException
ERXValidation.additionalValidationKeys
. If any validation
exceptions occur they are converted to an
ERXValidationException
and that is
thrown.validateObjectForSave
in class EOClassDescription
obj
- enterprise object to be savedNSValidation.ValidationException
- validation exceptionprotected boolean validateObjectValueDictWithInfo(ERXEntityClassDescription.ValidationObjectValue values, NSDictionary info, String cacheKey)
public void validateObjectWithUserInfo(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 EOClassDescription
key
- to be convertedpublic void readDefaultValues()
public void setDefaultAttributeValue(EOAttribute attr, String defaultValue)
public void setDefaultRelationshipValue(EORelationship rel, String defaultValue)
public void setDefaultValuesInObject(EOEnterpriseObject eo, EOEditingContext ec)
public void awakeObjectFromInsertion(EOEnterpriseObject eo, EOEditingContext ec)
awakeObjectFromInsertion
in class EOEntityClassDescription
public String inverseForRelationshipKey(String relationshipKey)
inverseForRelationshipKey
in class EOEntityClassDescription
public Class _enforcedKVCNumberClassForKey(String key)
_enforcedKVCNumberClassForKey
in class 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 – 2024 Project Wonder.