The thing is, as I expect Paul has found out, is that this won't work reliably. Something else can add the value to the database between when you make this check and when the SQL transaction is committed.

Chuck

On May 27, 2008, at 11:01 AM, Zak Burke wrote:

On Tue, May 27, 2008 at 5:45 AM, Paul Stringer <[EMAIL PROTECTED]> 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 protected])
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/webobjects-dev/chill%40global-village.net

This email sent to [EMAIL PROTECTED]


--

Practical WebObjects - for developers who want to increase their overall knowledge of WebObjects or who are trying to solve specific problems.
http://www.global-village.net/products/practical_webobjects





_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list      ([email protected])
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/webobjects-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to