// Get or create a user entity for a JID. This truncates the
// resource portion of the JID, so multiple user clients are
// consolidated into one record per account.
public static Entity getUserEntity(JID jid) {
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Transaction txn = datastore.beginTransaction();
String jidStr = jid.getId();
if (jidStr.indexOf("/") != -1) {
jidStr = jidStr.substring(0, jidStr.indexOf("/"));
}
Key userKey = KeyFactory.createKey("ChatUser", jidStr);
Entity userEntity;
try {
userEntity = datastore.get(txn, userKey);
} catch (EntityNotFoundException e) {
userEntity = new Entity(userKey);
userEntity.setProperty("jid", jidStr);
userEntity.setProperty("accepted_invitation", false);
userEntity.setProperty("is_subscribed", false);
userEntity.setProperty("is_available", false);
userEntity.setProperty("presence_show", "chat");
userEntity.setProperty("status_message", "");
userEntity.setProperty("last_chat_message", "");
datastore.put(txn, userEntity);
}
txn.commit();
return userEntity;
}