OrderHistoryPropertiesTraitDataProvider.java 3.27 KB
/*
 * 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;
    }
}