} else if (pvi != null) {
userLookupSql = "select * from " + userTableName
+ " where gsaname = ?";
key = pvi;
} else {
throw new RepositoryException("Attempted user lookup without a key");
}
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.logp(Level.FINEST, CLASS_NAME, METHOD,
"Looking up user with SQL: [" + userLookupSql + "] and key: "
+ key);
}
PreparedStatement pstmt = conn.prepareStatement(userLookupSql,
ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = null;
ResultSet generatedKeys = null;
try {
pstmt.setString(1, key.toLowerCase());
rs = pstmt.executeQuery();
if (rs.next()) {
// See if we need to update the pvi
if (pvi != null && !pvi.equals(rs.getString("gsaname"))) {
rs.updateString("gsaname", pvi.toLowerCase());
rs.updateRow();
}
LOGGER.logp(Level.FINEST, CLASS_NAME, METHOD,
Util.buildString("Found user ", key, " from cache"));
return rs.getLong("userid");
}
if (!createIfNotExists) {
return -1L;
}
Util.close(pstmt);
rs = null;
pstmt = conn.prepareStatement(
"insert into " + userTableName
+ "(notesname, gsaname) values (?, ?)",
Statement.RETURN_GENERATED_KEYS);
pstmt.setString(1, notesName.toLowerCase());
pstmt.setString(2, pvi.toLowerCase());
int rows = pstmt.executeUpdate();
if (rows == 0) {
throw new RepositoryException(
"Failed to create user record for " + notesName);
}
generatedKeys = pstmt.getGeneratedKeys();
if (generatedKeys.next()) {
LOGGER.logp(Level.FINE, CLASS_NAME, METHOD,
Util.buildString("New user ", notesName.toLowerCase(),
" [", pvi.toLowerCase(), "] is added to cache"));
return generatedKeys.getLong(1);
} else {
throw new RepositoryException(
"Failed to retrieve key for " + notesName);
}
} finally {
Util.close(generatedKeys);
Util.close(rs);