JcrPaymentMethodImpl.java 3.39 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.commons.ImageResource
 *  com.day.cq.wcm.api.Page
 *  org.apache.commons.lang.StringUtils
 *  org.apache.sling.adapter.SlingAdaptable
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceUtil
 */
package com.adobe.cq.commerce.impl;

import com.adobe.cq.commerce.api.CommerceException;
import com.adobe.cq.commerce.api.PaymentMethod;
import com.day.cq.commons.ImageResource;
import com.day.cq.wcm.api.Page;
import java.util.Iterator;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.adapter.SlingAdaptable;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceUtil;

public class JcrPaymentMethodImpl
extends SlingAdaptable
implements PaymentMethod {
    public static final String RESOURCE_TYPE = "commerce/components/paymentmethod";
    protected Page page;
    protected Resource resource;

    public JcrPaymentMethodImpl(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/paymentmethod")) {
            throw new CommerceException("Resource is not a commerce/components/paymentmethod");
        }
        this.resource = this.page.getContentResource();
    }

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

    @Override
    public String getPredicate() {
        return (String)ResourceUtil.getValueMap((Resource)this.resource).get("predicate", (Object)"payment");
    }

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

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

    @Override
    public String getImageUrl(String selector) {
        Resource image = this.resource.getChild("image");
        if (image != null) {
            if (image.getChild("file") != null) {
                selector = StringUtils.isEmpty((String)selector) ? ".thumb" : (selector.startsWith(".") ? ".thumb" + selector : ".thumb." + selector);
                return this.page.getPath() + selector + ".png";
            }
            ImageResource imageResource = new ImageResource(image);
            if (StringUtils.isNotEmpty((String)selector)) {
                imageResource.setSelector(selector);
            }
            return imageResource.getHref();
        }
        return null;
    }

    @Override
    public String getUIPath() {
        Resource parsys = this.resource.getChild("par");
        if (parsys != null && parsys.listChildren().hasNext()) {
            return parsys.getPath();
        }
        return null;
    }

    @Override
    public String getRedirectUrl() {
        return (String)ResourceUtil.getValueMap((Resource)this.resource).get("redirect", String.class);
    }

    @Override
    public <T> T getProperty(String name, Class<T> type) {
        return (T)ResourceUtil.getValueMap((Resource)this.resource).get(name, type);
    }

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