2 Replies Latest reply on Apr 20, 2007 2:40 PM by twocoasttb

    Update entity in authenticate method

    twocoasttb

      What is the correct way to update an entity in my authenticate method (called from Identity)? I want to update the user entity with a 'last connected' date. As far as I can tell, I have to handle the transaction manually, like this:

      @Name("authenticator")
      public class Authenticator {
      
       @In
       EntityManager entityManager;
      
       @Out(scope=ScopeType.SESSION, required=false)
       SiteUser currentUser;
      
       @In(create=true) @Out
       SessionPreferences prefs;
      
       public boolean authenticate() {
       Query q = entityManager.createQuery("from SiteUser su where upper(username) = upper(:username) and password = :password and active = true");
       q.setParameter("username", Identity.instance().getUsername());
       q.setParameter("password", Identity.instance().getPassword());
       if (q.getResultList().size() == 1) {
       currentUser = (SiteUser)q.getSingleResult();
       prefs.setCurrentOrganization(currentUser.getOrganization());
       Identity.instance().addRole("admin");
      
       try {
       UserTransaction utx = Transactions.getUserTransaction();
       utx.begin();
       entityManager.joinTransaction();
       currentUser.setLastConnected(new java.util.Date());
       utx.commit();
       } catch (Exception e) {
       log.error("Exception updating user: " + e.getMessage());
       }
      
       return true;
       }
       return false;
       }
      }


      Is there a better way to do it?