Skip to content

Commit

Permalink
DDP-6034: - moving changes from PR into prod branch (#320)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimoneMaiwald committed Dec 9, 2021
1 parent fa2fdb7 commit 4a191a9
Show file tree
Hide file tree
Showing 13 changed files with 509 additions and 23 deletions.
98 changes: 93 additions & 5 deletions src/main/java/org/broadinstitute/dsm/db/OncHistoryDetail.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,25 @@ public class OncHistoryDetail {
"oD.location_px, oD.histology, oD.accession_number, oD.facility, oD.phone, oD.fax, oD.notes, oD.additional_values_json, " +
"oD.request, oD.fax_sent, oD.fax_sent_by, oD.fax_confirmed, oD.fax_sent_2, oD.fax_sent_2_by, oD.fax_confirmed_2, oD.fax_sent_3, " +
"oD.fax_sent_3_by, oD.fax_confirmed_3, oD.tissue_received, oD.tissue_problem_option, oD.gender, oD.destruction_policy, oD.unable_obtain_tissue, " +
"tissue_id, t.notes, count_received, tissue_type, tissue_site, tumor_type, h_e, pathology_report, collaborator_sample_id, block_sent, scrolls_received, sk_id, sm_id, " +
"t.tissue_id, t.notes, count_received, tissue_type, tissue_site, tumor_type, h_e, pathology_report, collaborator_sample_id, block_sent, scrolls_received, sk_id, sm_id, " +
"sent_gp, first_sm_id, additional_tissue_value_json, expected_return, return_date, return_fedex_id, shl_work_number, tumor_percentage, tissue_sequence, " +
" scrolls_count, uss_count, h_e_count, blocks_count " +
" scrolls_count, uss_count, h_e_count, blocks_count, p.ddp_participant_id, sm.sm_id_value, sm.sm_id_type_id, sm.sm_id_pk, sm.deleted, sm.tissue_id, smt.sm_id_type " +
"FROM ddp_onc_history_detail oD " +
"LEFT JOIN ddp_medical_record m on (oD.medical_record_id = m.medical_record_id AND NOT oD.deleted <=> 1 AND NOT m.deleted <=> 1) " +
"LEFT JOIN ddp_institution inst on (inst.institution_id = m.institution_id) " +
"LEFT JOIN ddp_participant p on (p.participant_id = inst.participant_id) " +
"LEFT JOIN ddp_instance realm on (p.ddp_instance_id = realm.ddp_instance_id) " +
"LEFT JOIN ddp_tissue t on (oD.onc_history_detail_id = t.onc_history_detail_id AND NOT t.deleted <=> 1) " +
"LEFT JOIN sm_id sm on (sm.tissue_id = t.tissue_id AND NOT sm.deleted <=> 1 ) " +
"LEFT JOIN sm_id_type smt on (smt.sm_id_type_id = sm.sm_id_type_id ) " +
"WHERE realm.instance_name = ? ";
private static final String SQL_CREATE_ONC_HISTORY = "INSERT INTO ddp_onc_history_detail SET medical_record_id = ?, request = ?, last_changed = ?, changed_by = ?";
private static final String SQL_SELECT_ONC_HISTORY = "SELECT onc_history_detail_id, medical_record_id, date_px, type_px, location_px, histology, accession_number, facility," +
" phone, fax, notes, additional_values_json, request, fax_sent, fax_sent_by, fax_confirmed, fax_sent_2, fax_sent_2_by, fax_confirmed_2, fax_sent_3, fax_sent_3_by, fax_confirmed_3," +
" tissue_received, gender, tissue_problem_option, destruction_policy FROM ddp_onc_history_detail WHERE NOT (deleted <=> 1)";
private static final String SQL_SELECT_TISSUE_RECEIVED = "SELECT tissue_received FROM ddp_onc_history_detail WHERE onc_history_detail_id = ?";
private static final String SQL_INSERT_ONC_HISTORY_DETAIL = "INSERT INTO ddp_onc_history_detail SET medical_record_id = ?, request = ?, last_changed = ?, changed_by = ?";
public static final String SQL_ORDER_BY = " ORDER BY p.ddp_participant_id, inst.ddp_institution_id, oD.onc_history_detail_id ASC";
public static final String SQL_ORDER_BY = " ORDER BY p.ddp_participant_id, inst.ddp_institution_id, oD.onc_history_detail_id, t.tissue_id ASC";
public static final String SQL_SELECT_ONC_HISTORY_LAST_CHANGED = "SELECT oD.last_changed FROM ddp_institution inst " +
"LEFT JOIN ddp_participant as p on (p.participant_id = inst.participant_id) LEFT JOIN ddp_instance as ddp on (ddp.ddp_instance_id = p.ddp_instance_id) " +
"LEFT JOIN ddp_medical_record as m on (m.institution_id = inst.institution_id AND NOT m.deleted <=> 1) LEFT JOIN ddp_onc_history_detail as oD on (m.medical_record_id = oD.medical_record_id) " +
Expand Down Expand Up @@ -281,7 +283,12 @@ public static OncHistoryDetail getOncHistoryDetail(@NonNull ResultSet rs) throws
rs.getString(DBConstants.DESTRUCTION_POLICY),
rs.getBoolean(DBConstants.UNABLE_OBTAIN_TISSUE)
);

try {
oncHistoryDetail.setParticipantId(rs.getString(DBConstants.DDP_PARTICIPANT_ID));
}
catch (java.sql.SQLException e) {
oncHistoryDetail.setParticipantId(null);
}
return oncHistoryDetail;
}

