Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<property file="build.properties"/>
<property name="library" location="lib"/>
<property name="app_name" value="ohmage"/>
<property name="app_version" value="2.18.1"/>
<property name="app_version" value="2.19.0"/>
<property name="src" location="src"/>
<property name="test" location="test"/>
<property name="view" location="view"/>
Expand Down
11 changes: 11 additions & 0 deletions src/org/ohmage/query/IUserQueries.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
36 changes: 36 additions & 0 deletions src/org/ohmage/query/impl/UserQueries.java
Original file line number Diff line number Diff line change
Expand Up @@ -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(" +
Expand Down Expand Up @@ -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.
Expand Down
16 changes: 15 additions & 1 deletion src/org/ohmage/request/user/UserInfoReadRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}

}

/**
Expand All @@ -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);
Expand Down
21 changes: 21 additions & 0 deletions src/org/ohmage/service/UserServices.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -2313,5 +2332,7 @@ public boolean userExistsAndIsExternal(String username)
}

}



}