FreeShippingPromotionHandler.java 3.66 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.Properties
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.resource.ValueMap
 */
package com.adobe.cq.commerce.impl.promotion;

import com.adobe.cq.commerce.api.CommerceException;
import com.adobe.cq.commerce.api.CommerceSession;
import com.adobe.cq.commerce.api.PriceInfo;
import com.adobe.cq.commerce.api.promotion.Promotion;
import com.adobe.cq.commerce.api.promotion.PromotionHandler;
import com.adobe.cq.commerce.common.PriceFilter;
import java.math.BigDecimal;
import java.text.MessageFormat;
import java.util.Currency;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections.Predicate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.ValueMap;

@Component(metatype=0)
@Service
@Properties(value={@Property(name="service.description", value={"Promotion handler which removes shipping costs from the cart"}), @Property(name="commerce.promotion.type", value={"/libs/commerce/components/promotion/freeshipping"})})
public class FreeShippingPromotionHandler
implements PromotionHandler {
    @Override
    public PriceInfo applyCartEntryPromotion(CommerceSession commerceSession, Promotion promotion, CommerceSession.CartEntry cartEntry) throws CommerceException {
        return null;
    }

    @Override
    public PriceInfo applyOrderPromotion(CommerceSession commerceSession, Promotion promotion) throws CommerceException {
        return null;
    }

    @Override
    public PriceInfo applyShippingPromotion(CommerceSession commerceSession, Promotion promotion) throws CommerceException {
        List<PriceInfo> shipping = commerceSession.getCartPriceInfo(new PriceFilter("SHIPPING"));
        if (!shipping.isEmpty()) {
            return new PriceInfo(shipping.get(0).getAmount(), shipping.get(0).getCurrency());
        }
        return null;
    }

    @Deprecated
    @Override
    public String getMessage(SlingHttpServletRequest request, CommerceSession commerceSession, Promotion promotion) throws CommerceException {
        return null;
    }

    @Override
    public String getDescription(SlingHttpServletRequest request, CommerceSession commerceSession, Promotion promotion) throws CommerceException {
        return promotion.getDescription();
    }

    @Override
    public Map<Integer, String> getMessages(SlingHttpServletRequest request, CommerceSession commerceSession, Promotion promotion) throws CommerceException {
        String message;
        HashMap<Integer, String> messages = new HashMap<Integer, String>();
        ValueMap config = promotion.getConfig();
        List<PriceInfo> shipping = commerceSession.getCartPriceInfo(new PriceFilter("SHIPPING", "PRE_PROMO"));
        message = shipping.isEmpty() || shipping.get(0).getAmount().equals(BigDecimal.ZERO) ? (String)config.get("defaultMessage", String.class) : ((message = (String)config.get("amountMessage", String.class)) != null ? MessageFormat.format(message, "", shipping.get(0).getFormattedString()) : (String)config.get("defaultMessage", String.class));
        if (message != null) {
            messages.put(-1, message);
        }
        return messages;
    }

    @Override
    public void invalidateCaches() {
    }
}