diff --git a/build.xml b/build.xml index 143c82a8..81dc6fe3 100644 --- a/build.xml +++ b/build.xml @@ -3,7 +3,7 @@ - + diff --git a/src/org/ohmage/query/IUserQueries.java b/src/org/ohmage/query/IUserQueries.java index 4848f235..7792cec2 100644 --- a/src/org/ohmage/query/IUserQueries.java +++ b/src/org/ohmage/query/IUserQueries.java @@ -117,6 +117,17 @@ public void createUserRegistration( final String emailAddress, final String registrationId) throws DataAccessException; + + /** + * Returns id for a user + * + * @param username The username for which to retrieve the user id. + * + * @return Returns the string-ed user id + * + * @throws DataAccessException Thrown if there is an error. + */ + public String getUserIdFromUsername(String username) throws DataAccessException; /** * Returns whether or not a user exists. diff --git a/src/org/ohmage/query/impl/UserQueries.java b/src/org/ohmage/query/impl/UserQueries.java index 2e4ce096..4ef81c8a 100644 --- a/src/org/ohmage/query/impl/UserQueries.java +++ b/src/org/ohmage/query/impl/UserQueries.java @@ -55,6 +55,12 @@ * @author John Jenkins */ public class UserQueries extends Query implements IUserQueries { + // Returns the id for the user + private static final String SQL_GET_USER_ID_WITH_USERNAME = + "SELECT id " + + "FROM user " + + "WHERE username = ?"; + // Returns a boolean representing whether or not a user exists private static final String SQL_EXISTS_USER = "SELECT EXISTS(" + @@ -905,6 +911,36 @@ public void createUserRegistration( throw new DataAccessException("Error while attempting to rollback the transaction.", e); } } + + /** + * Returns the user's id + * + * @param username The user's username. + * + * @return The user's id + * + * @throws DataAccessException There was an error. + */ + @Override + public String getUserIdFromUsername(String username) + throws DataAccessException { + + try { + return getJdbcTemplate().queryForObject( + SQL_GET_USER_ID_WITH_USERNAME, + new Object[] { username }, + String.class + ); + } + catch(org.springframework.dao.DataAccessException e) { + throw new DataAccessException( + "Error executing SQL '" + + SQL_GET_USER_ID_WITH_USERNAME + + "' with parameter: " + + "%" + username + "%", + e); + } + } /** * Returns whether or not a user exists. diff --git a/src/org/ohmage/request/user/UserInfoReadRequest.java b/src/org/ohmage/request/user/UserInfoReadRequest.java index 9cdaa5eb..7cf3474f 100644 --- a/src/org/ohmage/request/user/UserInfoReadRequest.java +++ b/src/org/ohmage/request/user/UserInfoReadRequest.java @@ -65,6 +65,7 @@ public class UserInfoReadRequest extends UserRequest { private String gUsername; private UserSummary result = null; + private String _userId = ""; /** * Creates a new user info read request. @@ -140,6 +141,15 @@ public void service() { e.failRequest(this); e.logException(LOGGER); } + try { + _userId = UserServices.instance().getUserIdFromUsername(gUsername); + } + catch(ServiceException e) { + // Do not fail the request if user ID was not found + // e.failRequest(this); + e.logException(LOGGER); + } + } /** @@ -154,7 +164,11 @@ public void respond(HttpServletRequest httpRequest, HttpServletResponse httpResp if(result != null) { try { - jsonResult.put(gUsername, result.toJsonObject()); + JSONObject result_json = result.toJsonObject( ); + if( !_userId.equals( "" ) ) { + result_json.put( "user_id", _userId ); + } + jsonResult.put(gUsername, result_json); } catch(JSONException e) { LOGGER.error("There was an error building the JSONObject result.", e); diff --git a/src/org/ohmage/service/UserServices.java b/src/org/ohmage/service/UserServices.java index 01229ab4..11868994 100644 --- a/src/org/ohmage/service/UserServices.java +++ b/src/org/ohmage/service/UserServices.java @@ -1896,6 +1896,25 @@ public UserPersonal getUserPersonalInformation( throw new ServiceException(e); } } + + /** + * Retrieves the user id for a username + * + * @param username The username. + * + * @return The user's id + * + */ + public String getUserIdFromUsername( + final String username) throws ServiceException { + + try { + return userQueries.getUserIdFromUsername(username); + } + catch (DataAccessException e) { + throw new ServiceException(e); + } + } /** * Updates a user's account information. @@ -2313,5 +2332,7 @@ public boolean userExistsAndIsExternal(String username) } } + + }