TallyOperationExtensionImpl.java 4.11 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.cq.social.scf.Operation
 *  com.adobe.cq.social.scf.OperationException
 *  com.adobe.cq.social.scf.SocialComponent
 *  com.adobe.cq.social.tally.client.api.RatingSocialComponent
 *  com.adobe.cq.social.tally.client.api.TallyOperationExtension
 *  com.adobe.cq.social.tally.client.api.TallyOperationExtension$TallyOperation
 *  com.adobe.cq.social.tally.client.api.TallySocialComponent
 *  com.adobe.cq.social.ugcbase.SocialUtils
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.dam.commons.rating.impl;

import com.adobe.cq.social.scf.Operation;
import com.adobe.cq.social.scf.OperationException;
import com.adobe.cq.social.scf.SocialComponent;
import com.adobe.cq.social.tally.client.api.RatingSocialComponent;
import com.adobe.cq.social.tally.client.api.TallyOperationExtension;
import com.adobe.cq.social.tally.client.api.TallySocialComponent;
import com.adobe.cq.social.ugcbase.SocialUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Service
@Component
public class TallyOperationExtensionImpl
implements TallyOperationExtension {
    private static final Logger LOG = LoggerFactory.getLogger(TallyOperationExtensionImpl.class);
    private final String NAME = "assets.rating";

    public List<TallyOperationExtension.TallyOperation> getOperationsToHookInto() {
        ArrayList<TallyOperationExtension.TallyOperation> operations = new ArrayList<TallyOperationExtension.TallyOperation>();
        operations.add(TallyOperationExtension.TallyOperation.SET);
        operations.add(TallyOperationExtension.TallyOperation.UNSET);
        return operations;
    }

    public void afterAction(Operation operation, Session session, TallySocialComponent tally, Map<String, Object> map) throws OperationException {
        if (!map.containsKey("tallyGenerator") || !((String)map.get("tallyGenerator")).equals("assets")) {
            return;
        }
        if (!(tally instanceof RatingSocialComponent)) {
            return;
        }
        RatingSocialComponent rating = (RatingSocialComponent)tally;
        SocialUtils socialutils = (SocialUtils)rating.getSourceComponent().getResource().getResourceResolver().adaptTo(SocialUtils.class);
        Resource tallyTarget = rating.getSourceComponent().getResource();
        Resource assetUGCResource = tallyTarget.getParent();
        if (assetUGCResource == null) {
            LOG.info("Could not get parent of source component for rating: {}", (Object)tallyTarget.getPath());
            return;
        }
        String assetPath = socialutils.UGCToResourcePath(assetUGCResource);
        try {
            if (!session.itemExists(assetPath)) {
                LOG.info("Could not get asset node for: {}", (Object)assetPath);
                return;
            }
            Node asset = session.getNode(assetPath);
            asset.getNode("jcr:content").setProperty("averageRating", (double)rating.getAverageRating().floatValue());
            session.save();
        }
        catch (RepositoryException e) {
            throw new OperationException("Error in setting property for the node", (Throwable)e, -1);
        }
    }

    public void beforeAction(Operation operation, Session session, Resource resource, Map<String, Object> map) throws OperationException {
    }

    public String getName() {
        this.getClass();
        return "assets.rating";
    }

    public int getOrder() {
        return Integer.MAX_VALUE;
    }
}