Better key path creation
Better key path creation
- Subject: Better key path creation
- From: John Huss <email@hidden>
- Date: Fri, 17 Dec 2010 14:54:10 -0600
If anyone is interested, I recently came up with an EO template enhancement that allows for much better keypath creation using ERXKeys. It works by creating an inner class in each eo class that exposes methods for appending each of it's keys to a key path that is being built. Then you can construct key paths like this:
MovieRole.path.talent().photo().photo().key() // returns talent.photo.photo
instead of
MovieRole.TALENT.dot(Talent.PHOTO).dot(TalentPhoto.PHOTO).key()
OR
TalentPhoto.path.talent().roles().roleName().eq("Actor") // returns qualifier for talent.roles.roleName
instead of
TalentPhoto.TALENT.dot(Talent.ROLES).dot(Role.ROLE_NAME).eq("Actor")
This is an improvement for a few reasons:
1) More readable
2) Guarantees the validity of the whole key path, whereas before only the individual parts were checked
3) Code completion the whole way through eliminates thinking
4) More closely matches the syntax used to access the actual values
To add this functionality, paste the attached snippet into your own copy of the _WonderEntity.java template (mine is right after the class declaration).
Note: this requires a fairly recent version Wonder to work correctly.
John
public static final ${entity.classNameWithoutPackage}Path path = new ${entity.classNameWithoutPackage}Path(null);
public static class ${entity.classNameWithoutPackage}Path extends ERXKey<$entity.className> {
public ${entity.classNameWithoutPackage}Path(String key) {
super(key);
}
#foreach ($attribute in $entity.sortedClassAttributes)
#if ($attribute.name != "count" && $attribute.name != "key" && $attribute.name != "desc")
public ERXKey<#if($attribute.userInfo.ERXConstantClassName)$attribute.userInfo.ERXConstantClassName#else$attribute.javaClassName#end> ${attribute.name}() {
return this.append(${attribute.uppercaseUnderscoreName});
}
#else
public ERXKey<#if($attribute.userInfo.ERXConstantClassName)$attribute.userInfo.ERXConstantClassName#else$attribute.javaClassName#end> ${attribute.name}Key() {
return this.append(${attribute.uppercaseUnderscoreName});
}
#end
#end
#foreach ($relationship in $entity.sortedClassRelationships)
#if ($relationship.actualDestination.classNameWithDefault != "EOGenericRecord")
#if ($relationship.name != "count" && $relationship.name != "key" && $relationship.name != "desc")
public ${relationship.actualDestination.className}.${relationship.actualDestination.classNameWithoutPackage}Path ${relationship.name}() {
return new ${relationship.actualDestination.className}.${relationship.actualDestination.classNameWithoutPackage}Path(this.append(${relationship.uppercaseUnderscoreName}).key());
}
#else
public ${relationship.actualDestination.className}.${relationship.actualDestination.classNameWithoutPackage}Path ${relationship.name}Key() {
return new ${relationship.actualDestination.className}.${relationship.actualDestination.classNameWithoutPackage}Path(this.append(${relationship.uppercaseUnderscoreName}).key());
}
#end
#end
#end
}
_______________________________________________
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