Source:
<code>private void updateAccountAndRoles(Study study, Account account, StudyParticipant participant, boolean isNew) { account.setFirstName(participant.getFirstName()); account.setLastName(participant.getLastName()); account.setClientData(participant.getClientData()); account.setSharingScope(participant.getSharingScope()); account.setNotifyByEmail(participant.isNotifyByEmail()); account.setDataGroups(participant.getDataGroups()); account.setLanguages(participant.getLanguages()); account.setMigrationVersion(AccountDao.MIGRATION_VERSION); Set<Roles> callerRoles = BridgeUtils.getRequestContext().getCallerRoles(); if (isNew || callerIsAdmin(callerRoles)) { boolean clearCache = false; Set<AccountSubstudy> accountSubstudies = ImmutableSet.copyOf(account.getAccountSubstudies()); for (AccountSubstudy acctSubstudy : accountSubstudies) { if (!participant.getSubstudyIds().contains(acctSubstudy.getSubstudyId())) { externalIdService.unassignExternalId(account, acctSubstudy.getExternalId()); account.getAccountSubstudies().remove(acctSubstudy); clearCache = true; } } Set<String> existingSubstudyIds = account.getAccountSubstudies().stream() .map(AccountSubstudy::getSubstudyId).collect(Collectors.toSet()); for (String substudyId : participant.getSubstudyIds()) { if (!existingSubstudyIds.contains(substudyId)) { AccountSubstudy newSubstudy = AccountSubstudy.create( account.getStudyId(), substudyId, account.getId()); account.getAccountSubstudies().add(newSubstudy); clearCache = true; } } if (!isNew && clearCache) { cacheProvider.removeSessionByUserId(account.getId()); <START> } <END> } for (String attribute : study.getUserProfileAttributes()) { String value = participant.getAttributes().get(attribute); account.getAttributes().put(attribute, value); } if (callerIsAdmin(callerRoles)) { updateRoles(callerRoles, participant, account); } }</code><technical_language>Do unit tests for this logic</technical_language>

Target:
private void updateAccountAndRoles(Study study, Account account, StudyParticipant participant, boolean isNew) { account.setFirstName(participant.getFirstName()); account.setLastName(participant.getLastName()); account.setClientData(participant.getClientData()); account.setSharingScope(participant.getSharingScope()); account.setNotifyByEmail(participant.isNotifyByEmail()); account.setDataGroups(participant.getDataGroups()); account.setLanguages(participant.getLanguages()); account.setMigrationVersion(AccountDao.MIGRATION_VERSION); Set<Roles> callerRoles = BridgeUtils.getRequestContext().getCallerRoles(); if (isNew || callerRoles.contains(Roles.ADMIN)) { boolean clearCache = false; Set<AccountSubstudy> accountSubstudies = ImmutableSet.copyOf(account.getAccountSubstudies()); for (AccountSubstudy acctSubstudy : accountSubstudies) { if (!participant.getSubstudyIds().contains(acctSubstudy.getSubstudyId())) { externalIdService.unassignExternalId(account, acctSubstudy.getExternalId()); account.getAccountSubstudies().remove(acctSubstudy); clearCache = true; } } Set<String> existingSubstudyIds = account.getAccountSubstudies().stream() .map(AccountSubstudy::getSubstudyId).collect(Collectors.toSet()); for (String substudyId : participant.getSubstudyIds()) { if (!existingSubstudyIds.contains(substudyId)) { AccountSubstudy newSubstudy = AccountSubstudy.create( account.getStudyId(), substudyId, account.getId()); account.getAccountSubstudies().add(newSubstudy); clearCache = true; } } if (!isNew && clearCache) { cacheProvider.removeSessionByUserId(account.getId()); } } for (String attribute : study.getUserProfileAttributes()) { String value = participant.getAttributes().get(attribute); account.getAttributes().put(attribute, value); } if (callerIsAdmin(callerRoles)) { updateRoles(callerRoles, participant, account); } }