Expand All @@ -298,17 +305,97 @@ public static Map<String, List<OncHistoryDetail>> getOncHistoryDetails(@NonNull
public static Map<String, List<OncHistoryDetail>> getOncHistoryDetails(@NonNull String realm, String queryAddition) {
logger.info("Collection oncHistoryDetail information");
Map<String, List<OncHistoryDetail>> oncHistory = new HashMap<>();
HashMap<String, Tissue> tissues = new HashMap<>();
SimpleResult results = inTransaction((conn) -> {
SimpleResult dbVals = new SimpleResult();
try (PreparedStatement stmt = conn.prepareStatement(DBUtil.getFinalQuery(SQL_SELECT_ONC_HISTORY_DETAIL, queryAddition) + SQL_ORDER_BY)) {
stmt.setString(1, realm);
try (ResultSet rs = stmt.executeQuery()) {
Map<String, OncHistoryDetail> oncHistoryMap = new HashMap<>();
while (rs.next()) {
String ddpParticipantId = rs.getString(DBConstants.DDP_PARTICIPANT_ID);
String oncHistoryDetailId = rs.getString(DBConstants.ONC_HISTORY_DETAIL_ID);
TissueSmId tissueSmId = Tissue.getSMIds(rs);
Tissue tissue;
if (tissueSmId != null && tissues.containsKey(tissueSmId.getTissueId())) {
tissue = tissues.get(tissueSmId.getTissueId());
}
else {
tissue = Tissue.getTissue(rs);
}
if (tissueSmId != null) {
tissue.setSmIdBasedOnType(tissueSmId, rs);
}
tissues.put(tissue.getTissueId(), tissue);

//check if oncHistoryDetails is already in map
List<OncHistoryDetail> oncHistoryDataList = new ArrayList<>();
if (oncHistory.containsKey(ddpParticipantId)) {
oncHistoryDataList = oncHistory.get(ddpParticipantId);
}
else {
oncHistory.put(ddpParticipantId, oncHistoryDataList);
oncHistoryMap = new HashMap<>();
}

OncHistoryDetail oncHistoryDetail = null;
if (oncHistoryMap.containsKey(oncHistoryDetailId)) {
oncHistoryDetail = oncHistoryMap.get(oncHistoryDetailId);
oncHistoryDetail.getTissues().removeIf(tissue1 -> tissue1.getTissueId() == tissue.getTissueId());
oncHistoryDetail.addTissue(tissue);
}
else {
oncHistoryDetail = getOncHistoryDetail(rs);
oncHistoryDetail.addTissue(tissue);
oncHistoryDataList.add(oncHistoryDetail);
}
oncHistoryMap.put(oncHistoryDetailId, oncHistoryDetail);
}
}
}
catch (SQLException ex) {
dbVals.resultException = ex;
}
return dbVals;
});

if (results.resultException != null) {
throw new RuntimeException("Couldn't get list of oncHistories ", results.resultException);
}

logger.info("Got " + oncHistory.size() + " participants oncHistories in DSM DB for " + realm);
return oncHistory;
}

public static Map<String, List<OncHistoryDetail>> getOncHistoryDetails(@NonNull String realm, String queryAddition, List<String> values) {
logger.info("Collection oncHistoryDetail information");
Map<String, List<OncHistoryDetail>> oncHistory = new HashMap<>();
HashMap<String, Tissue> tissues = new HashMap<>();
SimpleResult results = inTransaction((conn) -> {
SimpleResult dbVals = new SimpleResult();
try (PreparedStatement stmt = conn.prepareStatement(DBUtil.getFinalQuery(SQL_SELECT_ONC_HISTORY_DETAIL, queryAddition) + SQL_ORDER_BY)) {
stmt.setString(1, realm);
for (int i = 0; i < values.size(); i++) {
stmt.setString(i + 2, values.get(i));
}
try (ResultSet rs = stmt.executeQuery()) {
Map<String, OncHistoryDetail> oncHistoryMap = new HashMap<>();
while (rs.next()) {
String ddpParticipantId = rs.getString(DBConstants.DDP_PARTICIPANT_ID);
String oncHistoryDetailId = rs.getString(DBConstants.ONC_HISTORY_DETAIL_ID);

Tissue tissue = Tissue.getTissue(rs);
TissueSmId tissueSmId = Tissue.getSMIds(rs);
Tissue tissue;
if (tissueSmId != null && tissues.containsKey(tissueSmId.getTissueId())) {
tissue = tissues.get(tissueSmId.getTissueId());
}
else {
tissue = Tissue.getTissue(rs);
}
if (tissueSmId != null) {
tissue.setSmIdBasedOnType(tissueSmId, rs);
}
tissues.put(tissue.getTissueId(), tissue);

//check if oncHistoryDetails is already in map
List<OncHistoryDetail> oncHistoryDataList = new ArrayList<>();
Expand All @@ -323,6 +410,7 @@ public static Map<String, List<OncHistoryDetail>> getOncHistoryDetails(@NonNull
OncHistoryDetail oncHistoryDetail = null;
if (oncHistoryMap.containsKey(oncHistoryDetailId)) {
oncHistoryDetail = oncHistoryMap.get(oncHistoryDetailId);
oncHistoryDetail.getTissues().removeIf(tissue1 -> tissue.getTissueId() == tissue1.getTissueId());
oncHistoryDetail.addTissue(tissue);
}
else {
Expand Down
75 changes: 67 additions & 8 deletions src/main/java/org/broadinstitute/dsm/db/Tissue.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static org.broadinstitute.ddp.db.TransactionWrapper.inTransaction;
Expand All @@ -30,7 +31,9 @@ public class Tissue {
private static final String SQL_SELECT_TISSUE = "SELECT tissue_id, onc_history_detail_id, notes, count_received, tissue_type, tissue_site, tumor_type, h_e, " +
"pathology_report, collaborator_sample_id, block_sent, expected_return, return_date, return_fedex_id, scrolls_received, sk_id, sm_id, " +
"scrolls_count, uss_count, blocks_count, h_e_count, first_sm_id, sent_gp, last_changed, changed_by, additional_tissue_value_json, shl_work_number, " +
"tumor_percentage, tissue_sequence FROM ddp_tissue t WHERE NOT (deleted <=> 1) AND onc_history_detail_id = ?";
"tumor_percentage, tissue_sequence, sm.sm_id_value, sm.sm_id_type_id, sm.sm_id_pk, sm.deleted, sm.tissue_id FROM ddp_tissue t " +
"LEFT JOIN sm_id sm on (sm.tissue_id = t.tissue_id AND NOT sm.deleted <=> 1 AND NOT t.deleted <=> 1) " +
"WHERE NOT (deleted <=> 1) AND onc_history_detail_id = ?";
private static final String SQL_INSERT_TISSUE = "INSERT INTO ddp_tissue SET onc_history_detail_id = ?, last_changed = ?, changed_by = ?";
public static final String SQL_SELECT_TISSUE_LAST_CHANGED = "SELECT t.last_changed FROM ddp_institution inst " +
"LEFT JOIN ddp_participant as p on (p.participant_id = inst.participant_id) LEFT JOIN ddp_instance as ddp on (ddp.ddp_instance_id = p.ddp_instance_id) " +
Expand Down Expand Up @@ -123,14 +126,19 @@ public class Tissue {
@ColumnName (DBConstants.H_E_COUNT)
private Integer hECount;

private List<TissueSmId> ussSMID;

private List<TissueSmId> scrollSMID;

private List<TissueSmId> heSMID;


public Tissue(String tissueId, String oncHistoryDetailId, String tNotes, Integer countReceived, String tissueType,
String tissueSite, String tumorType, String hE, String pathologyReport, String collaboratorSampleId,
String blockSent, String scrollsReceived, String skId, String smId, String sentGp, String firstSmId,
String additionalValues, String expectedReturn, String tissueReturnDate,
String returnFedexId, String shlWorkNumber, String tumorPercentage, String sequenceResults, Integer scrollsCount,
Integer ussCount, Integer blocksCount, Integer hECount) {
Integer ussCount, Integer blocksCount, Integer hECount, List<TissueSmId> ussSMIDs, List<TissueSmId> scrollSMIDs, List<TissueSmId> heSMID) {
this.tissueId = tissueId;
this.oncHistoryDetailId = oncHistoryDetailId;
this.tNotes = tNotes;
Expand Down Expand Up @@ -158,11 +166,14 @@ public Tissue(String tissueId, String oncHistoryDetailId, String tNotes, Integer
this.hECount = hECount;
this.blocksCount = blocksCount;
this.ussCount = ussCount;
this.scrollSMID = scrollSMIDs;
this.ussSMID = ussSMIDs;
this.heSMID = heSMID;
}

public static Tissue getTissue(@NonNull ResultSet rs) throws SQLException {
Tissue tissue = new Tissue(
rs.getString(DBConstants.TISSUE_ID),
rs.getString("t." + DBConstants.TISSUE_ID),
rs.getString(DBConstants.ONC_HISTORY_DETAIL_ID),
rs.getString(DBConstants.DDP_TISSUE_ALIAS + DBConstants.ALIAS_DELIMITER + DBConstants.NOTES),
rs.getInt(DBConstants.COUNT_RECEIVED),
Expand All @@ -188,19 +199,39 @@ public static Tissue getTissue(@NonNull ResultSet rs) throws SQLException {
rs.getInt(DBConstants.SCROLLS_COUNT),
rs.getInt(DBConstants.USS_COUNT),
rs.getInt(DBConstants.BLOCKS_COUNT),
rs.getInt(DBConstants.H_E_COUNT));
rs.getInt(DBConstants.H_E_COUNT),
new ArrayList<>(),
new ArrayList<>(),
new ArrayList<>());
return tissue;
}


public static TissueSmId getSMIds(ResultSet rs) {
return TissueSmId.getSMIdsForTissueId(rs);
}

public static List<Tissue> getTissue(@NonNull Connection conn, @NonNull String oncHistoryDetailId) {

List<Tissue> tissue = new ArrayList<>();
List<Tissue> tissueList = new ArrayList<>();
SimpleResult dbVals = new SimpleResult();
HashMap<String, Tissue> tissues = new HashMap<>();
try (PreparedStatement stmt = conn.prepareStatement(SQL_SELECT_TISSUE)) {
stmt.setString(1, oncHistoryDetailId);
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
tissue.add(getTissue(rs));
TissueSmId tissueSmId = getSMIds(rs);
Tissue tissue;
if (tissueSmId != null && tissues.containsKey(tissueSmId.getTissueId())) {
tissue = tissues.get(tissueSmId.getTissueId());
}
else {
tissue = getTissue(rs);
}
if (tissueSmId != null) {
tissue.setSmIdBasedOnType(tissueSmId, rs);
}
tissues.put(tissue.tissueId, tissue);
}
}
}
Expand All @@ -211,9 +242,37 @@ public static List<Tissue> getTissue(@NonNull Connection conn, @NonNull String o
if (dbVals.resultException != null) {
throw new RuntimeException("Error getting tissue for oncHistoryDetails w/ id " + oncHistoryDetailId, dbVals.resultException);
}
tissueList.addAll(tissues.values());
logger.info("Found " + tissueList.size() + " tissue for oncHistoryDetails w/ id " + oncHistoryDetailId);
return tissueList;
}

public void setSmIdBasedOnType(TissueSmId tissueSmId, ResultSet rs) {
if (tissueSmId == null || tissueSmId.getSmIdType() == null) {
return;
}
try {
String type = rs.getString(DBConstants.SM_ID_TYPE_TABLE_ALIAS + "." + DBConstants.SM_ID_TYPE);
switch (type.toLowerCase()) {
case "he": {
this.heSMID.add(tissueSmId);
break;

}
case "uss": {
this.ussSMID.add(tissueSmId);
break;
}
case "scrolls": {
this.scrollSMID.add(tissueSmId);
break;
}
}
}
catch (SQLException throwables) {
throwables.printStackTrace();
}

logger.info("Found " + tissue.size() + " tissue for oncHistoryDetails w/ id " + oncHistoryDetailId);
return tissue;
}

public static String createNewTissue(@NonNull String oncHistoryId, @NonNull String user) {
Expand Down
78 changes: 78 additions & 0 deletions src/main/java/org/broadinstitute/dsm/db/TissueSmId.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package org.broadinstitute.dsm.db;

import lombok.Data;
import org.broadinstitute.dsm.db.dao.ddp.tissue.TissueSMIDDao;
import org.broadinstitute.dsm.db.structure.ColumnName;
import org.broadinstitute.dsm.db.structure.TableName;
import org.broadinstitute.dsm.statics.DBConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.ResultSet;
import java.sql.SQLException;

@Data
@TableName (
name = DBConstants.SM_ID_TABLE,
alias = DBConstants.SM_ID_TABLE_ALIAS,
primaryKey = DBConstants.SM_ID_PK,
columnPrefix = "")
public class TissueSmId {

@ColumnName (DBConstants.SM_ID_VALUE)
private String smIdValue;

@ColumnName (DBConstants.SM_ID_TYPE_ID)
private String smIdType;

@ColumnName (DBConstants.SM_ID_TISSUE_ID)
private String tissueId;

@ColumnName (DBConstants.SM_ID_PK)
private String smIdPk;

@ColumnName (DBConstants.DELETED)
private Boolean deleted;

public static String HE = "he";
public static String USS = "uss";
public static String SCROLLS = "scrolls";
private static final Logger logger = LoggerFactory.getLogger(TissueSmId.class);

public TissueSmId() {
}

public TissueSmId(String smIdPk, String smIdType, String smIdValue, String tissueId) {
this.smIdPk = smIdPk;
this.smIdType = smIdType;
this.smIdValue = smIdValue;
this.tissueId = tissueId;
}


public static TissueSmId getSMIdsForTissueId(ResultSet rs) {
TissueSmId tissueSmId = null;

try {
if (rs.getString(DBConstants.SM_ID_PK) == null)
return null;
tissueSmId = new TissueSmId(
rs.getString(DBConstants.SM_ID_PK),
rs.getString(DBConstants.SM_ID_TYPE_ID),
rs.getString(DBConstants.SM_ID_VALUE),
rs.getString("sm."+DBConstants.TISSUE_ID)
);
if(tissueSmId!=null)
tissueSmId.setDeleted(rs.getBoolean("sm."+DBConstants.DELETED));
}
catch (SQLException e) {
logger.error("problem getting tissue sm ids", e);
}
return tissueSmId;
}

public String createNewSmId(String tissueId, String userId, String smIdType) {
String smIdId = new TissueSMIDDao().createNewSMIDForTissue(tissueId, userId, smIdType);
return smIdId;
}
}
Loading

0 comments on commit 4a191a9

Please sign in to comment.