|
Project Wonder 2.0 | ||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.webobjects.eocontrol.EOClassDescription com.webobjects.eoaccess.EOEntityClassDescription er.extensions.ERXEntityClassDescription
The main purpose of the ERXClassDescription class is
to throw 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:
This code is mainly a quick-and-dirty rewrite from PRValidation by Proteon.
{
// 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')";
}
);
}
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 attrbute's userInfo
Example:
"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";
}
Nested Class Summary | |
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
|
Nested classes inherited from class com.webobjects.eocontrol.EOClassDescription |
EOClassDescription.ClassDelegate |
Field Summary | |
static ERXEntityClassDescription.Factory |
_factory
Holds the default factory instance |
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 ERXLogger |
defaultLog
default logging support |
static ERXLogger |
log
logging support |
static ERXLogger |
validationLog
validation logging support |
Fields inherited from class com.webobjects.eoaccess.EOEntityClassDescription |
_entity |
Fields inherited from class com.webobjects.eocontrol.EOClassDescription |
_CLASS, ClassDescriptionNeededForClassNotification, ClassDescriptionNeededForEntityNameNotification, DeleteRuleCascade, DeleteRuleDeny, DeleteRuleNoAction, DeleteRuleNullify |
Constructor Summary | |
ERXEntityClassDescription(EOEntity entity)
Public constructor |
Method Summary | |
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 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 |
void |
modelFileDidChange(NSNotification n)
|
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)
|
Object |
validateValueForKey(Object obj,
String s)
This method is called to validate a value for a particular key. |
Methods inherited from class java.lang.Object |
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
public static final ERXLogger log
public static final ERXLogger validationLog
public static final ERXLogger defaultLog
protected NSDictionary _validationInfo
protected NSMutableDictionary _validationQualiferCache
protected NSMutableDictionary _initialDefaultValues
public static ERXEntityClassDescription.Factory _factory
protected String defaultKey
Constructor Detail |
public ERXEntityClassDescription(EOEntity entity)
entity
- that this class description corresponds toMethod Detail |
public 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.
obj
- enterprise object to be deleted
validation
- exception
NSValidation.ValidationException
public EOEntity entity()
protected void checkEntity()
public EOEnterpriseObject createInstanceWithEditingContext(EOEditingContext ec, EOGlobalID gid)
public void validateObjectForUpdate(EOEnterpriseObject obj) throws NSValidation.ValidationException
ERXValidationException
and that is
thrown.
obj
- enterprise object to be deleted
validation
- exception
NSValidation.ValidationException
public void validateObjectForInsert(EOEnterpriseObject obj) throws NSValidation.ValidationException
ERXValidationException
and that is
thrown.
obj
- enterprise object to be deleted
validation
- exception
NSValidation.ValidationException
public Object validateValueForKey(Object obj, String s) throws NSValidation.ValidationException
ERXValidationException
and that is
thrown.
obj
- value to be validateds
- property key to validate the value
against.
validation
- exception
NSValidation.ValidationException
public void validateObjectForSave(EOEnterpriseObject obj) throws NSValidation.ValidationException
ERXValidation.additionalValidationKeys
. If any validation
exceptions occur they are converted to an
ERXValidationException
and that is
thrown.
obj
- enterprise object to be saved
validation
- exception
NSValidation.ValidationException
protected boolean validateObjectValueDictWithInfo(ERXEntityClassDescription.ValidationObjectValue values, NSDictionary info, String cacheKey)
public void validateObjectWithUserInfo(EOEnterpriseObject object, Object value, String validationTypeString, String property)
public String displayNameForKey(String key)
key
- to be converted
public 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)
|
Last updated: Do, Dez 9, 2004 12:46 PM CET | ||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |