JcrVoucherImpl.java 4.46 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.i18n.I18n
 *  com.day.cq.wcm.api.Page
 *  javax.servlet.http.HttpServletRequest
 *  org.apache.commons.lang.StringUtils
 *  org.apache.sling.adapter.SlingAdaptable
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceUtil
 *  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.promotion.Voucher;
import com.day.cq.i18n.I18n;
import com.day.cq.wcm.api.Page;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.adapter.SlingAdaptable;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;

public class JcrVoucherImpl
extends SlingAdaptable
implements Voucher {
    public static final String VOUCHER_RESOURCE_TYPE = "commerce/components/voucher";
    public static final long VOUCHER_DEFAULT_PRIORITY = 100;
    protected Page page;
    protected Resource resource;

    public JcrVoucherImpl(Resource resource) throws CommerceException {
        this.page = (Page)resource.adaptTo(Page.class);
        if (this.page == null || !ResourceUtil.isA((Resource)resource.getChild("jcr:content"), (String)"commerce/components/voucher")) {
            throw new CommerceException("Resource is not a JcrVoucherImpl.");
        }
        this.resource = this.page.getContentResource();
    }

    @Override
    public String getCode() {
        return (String)this.getConfig().get("code", String.class);
    }

    @Override
    public String getPath() {
        return this.page.getPath();
    }

    @Override
    public String getTitle() {
        String title = this.page.getTitle();
        if (StringUtils.isNotEmpty((String)title)) {
            return title;
        }
        return this.getCode();
    }

    @Override
    public String getDescription() {
        return this.page.getDescription();
    }

    @Override
    public long getPriority() {
        for (Page parent = this.page.getParent(); parent != null; parent = parent.getParent()) {
            if (!ResourceUtil.isA((Resource)parent.getContentResource(), (String)"cq/personalization/components/campaignpage")) continue;
            return ((Integer)ResourceUtil.getValueMap((Resource)parent.getContentResource()).get("priority", (Object)100)).intValue();
        }
        return 100;
    }

    @Override
    public boolean isValid(SlingHttpServletRequest request) {
        for (Page parent = this.page.getParent(); parent != null; parent = parent.getParent()) {
            if (!ResourceUtil.isA((Resource)parent.getContentResource(), (String)"cq/personalization/components/campaignpage")) continue;
            return parent.isValid();
        }
        return false;
    }

    @Deprecated
    @Override
    public String getInvalidMessage(SlingHttpServletRequest request) {
        if (this.isValid(request)) {
            return "";
        }
        return this.getMessage(request);
    }

    @Override
    public String getMessage(SlingHttpServletRequest request) {
        if (this.isValid(request)) {
            return (String)this.getConfig().get("message", (Object)"");
        }
        I18n i18n = new I18n((HttpServletRequest)request);
        for (Page parent = this.page.getParent(); parent != null; parent = parent.getParent()) {
            if (!ResourceUtil.isA((Resource)parent.getContentResource(), (String)"cq/personalization/components/campaignpage")) continue;
            long time = parent.timeUntilValid();
            if (time > 0) {
                return i18n.get("Invalid coupon code.");
            }
            if (time >= 0) continue;
            return i18n.get("Coupon expired.");
        }
        return "";
    }

    @Override
    public ValueMap getConfig() {
        return (ValueMap)this.resource.adaptTo(ValueMap.class);
    }

    public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
        if (type == Resource.class) {
            return (AdapterType)this.resource;
        }
        if (type == Page.class) {
            return (AdapterType)this.page;
        }
        Object ret = super.adaptTo(type);
        if (ret == null) {
            ret = this.resource.adaptTo(type);
        }
        return (AdapterType)ret;
    }
}