On 2010-12-14, at 12:25 PM, Jesse Tayler wrote:
> Makes sense, to use init() but how to safely handle the authenticated user?
>
> I see there's a clazz setup used in BugTracker which seems to set a
> currentUser() which I'm guessing is a safe way to access the authenticated
> user (if I set it somewhere) and locally set an author or assignee
> relationship and otherwise get info from the current user.
You can do this in Session and access it this way: [ session().user() ], or you
can do some of it in the model and access it this way: [ Person.currentUser() ].
1. SESSION
protected Person _user;
public Person user() {
return _user;
}
public void setUser(Person user) {
_user = user;
ERXThreadStorage.takeValueForKey(user(), "user");
}
public void awake() {
super.awake();
// if we have a user, keep track of them
if (user() != null) {
ERXThreadStorage.takeValueForKey(user(), "user");
}
}
public void sleep() {
ERXThreadStorage.takeValueForKey(null, "user");
super.sleep();
}
2. PERSON
/**
* Gets the current user.
*
* @return current user for the thread
*/
public static Person currentUser() {
return (Person) ERXThreadStorage.valueForKey("user");
}
/**
* Gets the user as a local instance in the given context.
*
* @param ec
* editing context to pull a local copy of the user into
* @return user instance in the given editing context
*/
public static Person currentUser(EOEditingContext ec) {
Person currentUser = currentUser();
if (currentUser != null && currentUser.editingContext() != ec) {
EOEditingContext currentUserEc =
currentUser.editingContext();
currentUserEc.lock();
try {
Person localUser = (Person)
ERXEOControlUtilities
.localInstanceOfObject(ec,
currentUser);
currentUser = localUser;
} finally {
currentUserEc.unlock();
}
}
return currentUser;
}
>
> I see this clazz setup in the bug tracker eo templates, but I don't have
> those on my EREOs and don't know where they come from.
http://wiki.objectstyle.org/confluence/display/WOL/EOGenerator+Templates+and+Additions
Look for:
Consolidated WOnder Templates with "Clazz" pattern (Ramsey Gurley)
>
> I'm try to follow that code through, but maybe if someone has a basic
> explanation that would be great.
Docs on er.extensions.eof.EOEnterpriseObjectClazz should get you started. I'm
not sure that Clazz is necessary for this specific requirement that you have.
BugTracker uses it, but if you follow the code above, you can do it without
diving into Clazz quite yet :-)
David
>
> On Dec 14, 2010, at 2:28 PM, Ramsey Gurley wrote:
>
>> Hi Jesse,
>>
>> On Dec 14, 2010, at 1:16 PM, Jesse Tayler wrote:
>>
>>>
>>> I'm implementing a method to populate new EOs with default values before
>>> save.
>>>
>>> I see lots of ERXEnterpriseObject methods beyond awakeFromInsertion,
>>
>> I would recommend init() instead of awakeFromInsertion. init() was added to
>> fix a bug where awakeFromInsertion gets called twice after a cascade delete
>> fails validation... or something (^_^)
>>
>>> including nice looking things like DidInsertProcessor and so forth.
>>>
>>> I'd like to use the D2W rules to set values like "active" = "true" or
>>> "author" = "session.user"
>>
>> The d2wContext of your page is generally unavailable in your EO unless you
>> use thread locals.
>>
>> If you DO find yourself using thread locals in your model, I would suggest
>> you consider how you use them carefully so you don't end up with a model
>> that is totally dependent on one application.
>>
>>> Does anyone have some overview advice for those new to the more modern ER
>>> frameworks in terms of how to implement this common concept I'm after?
>>
>> ERXEntityClassDescription has some stuff for initializing values. So does
>> migrations.
>>
>> Ramsey
>>
>>>
>>> thanks for thoughts or insights --
>>>
>>
>>
>
> _______________________________________________
> 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/programmingosx%40mac.com
>
> This email sent to [email protected]
_______________________________________________
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]