package com.zburke; import java.util.Arrays; import org.apache.log4j.Logger; import com.webobjects.eoaccess.EOObjectNotAvailableException; import com.webobjects.eoaccess.EOUtilities; import com.webobjects.eocontrol.EOEditingContext; import com.webobjects.eocontrol.EOEnterpriseObject; import com.webobjects.eocontrol.EOGlobalID; import com.webobjects.foundation.NSDictionary; import com.webobjects.foundation.NSValidation; import er.extensions.ERXGenericRecord; /** * Superclass for all EOs * * update $Date: 2007/05/23 18:10:28 $ * * @author $Author: zburke $ * @version $Revision: 1.4 $ * */ public abstract class MyGenericRecord extends ERXGenericRecord { /** * 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) { logger.debug("no " + entityName() + " objects matching [" + key + "/" + this.valueForKey(key) + "] (HURRAY!)"); } // 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) { logger.debug("no " + entityName() + " objects matching " + values + " (HURRAY!)"); } // 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."); } } }