CFMAdapterFactory.java 4.89 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.cq.dam.cfm.ContentFragment
 *  com.adobe.cq.dam.cfm.ContentFragmentManager
 *  com.adobe.cq.dam.cfm.FragmentTemplate
 *  com.day.cq.dam.api.Asset
 *  org.apache.felix.scr.annotations.Component
 *  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.PersistenceException
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.resource.collection.ResourceCollection
 *  org.apache.sling.resource.collection.ResourceCollectionManager
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.dam.cfm.impl;

import com.adobe.cq.dam.cfm.ContentFragment;
import com.adobe.cq.dam.cfm.ContentFragmentManager;
import com.adobe.cq.dam.cfm.FragmentTemplate;
import com.adobe.cq.dam.cfm.impl.CFMUtils;
import com.adobe.cq.dam.cfm.impl.FragmentImpl;
import com.adobe.cq.dam.cfm.impl.FragmentTemplateImpl;
import com.adobe.cq.dam.cfm.impl.InvalidFragmentException;
import com.adobe.cq.dam.cfm.impl.InvalidTemplateException;
import com.day.cq.dam.api.Asset;
import org.apache.felix.scr.annotations.Component;
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.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.resource.collection.ResourceCollection;
import org.apache.sling.resource.collection.ResourceCollectionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Service
@Component(metatype=0)
public class CFMAdapterFactory
implements AdapterFactory {
    @Reference
    private ContentFragmentManager manager;
    @Property(name="adapters")
    public static final String[] ADAPTER_CLASSES = new String[]{ContentFragment.class.getName(), FragmentTemplate.class.getName()};
    @Property(name="adaptables")
    public static final String[] ADAPTABLE_CLASSES = new String[]{Resource.class.getName()};
    private final Logger log;
    @Reference
    private ResourceCollectionManager collectionManager;

    public CFMAdapterFactory() {
        this.log = LoggerFactory.getLogger(this.getClass());
    }

    private ContentFragment fromResource(Resource resource) {
        ResourceCollection associatedContent;
        Asset asset = (Asset)resource.adaptTo(Asset.class);
        if (asset == null) {
            return null;
        }
        Resource assocContentResource = resource.getChild("associated");
        ResourceCollection resourceCollection = associatedContent = assocContentResource != null ? this.collectionManager.getCollection(assocContentResource) : null;
        if (associatedContent == null) {
            try {
                associatedContent = CFMUtils.createEmptyAssocContent(resource, this.collectionManager);
            }
            catch (PersistenceException pe) {
                this.log.debug("Getting a content fragment failed due to missing associated content definition.");
                return null;
            }
        }
        try {
            return new FragmentImpl(asset, associatedContent, false);
        }
        catch (InvalidFragmentException ife) {
            this.log.debug("Getting a content fragment failed due to an invalid content structure.");
            return null;
        }
    }

    public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType> type) {
        if (adaptable instanceof Resource) {
            if (type == ContentFragment.class) {
                return (AdapterType)this.fromResource((Resource)adaptable);
            }
            if (type == FragmentTemplate.class) {
                try {
                    return (AdapterType)new FragmentTemplateImpl((Resource)adaptable);
                }
                catch (InvalidTemplateException ite) {
                    this.log.debug("Getting a content fragment template failed due to an invalid content structure.");
                    return null;
                }
            }
        }
        return null;
    }

    protected void bindManager(ContentFragmentManager contentFragmentManager) {
        this.manager = contentFragmentManager;
    }

    protected void unbindManager(ContentFragmentManager contentFragmentManager) {
        if (this.manager == contentFragmentManager) {
            this.manager = null;
        }
    }

    protected void bindCollectionManager(ResourceCollectionManager resourceCollectionManager) {
        this.collectionManager = resourceCollectionManager;
    }

    protected void unbindCollectionManager(ResourceCollectionManager resourceCollectionManager) {
        if (this.collectionManager == resourceCollectionManager) {
            this.collectionManager = null;
        }
    }
}