List<ChangeElectionConfirmation> confirmations = Collections.synchronizedList(new ArrayList<>());
Map<String, List<String>> accountPositionsMap = electionService.mapAccountPositions(changeElections);
AccountValidationDto accountValidationDto = accountRetrievalService.validateAccountAndHoldings(userProfile, accountPositionsMap);
List<ChangeElectionDto> invalidChangeElections = electionService.buildViolations(accountValidationDto);
List<ChangeElection> validChangeElections = electionService.filterValidChangeElections(changeElections, invalidChangeElections);
// check if positions in validChangeElections have valid electionType
// else throw bad request along with invalidChangeElections
electionService.validateChangeElectionRequest(changeElections.getChangeElections(), validChangeElections, invalidChangeElections, accountValidationDto);
if (CollectionUtils.isNotEmpty(validChangeElections)) {
Map<ElectionLevelType, List<ChangeElection>> electionLevelTypeMap = changeElectionRequestMapper.segregateAccountAndPositionLevelElections(validChangeElections);
if (CollectionUtils.isNotEmpty(electionLevelTypeMap.get(ElectionLevelType.ACCOUNT))) {
confirmations.addAll(accountChangeElectionService.updateAccountElections(userProfile,
electionLevelTypeMap.get(ElectionLevelType.ACCOUNT), accountValidationDto, validate));
}
if (CollectionUtils.isNotEmpty(electionLevelTypeMap.get(ElectionLevelType.POSITION))) {
confirmations.addAll(updatePositionsElections(electionLevelTypeMap.get(ElectionLevelType.POSITION),
accountValidationDto, userProfile.getPoid(), validate));
}
}
confirmations.addAll(changeElectionRequestMapper.mapDtoToConfirmationResponse(invalidChangeElections));
ChangeElectionConfirmations changeElectionConfirmations = ChangeElectionConfirmations.builder()
.confirmations(confirmations)
.build();
return changeElectionConfirmations;
}
/**
* Update Brokerage and TA Elections for positions
*
* @param positionLevelElectionRequest List<ChangeElection>
* @param accountValidationDto AccountValidationDto
* @param poId String
* @return List<ChangeElectionConfirmation>
*/
private List<ChangeElectionConfirmation> updatePositionsElections(List<ChangeElection> positionLevelElectionRequest,
AccountValidationDto accountValidationDto, String poId, boolean validate) {
// make call to OVV
ClientDetails clientDetails = clientRetrievalService.getClientRestrictions();
Map<String, List<ChangeElectionDto>> changeElectionDtoMap = electionService.validateChangeElection(positionLevelElectionRequest, accountValidationDto, clientDetails);
List<ChangeElectionDto> validChangeElections = changeElectionDtoMap.getOrDefault(VALID_CHANGE_ELECTIONS, new ArrayList<>());
List<ChangeElectionDto> brokerageChangeElectionDtoList = AccountUtils.getChangeElectionsByAccountType(validChangeElections, AccountType.BROKERAGE);
List<ChangeElectionDto> taChangeElectionDtoList = AccountUtils.getChangeElectionsByAccountType(validChangeElections, AccountType.MUTUAL_FUND);
List<ChangeElectionConfirmation> confirmations = Collections.synchronizedList(new ArrayList<>());
if (CollectionUtils.isNotEmpty(brokerageChangeElectionDtoList) || CollectionUtils.isNotEmpty(taChangeElectionDtoList)) {
PositionLevelElectionMapDto.PositionLevelElectionMapDtoBuilder positionLevelElectionMapDtoBuilder = electionService.
buildAccountDetails(accountValidationDto, poId, validChangeElections, clientDetails);
if (CollectionUtils.isNotEmpty(brokerageChangeElectionDtoList)) {
confirmations.addAll(brokeragePositionsElectionService.updateBrokeragePositions(accountValidationDto, poId,
brokerageChangeElectionDtoList, positionLevelElectionMapDtoBuilder, validate));
}
if (CollectionUtils.isNotEmpty(taChangeElectionDtoList)) {
confirmations.addAll(mutualFundPositionsElectionService.updateMutualFundPositions(poId, taChangeElectionDtoList,
positionLevelElectionMapDtoBuilder, accountValidationDto, validate));
}
}
confirmations.addAll(changeElectionRequestMapper.mapDtoToConfirmationResponse(changeElectionDtoMap
.get(INVALID_CHANGE_ELECTIONS)));
return confirmations;
}