OrderHistoryPropertiesTraitDataProvider.java
3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.commons.collections.Predicate
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Service
*/
package com.adobe.cq.commerce.impl;
import com.adobe.cq.commerce.api.CommerceException;
import com.adobe.cq.commerce.api.CommerceSession;
import com.adobe.cq.commerce.api.OrderHistoryTraitDataProvider;
import com.adobe.cq.commerce.api.PlacedOrder;
import com.adobe.cq.commerce.api.PlacedOrderResult;
import com.adobe.cq.commerce.api.PriceInfo;
import com.adobe.cq.commerce.api.Product;
import com.adobe.cq.commerce.common.PriceFilter;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.Currency;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.apache.commons.collections.Predicate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
@Service(value={OrderHistoryTraitDataProvider.class})
@Component(immediate=1, metatype=0)
public class OrderHistoryPropertiesTraitDataProvider
implements OrderHistoryTraitDataProvider {
@Override
public String getIdentifier() {
return "generic";
}
@Override
public String getName() {
return "Order History Property";
}
@Override
public Map<String, Object> getTraitData(PlacedOrderResult result) throws CommerceException {
NumberFormat nf;
int orderCount = 0;
int itemCount = 0;
BigDecimal totalValue = BigDecimal.ZERO;
Locale locale = null;
Currency currency = null;
HashSet<String> skuSet = new HashSet<String>();
for (PlacedOrder order : result.getOrders()) {
++orderCount;
for (CommerceSession.CartEntry entry : order.getCartEntries()) {
skuSet.add(entry.getProduct().getSKU());
itemCount += entry.getQuantity();
}
List<PriceInfo> orderTotal = order.getCartPriceInfo(new PriceFilter("ORDER", "TOTAL"));
if (orderTotal.size() <= 0) continue;
PriceInfo priceInfo = orderTotal.get(0);
totalValue = totalValue.add(priceInfo.getAmount());
Locale locale1 = priceInfo.getLocale();
if (locale == null && locale1 != null) {
locale = locale1;
}
Currency currency1 = priceInfo.getCurrency();
if (currency != null || currency1 == null) continue;
currency = currency1;
}
if (locale != null) {
nf = NumberFormat.getCurrencyInstance(locale);
} else if (currency != null) {
nf = NumberFormat.getCurrencyInstance();
nf.setCurrency(currency);
} else {
nf = NumberFormat.getCurrencyInstance(Locale.US);
}
if (totalValue.doubleValue() > 100.0) {
nf.setMaximumFractionDigits(0);
}
HashMap<String, Object> data = new HashMap<String, Object>();
data.put("ordercount", orderCount);
data.put("ordervalue", totalValue);
data.put("valueformatted", nf.format(totalValue));
data.put("itemcount", itemCount);
data.put("productcount", skuSet.size());
return data;
}
}