Project Wonder 2.0

er.extensions
Class ERXEntityClassDescription

java.lang.Object
  extended bycom.webobjects.eocontrol.EOClassDescription
      extended bycom.webobjects.eoaccess.EOEntityClassDescription
          extended byer.extensions.ERXEntityClassDescription
All Implemented Interfaces:
Serializable

public class ERXEntityClassDescription
extends EOEntityClassDescription

The main purpose of the ERXClassDescription class is to throw ERXValidationExceptions 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:

 {
     // 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')";
          }
          );
 }
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 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";

 }

If you wish to provide your own class description subclass see the documentation associated with the Factory inner class.

See Also:
Serialized Form

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 com.webobjects.eoaccess.EOEntityClassDescription
_awakeFromRegistration, _concreteSubClassDescriptionForDeferredFault, _enforcedKVCNumberClassForKey, _globalIDWithEntityName, _newDictionaryForProperties, allAttributeKeys, allPropertyKeys, allToManyRelationshipKeys, allToOneRelationshipKeys, attributeKeys, classDescriptionForDestinationKey, classForAttributeKey, clientAttributeKeys, clientToManyRelationshipKeys, clientToOneRelationshipKeys, defaultFormatterForKey, deleteRuleForRelationshipKey, entityName, fetchSpecificationNamed, inverseForRelationshipKey, ownsDestinationObjectsForRelationshipKey, readResolve, superClassDescription, toManyRelationshipKeys, toOneRelationshipKeys, validateObjectForSave
 
Methods inherited from class com.webobjects.eocontrol.EOClassDescription
_decimalFormatter, _integerFormatter, _timestampFormatter, awakeObjectFromFetch, classDelegate, classDescriptionForClass, classDescriptionForEntityName, classDescriptionForKeyPath, defaultFormatterForKeyPath, finalize, invalidateClassDescriptionCache, propagateDeleteForObject, registerClassDescription, setClassDelegate, toString, userPresentableDescriptionForObject
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

log

public static final ERXLogger log
logging support


validationLog

public static final ERXLogger validationLog
validation logging support


defaultLog

public static final ERXLogger defaultLog
default logging support


_validationInfo

protected NSDictionary _validationInfo
Holds validation info from the entities user info dictionary


_validationQualiferCache

protected NSMutableDictionary _validationQualiferCache
Holds validation qualifiers


_initialDefaultValues

protected NSMutableDictionary _initialDefaultValues
Holds default values


_factory

public static ERXEntityClassDescription.Factory _factory
Holds the default factory instance


defaultKey

protected String defaultKey
default handling

Constructor Detail

ERXEntityClassDescription

public ERXEntityClassDescription(EOEntity entity)
Public constructor

Parameters:
entity - that this class description corresponds to
Method Detail

factory

public static ERXEntityClassDescription.Factory factory()
getter for the factory


registerDescription

public static void registerDescription()
This method is called by the principal class of the framework when the framework's NSBundle is loaded. This method registers an observer, either a Factory object, ehich is an inner class of this class or a custom Factory subclass specified in the property: er.extensions.ERXClassDescription.factoryClass. This observer listens for notifications when a model is loaded or a class description is needed and responds by creating and registering custom class descriptions.


modelFileDidChange

public void modelFileDidChange(NSNotification n)

validateObjectForDelete

public void validateObjectForDelete(EOEnterpriseObject obj)
                             throws NSValidation.ValidationException
This method is called when an object is about to be deleted. If any validation exceptions occur they are converted to an ERXValidationException and that is thrown.

Parameters:
obj - enterprise object to be deleted
Throws:
validation - exception
NSValidation.ValidationException

entity

public EOEntity entity()
Overridden to perform a check if the entity is still in a model group. This can happen if you remove the entity, clone it to change things and re-add it afterwards.


checkEntity

protected void checkEntity()

createInstanceWithEditingContext

public EOEnterpriseObject createInstanceWithEditingContext(EOEditingContext ec,
                                                           EOGlobalID gid)

validateObjectForUpdate

public void validateObjectForUpdate(EOEnterpriseObject obj)
                             throws NSValidation.ValidationException
This method is called when an object is about to be updated. If any validation exceptions occur they are converted to an ERXValidationException and that is thrown.

Parameters:
obj - enterprise object to be deleted
Throws:
validation - exception
NSValidation.ValidationException

validateObjectForInsert

public void validateObjectForInsert(EOEnterpriseObject obj)
                             throws NSValidation.ValidationException
This method is called when an object is about to be inserted. If any validation exceptions occur they are converted to an ERXValidationException and that is thrown.

Parameters:
obj - enterprise object to be deleted
Throws:
validation - exception
NSValidation.ValidationException

validateValueForKey

public Object validateValueForKey(Object obj,
                                  String s)
                           throws NSValidation.ValidationException
This method is called to validate a value for a particular key. Typcial validation exceptions that might occur are non-null constraints or string is greater in length than is allowed. If a validation exception does occur they are converted to an ERXValidationException and that is thrown.

Parameters:
obj - value to be validated
s - property key to validate the value against.
Throws:
validation - exception
NSValidation.ValidationException

validateObjectForSave

public void validateObjectForSave(EOEnterpriseObject obj)
                           throws NSValidation.ValidationException
This method is called when an object is about to be saved. Adds support for extra validation keys to be set in an array in the entity's userInfo under the keypath ERXValidation.additionalValidationKeys. If any validation exceptions occur they are converted to an ERXValidationException and that is thrown.

Parameters:
obj - enterprise object to be saved
Throws:
validation - exception
NSValidation.ValidationException

validateObjectValueDictWithInfo

protected boolean validateObjectValueDictWithInfo(ERXEntityClassDescription.ValidationObjectValue values,
                                                  NSDictionary info,
                                                  String cacheKey)

validateObjectWithUserInfo

public void validateObjectWithUserInfo(EOEnterpriseObject object,
                                       Object value,
                                       String validationTypeString,
                                       String property)

displayNameForKey

public String displayNameForKey(String key)
Calculates a display name for a key using an improved method.

Parameters:
key - to be converted
Returns:
pretty display name

readDefaultValues

public void readDefaultValues()

setDefaultAttributeValue

public void setDefaultAttributeValue(EOAttribute attr,
                                     String defaultValue)

setDefaultRelationshipValue

public void setDefaultRelationshipValue(EORelationship rel,
                                        String defaultValue)

setDefaultValuesInObject

public void setDefaultValuesInObject(EOEnterpriseObject eo,
                                     EOEditingContext ec)

awakeObjectFromInsertion

public void awakeObjectFromInsertion(EOEnterpriseObject eo,
                                     EOEditingContext ec)

Last updated: Do, Dez 9, 2004 • 12:46 PM CET

Copyright © 2002 – 2004 Project Wonder.