Skip to content

Commit

Permalink
Merge branch 'develop' into PEPPER-853_zeroSmIdFix
Browse files Browse the repository at this point in the history
  • Loading branch information
pegahtah authored Aug 3, 2023
2 parents ae8bc14 + d126c98 commit 45efeed
Show file tree
Hide file tree
Showing 37 changed files with 1,254 additions and 512 deletions.
5 changes: 5 additions & 0 deletions pepper-apis/dsm-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@
<!-- <version>1.4.11</version> -->
</dependency>

<dependency>
<groupId>commons-validator</groupId>
<artifactId>commons-validator</artifactId>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,6 @@ protected void setupCustomRouting(@NonNull Config cfg) {
setupDDPConfigurationLookup(cfg.getString(ApplicationConfigConstants.DDP));

AuthenticationRoute authenticationRoute = new AuthenticationRoute(auth0Util,
userUtil,
cfg.getString(ApplicationConfigConstants.AUTH0_DOMAIN),
cfg.getString(ApplicationConfigConstants.AUTH0_MGT_SECRET),
cfg.getString(ApplicationConfigConstants.AUTH0_MGT_KEY),
Expand Down Expand Up @@ -930,11 +929,11 @@ private void setupAdminRoutes() {
UserRoleRoute userRoleRoute = new UserRoleRoute();
get(uiRoot + RoutePath.USER_ROLE, userRoleRoute, new JsonTransformer());
post(uiRoot + RoutePath.USER_ROLE, userRoleRoute, new JsonTransformer());
delete(uiRoot + RoutePath.USER_ROLE, userRoleRoute, new JsonTransformer());
put(uiRoot + RoutePath.USER_ROLE, userRoleRoute, new JsonTransformer());

UserRoute userRoute = new UserRoute();
post(uiRoot + RoutePath.USER, userRoute, new JsonTransformer());
delete(uiRoot + RoutePath.USER, userRoute, new JsonTransformer());
put(uiRoot + RoutePath.USER, userRoute, new JsonTransformer());
}


Expand Down Expand Up @@ -1060,6 +1059,8 @@ private void setupRouteGenericErrorHandlers() {
response.body(exception.getMessage());
});
exception(DsmInternalError.class, (exception, request, response) -> {
logger.error("Internal error {}", exception.toString());
exception.printStackTrace();
response.status(500);
response.body(exception.getMessage());
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import lombok.Data;
import lombok.NonNull;
import org.broadinstitute.dsm.exception.DsmInternalError;
import org.broadinstitute.dsm.statics.DBConstants;
import org.broadinstitute.dsm.statics.QueryExtension;
import org.broadinstitute.lddp.db.SimpleResult;
Expand All @@ -27,6 +29,8 @@ public class UserSettings {
+ "WHERE user.user_id = settings.user_id AND user.is_active = 1";
private static final String SQL_INSERT_USER_SETTINGS = "INSERT INTO user_settings SET user_id = ?";

private static final String SQL_DELETE_USER_SETTINGS = "DELETE FROM user_settings WHERE user_id = ?";

private static final String USER_ID = "userId";

private int rowsOnPage;
Expand Down Expand Up @@ -104,12 +108,38 @@ public static UserSettings getUserSettings(@NonNull String email) {
return us;
}

public static void insertUserSetting(Connection conn, int userId) {
try (PreparedStatement insertKit = conn.prepareStatement(SQL_INSERT_USER_SETTINGS)) {
insertKit.setInt(1, userId);
insertKit.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException("Error inserting new user_settings ", e);
public static int createUserSettings(int userId) {
return inTransaction(conn -> insertUserSetting(conn, userId));
}

public static int insertUserSetting(Connection conn, int userId) {
int id = -1;
try (PreparedStatement stmt = conn.prepareStatement(SQL_INSERT_USER_SETTINGS, Statement.RETURN_GENERATED_KEYS)) {
stmt.setInt(1, userId);
int result = stmt.executeUpdate();
if (result != 1) {
throw new DsmInternalError("Error inserting user setting. Result count was " + result);
}
try (ResultSet rs = stmt.getGeneratedKeys()) {
if (rs.next()) {
id = rs.getInt(1);
}
}
} catch (SQLException ex) {
throw new DsmInternalError("Error inserting user setting", ex);
}
return id;
}

public static int deleteUserSettings(int userId) {
return inTransaction(conn -> {
try (PreparedStatement stmt = conn.prepareStatement(SQL_DELETE_USER_SETTINGS)) {
stmt.setInt(1, userId);
return stmt.executeUpdate();
} catch (SQLException ex) {
String msg = String.format("Error deleting user settings: userId=%d", userId);
throw new DsmInternalError(msg, ex);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ public class UserDao implements Dao<UserDto> {
public static final String PHONE_NUMBER = "phone_number";
public static final String IS_ACTIVE = "is_active";
private static final String SQL_INSERT_USER = "INSERT INTO access_user (name, email, phone_number, is_active) VALUES (?,?,?,?)";
private static final String SQL_UPDATE_USER =
"UPDATE access_user SET name = ?, phone_number = ?, is_active = ? WHERE user_id = ?";
private static final String SQL_DELETE_USER_BY_ID = "DELETE FROM access_user WHERE user_id = ?";
private static final String SQL_SELECT_USER_BY_EMAIL =
"SELECT user.user_id, user.name, user.email, user.phone_number, user.is_active FROM access_user user "
+ "WHERE user.email = ?";
+ "WHERE UPPER(user.email) = ?";
private static final String SQL_SELECT_USER_BY_ID =
"SELECT user.user_id, user.name, user.email, user.phone_number, user.is_active FROM access_user user "
+ "WHERE user.user_id = ?";
Expand All @@ -36,7 +38,7 @@ public Optional<UserDto> getUserByEmail(@NonNull String email) {
SimpleResult results = inTransaction((conn) -> {
SimpleResult dbVals = new SimpleResult();
try (PreparedStatement stmt = conn.prepareStatement(SQL_SELECT_USER_BY_EMAIL)) {
stmt.setString(1, email);
stmt.setString(1, email.toUpperCase());
try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
dbVals.resultValue = new UserDto(rs.getInt(USER_ID),
Expand Down Expand Up @@ -87,7 +89,7 @@ public Optional<UserDto> get(long userId) {

@Override
public int create(UserDto userDto) {
String email = userDto.getEmail().orElse(null);
String email = userDto.getEmailOrThrow();
if (StringUtils.isBlank(email)) {
throw new DsmInternalError("Error inserting user: email is blank");
}
Expand All @@ -110,6 +112,26 @@ public int create(UserDto userDto) {
});
}

public static void update(int userId, UserDto userDto) {
String email = userDto.getEmailOrThrow();
String errorMsg = "Error updating user " + email;
int res = inTransaction(conn -> {
try (PreparedStatement stmt = conn.prepareStatement(SQL_UPDATE_USER)) {
stmt.setString(1, userDto.getName().orElse(null));
stmt.setString(2, userDto.getPhoneNumber().orElse(null));
stmt.setInt(3, userDto.getIsActive().orElse(1));
stmt.setInt(4, userId);
int result = stmt.executeUpdate();
if (result != 1) {
throw new DsmInternalError(errorMsg + " Result count was " + result);
}
return result;
} catch (SQLException ex) {
throw new DsmInternalError(errorMsg, ex);
}
});
}

@Override
public int delete(int id) {
SimpleResult simpleResult = DaoUtil.deleteById(id, SQL_DELETE_USER_BY_ID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import lombok.AllArgsConstructor;
import lombok.Setter;
import org.broadinstitute.dsm.exception.DsmInternalError;

@Setter
@AllArgsConstructor
Expand Down Expand Up @@ -36,10 +37,20 @@ public Optional<String> getEmail() {
return Optional.ofNullable(email);
}

// TODO: getEmail should always throw on a missing email since it is not nullable
// but there are a lot of callers. Feel free to chase them all down and rename this method -DC
public String getEmailOrThrow() {
return getEmail().orElseThrow(() -> new DsmInternalError("User email cannot be null"));
}

public Optional<String> getPhoneNumber() {
return Optional.ofNullable(phoneNumber);
}
public Optional<Integer> getIsActive() {
return Optional.ofNullable(isActive);
}

public boolean isActive() {
return getIsActive().orElse(0) == 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected void fillLabelsValues() {
MultiSearchResponse.Item response = msearch.getResponses()[i];
long count = 0;
if (response != null && response.getResponse() != null) {
count = response.getResponse().getHits().getTotalHits();
count = response.getResponse().getHits().getTotalHits().value;
}
fillData(values, labels, dashboardDto.getLabels().get(i), count);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public DashboardData get() {
if (response != null && response.getResponse() != null) {
return new CountData(dashboardDto.getDisplayType(), Collections.emptyList(),
dashboardDto.getSize(), dashboardDto.getDisplayText(), dashboardDto.getOrder(),
response.getResponse().getHits().getTotalHits()
response.getResponse().getHits().getTotalHits().value
);
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public class FieldTypeExtractor implements TypeExtractor<Map<String, String>> {
@Override
public Map<String, String> extract() {
if (isFieldsNotCached()) {
Map<String, GetFieldMappingsResponse.FieldMappingMetaData> mapping = getMapping().get(index);
Map<String, GetFieldMappingsResponse.FieldMappingMetadata> mapping = getMapping().get(index);
Map<String, String> fieldTypeMapping = new HashMap<>();
for (Map.Entry<String, GetFieldMappingsResponse.FieldMappingMetaData> entry : mapping.entrySet()) {
for (Map.Entry<String, GetFieldMappingsResponse.FieldMappingMetadata> entry : mapping.entrySet()) {
fieldTypeMapping.put(getRightMostFieldName(entry.getKey()), extractType(entry.getKey(), entry.getValue()));
}
cachedFieldTypes.putAll(fieldTypeMapping);
Expand All @@ -44,13 +44,13 @@ private List<String> notCachedFields() {
.collect(Collectors.toList());
}

private String extractType(String fullFieldName, GetFieldMappingsResponse.FieldMappingMetaData value) {
private String extractType(String fullFieldName, GetFieldMappingsResponse.FieldMappingMetadata value) {
String key = getRightMostFieldName(fullFieldName);
return (String) ((Map<String, Object>) value.sourceAsMap().get(key)).get(MappingGenerator.TYPE);
}

private Map<String, Map<String, GetFieldMappingsResponse.FieldMappingMetaData>> getMapping() {
Map<String, Map<String, GetFieldMappingsResponse.FieldMappingMetaData>> result = new HashMap<>();
private Map<String, Map<String, GetFieldMappingsResponse.FieldMappingMetadata>> getMapping() {
Map<String, Map<String, GetFieldMappingsResponse.FieldMappingMetadata>> result = new HashMap<>();
GetFieldMappingsRequest request = new GetFieldMappingsRequest();
request.indices(index);
String[] fields = this.notCachedFields().toArray(new String[] {});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public ElasticSearch getParticipantsWithinRange(String esParticipantsIndex, int
}
List<ElasticSearchParticipantDto> esParticipants = parseSourceMaps(response.getHits().getHits());
logger.info("Got " + esParticipants.size() + " participants from ES for instance " + esParticipantsIndex);
return new ElasticSearch(esParticipants, response.getHits().getTotalHits());
return new ElasticSearch(esParticipants, response.getHits().getTotalHits().value);
}

@Override
Expand All @@ -199,7 +199,7 @@ public ElasticSearch getParticipantsByIds(String esIndex, List<String> participa
List<ElasticSearchParticipantDto> esParticipants = parseSourceMaps(response.getHits().getHits());

logger.info("Got " + esParticipants.size() + " participants from ES for instance " + esIndex);
return new ElasticSearch(esParticipants, response.getHits().getTotalHits());
return new ElasticSearch(esParticipants, response.getHits().getTotalHits().value);
}

@Override
Expand Down Expand Up @@ -247,7 +247,7 @@ public ElasticSearch getParticipantsByRangeAndFilter(String esParticipantsIndex,
}
List<ElasticSearchParticipantDto> esParticipants = parseSourceMaps(response.getHits().getHits());
logger.info("Got " + esParticipants.size() + " participants from ES for instance " + esParticipantsIndex);
return new ElasticSearch(esParticipants, response.getHits().getTotalHits());
return new ElasticSearch(esParticipants, response.getHits().getTotalHits().value);
}

private AbstractQueryBuilder addOsteo2Filter(AbstractQueryBuilder queryBuilder) {
Expand Down Expand Up @@ -287,7 +287,7 @@ public ElasticSearch getParticipantsByRangeAndIds(String participantIndexES, int
}
List<ElasticSearchParticipantDto> esParticipants = parseSourceMaps(response.getHits().getHits());
logger.info("Got " + esParticipants.size() + " participants from ES for instance " + participantIndexES);
return new ElasticSearch(esParticipants, response.getHits().getTotalHits());
return new ElasticSearch(esParticipants, response.getHits().getTotalHits().value);
}

@Override
Expand Down Expand Up @@ -331,7 +331,7 @@ public ElasticSearch getAllParticipantsDataByInstanceIndex(String esParticipants
}
List<ElasticSearchParticipantDto> elasticSearchParticipantDtos = parseSourceMaps(searchResponse.getHits().getHits());
logger.info("Got " + elasticSearchParticipantDtos.size() + " participants from ES for instance " + esParticipantsIndex);
return new ElasticSearch(elasticSearchParticipantDtos, searchResponse.getHits().getTotalHits());
return new ElasticSearch(elasticSearchParticipantDtos, searchResponse.getHits().getTotalHits().value);
}

private BoolQueryBuilder getBoolQueryOfParticipantsId(List<String> participantIds) {
Expand Down
Loading

0 comments on commit 45efeed

Please sign in to comment.