Markus,

I do this a lot with ERXThreadStorage.

eg. in my session class:

        /**
         * @param user set the current user
         */
        public void setAuthenticatedUser(User user) {
                _authenticatedUser = 
user.localInstanceIn(defaultEditingContext());
                ERCoreBusinessLogic.setActor(_authenticatedUser);
        }


And in my EO:

        @Override public void init(EOEditingContext ec) {
                super.init(ec);
                setCreatedDateTime(new NSTimestamp());
                
setCreatedUser((User)ERCoreBusinessLogic.actor(editingContext()));
                setStatus(Status.DRAFT);
        }


If you look at the source for ERCoreBusinessLogic.setActor(), then it is simply:

    /**
     * Sets the actor in the current thread storage.
     * @param actor current user for this thread
     */
    public static void setActor(EOEnterpriseObject actor) {
        if (log.isDebugEnabled())
            log.debug("Setting actor to : "+actor);
        if (actor != null) {
            ERXThreadStorage.takeValueForKey(actor, "actor");
        } else {
            ERXThreadStorage.removeValueForKey("actor");
        }
    }

    /**
     * Gets the actor as a local instance in the given context.
     * @param ec editing context to pull a local copy of the actor
     *          into
     * @return actor instance in the given editing context
     */
    public static EOEnterpriseObject actor(EOEditingContext ec) {
        EOEnterpriseObject actor = actor();
        if (actor != null && actor.editingContext() != ec) {
            EOEditingContext actorEc = actor.editingContext();
            actorEc.lock();
            try {
                EOEnterpriseObject localActor = 
ERXEOControlUtilities.localInstanceOfObject(ec, actor);
                try {
                        if(actor instanceof ERCoreUserInterface) {
                                NSArray prefs = 
((ERCoreUserInterface)actor).preferences();
                                prefs = 
ERXEOControlUtilities.localInstancesOfObjects(ec, prefs);
                                
((ERCoreUserInterface)localActor).setPreferences(prefs);
                        }
                } catch(RuntimeException ex) {
                        log.error("Error while setting getting actor's 
preferences: " + ex, ex);
                }
                        actor = localActor;
            } finally {
                actorEc.unlock();                
            }
        }
        return actor;
    }

Anjo Krank’s BugTracker example uses this as well.

Mark

> On 7 Nov 2015, at 14:56, Markus Ruggiero <[email protected]> wrote:
> 
> Folks,
> 
> I have the following problem:
> My application must support different geographical regions. A logged-in user 
> is assigned a region. This uses info is stored in Session. My EOs have 
> business logic that must act region specific. How can I let the EOs know 
> about the current region (from session().currentUser().region() )? The 
> business logic is in its own framework and knows nothing about sessions 
> (which is how it supposed to be). Would ERXThreadStorage be of any help here?
> 
> Example of what I need:
> Entity Product has a region specific product description. So simple accessors 
> "description()" and "setDescription(String text)" are not good enough. The 
> logic inside these accessors MUST take the current user's region into account.
> 
> I used to have my own editing context class with a session instance variable. 
> This worked before because I had the business logic and all the controller 
> code in the same project. Now with the separation into different projects 
> this does not work anymore (which is actually quite ok, I know it was a 
> kludge before).
> 
> Thanks for any help
> ---markus---
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Webobjects-dev mailing list      ([email protected])
> Help/Unsubscribe/Update your Subscription:
> https://lists.apple.com/mailman/options/webobjects-dev/mark%40wardle.org
> 
> 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:
https://lists.apple.com/mailman/options/webobjects-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to