Skip to content

Instantly share code, notes, and snippets.

@abhishek-sisodiya
Last active August 12, 2019 13:20
Show Gist options
  • Save abhishek-sisodiya/70fc9aa4ef215b09d79d53ba09a784de to your computer and use it in GitHub Desktop.
Save abhishek-sisodiya/70fc9aa4ef215b09d79d53ba09a784de to your computer and use it in GitHub Desktop.
private int MAX_PROVIDER_ID = 0;
public Map<String, Object> summaryProvider() throws Exception {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String previousYear = dateFormat.format(getPreviousYear());
String yesterday = dateFormat.format(yesterday());
Map<String, Object> json = new HashMap<String, Object>();
Map<String, Object> fromCache = cacheManager.get("summaryProvider", "FAV_PROVIDER_ID", Map.class);
if (fromCache != null) {
return fromCache;
} else {
try {
List<VotingDTO> votes = votingRepository.findByDate(previousYear);
if (votes.size() > 0) {
favProvider(votes);
} else {
List<VotingDTO> yesterdayVotes = votingRepository.findByDate(yesterday);
if (yesterdayVotes.size() > 0) {
favProvider(yesterdayVotes);
}
}
} catch (Exception e) {
System.out.println(e);
}
json.put("FAV_PROVIDER_ID", MAX_PROVIDER_ID);
cacheManager.put("summaryProvider", "FAV_PROVIDER_ID", json);
cacheManager.expire("summaryProvider");
}
Map<String, Object> fromCacheAfterAdd = cacheManager.get("summaryProvider", "FAV_PROVIDER_ID", Map.class);
return fromCacheAfterAdd;
}
private int favProvider(List<VotingDTO> votes) {
List<Integer> providerIDS = new ArrayList<Integer>();
for (VotingDTO vote : votes) {
providerIDS.add(vote.getProvider().getId());
}
int maxCounter = 0;
for (int index = 0; index < providerIDS.size(); index++) {
int counter = 1;
for (int innerIndex = index + 1; innerIndex < providerIDS.size(); innerIndex++) {
if (providerIDS.get(index) == providerIDS.get(innerIndex)) {
counter++;
}
}
if (maxCounter < counter) {
maxCounter = counter;
MAX_PROVIDER_ID = providerIDS.get(index);
}
}
return MAX_PROVIDER_ID;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment