Skip to content

Commit

Permalink
Local testing
Browse files Browse the repository at this point in the history
  • Loading branch information
dexamundsen committed Sep 19, 2024
1 parent ab198d7 commit 50ac496
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 102 deletions.
12 changes: 6 additions & 6 deletions service/src/main/java/bio/terra/tanagra/db/AnnotationDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void createAnnotationKey(String cohortId, AnnotationKey annotationKey) {
annotationKey.getDisplayName(),
annotationKey.getDescription(),
annotationKey.getDataType().name());
JdbcUtils.insertRows(jdbcTemplate, sql, "annotation_key", List.of(keyParamSets));
JdbcUtils.insertRows(jdbcTemplate, "annotation_key", sql, List.of(keyParamSets));

if (!annotationKey.getEnumVals().isEmpty()) {
sql =
Expand All @@ -98,7 +98,7 @@ public void createAnnotationKey(String cohortId, AnnotationKey annotationKey) {
annotationKey.getEnumVals().stream()
.map(val -> buildEnumValueParam(cohortId, annotationKey.getId(), val))
.toList();
JdbcUtils.insertRows(jdbcTemplate, sql, "annotation_key_enum_value", enumValueParamSets);
JdbcUtils.insertRows(jdbcTemplate, "annotation_key_enum_value", sql, enumValueParamSets);
}
}

Expand Down Expand Up @@ -279,7 +279,7 @@ public void updateAnnotationValues(
av.getStringVal(),
av.getDateVal()))
.toList();
JdbcUtils.insertRows(jdbcTemplate, sql, "annotation_value", valueParamSets);
JdbcUtils.insertRows(jdbcTemplate, "annotation_value", sql, valueParamSets);
}

@ReadTransaction
Expand All @@ -290,7 +290,7 @@ public List<AnnotationValue.Builder> getAllAnnotationValues(String cohortId) {
return jdbcTemplate.query(sql, params, ANNOTATION_VALUE_ROW_MAPPER);
}

private MapSqlParameterSource buildKeyParam(
static MapSqlParameterSource buildKeyParam(
String cohortId, String id, String displayName, String description, String dataTypeName) {
return new MapSqlParameterSource()
.addValue("cohort_id", cohortId)
Expand All @@ -300,15 +300,15 @@ private MapSqlParameterSource buildKeyParam(
.addValue("data_type", dataTypeName);
}

private MapSqlParameterSource buildEnumValueParam(
static MapSqlParameterSource buildEnumValueParam(
String cohortId, String keyId, String enumValue) {
return new MapSqlParameterSource()
.addValue("cohort_id", cohortId)
.addValue("annotation_key_id", keyId)
.addValue("enum", enumValue);
}

private MapSqlParameterSource buildValueParam(
static MapSqlParameterSource buildValueParam(
String cohortId,
String keyId,
String reviewId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,33 @@
package bio.terra.tanagra.db;

import bio.terra.common.db.WriteTransaction;
import bio.terra.tanagra.service.artifact.model.Cohort;
import bio.terra.tanagra.service.artifact.model.CohortRevision;
import bio.terra.tanagra.service.artifact.model.Review;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Component;

// A Dao for all artifacts: this is needed to avoid circular dependency errors
@Component
public class AllArtifactsDao {
public class ArtifactsDao {
private final NamedParameterJdbcTemplate jdbcTemplate;
private final CohortDao cohortDao;
private final ReviewDao reviewDao;
private final AnnotationDao annotationDao;

@Autowired
public AllArtifactsDao(
NamedParameterJdbcTemplate jdbcTemplate,
CohortDao cohortDao,
ReviewDao reviewDao,
AnnotationDao annotationDao) {
public ArtifactsDao(
NamedParameterJdbcTemplate jdbcTemplate, CohortDao cohortDao, ReviewDao reviewDao) {
this.jdbcTemplate = jdbcTemplate;
this.cohortDao = cohortDao;
this.reviewDao = reviewDao;
this.annotationDao = annotationDao;
}

/**
Expand All @@ -46,16 +42,24 @@ public String cloneCohort(
String displayName,
String description) {
// Cohort
Cohort ogCohort = cohortDao.getCohort(ogCohortId);
String clCohortId = RandomStringUtils.randomAlphanumeric(10);

cohortDao.insertCohortRow(
destinationStudyId,
clCohortId,
ogCohort.getUnderlay(),
userEmail,
displayName,
description);
// underlay: same as that of the original rows
MapSqlParameterSource cohortParamSets =
new MapSqlParameterSource()
.addValue("dst_study_id", destinationStudyId)
.addValue("og_cohort_id", ogCohortId)
.addValue("cl_cohort_id", clCohortId)
.addValue("user_email", userEmail)
.addValue("display_name", displayName)
.addValue("description", description);

String sql =
"INSERT INTO cohort (study_id, id, underlay, created_by, last_modified_by, display_name, description, is_deleted) "
+ "(SELECT :dst_study_id, :cl_cohort_id, underlay, :user_email, :user_email, :display_name, :description, false "
+ "FROM cohort "
+ "WHERE id = :og_cohort_id)";
JdbcUtils.insertRows(jdbcTemplate, "primary_entity_instance", sql, List.of(cohortParamSets));

// Reviews
List<Review> ogReviews = reviewDao.getAllReviews(ogCohortId, 0, Integer.MAX_VALUE);
Expand All @@ -73,7 +77,7 @@ public String cloneCohort(
reviewIdMap.put(ogReviewId, clReviewId);

reviewParamSets.add(
reviewDao.buildReviewParam(
ReviewDao.buildReviewParam(
clCohortId,
clReviewId,
review.getSize(),
Expand All @@ -98,7 +102,7 @@ public String cloneCohort(
reviewDao.insertReviewRows(reviewParamSets); // fk: cohort(id)

// Primary Entity Instance - fk: review(id)
String sql =
sql =
"INSERT INTO primary_entity_instance (review_id, id, stable_index) "
+ "(SELECT :cl_review_id, id, stable_index "
+ "FROM primary_entity_instance "
Expand All @@ -108,29 +112,30 @@ public String cloneCohort(
List<MapSqlParameterSource> revisionParamSets = new ArrayList<>();
List<MapSqlParameterSource> criteriaParamSets = new ArrayList<>();

ogCohort
.getRevisions()
.forEach(
revision -> {
String ogRevisionId = revision.getId();
String clRevisionId = RandomStringUtils.randomAlphanumeric(10);

revisionParamSets.add(
cohortDao.buildRevisionParam(
clCohortId,
clRevisionId,
revision.getVersion(),
revision.isMostRecent(),
revision.isEditable(),
userEmail,
revision.getRecordsCount(),
reviewIdMap.get(ogRevisionIdReviewIdMap.get(ogRevisionId))));

criteriaParamSets.add(
new MapSqlParameterSource()
.addValue("og_cohort_revision_id", ogRevisionId)
.addValue("cl_cohort_revision_id", clRevisionId));
});
List<Pair<String, CohortRevision.Builder>> ogRevisions =
cohortDao.getRevisions(Set.of(ogCohortId), false);
ogRevisions.forEach(
builderPair -> {
CohortRevision ogRevision = builderPair.getValue().build();
String ogRevisionId = ogRevision.getId();
String clRevisionId = RandomStringUtils.randomAlphanumeric(10);

revisionParamSets.add(
CohortDao.buildRevisionParam(
clCohortId,
clRevisionId,
ogRevision.getVersion(),
ogRevision.isMostRecent(),
ogRevision.isEditable(),
userEmail,
ogRevision.getRecordsCount(),
reviewIdMap.get(ogRevisionIdReviewIdMap.get(ogRevisionId))));

criteriaParamSets.add(
new MapSqlParameterSource()
.addValue("og_cohort_revision_id", ogRevisionId)
.addValue("cl_cohort_revision_id", clRevisionId));
});

// Cohort revision - fk: cohort(id), review(id)
sql =
Expand Down Expand Up @@ -172,10 +177,8 @@ public String cloneCohort(
+ "WHERE cohort_revision_id = :og_cohort_revision_id)";
JdbcUtils.insertRows(jdbcTemplate, "criteria_tag", sql, criteriaParamSets);

int ogAnnotKeyCount = annotationDao.getAnnotationKeysCount(ogCohortId);
List<MapSqlParameterSource> cohortIdParamSets =
Collections.nCopies(
ogAnnotKeyCount,
List.of(
new MapSqlParameterSource()
.addValue("og_cohort_id", ogCohortId)
.addValue("cl_cohort_id", clCohortId));
Expand Down
85 changes: 47 additions & 38 deletions service/src/main/java/bio/terra/tanagra/db/CohortDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,15 @@ public void deleteCohort(String id) {

@WriteTransaction
public void createCohort(String studyId, Cohort cohort) {
insertCohortRow(
studyId,
cohort.getId(),
cohort.getUnderlay(),
cohort.getCreatedBy(),
cohort.getDisplayName(),
cohort.getDescription());
MapSqlParameterSource cohortParamSets =
buildCohortParam(
studyId,
cohort.getId(),
cohort.getUnderlay(),
cohort.getCreatedBy(),
cohort.getDisplayName(),
cohort.getDescription());
insertCohortRows(List.of(cohortParamSets));

// Write the first cohort revision.
createRevision(cohort.getId(), cohort.getMostRecentRevision());
Expand Down Expand Up @@ -335,15 +337,8 @@ private List<Cohort> getCohortsHelper(String cohortsSql, MapSqlParameterSource c
}

// Fetch the most recent cohort revisions. (cohort id -> cohort revision)
String sql =
COHORT_REVISION_SELECT_SQL + " WHERE cohort_id IN (:cohort_ids) AND is_most_recent";
MapSqlParameterSource params =
new MapSqlParameterSource()
.addValue(
"cohort_ids",
cohorts.stream().map(Cohort.Builder::getId).collect(Collectors.toSet()));
List<Pair<String, CohortRevision.Builder>> cohortRevisions =
jdbcTemplate.query(sql, params, COHORT_REVISION_ROW_MAPPER);
getRevisions(cohorts.stream().map(Cohort.Builder::getId).collect(Collectors.toSet()), true);

// Populate the criteria.
getCriteriaHelper(cohortRevisions);
Expand Down Expand Up @@ -387,6 +382,18 @@ public void createRevision(String cohortId, CohortRevision cohortRevision) {
updateCriteriaHelper(cohortRevision.getId(), cohortRevision.getSections());
}

@ReadTransaction
public List<Pair<String, CohortRevision.Builder>> getRevisions(
Set<String> cohortIds, boolean mostRecentOnly) {
// Fetch the most recent cohort revisions. (cohort id -> cohort revision)
String sql = COHORT_REVISION_SELECT_SQL + " WHERE cohort_id IN (:cohort_ids)";
if (mostRecentOnly) {
sql += " AND is_most_recent";
}
MapSqlParameterSource params = new MapSqlParameterSource().addValue("cohort_ids", cohortIds);
return jdbcTemplate.query(sql, params, COHORT_REVISION_ROW_MAPPER);
}

@ReadTransaction
public void getCriteriaHelper(List<Pair<String, CohortRevision.Builder>> cohortRevisions) {
// Fetch criteria group sections. (cohort revision id -> criteria group section)
Expand Down Expand Up @@ -706,57 +713,59 @@ private void buildParamsForGroupCriteriaTag(
}
}

void insertCohortRow(
String studyId,
String id,
String underlay,
String userEmail,
String displayName,
String description) {
void insertCohortRows(List<MapSqlParameterSource> cohortParamSets) {
// Write the cohort. The created and last_modified fields are set by the DB automatically on
// insert.
String sql =
"INSERT INTO cohort (study_id, id, underlay, created_by, last_modified_by, display_name, description, is_deleted) "
+ "VALUES (:study_id, :id, :underlay, :created_by, :last_modified_by, :display_name, :description, false)";
MapSqlParameterSource params =
new MapSqlParameterSource()
.addValue("study_id", studyId)
.addValue("id", id)
.addValue("underlay", underlay)
.addValue("created_by", userEmail)
.addValue("last_modified_by", userEmail)
.addValue("display_name", displayName)
.addValue("description", description);
JdbcUtils.insertRows(jdbcTemplate, sql, "cohort", List.of(params));
JdbcUtils.insertRows(jdbcTemplate, "cohort", sql, cohortParamSets);
}

private void insertCriteriaRows(
void insertCriteriaRows(
List<MapSqlParameterSource> sectionParamSets,
List<MapSqlParameterSource> groupParamSets,
List<MapSqlParameterSource> criteriaParamSets,
List<MapSqlParameterSource> tagParamSets) {
String sql =
"INSERT INTO criteria_group_section (cohort_revision_id, id, display_name, operator, is_excluded, is_disabled, first_condition_reducing_operator, second_condition_reducing_operator, join_operator, join_operator_value, list_index) "
+ "VALUES (:cohort_revision_id, :id, :display_name, :operator, :is_excluded, :is_disabled, :first_condition_reducing_operator, :second_condition_reducing_operator, :join_operator, :join_operator_value, :list_index)";
JdbcUtils.insertRows(jdbcTemplate, sql, "criteria_group_section", sectionParamSets);
JdbcUtils.insertRows(jdbcTemplate, "criteria_group_section", sql, sectionParamSets);

sql =
"INSERT INTO criteria_group (cohort_revision_id, criteria_group_section_id, id, display_name, condition_index, list_index, is_disabled) "
+ "VALUES (:cohort_revision_id, :criteria_group_section_id, :id, :display_name, :condition_index, :list_index, :is_disabled)";
JdbcUtils.insertRows(jdbcTemplate, sql, "criteria_group", groupParamSets);
JdbcUtils.insertRows(jdbcTemplate, "criteria_group", sql, groupParamSets);

sql =
"INSERT INTO criteria (cohort_revision_id, criteria_group_section_id, criteria_group_id, id, display_name, plugin_name, plugin_version, selector_or_modifier_name, selection_data, ui_config, list_index) "
+ "VALUES (:cohort_revision_id, :criteria_group_section_id, :criteria_group_id, :id, :display_name, :plugin_name, :plugin_version, :selector_or_modifier_name, :selection_data, :ui_config, :list_index)";
JdbcUtils.insertRows(jdbcTemplate, sql, "criteria", criteriaParamSets);
JdbcUtils.insertRows(jdbcTemplate, "criteria", sql, criteriaParamSets);

sql =
"INSERT INTO criteria_tag (cohort_revision_id, criteria_group_section_id, criteria_group_id, criteria_id, criteria_key, criteria_value) "
+ "VALUES (:cohort_revision_id, :criteria_group_section_id, :criteria_group_id, :criteria_id, :key, :value)";
JdbcUtils.insertRows(jdbcTemplate, sql, "criteria_tag", tagParamSets);
JdbcUtils.insertRows(jdbcTemplate, "criteria_tag", sql, tagParamSets);
}

static MapSqlParameterSource buildCohortParam(
String studyId,
String id,
String underlay,
String userEmail,
String displayName,
String description) {
return new MapSqlParameterSource()
.addValue("study_id", studyId)
.addValue("id", id)
.addValue("underlay", underlay)
.addValue("created_by", userEmail)
.addValue("last_modified_by", userEmail)
.addValue("display_name", displayName)
.addValue("description", description);
}

MapSqlParameterSource buildRevisionParam(
static MapSqlParameterSource buildRevisionParam(
String cohortId,
String id,
int version,
Expand Down
6 changes: 3 additions & 3 deletions service/src/main/java/bio/terra/tanagra/db/ReviewDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,10 @@ void insertReviewRows(List<MapSqlParameterSource> reviewParamSets) {
String sql =
"INSERT INTO review (cohort_id, id, size, display_name, description, created_by, last_modified_by, is_deleted) "
+ "VALUES (:cohort_id, :id, :size, :display_name, :description, :created_by, :last_modified_by, false)";
JdbcUtils.insertRows(jdbcTemplate, sql, "review", reviewParamSets);
JdbcUtils.insertRows(jdbcTemplate, "review", sql, reviewParamSets);
}

MapSqlParameterSource buildReviewParam(
static MapSqlParameterSource buildReviewParam(
String cohortId,
String id,
int size,
Expand All @@ -270,7 +270,7 @@ MapSqlParameterSource buildReviewParam(
.addValue("last_modified_by", userEmail);
}

private MapSqlParameterSource buildPrimaryEntityParam(String reviewId, Long id, int stableIndex) {
static MapSqlParameterSource buildPrimaryEntityParam(String reviewId, Long id, int stableIndex) {
return new MapSqlParameterSource()
.addValue("review_id", reviewId)
.addValue("id", id)
Expand Down
Loading

0 comments on commit 50ac496

Please sign in to comment.