long totalImpressions = 0;
// Aggregate totals by clickType starting with '0' values for each type to avoid NPEs
Map<String, NameImprClicks> totalsByClickType = new HashMap<String, NameImprClicks>();
totalsByClickType.put("Sitelink", new NameImprClicks("Sitelink"));
totalsByClickType.put("Call", new NameImprClicks("Call"));
totalsByClickType.put("App", new NameImprClicks("App"));
totalsByClickType.put("Location", new NameImprClicks("Location"));
totalsByClickType.put("Review", new NameImprClicks("Review"));
for (Report report : monthlyReports) {
int placeholderType = ((ReportPlaceholderFeedItem) report).getFeedPlaceholderType();
String clickType = "Headline";
switch (placeholderType) {
case 1:
clickType = "Sitelink";
break;
case 2:
clickType = "Call";
break;
case 3:
clickType = "App";
break;
case 7:
clickType = "Location";
break;
case 8:
clickType = "Review";
break;
}
Long impressions = ((ReportPlaceholderFeedItem) report).getImpressions();
Long clicks = ((ReportPlaceholderFeedItem) report).getClicks();
boolean isSelfAction = ((ReportPlaceholderFeedItem) report).isSelfAction();
if (!clickType.equals("Headline")) {
if (clickType.equals("Sitelink")) {
sitelinks++;
}
if(isSelfAction) {
if (adExtensionsMap.containsKey(clickType)) {
NameImprClicks oldValues = adExtensionsMap.get(clickType);
oldValues.impressions += impressions;
oldValues.clicks += clicks;
totalClicks += clicks;
totalImpressions += impressions;
adExtensionsMap.put(clickType, oldValues);
} else {
NameImprClicks values = new NameImprClicks();
values.impressions = impressions;
values.clicks = clicks;
totalClicks += clicks;
totalImpressions += impressions;
adExtensionsMap.put(clickType, values);
}
// Add total values for each clickType
NameImprClicks clickTypeNic;
if(totalsByClickType.containsKey(clickType)) {
clickTypeNic = totalsByClickType.get(clickType);
clickTypeNic.impressions += impressions;
clickTypeNic.clicks += clicks;
}
}
}
}
List<NameImprClicks> adExtensions = new ArrayList<NameImprClicks>();
for (Map.Entry<String, NameImprClicks> entry : adExtensionsMap.entrySet()) {
NameImprClicks nic = new NameImprClicks();
nic.clickType = entry.getKey();
if (nic.clickType.equals("Sitelink")) {
nic.clickType = "Sitelinks (x" + sitelinks + ")";
}
nic.clicks = entry.getValue().clicks;
nic.impressions = entry.getValue().impressions;
adExtensions.add(nic);
}
NameImprClicks nic = new NameImprClicks();
nic.clickType = "Total";
nic.clicks = totalClicks;
nic.impressions = totalImpressions;
adExtensions.add(nic);