Monday, June 15, 2009

User Datastore Interaction

Okay, here's the complete code (in RPXResults.java) for creating users only if they don't already exist:


PersistenceManager pm = PMF.get().getPersistenceManager();

Query query = pm.newQuery("select from omega.server.User where identifier == identifierParam");
query.declareParameters("String identifierParam");

try
{
 List results = (List) query.execute(identifier);

 if (results.size() == 0)
 {
  User user = new User ();
  user.setIdentifier(identifier);
  pm.makePersistent(user);
 }
 else
 {
  User user = results.get(0);
 }
}
finally
{
 query.closeAll();
 pm.close();
}


We could do more about populating User fields from the RPX profile, but this is reasonably complete otherwise. Does this mean the user is logged in?

Unfortunately, no.

By the end of the code, we have loaded the User instance from the datastore (or created it). To get the other pages in the application, and the GWT widgets, to know about the user we have to start a session.

Next post, sessions.

2 comments:

  1. Hello I was wondering if this will detect the same user loging with 2 different accounts, google and facebook for example, thanks

    ReplyDelete
  2. Hi Alex, no it won't. The easiest way to do that is to pay for the upgraded version of RPX, and they'll do that for you. The harder way is to build your database to keep keys from multiple OpenID accounts in the user record.

    ReplyDelete