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

DataBiosphere / consent / #5166

24 Jun 2024 06:02PM UTC coverage: 76.663% (-0.02%) from 76.68%
#5166

push

web-flow
DCJ-445: Update Row Helper Conditional (#2347)

60 of 69 new or added lines in 14 files covered. (86.96%)

2 existing lines in 2 files now uncovered.

9901 of 12915 relevant lines covered (76.66%)

0.77 hits per line

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

84.0
/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 java.util.Optional;
6
import org.broadinstitute.consent.http.models.Institution;
7
import org.broadinstitute.consent.http.models.LibraryCard;
8
import org.broadinstitute.consent.http.models.User;
9
import org.broadinstitute.consent.http.models.UserProperty;
10
import org.broadinstitute.consent.http.models.UserRole;
11
import org.jdbi.v3.core.mapper.MappingException;
12
import org.jdbi.v3.core.result.LinkedHashMapRowReducer;
13
import org.jdbi.v3.core.result.RowView;
14

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

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

35
    try {
36
      // Some queries look for `user_role_id` while those that use a prefix look for `u_user_role_id`
37
      Integer userRoleId = null;
1✔
38
      if (hasNonZeroColumn(rowView, "user_role_id")) {
1✔
39
        userRoleId = rowView.getColumn("user_role_id", Integer.class);
1✔
40
      } else if (hasNonZeroColumn(rowView, "ur_user_role_id")) {
1✔
41
        userRoleId = rowView.getColumn("ur_user_role_id", Integer.class);
1✔
42
      }
43
      if (Objects.nonNull(userRoleId)) {
1✔
44
        UserRole ur = rowView.getRow(UserRole.class);
1✔
45
        user.addRole(ur);
1✔
46
      }
47
    } catch (MappingException e) {
×
48
      // Ignore any attempt to map a column that doesn't exist
49
    }
1✔
50
    try {
51
      if (Objects.nonNull(rowView.getColumn("i_id", Integer.class))) {
1✔
52
        Institution institution = rowView.getRow(Institution.class);
1✔
53
        // There are unusual cases where we somehow create an institution with null values
54
        if (Objects.nonNull(institution.getId())) {
1✔
55
          user.setInstitution(institution);
1✔
56
        }
57
      }
58
    } catch (MappingException e) {
1✔
59
      //Ignore institution mapping errors, possible for new users to not have an institution
60
    }
1✔
61
    //user role join can cause duplication of data if done in tandem with joins on other tables
62
    //ex) The same LC can end up being repeated multiple times
63
    //Below only adds LC if not currently saved on the array
64
    try {
65
      if (rowView.getColumn("lc_id", Integer.class) != null) {
1✔
66
        int lcId = rowView.getColumn("lc_id", Integer.class);
1✔
67
        LibraryCard lc;
68
        Optional<LibraryCard> existingLibraryCard = user.getLibraryCards() == null ?
1✔
69
            Optional.empty() :
1✔
70
            user.getLibraryCards().stream()
×
NEW
71
                .filter(card -> card.getId().equals(lcId))
×
72
                .findFirst();
1✔
73
        lc = existingLibraryCard.orElseGet(() -> rowView.getRow(LibraryCard.class));
1✔
74
        try {
75
          if (Objects.nonNull(rowView.getColumn("lci_id", Integer.class))) {
1✔
76
            Institution institution = rowView.getRow(Institution.class);
1✔
77
            // There are unusual cases where we somehow create an institution with null values
78
            if (Objects.nonNull(institution.getId())) {
1✔
79
              lc.setInstitution(institution);
1✔
80
            }
81
          }
82
        } catch (MappingException e) {
×
83
          // Ignore institution mapping errors
84
        }
1✔
85
        if (rowView.getColumn("lc_daa_id", Integer.class) != null) {
1✔
86
          lc.addDaa(rowView.getColumn("lc_daa_id", Integer.class));
1✔
87
        }
88
        user.addLibraryCard(lc);
1✔
89
      }
90
    } catch (MappingException e) {
1✔
91
      //Ignore exceptions here, user may not have a library card issued under this instiution
92
    }
1✔
93
    try {
94
      if (Objects.nonNull(rowView.getColumn("up_property_id", Integer.class))) {
×
95
        UserProperty p = rowView.getRow(UserProperty.class);
×
96
        user.addProperty(p);
×
97
      }
98
    } catch (MappingException e) {
1✔
99
      // Ignore any attempt to map a column that doesn't exist
100
    }
×
101
  }
1✔
102
}
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