Re: How to Retrieve Session User?
Re: How to Retrieve Session User?
- Subject: Re: How to Retrieve Session User?
- From: Fred Shurtleff <email@hidden>
- Date: Thu, 05 Apr 2007 22:13:25 -0400
Art - You have raised some interesting points not previously
highlighted. I was aware that the primary key was used for fetching, but
had not noticed the use of the special method -
objectWithPrimaryKeyValue. Also I know what you are saying about the use
of the primary key use and EOF discouraging its explicit usage; but this
does not appear to be such a bad transgression in that it is not ever
displayed nor used as a user manipulated attribute.
Re:EOSharedEditingContext it is 'crossed out' in the Eclipse IDE and
shows a context message indicating this is a deprecated method. But then
the code you see was written in 2002 for a demo app in "WebObjects
Developer's Guide"(SAMS Publisher).
Your points are well-taken :-)
Art Isbell wrote:
On Apr 5, 2007, at 12:02 PM, Chuck Hill wrote:
public WOActionResults postsAction() {
WOComponent nextPage = pageWithName("TopicPosts");
Number topicID = request().numericFormValueForKey("topicID", new
NSNumberFormatter());
EOEnterpriseObject topic =
EOUtilities.objectWithPrimaryKeyValue(WOApplication.application().sharedEditingContext(),
"Topic", topicID);
That rather looks like an abuse of sharedEditingContext().
That it does! According to the EOSharedEditingContext docs, only
4 EOSharedEditingContext methods that make changes to the shared
editing context are thread-safe. We don't know whether
EOUtilities.objectWithPrimaryKeyValue() uses one of these thread-safe
methods. If it doesn't, the shared editing context must be locked
prior to its use, something that can be difficult to do correctly.
Instead, fetch all shared objects that an app instance will need
in the Application constructor using one of the thread-safe methods.
Then access these shared objects by sending the shared editing context
either an objectsByEntityName() or
objectsByEntityNameAndFetchSpecificationName() message, getting the
array of objects for the particular entity (e.g., Topic) from the
returned dictionaries, and then filtering this array, if necessary, to
get the object(s) desired.
Also, exposing the Topic primary key, normally a meaningless
integer, and then using it to get a Topic object isn't the pattern
usually used for the Enterprise Objects Framework (EOF). Instead, an
attribute whose value is unique and meaningful to humans is a better
choice for accessing an object. E.g., topicName, if such attribute
exists.
Number topicID = request().numericFormValueForKey("topicID", new
NSNumberFormatter());
EOEnterpriseObject topic =
EOUtilities.objectWithPrimaryKeyValue(WOApplication.application().sharedEditingContext(),
"Topic", topicID);
EOEnterpriseObject topic;
String topicName = request().formValueForKey("topicName");
if (topicName != null) {
NSArray topics =
WOApplication.application().sharedEditingContext().objectsByEntityName().objectForKey("Topic");
EOQualifier qual = new EOKeyValueQualifier("topicName",
EOQualifier.QualifierOperatorLike, topicName);
topic =
(EOEnterpriseObject)EOQualifier.filteredArrayWithQualifier(topics,
qual).lastObject();
}
else {
topic = null;
}
if (topic == null) {
// Deal with no Topic object.
}
While this may appear more verbose,
EOUtilities.objectWithPrimaryKeyValue() can throw 3 exceptions, so it
should be in a "try" block with "catch" blocks dealing at least one of
these exceptions. Once you've done all
Number index =
request().numericFormValueForKey("displayBatchIndex", new
NSNumberFormatter());
// set variables
nextPage.takeValueForKeyPath(topic,
"postsDisplayGroup.masterObject");
if (index != null) nextPage.takeValueForKeyPath(index,
"postsDisplayGroup.currentBatchIndex");
return nextPage;
Also, as others have warned, using KVC to set and get values
increases the probability of run-time exceptions should you mistype a
key or key path. Such errors won't be caught by the compiler.
Instead, send actual Java messages. Any mistyping of these messages
will be caught at compile time rather than at run time which is
much-preferred :-)
WODisplayGroup postsDG = nextPage.postsDisplayGroup();
postsDG.setMasterObject(topic);
if (index != null) postsDG.setCurrentBatchIndex(index);
Aloha,
Art
_______________________________________________
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
_______________________________________________
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