0

I have a problem with getting userId and another attributes to save and persist in my database, like auditing actions and save createdBy and modifiedBy in database before save or update any bean, I found helpful answers for createdBy and modifiedBy in this question : Is it possible to use @PrePersist and @PreUpdate with eBean and Play! 2.0? :

This is my class :

@MappedSuperclass
public abstract class BaseModel extends Model {

    @Id
    @Column
    @GeneratedValue(strategy=GenerationType.AUTO)
    public Integer id;

    @Column
    @CreatedTimestamp
    public Timestamp createdDate;

    @Column
    @UpdatedTimestamp
    public Timestamp updatedDate;

    @Column
    @Version
    private long version=0;

    @Column
    public String updatedBy;

    @Column
    public String createdBy;


    public BaseModel() {
    }

    @Override
    public void save() {
        boolean isInstanceOfAccount = this instanceof Account;
        if (!isInstanceOfAccount) {
            int userId = Integer.parseInt(session().get(ViewModelsConstants.User.USER_ID);
            updatedBy = String.valueOf(userId);
            // etc to 
        }
        super.save();
    }
}

You can note that i have override the save method (so i'll do to update) so when I call my bean like product.save() I save the id for who did that add or update. but I have a similar unanswered issue to Session inside Model in play framework,

I maybe can add method have parameters to save and call super like :

public void save(int userId) {
    boolean isInstanceOfAccount = this instanceof Account;
    if (!isInstanceOfAccount) {
        updatedBy = String.valueOf(userId );
        // etc to 
    }
    super.save();
}

But I have already exist code and I don't want to do modification in all my classes, and if I want to add more or remove parameters I need then to refactor


What the coolest (best practice) solution for this?

My Simple stupid question now : how I can pass from session to model OR how i can save parameter (like userId when he login) somewhere to use in model when I save or update it?

Community
  • 1
  • 1
Al-Mothafar
  • 7,949
  • 7
  • 68
  • 102

1 Answers1

0

I think its so old question that I found its solution when I experienced, but if you looking to the answer you can see my newest answer and question here :

Play framework and ionic for mobile I need Security without cookies but token

Its store tokens without cookies, or you can use cookies and read it from controllers.

Community
  • 1
  • 1
Al-Mothafar
  • 7,949
  • 7
  • 68
  • 102