Re: Validation Problem
Re: Validation Problem
- Subject: Re: Validation Problem
- From: mmalcolm crawford <email@hidden>
- Date: Mon, 28 Apr 2003 12:59:42 -0700
On Monday, April 28, 2003, at 12:31 AM, Jonathan Fleming wrote:
Thank you very much mmalcolm for all of your replied posts on this
subject, the last two contained bits of code I never knew could be
created. Very education.
An EOModel "file" describes a collection of (EOEntity, EOAttribute, and
EORelationship) objects that will be created when the file is loaded.
There are also qualifier objects other than EOQualifier. An
alternative way of writing the query might be as follows:
public String validateTitle(String newTitle)
throws NSValidation.ValidationException {
if (newTitle != null) {
boolean isValid = false;
// find other movie with same title, different ID
EOQualifier qual = new EOKeyValueQualifier("title",
EOQualifier.QualifierOperatorLike,
newTitle);
EOEditingContext ec = this.editingContext();
if (ec != null) {
NSDictionary pkDict = EOUtilities.primaryKeyForObject(
this.editingContext(), this);
Integer pk = (Integer)pkDict.valueForKey("movieId");
EOQualifier pkQual = new EOKeyValueQualifier("movieId",
EOQualifier.QualifierOperatorEqual,
pk);
pkQual = new EONotQualifier(pkQual);
NSArray both = new NSArray(new Object[] {qual, pkQual});
qual = new EOAndQualifier(both);
}
else {
ec = new EOEditingContext();
}
EOFetchSpecification fs = new EOFetchSpecification("Movie",
qual, null);
fs.setFetchesRawRows(true);
fs.setRawRowKeyPaths(new NSArray("movieId"));
NSArray array = ec.objectsWithFetchSpecification(fs);
if (array.count() == 0) {
isValid = true;
}
if (!isValid) {
throw new NSValidation.ValidationException("Movie
titles must be unique");
}
}
return newTitle;
}
In place of:
public String validateTitle(String newTitle)
throws NSValidation.ValidationException {
if (newTitle != null) {
boolean isValid = false;
// find other movie with same title, different ID
String qualifierString = "title like '" + newTitle + "'";
EOEditingContext ec = this.editingContext();
if (ec != null) {
NSDictionary pkDict = EOUtilities.primaryKeyForObject(
this.editingContext(), this);
Integer pk = (Integer)pkDict.valueForKey("movieId");
qualifierString = qualifierString + " AND movieId <> "
+ pk;
}
else {
ec = new EOEditingContext();
}
EOQualifier qual =
EOQualifier.qualifierWithQualifierFormat(qualifierString, null);
EOFetchSpecification fs = new EOFetchSpecification("Movie",
qual, null);
fs.setFetchesRawRows(true);
fs.setRawRowKeyPaths(new NSArray("movieId"));
NSArray array = ec.objectsWithFetchSpecification(fs);
// ...
Most of these topics are discussed in the online documentation:
<http://developer.apple.com/techpubs/webobjects/Enterprise_Objects/
index.html>
mmalc
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.