AbstractCommentingProvider.java 13.2 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  aQute.bnd.annotation.ConsumerType
 *  javax.jcr.Binary
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.jcr.ValueFactory
 *  org.apache.commons.collections.Predicate
 *  org.apache.commons.collections.iterators.FilterIterator
 *  org.apache.commons.lang.StringUtils
 *  org.apache.jackrabbit.commons.JcrUtils
 *  org.apache.jackrabbit.util.Text
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceUtil
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.granite.comments;

import aQute.bnd.annotation.ConsumerType;
import com.adobe.granite.comments.CommentException;
import com.adobe.granite.comments.CommentingProvider;
import com.adobe.granite.comments.internal.Util;
import java.io.InputStream;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.ValueFactory;
import org.apache.commons.collections.Predicate;
import org.apache.commons.collections.iterators.FilterIterator;
import org.apache.commons.lang.StringUtils;
import org.apache.jackrabbit.commons.JcrUtils;
import org.apache.jackrabbit.util.Text;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
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.
 */
@ConsumerType
public abstract class AbstractCommentingProvider
implements CommentingProvider {
    private final Logger log = LoggerFactory.getLogger(AbstractCommentingProvider.class);
    private final Predicate COMMENT_RESOURCE_TYPE_PREDICATE;
    public static final String RELATIVE_TARGET_ROOT = "comments";
    public static final String PN_ANNOTATIONDATA = "annotationData";
    public static final String PN_AUTHOR = "author";
    public static final String PN_MESSAGE = "jcr:description";
    public static final String JCR_CREATED_BY = "jcr:createdBy";
    public static final String SLING_RESOURCE_TYPE = "sling:resourceType";

    public AbstractCommentingProvider() {
        this.COMMENT_RESOURCE_TYPE_PREDICATE = new Predicate(){

            public boolean evaluate(Object o) {
                Resource resource = (Resource)o;
                return ResourceUtil.isA((Resource)resource, (String)AbstractCommentingProvider.this.getCommentResourceType());
            }
        };
    }

    public final Resource createCollectionResource(Resource target) {
        String rootPath = this.getCollectionResourcePath(target);
        Session session = (Session)target.getResourceResolver().adaptTo(Session.class);
        try {
            if (session.itemExists(rootPath)) {
                throw new CommentException("Collection already exists: " + rootPath);
            }
            Node collectionRoot = this.createCollectionNode(rootPath, session);
            if (StringUtils.isNotBlank((String)this.getCollectionResourceType())) {
                collectionRoot.setProperty("sling:resourceType", this.getCollectionResourceType());
            }
            collectionRoot.addMixin("{http://www.jcp.org/jcr/mix/1.0}lastModified");
            if (!collectionRoot.hasProperty("jcr:created")) {
                collectionRoot.setProperty("jcr:created", Calendar.getInstance());
            }
            if (!collectionRoot.hasProperty("jcr:createdBy")) {
                collectionRoot.setProperty("jcr:createdBy", session.getUserID());
            }
            this.customizeCollectionNode(target, collectionRoot);
            session.save();
            return target.getResourceResolver().getResource(collectionRoot.getPath());
        }
        catch (RepositoryException e) {
            this.log.error("error while creating collection root for target [{}]: ", (Object)target.getPath(), (Object)e);
            throw new CommentException("Could not create collection root for target: " + target.getPath(), (Throwable)e);
        }
    }

    public final Resource createCommentResource(Resource collectionResource, String message, String author, String annotationData) {
        try {
            ResourceResolver resolver = collectionResource.getResourceResolver();
            Session session = (Session)resolver.adaptTo(Session.class);
            Node collectionNode = session.getNode(collectionResource.getPath());
            String commentPath = this.getCommentResourcePath(collectionResource, message);
            Node commentNode = this.createCommentNode(commentPath, collectionNode, session);
            if (StringUtils.isNotBlank((String)this.getCommentResourceType())) {
                commentNode.setProperty("sling:resourceType", this.getCommentResourceType());
            }
            commentNode.setProperty("jcr:description", message);
            this.customizeCommentNode(collectionResource, commentNode);
            Calendar now = Calendar.getInstance();
            commentNode.addMixin("{http://www.jcp.org/jcr/mix/1.0}lastModified");
            if (!commentNode.hasProperty("jcr:created")) {
                commentNode.setProperty("jcr:created", now);
            }
            if (!commentNode.hasProperty("jcr:createdBy")) {
                commentNode.setProperty("jcr:createdBy", session.getUserID());
            }
            if (StringUtils.isNotBlank((String)author)) {
                commentNode.setProperty("author", author);
            }
            if (StringUtils.isNotBlank((String)annotationData)) {
                commentNode.setProperty("annotationData", annotationData);
            }
            collectionNode.setProperty("jcr:lastModified", now);
            session.save();
            return resolver.getResource(commentNode.getPath());
        }
        catch (RepositoryException e) {
            this.log.error("error while creating comment for collection [{}]: ", (Object)collectionResource.getPath(), (Object)e);
            throw new CommentException("Could not x for collection: " + collectionResource.getPath(), (Throwable)e);
        }
    }

    public Iterator<Resource> getCommentResources(Resource collectionResource) {
        Iterator children = collectionResource.listChildren();
        return new FilterIterator(children, this.COMMENT_RESOURCE_TYPE_PREDICATE);
    }

    public void removeCommentResource(Resource resource) {
        try {
            Session session = (Session)resource.getResourceResolver().adaptTo(Session.class);
            Node node = session.getNode(resource.getPath());
            node.remove();
            session.save();
        }
        catch (RepositoryException e) {
            this.log.error("error while removing comment [{}]: ", (Object)resource.getPath(), (Object)e);
            throw new CommentException("Could not remove comment: " + resource.getPath(), (Throwable)e);
        }
    }

    public void removeCollectionResource(Resource resource) {
        try {
            ResourceResolver resolver = resource.getResourceResolver();
            if (null != resolver.getResource(resource.getPath())) {
                Session session = (Session)resolver.adaptTo(Session.class);
                session.removeItem(resource.getPath());
                session.save();
            }
        }
        catch (RepositoryException e) {
            this.log.error("error while removing collection [{}]: ", (Object)resource.getPath(), (Object)e);
            throw new CommentException("Could not remove collection: " + resource.getPath(), (Throwable)e);
        }
    }

    public final Resource createAttachmentResource(Resource commentResource, String name, InputStream inputStream, String mimeType) {
        try {
            ResourceResolver resolver = commentResource.getResourceResolver();
            Session session = (Session)resolver.adaptTo(Session.class);
            Calendar time = Calendar.getInstance();
            Node node = session.getNode(commentResource.getPath());
            Node attachment = JcrUtils.getOrCreateUniqueByPath((Node)node, (String)this.getAttachmentResourcePath(name), (String)"nt:file");
            Node content = attachment.addNode("jcr:content", "nt:unstructured");
            content.setProperty("jcr:created", time);
            content.setProperty("jcr:createdBy", node.getSession().getUserID());
            content.setProperty("jcr:lastModified", time);
            content.setProperty("jcr:mimeType", mimeType);
            content.setProperty("jcr:data", node.getSession().getValueFactory().createBinary(inputStream));
            this.customizeAttachmentNode(commentResource, content);
            session.save();
            return resolver.getResource(attachment.getPath());
        }
        catch (RepositoryException e) {
            this.log.error("error while creating attachment for comment [{}]: ", (Object)commentResource.getPath(), (Object)e);
            throw new CommentException("Could not create attachment for comment: " + commentResource.getPath(), (Throwable)e);
        }
    }

    public Resource getAttachmentResource(Resource commentResource, String name) {
        Resource child = commentResource.getChild(this.getAttachmentResourcePath(name));
        return ResourceUtil.isA((Resource)child, (String)"nt:file") ? child : null;
    }

    public void removeAttachmentResource(Resource commentResource, String name) {
        try {
            Resource attachment = commentResource.getChild(this.getAttachmentResourcePath(name));
            if (null != attachment) {
                Session session = (Session)attachment.getResourceResolver().adaptTo(Session.class);
                session.removeItem(attachment.getPath());
                session.save();
            }
        }
        catch (RepositoryException e) {
            this.log.error("error while removing attachment from comment [{}]: ", (Object)commentResource.getPath(), (Object)e);
            throw new CommentException("Could not remove attachment from comment: " + commentResource.getPath(), (Throwable)e);
        }
    }

    public final Map<String, Resource> getAttachmentMap(Resource commentResource) throws CommentException {
        HashMap<String, Resource> attachments = new HashMap<String, Resource>();
        Iterator<Resource> iterator = this.getAttachments(commentResource);
        while (iterator.hasNext()) {
            Resource child = iterator.next();
            if (!ResourceUtil.isA((Resource)child, (String)"nt:file")) continue;
            attachments.put(child.getName(), child);
        }
        return attachments;
    }

    protected final Resource getCollectionResource(Resource target) throws CommentException {
        return target.getResourceResolver().getResource(this.getCollectionResourcePath(target));
    }

    protected Node createCollectionNode(String rootPath, Session session) {
        try {
            Node root = null;
            String path = rootPath;
            while (root == null && StringUtils.isNotBlank((String)path)) {
                if (!session.nodeExists(path = Text.getRelativeParent((String)path, (int)1))) continue;
                root = session.getNode(path);
            }
            if (root != null) {
                String relPath = StringUtils.removeStart((String)rootPath, (String)(path + '/'));
                return JcrUtils.getOrCreateByPath((Node)root, (String)relPath, (boolean)false, (String)"nt:unstructured", (String)"nt:unstructured", (boolean)false);
            }
            throw new CommentException("Unable to access parent nodes of new collection");
        }
        catch (RepositoryException e) {
            throw new CommentException("Error creating collection root at: " + rootPath, (Throwable)e);
        }
    }

    protected Node createCommentNode(String commentPath, Node collectionNode, Session session) {
        try {
            return JcrUtils.getOrCreateUniqueByPath((Node)collectionNode, (String)commentPath, (String)"nt:unstructured");
        }
        catch (RepositoryException e) {
            throw new CommentException("Error creating comment node at: " + commentPath, (Throwable)e);
        }
    }

    protected void customizeCollectionNode(Resource target, Node collectionNode) {
    }

    protected void customizeCommentNode(Resource collectionResource, Node commentNode) {
    }

    protected void customizeAttachmentNode(Resource commentResource, Node attachmentContentNode) {
    }

    protected String getCollectionResourcePath(Resource target) {
        Resource contentResource = target.getChild("jcr:content");
        String path = null != contentResource ? contentResource.getPath() : target.getPath();
        return path + "/" + "comments";
    }

    protected String getCommentResourcePath(Resource collectionResource, String message) {
        return Util.createValidName(message);
    }

    protected String getAttachmentResourcePath(String name) {
        return name;
    }

    protected Iterator<Resource> getAttachments(Resource commentResource) {
        return commentResource.listChildren();
    }

    public abstract String getCollectionResourceType();

    public abstract String getCommentResourceType();

}