MCMAdapterFactory.java 6.59 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.wcm.api.Page
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Properties
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.adapter.AdapterFactory
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ValueMap
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.mcm.impl;

import com.day.cq.mcm.api.Brand;
import com.day.cq.mcm.api.Campaign;
import com.day.cq.mcm.api.Experience;
import com.day.cq.mcm.api.MCMFacade;
import com.day.cq.mcm.api.MCMPlugin;
import com.day.cq.mcm.api.MCMResourceType;
import com.day.cq.mcm.api.Touchpoint;
import com.day.cq.mcm.impl.BrandImpl;
import com.day.cq.mcm.impl.CampaignImpl;
import com.day.cq.mcm.util.NormalizedResource;
import com.day.cq.wcm.api.Page;
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.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.adapter.AdapterFactory;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=0)
@Service(value={AdapterFactory.class})
@Properties(value={@Property(name="service.description", value={"MCM Adapter Factory, adapts to MCM classes."}), @Property(name="adaptables", value={"org.apache.sling.api.resource.Resource", "com.day.cq.wcm.api.Page"}), @Property(name="adapters", value={"com.day.cq.mcm.api.Campaign", "com.day.cq.mcm.api.Brand", "com.day.cq.mcm.api.Experience", "com.day.cq.mcm.api.Touchpoint"})})
public class MCMAdapterFactory
implements AdapterFactory {
    private static final Logger log = LoggerFactory.getLogger(MCMAdapterFactory.class);
    @Reference
    private MCMFacade mcmFacade = null;

    /*
     * Enabled aggressive block sorting
     */
    public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType> type) {
        log.debug("Adapting {} to {}", adaptable, type);
        Resource resource = null;
        Page page = null;
        if (adaptable instanceof Page) {
            page = (Page)adaptable;
            resource = (Resource)page.adaptTo(Resource.class);
        } else {
            if (!(adaptable instanceof Resource)) {
                log.warn("Could not adapt '" + adaptable + "' to " + type + ". ");
                return null;
            }
            resource = (Resource)adaptable;
        }
        NormalizedResource normalizedResource = new NormalizedResource();
        if (resource != null) {
            normalizedResource.setResource(resource);
        }
        if (type.isAssignableFrom(Experience.class)) {
            if (resource == null) {
                log.warn("Could not adapt '" + adaptable + "' to Experience. " + "Only Resource and Page can be adapted.");
                return null;
            }
            if (!this.verifyMCMType(normalizedResource, MCMResourceType.Experience)) {
                log.warn("Cannot adapt {} to Experience, not a known experience resource type.", adaptable);
                return null;
            }
            MCMPlugin p = this.mcmFacade.getPlugin(resource);
            if (p != null) return (AdapterType)p.makeExperience(resource);
            log.warn("Cannot adapt {} to {}, no plugin for it registered.", adaptable, type);
            return null;
        }
        if (type.isAssignableFrom(Touchpoint.class)) {
            if (resource == null) {
                log.warn("Could not adapt '" + adaptable + "' to Touchpoint. " + "Only Resource and Page can be adapted.");
                return null;
            }
            MCMPlugin p = this.mcmFacade.getPlugin(resource);
            if (!this.verifyMCMType(normalizedResource, MCMResourceType.Touchpoint)) {
                log.warn("Cannot adapt {} to Touchpoint, not a known touchpoint resource type.", adaptable);
                return null;
            }
            if (p != null) return (AdapterType)p.makeTouchpoint(resource);
            log.warn("Cannot adapt {} to {}, no plugin for it registered.", adaptable, type);
            return null;
        }
        if (type == Campaign.class) {
            if (resource == null) {
                log.warn("Could not adapt '" + adaptable + "' to Campaign. " + "Only Resource and Page can be adapted.");
                return null;
            }
            if (!this.verifyType(normalizedResource, "cq/personalization/components/campaignpage")) {
                log.warn("Could not adapt '" + adaptable + "' to Campaign. " + "ComponentType not matching.");
                return null;
            }
            CampaignImpl ci = new CampaignImpl();
            ci.setNormalizedResource(normalizedResource);
            return (AdapterType)ci;
        }
        if (type != Brand.class) {
            log.error("Wrong component config or framework problem: Cannot adapt to '" + type + "'.");
            return null;
        }
        if (resource == null) {
            log.warn("Could not adapt '" + adaptable + "' to Brand. " + "Only Resource and Page can be adapted.");
            return null;
        }
        if (!this.verifyType(normalizedResource, "mcm/components/brandpage")) {
            log.warn("Could not adapt '" + adaptable + "' to Brand. " + "ComponentType not matching.");
            return null;
        }
        BrandImpl bi = new BrandImpl();
        bi.setNormalizedResource(normalizedResource);
        return (AdapterType)bi;
    }

    private boolean verifyMCMType(NormalizedResource normalizedResource, MCMResourceType mcmType) {
        ValueMap contentVals = normalizedResource.getContentVals();
        String resType = contentVals != null ? (String)contentVals.get("sling:resourceType", String.class) : null;
        return mcmType.equals((Object)this.mcmFacade.getMCMType(resType));
    }

    private boolean verifyType(NormalizedResource nr, String compType) {
        ValueMap contentVals = nr.getContentVals();
        if (contentVals != null) {
            String checkedType = (String)contentVals.get("sling:resourceType", String.class);
            return compType.equals(checkedType);
        }
        return false;
    }

    protected void bindMcmFacade(MCMFacade mCMFacade) {
        this.mcmFacade = mCMFacade;
    }

    protected void unbindMcmFacade(MCMFacade mCMFacade) {
        if (this.mcmFacade == mCMFacade) {
            this.mcmFacade = null;
        }
    }
}