JcrSocialGraph.java 14.2 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.security.user.UserProperties
 *  com.adobe.granite.security.user.UserPropertiesManager
 *  com.adobe.granite.security.user.UserPropertiesService
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  org.apache.commons.lang.ArrayUtils
 *  org.apache.jackrabbit.api.JackrabbitSession
 *  org.apache.jackrabbit.api.security.user.Authorizable
 *  org.apache.jackrabbit.api.security.user.Group
 *  org.apache.jackrabbit.api.security.user.UserManager
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.granite.socialgraph.impl;

import com.adobe.granite.security.user.UserProperties;
import com.adobe.granite.security.user.UserPropertiesManager;
import com.adobe.granite.security.user.UserPropertiesService;
import com.adobe.granite.socialgraph.GraphNode;
import com.adobe.granite.socialgraph.Relationship;
import com.adobe.granite.socialgraph.SocialGraph;
import com.adobe.granite.socialgraph.SocialGraphException;
import com.adobe.granite.socialgraph.impl.JcrGraphNode;
import com.adobe.granite.socialgraph.impl.SocialGraphConfig;
import com.adobe.granite.socialgraph.impl.VirtualRelationship;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.commons.lang.ArrayUtils;
import org.apache.jackrabbit.api.JackrabbitSession;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.security.user.Group;
import org.apache.jackrabbit.api.security.user.UserManager;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/*
 * This class specifies class file version 49.0 but uses Java 6 signatures.  Assumed Java 6.
 */
