Re: ERXValidationException out of a Unique Constraint Violation
Re: ERXValidationException out of a Unique Constraint Violation
- Subject: Re: ERXValidationException out of a Unique Constraint Violation
- From: "Zak Burke" <email@hidden>
- Date: Tue, 27 May 2008 14:01:31 -0400
On Tue, May 27, 2008 at 5:45 AM, Paul Stringer <email@hidden> wrote:
> I'm following the PracticalWebObjects advice to handle unique values
> validation at the database and then handle it in thrown
> EOGeneralAdaptorExceptions. Wish I'd checked this before I wasted time
> pre-checking for all this in validateKey() which was my first approach,
> follow the advice don't do it! :)
[...]
> Any better ways to approach this or am I basically on the right track. The
> only thing I see as a potential problem is that the errors that come back
> maybe database specific and so therefore the code is not db independant. I'm
> using FrontBase right now if it makes any difference.
I wrote some simple "validateUniquenessOf..." methods that you can
stash in a custom EO superclass. The content is pasted below; you can
also just search for "uniquness" on www.wocode.com.
Cheers,
zak.
public class EOChildClass extends EOParentClass
{
public void validateForSave()
{
// make sure entityKey value is unique
// corresponds to a DB constraint of "unique(column_name)"
validateUniquenessOf("entityKey");
// make sure dictionary pairs are unique
// corresponds to a DB constraint of "unique(column_name_1, column_name_2)"
NSMutableDictionary d = new NSMutableDictionary();
d.setObjectForKey(entityValue1, "entityKey1");
d.setObjectForKey(entityValue2, "entityKey2");
validateUniquenessOf(d);
}
}
public abstract class EOParentClass extends EOGenericRecord
{
/**
* Return true if this object has not yet been persisted, false otherwise.
* @return true if this object is new (unpersisted), false otherwise.
*/
public boolean isNew()
{
return editingContext().globalIDForObject(this).isTemporary();
}
/**
* Throw if a matching EO already exists.
*
* @param key name of the property on the EO-class to validate
*
* @throws NSValidation.ValidationException if an EO with the same
property value already exists
*/
public void validateUniquenessOf(String key)
{
try
{
EOEnterpriseObject item =
EOUtilities.objectMatchingKeyAndValue(editingContext(), entityName(),
key, this.valueForKey(key));
if (isNew())
throw new NSValidation.ValidationException("An item with this name
already exists. Pick a different name.");
else if (! item.equals(this))
throw new NSValidation.ValidationException("You can't rename this item
because an item with the new name already exists. Pick a different
name.");
}
// nothing to do if there are no matching objects
catch (EOObjectNotAvailableException e)
{
}
// bummmer. the item's properties are already non-unique.
catch (EOUtilities.MoreThanOneException e)
{
throw new NSValidation.ValidationException("An item with this name
already exists. Pick a different name.");
}
}
/**
* Throw if a matching EO already exists.
*
* @param values hash of property-value pairs on the EO-class to validate
*
* @throws NSValidation.ValidationException if an EO with the same
property-value pairs already exists
*/
public void validateUniquenessOf(NSDictionary values)
{
try
{
EOEnterpriseObject item =
EOUtilities.objectMatchingValues(editingContext(), entityName(),
values);
if (isNew())
throw new NSValidation.ValidationException("An item with this name
already exists. Pick a different name.");
else if (! item.equals(this))
throw new NSValidation.ValidationException("You can't rename this item
because an item with the new name already exists. Pick a different
name.");
}
// nothing to do if there are no matching objects; this is exactly what we want!
catch (EOObjectNotAvailableException e)
{
}
// bummmer. the item's properties are already non-unique.
catch (EOUtilities.MoreThanOneException e)
{
throw new NSValidation.ValidationException("An item with this name
already exists. Pick a different name.");
}
}
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden