• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

DataBiosphere / consent / #5971

28 May 2025 01:45PM UTC coverage: 78.503% (-0.06%) from 78.558%
#5971

push

web-flow
DT-1662: Ensure single Library Card per User (#2536)

83 of 86 new or added lines in 9 files covered. (96.51%)

4 existing lines in 3 files now uncovered.

10028 of 12774 relevant lines covered (78.5%)

0.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.68
/src/main/java/org/broadinstitute/consent/http/db/mapper/UserWithRolesReducer.java
1
package org.broadinstitute.consent.http.db.mapper;
2

3
import java.util.Map;
4
import java.util.Objects;
5
import org.broadinstitute.consent.http.models.Institution;
6
import org.broadinstitute.consent.http.models.LibraryCard;
7
import org.broadinstitute.consent.http.models.User;
8
import org.broadinstitute.consent.http.models.UserProperty;
9
import org.broadinstitute.consent.http.models.UserRole;
10
import org.jdbi.v3.core.mapper.MappingException;
11
import org.jdbi.v3.core.result.LinkedHashMapRowReducer;
12
import org.jdbi.v3.core.result.RowView;
13

14
/**
15
 * This class works well for individual Users as well as collections.
16
 */
17
public class UserWithRolesReducer implements LinkedHashMapRowReducer<Integer, User>,
1✔
18
    RowMapperHelper {
19

20
  @Override
21
  public void accumulate(Map<Integer, User> map, RowView rowView) {
22
    // Some queries look for `user_id` while those that use a prefix look for `u_user_id`
23
    Integer userId = 0;
1✔
24
    if (hasNonZeroColumn(rowView, "user_id")) {
1✔
25
      userId = rowView.getColumn("user_id", Integer.class);
1✔
26
    } else if (hasNonZeroColumn(rowView, "u_user_id")) {
1✔
27
      userId = rowView.getColumn("u_user_id", Integer.class);
1✔
28
    }
29
    User user =
1✔
30
        map.computeIfAbsent(
1✔
31
            userId,
32
            id -> rowView.getRow(User.class));
1✔
33

34
    try {
35
      UserRole userRole = mapUserRoleFromRowView(rowView, userId);
1✔
36
      if (userRole != null) {
1✔
37
        user.addRole(userRole);
1✔
38
      }
39
    } catch (MappingException e) {
×
NEW
40
      logWarn("Error adding User Role to User", e);
×
41
    }
1✔
42
    try {
43
      if (Objects.nonNull(rowView.getColumn("i_id", Integer.class))) {
1✔
44
        Institution institution = rowView.getRow(Institution.class);
1✔
45
        // There are unusual cases where we somehow create an institution with null values
46
        if (Objects.nonNull(institution.getId())) {
1✔
47
          user.setInstitution(institution);
1✔
48
        }
49
      }
50
    } catch (MappingException e) {
1✔
51
      logDebug("Error adding Institution to User: %s".formatted(e.getMessage()));
1✔
52
    }
1✔
53
    //user role join can cause duplication of data if done in tandem with joins on other tables
54
    //ex) The same LC can end up being repeated multiple times
55
    //Below only adds LC if not currently saved on the array
56
    try {
57
      if (rowView.getColumn("lc_id", Integer.class) != null) {
1✔
58
        LibraryCard lc = rowView.getRow(LibraryCard.class);
1✔
59
        if (rowView.getColumn("lc_daa_id", Integer.class) != null) {
1✔
60
          lc.addDaa(rowView.getColumn("lc_daa_id", Integer.class));
1✔
61
        }
62
        user.setLibraryCard(lc);
1✔
63
      }
64
    } catch (MappingException e) {
1✔
65
      logDebug("Error adding Library Card to User: %s".formatted(e.getMessage()));
1✔
66
    }
1✔
67
    try {
68
      if (Objects.nonNull(rowView.getColumn("up_property_id", Integer.class))) {
×
69
        UserProperty p = rowView.getRow(UserProperty.class);
×
70
        user.addProperty(p);
×
71
      }
72
    } catch (MappingException e) {
1✔
73
      logDebug("Error adding User Property to User: %s".formatted(e.getMessage()));
1✔
NEW
74
    }
×
75
  }
1✔
76

77
  // Some queries look for `user_role_id` while those that use a prefix look for `u_user_role_id`
78
  private UserRole mapUserRoleFromRowView(RowView rowView, Integer userId) {
79
    Integer userRoleId;
80
    Integer roleId;
81
    String name;
82
    Integer dacId = null;
1✔
83
    // Some queries look for `user_role_id` while those that use a prefix look for `ur_user_role_id`
84
    if (hasNonZeroColumn(rowView, "user_role_id")) {
1✔
85
      userRoleId = rowView.getColumn("user_role_id", Integer.class);
1✔
86
      roleId = rowView.getColumn("role_id", Integer.class);
1✔
87
      name = rowView.getColumn("name", String.class);
1✔
88
      if (hasNonZeroColumn(rowView, "dac_id")) {
1✔
89
        dacId = rowView.getColumn("dac_id", Integer.class);
1✔
90
      }
91
      return new UserRole(userRoleId, userId, roleId, name, dacId);
1✔
92
    } else if (hasNonZeroColumn(rowView, "ur_user_role_id")) {
1✔
93
      userRoleId = rowView.getColumn("ur_user_role_id", Integer.class);
1✔
94
      roleId = rowView.getColumn("ur_role_id", Integer.class);
1✔
95
      name = rowView.getColumn("ur_name", String.class);
1✔
96
      if (hasNonZeroColumn(rowView, "ur_dac_id")) {
1✔
97
        dacId = rowView.getColumn("ur_dac_id", Integer.class);
1✔
98
      }
99
      return new UserRole(userRoleId, userId, roleId, name, dacId);
1✔
100
    }
101
    return null;
1✔
102
  }
103
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc