CommentingProviderImpl.java 4.29 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  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.adapter.AdapterFactory
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceUtil
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.granite.comments.internal;

import com.adobe.granite.comments.AbstractCommentingProvider;
import com.adobe.granite.comments.Comment;
import com.adobe.granite.comments.CommentCollection;
import com.adobe.granite.comments.CommentingProvider;
import com.adobe.granite.comments.internal.CommentCollectionImpl;
import java.util.List;
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.adapter.AdapterFactory;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/*
 * This class specifies class file version 49.0 but uses Java 6 signatures.  Assumed Java 6.
 */
@Component
@Service(value={CommentingProvider.class, AdapterFactory.class})
@Properties(value={@Property(name="provider.commentTypes", classValue={Comment.class}), @Property(name="provider.collectionTypes", classValue={CommentCollection.class})})
public class CommentingProviderImpl
extends AbstractCommentingProvider
implements AdapterFactory {
    private final Logger log = LoggerFactory.getLogger(CommentingProviderImpl.class);
    @Property(name="adapters")
    public static final String[] ADAPTER_CLASSES = new String[]{Comment.class.getName(), CommentCollection.class.getName()};
    @Property(name="adaptables")
    public static final String[] ADAPTABLE_CLASSES = new String[]{Resource.class.getName()};
    public static final String RESOURCE_TYPE_COLLECTION = "granite/comments/components/collection";
    public static final String RESOURCE_TYPE_COMMENT = "granite/comments/components/comment";

    @Override
    public <C extends CommentCollection> C createCollection(Resource target, Class<C> collectionType) {
        return (C)new CommentCollectionImpl(target, this.createCollectionResource(target), this);
    }

    @Override
    public <C extends CommentCollection> C getCollection(Resource target, Class<C> collectionType) {
        Resource root = this.getCollectionResource(target);
        return (C)(null != root ? new CommentCollectionImpl(target, root, this) : null);
    }

    @Override
    public String getCollectionResourceType() {
        return "granite/comments/components/collection";
    }

    @Override
    public String getCommentResourceType() {
        return "granite/comments/components/comment";
    }

    public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType> adapter) {
        if (adaptable instanceof Resource) {
            return this.getAdapter((Resource)adaptable, adapter);
        }
        this.log.warn("Could not adapt object [{}] to adapter [{}]", adaptable, adapter);
        return null;
    }

    private <AdapterType> AdapterType getAdapter(Resource resource, Class<AdapterType> adapter) {
        if (adapter == Comment.class) {
            CommentCollection collection = this.getCollection(resource.getParent().getParent(), CommentCollection.class);
            if (null != collection) {
                for (Comment comment : collection.getCommentList()) {
                    if (!comment.getPath().equals(resource.getPath())) continue;
                    return (AdapterType)comment;
                }
            }
        } else if (adapter == CommentCollection.class && (ResourceUtil.isA((Resource)resource, (String)this.getCollectionResourceType()) || CommentingProviderImpl.isCollectionResource(resource))) {
            return (AdapterType)this.getCollection(resource.getParent(), CommentCollection.class);
        }
        this.log.warn("Could not adapt object [{}] to adapter [{}]", (Object)resource, adapter);
        return null;
    }

    private static boolean isCollectionResource(Resource resource) {
        return "comments".equals(resource.getName());
    }
}