public class JcrSocialGraph
implements SocialGraph {
    private static final Logger log = LoggerFactory.getLogger(JcrSocialGraph.class);
    private static final String USER_PROPERTIES_NODE = "profile";
    private final ResourceResolver resolver;
    private final Session session;
    private final UserPropertiesManager upMgr;
    private final UserPropertiesService upSvc;
    private final UserManager uMgr;
    private final SocialGraphConfig config;

    public JcrSocialGraph(UserPropertiesService upSvc, ResourceResolver resolver, SocialGraphConfig config) throws RepositoryException {
        this.upSvc = upSvc;
        this.upMgr = upSvc.createUserPropertiesManager(resolver);
        this.resolver = resolver;
        this.config = config;
        this.session = (Session)resolver.adaptTo(Session.class);
        this.uMgr = ((JackrabbitSession)this.session).getUserManager();
    }

    @Override
    public GraphNode getNode(String id) {
        if (id.startsWith("/")) {
            return new JcrGraphNode(this, id, null, false);
        }
        try {
            UserProperties up = this.getUserProperties(id);
            if (up != null) {
                return new JcrGraphNode(this, up.getAuthorizablePath(), id, up.isGroupProperties());
            }
            log.debug("Unable to get graph node. ID '{}' refers neither to a user nor a group.", (Object)id);
            return null;
        }
        catch (RepositoryException e) {
            log.error("Error while reading authorizable " + id, (Throwable)e);
            throw new SocialGraphException("Error while reading authorizable", (Throwable)e);
        }
    }

    protected UserProperties getUserProperties(String id) throws RepositoryException {
        UserProperties up = this.upMgr.getUserProperties(id, "profile");
        if (up == null) {
            up = this.upMgr.getUserProperties(id, null);
        }
        return up;
    }

    protected UserProperties getUserPropertiesByPath(String path) throws RepositoryException {
        String id = this.upSvc.getAuthorizableId(path);
        return id == null ? null : this.getUserProperties(id);
    }

    public Session getSession() {
        return this.session;
    }

    @Override
    public void save() {
        try {
            this.session.save();
        }
        catch (RepositoryException e) {
            log.error("Error while saving changes", (Throwable)e);
            throw new SocialGraphException("Error while saving changes", (Throwable)e);
        }
    }

    @Override
    public void refresh(boolean keepChanges) {
        try {
            this.session.refresh(keepChanges);
        }
        catch (RepositoryException e) {
            log.error("Error while refresh changes", (Throwable)e);
            throw new SocialGraphException("Error while refresh changes", (Throwable)e);
        }
    }

    protected Resource getResource(String path) {
        return this.resolver.getResource(path);
    }

    public /* varargs */ void fillOutgoingVirtualRelationships(String id, Map<String, Relationship> relations, String ... types) throws RepositoryException {
        try {
            Authorizable auth = this.uMgr.getAuthorizable(id);
            if (auth instanceof Group) {
                this.fillOutgoingGroupRelationships((Group)auth, relations, types);
            } else {
                UserProperties up = this.getUserProperties(id);
                if (up != null && !up.isGroupProperties()) {
                    this.fillOutgoingRelationships(id, relations, types);
                }
            }
        }
        catch (RepositoryException e) {
            log.error("Error while creating virtual relationships", (Throwable)e);
        }
    }

    private void fillOutgoingGroupRelationships(Group grp, Map<String, Relationship> relations, String[] types) throws RepositoryException {
        UserProperties a;
        VirtualRelationship rel;
        if (types != null && types.length > 0 && ArrayUtils.indexOf((Object[])types, (Object)this.config.getGroupOutgoingRelationship()) < 0) {
            return;
        }
        if (this.config.getOutgoingExcludedGroups().contains(grp.getID())) {
            return;
        }
        Iterator iter = this.upMgr.getMemberUserProperties(grp, null, false);
        while (iter.hasNext()) {
            a = (UserProperties)iter.next();
            if (a.isGroupProperties()) continue;
            rel = new VirtualRelationship(this, grp.getID(), a.getAuthorizableID(), this.config.getGroupOutgoingRelationship());
            relations.put(rel.signature(), rel);
        }
        iter = this.upMgr.getMemberUserProperties(grp, "profile", false);
        while (iter.hasNext()) {
            a = (UserProperties)iter.next();
            if (a.isGroupProperties()) continue;
            rel = new VirtualRelationship(this, grp.getID(), a.getAuthorizableID(), this.config.getGroupOutgoingRelationship());
            relations.put(rel.signature(), rel);
        }
    }

    private void fillOutgoingRelationships(String userId, Map<String, Relationship> relations, String[] types) throws RepositoryException {
        UserProperties a;
        VirtualRelationship rel;
        String groupId;
        if (types != null && types.length > 0 && ArrayUtils.indexOf((Object[])types, (Object)this.config.getGroupIncomingRelationship()) < 0) {
            return;
        }
        Iterator iter = this.upMgr.getMemberOfUserProperties(userId, null, false);
        while (iter.hasNext()) {
            a = (UserProperties)iter.next();
            groupId = a.getAuthorizableID();
            if (this.config.getIncomingExcludedGroups().contains(groupId)) continue;
            rel = new VirtualRelationship(this, userId, groupId, this.config.getGroupIncomingRelationship());
            relations.put(rel.signature(), rel);
        }
        iter = this.upMgr.getMemberOfUserProperties(userId, "profile", false);
        while (iter.hasNext()) {
            a = (UserProperties)iter.next();
            groupId = a.getAuthorizableID();
            if (this.config.getIncomingExcludedGroups().contains(groupId)) continue;
            rel = new VirtualRelationship(this, userId, groupId, this.config.getGroupIncomingRelationship());
            relations.put(rel.signature(), rel);
        }
    }

    public /* varargs */ void fillIncomingVirtualRelationships(String id, Map<String, Relationship> relations, String ... types) throws RepositoryException {
        try {
            Authorizable auth = this.uMgr.getAuthorizable(id);
            if (auth instanceof Group) {
                this.fillIncomingGroupRelationships((Group)auth, relations, types);
            } else {
                UserProperties up = this.getUserProperties(id);
                if (up != null && !up.isGroupProperties()) {
                    this.fillIncomingRelationships(id, relations, types);
                }
            }
        }
        catch (RepositoryException e) {
            log.error("Error while creating virtual relationships", (Throwable)e);
        }
    }

    public Relationship getVirtualRelationship(String fromId, String toId, String type) {
        try {
            Authorizable from = this.uMgr.getAuthorizable(fromId);
            Authorizable to = this.uMgr.getAuthorizable(toId);
            if (from != null && to != null) {
                if (from instanceof Group && !(to instanceof Group)) {
                    return this.getOutgoingRelationship((Group)from, toId, type);
                }
                if (!(from instanceof Group) && to instanceof Group) {
                    return this.getIncomingRelationship(fromId, (Group)to, type);
                }
            }
        }
        catch (RepositoryException e) {
            log.error("Error while creating virtual relationships", (Throwable)e);
        }
        return null;
    }

    private void fillIncomingGroupRelationships(Group grp, Map<String, Relationship> relations, String[] types) throws RepositoryException {
        UserProperties a;
        VirtualRelationship rel;
        if (types != null && types.length > 0 && ArrayUtils.indexOf((Object[])types, (Object)this.config.getGroupIncomingRelationship()) < 0) {
            return;
        }
        if (this.config.getIncomingExcludedGroups().contains(grp.getID())) {
            return;
        }
        Iterator iter = this.upMgr.getMemberUserProperties(grp, null, false);
        while (iter.hasNext()) {
            a = (UserProperties)iter.next();
            if (a.isGroupProperties()) continue;
            rel = new VirtualRelationship(this, a.getAuthorizableID(), grp.getID(), this.config.getGroupIncomingRelationship());
            relations.put(rel.signature(), rel);
        }
        iter = this.upMgr.getMemberUserProperties(grp, "profile", false);
        while (iter.hasNext()) {
            a = (UserProperties)iter.next();
            if (a.isGroupProperties()) continue;
            rel = new VirtualRelationship(this, a.getAuthorizableID(), grp.getID(), this.config.getGroupIncomingRelationship());
            relations.put(rel.signature(), rel);
        }
    }

    private void fillIncomingRelationships(String userId, Map<String, Relationship> relations, String[] types) throws RepositoryException {
        UserProperties a;
        VirtualRelationship rel;
        String groupId;
        if (types != null && types.length > 0 && ArrayUtils.indexOf((Object[])types, (Object)this.config.getGroupOutgoingRelationship()) < 0) {
            return;
        }
        Iterator iter = this.upMgr.getMemberOfUserProperties(userId, null, false);
        while (iter.hasNext()) {
            a = (UserProperties)iter.next();
            groupId = a.getAuthorizableID();
            if (this.config.getOutgoingExcludedGroups().contains(groupId)) continue;
            rel = new VirtualRelationship(this, groupId, userId, this.config.getGroupOutgoingRelationship());
            relations.put(rel.signature(), rel);
        }
        iter = this.upMgr.getMemberOfUserProperties(userId, "profile", false);
        while (iter.hasNext()) {
            a = (UserProperties)iter.next();
            groupId = a.getAuthorizableID();
            if (this.config.getOutgoingExcludedGroups().contains(groupId)) continue;
            rel = new VirtualRelationship(this, groupId, userId, this.config.getGroupOutgoingRelationship());
            relations.put(rel.signature(), rel);
        }
    }

    private Relationship getIncomingRelationship(String fromUserId, Group to, String type) throws RepositoryException {
        UserProperties a;
        if (type == null || !type.equals(this.config.getGroupIncomingRelationship())) {
            return null;
        }
        if (this.config.getIncomingExcludedGroups().contains(to.getID())) {
            return null;
        }
        Iterator iter = this.upMgr.getMemberUserProperties(to, null, false);
        while (iter.hasNext()) {
            a = (UserProperties)iter.next();
            if (!a.getAuthorizableID().equals(fromUserId)) continue;
            return new VirtualRelationship(this, fromUserId, to.getID(), type);
        }
        iter = this.upMgr.getMemberUserProperties(to, "profile", false);
        while (iter.hasNext()) {
            a = (UserProperties)iter.next();
            if (!a.getAuthorizableID().equals(fromUserId)) continue;
            return new VirtualRelationship(this, fromUserId, to.getID(), type);
        }
        return null;
    }

    private Relationship getOutgoingRelationship(Group from, String toUserId, String type) throws RepositoryException {
        UserProperties a;
        if (type == null || !type.equals(this.config.getGroupOutgoingRelationship())) {
            return null;
        }
        if (this.config.getOutgoingExcludedGroups().contains(from.getID())) {
            return null;
        }
        Iterator iter = this.upMgr.getMemberUserProperties(from, null, false);
        while (iter.hasNext()) {
            a = (UserProperties)iter.next();
            if (!a.getAuthorizableID().equals(toUserId)) continue;
            return new VirtualRelationship(this, from.getID(), toUserId, type);
        }
        iter = this.upMgr.getMemberUserProperties(from, "profile", false);
        while (iter.hasNext()) {
            a = (UserProperties)iter.next();
            if (!a.getAuthorizableID().equals(toUserId)) continue;
            return new VirtualRelationship(this, from.getID(), toUserId, type);
        }
        return null;
    }
}