From 989168d81f35d8033e430108633e438228c677dc Mon Sep 17 00:00:00 2001 From: Jan Cortiel Date: Wed, 11 Dec 2024 15:33:45 +0100 Subject: [PATCH 1/2] #436: Refactor participant status update with null-check for 'start'. Revised the parameter source construction to add 'start' only if it's not null. This prevents potential issues with null values while maintaining functionality. The update ensures cleaner and safer code execution. --- .../more/data/repository/StudyRepository.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/main/java/io/redlink/more/data/repository/StudyRepository.java b/src/main/java/io/redlink/more/data/repository/StudyRepository.java index d6869e9..0334875 100644 --- a/src/main/java/io/redlink/more/data/repository/StudyRepository.java +++ b/src/main/java/io/redlink/more/data/repository/StudyRepository.java @@ -287,12 +287,15 @@ private void updateParticipantStatus(long studyId, int groupId, int participantI ); } - namedTemplate.update(SQL_SET_PARTICIPANT_STATUS, - toParameterSource(studyId, participantId) - .addValue("start", start) - .addValue("oldStatus", oldStatus) - .addValue("newStatus", newStatus) - ); + var parameterSource = toParameterSource(studyId, participantId) + .addValue("oldStatus", oldStatus) + .addValue("newStatus", newStatus); + + if (start != null) { + parameterSource.addValue("start", start); + } + + namedTemplate.update(SQL_SET_PARTICIPANT_STATUS, parameterSource); } private void storeConsent(long studyId, int participantId, ParticipantConsent consent) { From 9df504805b0d791cea497242e2b6b651d7d48ce7 Mon Sep 17 00:00:00 2001 From: Jan Cortiel Date: Thu, 12 Dec 2024 08:48:36 +0100 Subject: [PATCH 2/2] #436: Refactor SQL statements to use consistent multiline strings. Updated SQL queries to use the multiline string (`"""`) format for improved readability and maintainability. Simplified logic for setting participant status by consolidating parameter handling. --- .../more/data/repository/StudyRepository.java | 81 ++++++++++--------- 1 file changed, 44 insertions(+), 37 deletions(-) diff --git a/src/main/java/io/redlink/more/data/repository/StudyRepository.java b/src/main/java/io/redlink/more/data/repository/StudyRepository.java index 0334875..b3fd04f 100644 --- a/src/main/java/io/redlink/more/data/repository/StudyRepository.java +++ b/src/main/java/io/redlink/more/data/repository/StudyRepository.java @@ -66,42 +66,50 @@ INNER JOIN studies s on (s.study_id = pt.study_id) "DELETE FROM registration_tokens WHERE token = ?"; private static final String SQL_INSERT_CREDENTIALS = - "WITH data as (SELECT :api_secret as api_secret, :study_id as study_id, :participant_id as participant_id) " + - "INSERT INTO api_credentials (api_id, api_secret, study_id, participant_id) " + - "SELECT md5(study_id::text || random()::text), api_secret, study_id, participant_id FROM data " + - "RETURNING api_id"; + """ + WITH data as (SELECT :api_secret as api_secret, :study_id as study_id, :participant_id as participant_id) + INSERT INTO api_credentials (api_id, api_secret, study_id, participant_id) + SELECT md5(study_id::text || random()::text), api_secret, study_id, participant_id FROM data + RETURNING api_id"""; private static final String SQL_CLEAR_CREDENTIALS = - "DELETE FROM api_credentials " + - "WHERE api_id = :api_id " + - "RETURNING study_id, participant_id"; + """ + DELETE FROM api_credentials + WHERE api_id = :api_id + RETURNING study_id, participant_id"""; private static final String SQL_INSERT_STUDY_CONSENT = - "INSERT INTO participation_consents(study_id, participant_id, accepted, origin, content_md5) VALUES (:study_id, :participant_id, :accepted, :origin, :content_md5) " + - "ON CONFLICT (study_id, participant_id) DO " + - " UPDATE SET accepted = excluded.accepted, origin = excluded.origin, content_md5 = excluded.content_md5, " + - " consent_timestamp = now(), consent_withdrawn = NULL"; + """ + INSERT INTO participation_consents(study_id, participant_id, accepted, origin, content_md5) VALUES (:study_id, :participant_id, :accepted, :origin, :content_md5) + ON CONFLICT (study_id, participant_id) DO + UPDATE SET accepted = excluded.accepted, origin = excluded.origin, content_md5 = excluded.content_md5, + consent_timestamp = now(), consent_withdrawn = NULL"""; private static final String SQL_WITHDRAW_STUDY_CONSENT = - "UPDATE participation_consents " + - "SET consent_withdrawn = now() " + - "WHERE study_id = :study_id AND participant_id = :participant_id"; + """ + UPDATE participation_consents + SET consent_withdrawn = now() + WHERE study_id = :study_id AND participant_id = :participant_id"""; private static final String SQL_INSERT_OBSERVATION_CONSENT = - "INSERT INTO observation_consents(study_id, participant_id, observation_id) VALUES (:study_id, :participant_id, :observation_id) " + - "ON CONFLICT (study_id, participant_id, observation_id) DO NOTHING"; + """ + INSERT INTO observation_consents(study_id, participant_id, observation_id) VALUES (:study_id, :participant_id, :observation_id) + ON CONFLICT (study_id, participant_id, observation_id) DO NOTHING"""; private static final String SQL_SET_PARTICIPANT_STATUS = - "UPDATE participants " + - "SET status = :newStatus::participant_status, start = :start, modified = now() " + - "WHERE study_id = :study_id AND participant_id = :participant_id AND status = :oldStatus::participant_status"; + """ + UPDATE participants + SET status = :newStatus::participant_status, start = COALESCE(:start, start), modified = now() + WHERE study_id = :study_id AND participant_id = :participant_id AND status = :oldStatus::participant_status"""; private static final String SQL_LIST_PARTICIPANTS_BY_STUDY = - "SELECT participant_id, alias, status, sg.study_group_id, sg.title as study_group_title, start " + - "FROM participants p LEFT OUTER JOIN study_groups sg ON ( p.study_id = sg.study_id AND p.study_group_id = sg.study_group_id ) " + - "WHERE p.study_id = :study_id " + - "AND (p.study_group_id = :study_group_id OR :study_group_id::INT IS NULL)"; + """ + SELECT participant_id, alias, status, sg.study_group_id, sg.title as study_group_title, start + FROM participants p LEFT OUTER JOIN study_groups sg ON ( p.study_id = sg.study_id AND p.study_group_id = sg.study_group_id ) + WHERE p.study_id = :study_id + AND (p.study_group_id = :study_group_id OR :study_group_id::INT IS NULL)"""; private static final String GET_OBSERVATION_PROPERTIES_FOR_PARTICIPANT = - "SELECT properties FROM participant_observation_properties " + - "WHERE study_id = ? AND participant_id = ? AND observation_id = ?"; + """ + SELECT properties FROM participant_observation_properties + WHERE study_id = ? AND participant_id = ? AND observation_id = ?"""; private static final String GET_API_ROUTING_INFO_BY_API_TOKEN = """ SELECT t.study_id, t.observation_id, o.study_group_id, o.type, t.token, @@ -115,15 +123,17 @@ INNER JOIN studies s ON (t.study_id = s.study_id) private static final String GET_OBSERVATION_SCHEDULE = "SELECT schedule FROM observations WHERE study_id = ? AND observation_id = ?"; private static final String GET_PARTICIPANT_INFO_AND_START_DURATION_END_FOR_STUDY_AND_PARTICIPANT = - "SELECT start, participant_id, alias, COALESCE(sg.duration, s.duration) AS duration, s.planned_end_date FROM participants p " + - "LEFT OUTER JOIN study_groups sg on p.study_id = sg.study_id and p.study_group_id = sg.study_group_id " + - "JOIN studies s on p.study_id = s.study_id " + - "WHERE p.study_id = ? AND participant_id = ?"; + """ + SELECT start, participant_id, alias, COALESCE(sg.duration, s.duration) AS duration, s.planned_end_date FROM participants p + LEFT OUTER JOIN study_groups sg on p.study_id = sg.study_id and p.study_group_id = sg.study_group_id + JOIN studies s on p.study_id = s.study_id + WHERE p.study_id = ? AND participant_id = ?"""; private static final String GET_DURATION_INFO_FOR_STUDY = - "SELECT sg.study_group_id as groupid, sg.duration AS groupduration, s.duration AS studyduration, s.planned_end_date AS enddate, s.planned_start_date AS startdate FROM studies s " + - "LEFT OUTER JOIN study_groups sg on s.study_id = sg.study_id " + - "WHERE s.study_id = ?"; + """ + SELECT sg.study_group_id as groupid, sg.duration AS groupduration, s.duration AS studyduration, s.planned_end_date AS enddate, s.planned_start_date AS startdate FROM studies s + LEFT OUTER JOIN study_groups sg on s.study_id = sg.study_id + WHERE s.study_id = ?"""; private final JdbcTemplate jdbcTemplate; private final NamedParameterJdbcTemplate namedTemplate; @@ -289,11 +299,8 @@ private void updateParticipantStatus(long studyId, int groupId, int participantI var parameterSource = toParameterSource(studyId, participantId) .addValue("oldStatus", oldStatus) - .addValue("newStatus", newStatus); - - if (start != null) { - parameterSource.addValue("start", start); - } + .addValue("newStatus", newStatus) + .addValue("start", start); namedTemplate.update(SQL_SET_PARTICIPANT_STATUS, parameterSource); }