Last active
May 7, 2017 17:52
-
-
Save garystafford/992f881a6af7deefa55a86a6edf22a02 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RabbitListener(queues = "voter.rpc.requests") | |
private String getCandidatesMessageRpc(String requestMessage) { | |
logger.debug("Request message: {}", requestMessage); | |
logger.debug("Sending RPC response message with list of candidates..."); | |
List<CandidateVoterView> candidates = getByElection(requestMessage); | |
return serializeToJson(candidates); | |
} | |
private List<CandidateVoterView> getByElection(String election) { | |
Aggregation aggregation = Aggregation.newAggregation( | |
Aggregation.match(Criteria.where("election").is(election)), | |
project("firstName", "lastName", "politicalParty", "election") | |
.andExpression("concat(firstName,' ', lastName)") | |
.as("fullName"), | |
sort(Sort.Direction.ASC, "lastName") | |
); | |
AggregationResults<CandidateVoterView> groupResults | |
= mongoTemplate.aggregate(aggregation, Candidate.class, CandidateVoterView.class); | |
return groupResults.getMappedResults(); | |
} | |
private String serializeToJson(List<CandidateVoterView> candidates) { | |
ObjectMapper mapper = new ObjectMapper(); | |
String jsonInString = ""; | |
final Map<String, List<CandidateVoterView>> dataMap = new HashMap<>(); | |
dataMap.put("candidates", candidates); | |
try { | |
jsonInString = mapper.writeValueAsString(dataMap); | |
} catch (JsonProcessingException e) { | |
logger.info(String.valueOf(e)); | |
} | |
logger.debug(jsonInString); | |
return jsonInString; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment