3

I am trying to create a relationship between the User model of the play-authenticate module and my model - Books. I am using play 2.0.4 and I have integrated play-authenticate into my project. In my controllers/Application I have the getLocalUser method:

public static User getLocalUser(final Session session) {
    final User localUser = User.findByAuthUserIdentity(PlayAuthenticate
            .getUser(session));
    return localUser;
}

I am trying override Model's method in my Books model to save the user who adds/modify book records.

public void save() {
    User logged = Application.getLocalUser(session());
    if (logged != null) {
        this.createUser = logged;
        this.modifyUser = logged;
    }
    super.save();
}

public void update(Object o) {
    User logged = Application.getLocalUser(session());
    if (logged != null) {
        this.modifyUser = logged;
    }
    super.update(o);
}

The error I get when I do this, which I do not understand is:

The method session() is undefined for the type Books.

I'm not sure how this can be as I do have access to the controller/application methods because of my import:

import controllers.Application;

Update: *

public static Result createBook() {
        Form<Book> filledForm = bookForm.bindFromRequest();

        if(filledForm.hasErrors()) {
            return badRequest(
                views.html.bookCreator.render(Book.all(), filledForm)
            );
        }
        else {
            Book.create(filledForm.get());
                    //Book.save(getLocalUser(session()));
            return redirect(routes.Application.createSuccess());
        }
    }*

1 Answers1

2

First it is a very bad idea to call session() within your models classes, because if you do this, you'll create some kind of cyclic dependencies (presentation layer -> model -> presentation layer).

If you really want to keep it this way, just do this:

Controller:

public static User getLocalUser() {
    final User localUser = User.findByAuthUserIdentity(PlayAuthenticate
            .getUser(session()));
    return localUser;
}

Model:

public void save() {
    User logged = Application.getLocalUser();
    if (logged != null) {
        this.createUser = logged;
        this.modifyUser = logged;
    }
    super.save();
}

public void update(Object o) {
    User logged = Application.getLocalUser();
    if (logged != null) {
        this.modifyUser = logged;
    }
    super.update(o);
}

But I'd prefer to do this:

Controller:

public Result anAction() {
    ...
    save(getLocalUser(session()));
    ...
}

Model:

public void save(final User logged) {
    if (logged != null) {
        this.createUser = logged;
        this.modifyUser = logged;
    }
    super.save();
}

public void update(Object o, final User logged) {
    if (logged != null) {
        this.modifyUser = logged;
    }
    super.update(o);
}
ndeverge
  • 21,378
  • 4
  • 56
  • 85
  • Thanks for your response. I want to save the user associated with Book objects when they are created (The user who is logged in at that time). I have updated the question to include the application method in the controller where I actually create a book. My issue with calling the Book.save method issue in this case is making a static reference to a non-static method. Is it ok to make the save and update overrides static? –  Mar 31 '13 at 19:27
  • Ok, I did this and I also changed the model variables modifyUser and createUser to be static as well. The only issue I seem to be getting is again making a static reference to the non-static method "save" belonging to model. –  Mar 31 '13 at 20:08